/** * Transform directive to docbook * * Create a docbook XML structure at the directives position in the * document. * * @param DOMDocument $document * @param DOMElement $root * @return void */ public function toDocbook(DOMDocument $document, DOMElement $root) { $file = $this->getFile(trim($this->node->parameters)); if ($root->tagName === 'para') { // The include is inline. There is no way to handle the embedding // of another document inside a paragraph properly. throw new ezcDocumentVisitException(E_PARSE, 'Caanot embed include inside a paragraph.'); } if (isset($this->node->options['literal'])) { // If the file should be included as a literal, just pass it // through in such a block. $literal = $document->createElement('literallayout', htmlspecialchars(file_get_contents($file))); $root->appendChild($literal); } else { // Otherwise we reenter the complete parsing process with the new file. $doc = new ezcDocumentRst(); $doc->loadFile($file); // Get docbook DOM tree from the parsed file $docbook = $doc->getAsDocbook(); $dom = $docbook->getDomDocument(); // Import and add the complete parsed document. $article = $dom->getElementsByTagName('article')->item(0); foreach ($article->childNodes as $child) { $imported = $document->importNode($child, true); $root->appendChild($imported); } } }
/** * @dataProvider getErroneousTestDocuments */ public function testParseErroneousRstFile($file, $message) { try { $document = new ezcDocumentRst(); $document->options->errorReporting = E_PARSE | E_ERROR | E_WARNING; $document->registerDirective('my_custom_directive', 'ezcDocumentTestDummyDirective'); $document->registerDirective('user', 'ezcDocumentTestDummyDirective'); $document->registerDirective('book', 'ezcDocumentTestDummyDirective'); $document->registerDirective('function', 'ezcDocumentTestDummyDirective'); $document->registerDirective('replace', 'ezcDocumentTestDummyDirective'); $document->loadFile($file); $docbook = $document->getAsDocbook(); $xml = $docbook->save(); $document = $parser->parse($tokenizer->tokenizeFile($file)); $this->fail('Expected some exception.'); } catch (ezcDocumentException $e) { $this->assertSame($message, $e->getMessage(), 'Different parse error expected.'); } }
<?php require 'tutorial_autoload.php'; // Convert some input RSTfile to docbook $document = new ezcDocumentRst(); $document->loadFile('./article/introduction.txt'); $pdf = new ezcDocumentPdf(); $pdf->options->errorReporting = E_PARSE | E_ERROR | E_WARNING; $pdf->createFromDocbook($document->getAsDocbook()); file_put_contents(__FILE__ . '.pdf', $pdf);
<?php require 'tutorial_autoload.php'; $document = new ezcDocumentRst(); $document->loadFile('../tutorial.txt'); $docbook = $document->getAsDocbook(); echo $docbook->save();