/** * Create document from structure * * Build a ezcDocumentDocument object from the structure created during the * visiting process. * * @param mixed $content * @return ezcDocumentDocbook */ protected function createDocument($content) { $document = $content->ownerDocument; $ezxml = new ezcDocumentDocbook(); $ezxml->setDomDocument($document); return $ezxml; }
public function testSerializeXmlFormat() { $doc = new ezcDocumentDocbook(); $doc->options->indentXml = true; $doc->loadFile(dirname(__FILE__) . '/files/xhtml_sample_basic.xml'); $this->assertEquals(file_get_contents(dirname(__FILE__) . '/files/xhtml_sample_basic_indented.xml'), $doc->save()); }
/** * Converts the given DocBook $source to an ODT document. * * This method receives a DocBook $source document and returns the * converters ODT document. * * @param ezcDocumentDocbook $source * @return ezcDocumentOdt */ public function convert($source) { $destination = $this->initializeDocument(); $docBookDom = $this->makeLocateable($source->getDomDocument()); $this->options->styler->init($destination->ownerDocument); $this->imageLocator = new ezcDocumentOdtImageLocator($source); $destination = $this->visitChildren($docBookDom, $destination); return $this->createDocument($destination); }
/** * @dataProvider getTestDocuments */ public function testCreateFromDocbook($to, $from) { // Tested for correctness in converter tests! $docbook = new ezcDocumentDocbook(); $docbook->loadFile($from); $document = new ezcDocumentOdt(); $document->createFromDocbook($docbook); $this->assertNotNull($document->getDomDocument()); }
/** * Create document from docbook document * * A document of the docbook format is provided and the internal document * structure should be created out of this. * * This method is required for all formats to have one central format, so * that each format can be compiled into each other format using docbook as * an intermediate format. * * You may of course just call an existing converter for this conversion. * * @param ezcDocumentDocbook $document * @return void */ public function createFromDocbook(ezcDocumentDocbook $document) { if ($this->options->validate && $document->validateString($document) !== true) { $this->triggerError(E_WARNING, "You try to convert an invalid docbook document. This may lead to invalid output."); } $this->path = $document->getPath(); $converter = new ezcDocumentDocbookToEzXmlConverter(); $converter->options->errorReporting = $this->options->errorReporting; $doc = $converter->convert($document); $this->document = $doc->getDomDocument(); }
/** * Test rendering of a full document * * Test the rendering of a given full document with an * additional set of user configured styles. * * @param string $file * @param string $fileName * @param array $styles * @return void */ protected function renderFullDocument($file, $fileName, array $styles = array()) { $docbook = new ezcDocumentDocbook(); $docbook->loadFile($file); $style = new ezcDocumentPcssStyleInferencer(); $style->appendStyleDirectives(array(new ezcDocumentPcssLayoutDirective(array('article'), array('font-family' => 'serif', 'line-height' => '1')), new ezcDocumentPcssLayoutDirective(array('title'), array('font-family' => 'sans-serif')))); $style->appendStyleDirectives($styles); $renderer = new ezcDocumentPdfMainRenderer(new ezcDocumentPdfSvgDriver(), $style, new ezcDocumentPdfOptions()); $pdf = $renderer->render($docbook, new ezcDocumentPdfDefaultHyphenator()); file_put_contents($this->tempDir . $fileName, $pdf); $this->assertXmlFileEqualsXmlFile($this->basePath . 'renderer/' . $fileName, $this->tempDir . $fileName); }
public function testRenderCustomStyleAndAdditionalPdfParts() { $docbook = new ezcDocumentDocbook(); $docbook->loadFile(dirname(__FILE__) . '/../files/pdf/long_text.xml'); $pdfDoc = new ezcDocumentPdf(new ezcDocumentPdfOptions(array('driver' => new ezcDocumentPdfSvgDriver()))); $pdfDoc->loadStyles(dirname(__FILE__) . '/../files/pdf/custom.css'); $pdfDoc->registerPdfPart(new ezcDocumentPdfHeaderPdfPart()); $pdfDoc->createFromDocbook($docbook); $pdf = (string) $pdfDoc; file_put_contents($this->tempDir . ($fileName = __CLASS__ . '_' . __FUNCTION__ . '.svg'), $pdf); $this->assertXmlFileEqualsXmlFile($this->basePath . 'renderer/' . $fileName, $this->tempDir . $fileName); }
public function testWriteConfluenceDocument() { $docbook = new ezcDocumentDocbook(); $docbook->loadFile(dirname(__FILE__) . '/files/wiki/confluence/s_002_inline_markup.xml'); try { $wiki = new ezcDocumentConfluenceWiki(); $wiki->createFromDocbook($docbook); $wiki->save(); $this->fail('Expected ezcDocumentMissingVisitorException'); } catch (ezcDocumentMissingVisitorException $e) { /* Expected */ } }
/** * @dataProvider getTestDocuments */ public function testStylerCalls() { $testDocs = self::getTestDocuments(); if (count($testDocs) < 1) { throw new RuntimeException('Missing test documents.'); } $doc = new ezcDocumentDocbook(); $doc->loadFile($testDocs[0][0]); $stylerMock = new ezcDocumentOdtTestStyler(); $converter = new ezcDocumentDocbookToOdtConverter(new ezcDocumentDocbookToOdtConverterOptions(array('styler' => $stylerMock))); $created = $converter->convert($doc); $this->assertTrue($stylerMock->odtDocument instanceof DOMDocument); $this->assertEquals(38, count($stylerMock->seenElements)); }
public function testRenderDefinitionListWrapped() { $mock = $this->getMock('ezcTestDocumentPdfMockDriver', array('createPage', 'drawWord')); // Expectations $mock->expects($this->at(0))->method('createPage')->with($this->equalTo(100, 1.0), $this->equalTo(100, 1.0)); $mock->expects($this->at(1))->method('drawWord')->with($this->equalTo(10, 1.0), $this->equalTo(18, 1.0), $this->equalTo("A")); $mock->expects($this->at(4))->method('drawWord')->with($this->equalTo(10, 1.0), $this->equalTo(29.2, 1.0), $this->equalTo("is")); $mock->expects($this->at(10))->method('drawWord')->with($this->equalTo(15, 1.0), $this->equalTo(62.8, 1.0), $this->equalTo("The")); $mock->expects($this->at(20))->method('createPage')->with($this->equalTo(100, 1.0), $this->equalTo(100, 1.0)); $mock->expects($this->at(21))->method('drawWord')->with($this->equalTo(15, 1.0), $this->equalTo(18, 1.0), $this->equalTo("to")); $docbook = new ezcDocumentDocbook(); $docbook->loadFile(dirname(__FILE__) . '/../files/pdf/variablelist_long.xml'); $renderer = new ezcDocumentPdfMainRenderer($mock, $this->styles); $pdf = $renderer->render($docbook, new ezcDocumentPdfDefaultHyphenator()); }
public function testCreateFromDocbook() { $from = dirname(__FILE__) . '/files/docbook/xhtml/s_001_empty.xml'; $to = dirname(__FILE__) . '/files/docbook/xhtml/s_001_empty.html'; $docbook = new ezcDocumentDocbook(); $docbook->loadFile($from); $document = new ezcDocumentXhtml(); $document->createFromDocbook($docbook); // Store test file, to have something to compare on failure $tempDir = $this->createTempDir('docbook_xhtml_') . '/'; file_put_contents($tempDir . basename($to), $xml = $document->save()); $this->assertEquals(file_get_contents($to), $xml, 'Document not visited as expected.'); // Remove tempdir, when nothing failed. $this->removeTempDir(); }
public function testRenderBlockquote() { // Additional formatting $mock = $this->getMock('ezcTestDocumentPdfMockDriver', array('createPage', 'drawWord')); // Expectations $mock->expects($this->at(0))->method('createPage')->with($this->equalTo(100, 1.0), $this->equalTo(100, 1.0)); $mock->expects($this->at(1))->method('drawWord')->with($this->equalTo(20, 1.0), $this->equalTo(21, 1.0), $this->equalTo("Some")); $mock->expects($this->at(2))->method('drawWord')->with($this->equalTo(35, 1.0), $this->equalTo(21, 1.0), $this->equalTo("random")); $mock->expects($this->at(4))->method('drawWord')->with($this->equalTo(20, 1.0), $this->equalTo(29.4, 1.0), $this->equalTo("which")); $mock->expects($this->at(13))->method('drawWord')->with($this->equalTo(25, 1.0), $this->equalTo(46.8, 1.0), $this->equalTo("Name")); $docbook = new ezcDocumentDocbook(); $docbook->loadFile(dirname(__FILE__) . '/../files/pdf/blockquote.xml'); $renderer = new ezcDocumentPdfMainRenderer($mock, $this->styles); $pdf = $renderer->render($docbook, new ezcDocumentPdfDefaultHyphenator()); }
public function testRenderLiterallayoutWrapped() { // Additional formatting $mock = $this->getMock('ezcTestDocumentPdfMockDriver', array('createPage', 'drawWord', 'drawPolygon', 'drawPolyline')); // Expectations $mock->expects($this->at(0))->method('createPage')->with($this->equalTo(100, 1.0), $this->equalTo(100, 1.0)); $mock->expects($this->at(5))->method('drawWord')->with($this->equalTo(12, 1.0), $this->equalTo(18, 1.0), $this->equalTo("1")); $mock->expects($this->at(13))->method('drawWord')->with($this->equalTo(12, 1.0), $this->equalTo(85.2, 1.0), $this->equalTo("9")); $mock->expects($this->at(14))->method('createPage')->with($this->equalTo(100, 1.0), $this->equalTo(100, 1.0)); $mock->expects($this->at(27))->method('createPage')->with($this->equalTo(100, 1.0), $this->equalTo(100, 1.0)); $mock->expects($this->at(32))->method('drawWord')->with($this->equalTo(12, 1.0), $this->equalTo(18, 1.0), $this->equalTo("9")); $mock->expects($this->at(33))->method('drawWord')->with($this->equalTo(12, 1.0), $this->equalTo(26.4, 1.0), $this->equalTo("0")); $docbook = new ezcDocumentDocbook(); $docbook->loadFile(dirname(__FILE__) . '/../files/pdf/literallayout_long.xml'); $renderer = new ezcDocumentPdfMainRenderer($mock, $this->styles); $pdf = $renderer->render($docbook, new ezcDocumentPdfDefaultHyphenator()); }
/** * @dataProvider getTestDocuments */ public function testLoadXmlDocumentFromFile($from, $to) { if (!is_file($to)) { $this->markTestSkipped("Comparision file '{$to}' not yet defined."); } $doc = new ezcDocumentDocbook(); $doc->loadFile($from); $converter = new ezcDocumentDocbookToWikiConverter(); $created = $converter->convert($doc); $this->assertTrue($created instanceof ezcDocumentWiki); // Store test file, to have something to compare on failure $tempDir = $this->createTempDir('docbook_rst_') . '/'; file_put_contents($tempDir . basename($to), $text = $created->save()); $this->assertTrue(($errors = $created->validateString($text)) === true, is_array($errors) ? implode(PHP_EOL, $errors) : 'Expected true'); $this->assertEquals(file_get_contents($to), $text); // Remove tempdir, when nothing failed. $this->removeTempDir(); }
/** * @dataProvider getTestDocuments */ public function testLoadXmlDocumentFromFile($from, $to) { if (!ezcBaseFeatures::hasExtensionSupport('xsl')) { $this->markTestSkipped('You need XSLT support for this test.'); } if (!is_file($to)) { $this->markTestSkipped("Comparision file '{$to}' not yet defined."); } $doc = new ezcDocumentDocbook(); $doc->loadFile($from); $converter = new ezcDocumentDocbookToHtmlXsltConverter(); $created = $converter->convert($doc); $this->assertTrue($created instanceof ezcDocumentXhtml); // Replace creator string in generated document, as this may change too // often for proper testing. $dom = $created->getDomDocument(); $domContent = $dom->saveXml(); // Just test that some kind of HTML has been created - everything is up // to the used XSL and may change any time. $this->assertTrue(strpos($domContent, '<html') !== false); // Remove tempdir, when nothing failed. $this->removeTempDir(); }
public function testRenderParagraphSplittingWithBorderLastPage() { // Additional formatting $this->styles->appendStyleDirectives(array(new ezcDocumentPcssLayoutDirective(array('para'), array('line-height' => '1', 'padding' => '5', 'margin' => '5', 'border' => '1mm solid #A00000', 'background-color' => '#eedbdb')))); $mock = $this->getMock('ezcTestDocumentPdfMockDriver', array('drawPolyline', 'drawPolygon', 'createPage', 'drawWord')); // Expectations for third page $mock->expects($this->at(38))->method('createPage')->with($this->equalTo(100, 1.0), $this->equalTo(100, 1.0)); $mock->expects($this->at(39))->method('drawPolygon')->with($this->equalTo(array(array(15, 15), array(85, 15), array(85, 51), array(15, 51)), 1.0), $this->equalTo(array('red' => 0.93, 'green' => 0.86, 'blue' => 0.86, 'alpha' => 0), 0.01)); $mock->expects($this->at(40))->method('drawPolyline')->with($this->equalTo(array(array(15.5, 15.5), array(15.5, 50.5)), 0.1), $this->equalTo(array('red' => 0.63, 'green' => 0.0, 'blue' => 0.0, 'alpha' => 0), 0.01)); $mock->expects($this->at(41))->method('drawPolyline')->with($this->equalTo(array(array(84.5, 15.5), array(84.5, 50.5)), 0.1), $this->equalTo(array('red' => 0.63, 'green' => 0.0, 'blue' => 0.0, 'alpha' => 0), 0.01)); $mock->expects($this->at(42))->method('drawPolyline')->with($this->equalTo(array(array(84.5, 50.5), array(15.5, 50.5)), 0.1), $this->equalTo(array('red' => 0.63, 'green' => 0.0, 'blue' => 0.0, 'alpha' => 0), 0.01)); $mock->expects($this->at(43))->method('drawWord')->with($this->equalTo(21, 1.0), $this->equalTo(29, 1.0), $this->equalTo('be')); $docbook = new ezcDocumentDocbook(); $docbook->loadFile(dirname(__FILE__) . '/../files/pdf/wrapping.xml'); $renderer = new ezcDocumentPdfMainRenderer($mock, $this->styles); $pdf = $renderer->render($docbook, new ezcDocumentPdfDefaultHyphenator()); }
/** * Create document from docbook document * * A document of the docbook format is provided and the internal document * structure should be created out of this. * * This method is required for all formats to have one central format, so * that each format can be compiled into each other format using docbook as * an intermediate format. * * You may of course just call an existing converter for this conversion. * * @param ezcDocumentDocbook $document * @return void */ public function createFromDocbook(ezcDocumentDocbook $document) { $this->document = $document->getDomDocument(); }
<?php require 'tutorial_autoload.php'; $docbook = new ezcDocumentDocbook(); $docbook->loadFile('docbook.xml'); $converter = new ezcDocumentDocbookToOdtConverter(); $converter->options->styler->addStylesheetFile('custom.css'); $odt = $converter->convert($docbook); echo $odt->save();
public function testWithStylesheets() { $from = dirname(__FILE__) . '/files/docbook/xhtml/s_021_field_list.xml'; $to = dirname(__FILE__) . '/files/docbook/xhtml/s_021_field_list_stylesheets.html'; $doc = new ezcDocumentDocbook(); $doc->loadFile($from); $converter = new ezcDocumentDocbookToHtmlConverter(); $converter->options->formatOutput = true; $converter->options->dublinCoreMetadata = true; $converter->options->styleSheets = array('foo.css', 'http://example.org/bar.css'); $created = $converter->convert($doc); $this->assertTrue($created instanceof ezcDocumentXhtml); // Store test file, to have something to compare on failure $tempDir = $this->createTempDir('docbook_html_custom_') . '/'; file_put_contents($tempDir . basename($to), $xml = $created->save()); $this->assertEquals(file_get_contents($to), $xml); // Remove tempdir, when nothing failed. $this->removeTempDir(); }
<?php require 'tutorial_autoload.php'; $docbook = new ezcDocumentDocbook(); $docbook->loadFile('address.xml'); class myAddressElementHandler extends ezcDocumentDocbookToRstBaseHandler { public function handle(ezcDocumentElementVisitorConverter $converter, DOMElement $node, $root) { $root .= $this->renderDirective('address', $node->textContent, array()); return $root; } } $converter = new ezcDocumentDocbookToRstConverter(); $converter->setElementHandler('docbook', 'address', new myAddressElementHandler()); $rst = $converter->convert($docbook); echo $rst->save();
/** * Create document from docbook document * * A document of the docbook format is provided and the internal document * structure should be created out of this. * * This method is required for all formats to have one central format, so * that each format can be compiled into each other format using docbook as * an intermediate format. * * You may of course just call an existing converter for this conversion. * * @param ezcDocumentDocbook $document * @return void */ public function createFromDocbook(ezcDocumentDocbook $document) { if ($this->options->validate && $document->validateString($document) !== true) { $this->triggerError(E_WARNING, "You try to convert an invalid docbook document. This may lead to invalid output."); } $this->path = $document->getPath(); $this->options->driver->setOptions($this->options); $renderer = new ezcDocumentPdfMainRenderer($this->options->driver, $this->styles, $this->options); foreach ($this->pdfParts as $part) { $renderer->registerPdfPart($part); } $this->content = $renderer->render($document, $this->options->hyphenator, $this->options->tokenizer); // Merge errors from renderer $this->errors = array_merge($this->errors, $renderer->getErrors()); }
public function testValidateInvalidDocbook() { $doc = new ezcDocumentDocbook(); $this->assertTrue(is_array($errors = $doc->validateFile(dirname(__FILE__) . '/files/docbook/invalid.xml'))); $this->assertSame('Error in 4:0: Element \'{http://docbook.org/ns/docbook}section\', attribute \'id\': The attribute \'id\' is not allowed..', (string) $errors[0]); }
<?php require 'tutorial_autoload.php'; $docbook = new ezcDocumentDocbook(); $docbook->loadFile('docbook.xml'); $odt = new ezcDocumentOdt(); $odt->createFromDocbook($docbook); echo $odt->save();
/** * Return document compiled to the docbook format * * The internal document structure is compiled to the docbook format and * the resulting docbook document is returned. * * This method is required for all formats to have one central format, so * that each format can be compiled into each other format using docbook as * an intermediate format. * * You may of course just call an existing converter for this conversion. * * @return ezcDocumentDocbook */ public function getAsDocbook() { $tokenizer = $this->options->tokenizer; $parser = new ezcDocumentWikiParser(); $parser->options->errorReporting = $this->options->errorReporting; $this->ast = $parser->parse($tokenizer->tokenizeString($this->contents)); $document = new ezcDocumentDocbook(); $visitor = new ezcDocumentWikiDocbookVisitor($this, $this->path); $document->setDomDocument($visitor->visit($this->ast, $this->path)); return $document; }
/** * Convert documents between two formats * * Convert documents of the given type to the requested type. * * @param ezcDocumentDocbook $source * @return ezcDocumentDocument */ public function convert($source) { $destination = $this->initializeDocument(); $destination = $this->visitChildren($source->getDomDocument(), $destination); return $this->createDocument($destination); }
/** * @dataProvider getItemizedListTypes */ public function testRenderItemizedListTypes($type, array $items) { $mock = $this->getMock('ezcTestDocumentPdfMockDriver', array('createPage', 'drawWord')); // Expectations $mock->expects($this->at(1))->method('drawWord')->with($this->equalTo(10, 1.0), $this->equalTo(18, 1.0), $this->equalTo($items[0])); $mock->expects($this->at(4))->method('drawWord')->with($this->equalTo(10, 1.0), $this->equalTo(33, 1.0), $this->equalTo($items[1])); $docbook = new ezcDocumentDocbook(); $docbook->loadFile(dirname(__FILE__) . '/../files/pdf/bullet_list.xml'); // Set numeration type in document $dom = $docbook->getDomDocument(); $xpath = new DOMXPath($dom); $xpath->registerNamespace('doc', 'http://docbook.org/ns/docbook'); $list = $xpath->evaluate('//doc:itemizedlist')->item(0); $list->setAttribute('mark', $type); $docbook->setDomDocument($dom); $renderer = new ezcDocumentPdfMainRenderer($mock, $this->styles); $pdf = $renderer->render($docbook, new ezcDocumentPdfDefaultHyphenator()); }
/** * Render given document * * Returns the rendered PDF as string * * @param ezcDocumentDocbook $document * @param ezcDocumentPdfHyphenator $hyphenator * @param ezcDocumentPdfTokenizer $tokenizer * @return string */ public function render(ezcDocumentDocbook $document, ezcDocumentPdfHyphenator $hyphenator = null, ezcDocumentPdfTokenizer $tokenizer = null) { $this->hyphenator = $hyphenator !== null ? $hyphenator : new ezcDocumentPdfDefaultHyphenator(); $this->tokenizer = $tokenizer !== null ? $tokenizer : new ezcDocumentPdfDefaultTokenizer(); $this->document = $document; // Register custom fonts in driver $this->registerFonts(); // Inject custom element class, for style inferencing $dom = $document->getDomDocument(); // Reload the XML document with to a DOMDocument with a custom element // class. Just registering it on the existing document seems not to // work in all cases. $reloaded = new DOMDocument(); $reloaded->registerNodeClass('DOMElement', 'ezcDocumentLocateableDomElement'); $reloaded->loadXml($dom->saveXml()); $this->process($reloaded); return $this->driver->save(); }
/** * Return document compiled to the docbook format * * The internal document structure is compiled to the docbook format and * the resulting docbook document is returned. * * This method is required for all formats to have one central format, so * that each format can be compiled into each other format using docbook as * an intermediate format. * * You may of course just call an existing converter for this conversion. * * @return ezcDocumentDocbook */ public function getAsDocbook() { foreach ($this->filters as $filter) { $filter->filter($this->document); } $docbook = new ezcDocumentDocbook(); $docbook->setDomDocument($this->buildDocbookDocument($this->document)); return $docbook; }
public function testRenderCustomFont() { $docbook = new ezcDocumentDocbook(); $docbook->loadFile(dirname(__FILE__) . '/../files/pdf/wrapping.xml'); $style = new ezcDocumentPcssStyleInferencer(); $style->appendStyleDirectives(array(new ezcDocumentPcssLayoutDirective(array('article'), array('font-family' => 'my-font')), new ezcDocumentPcssDeclarationDirective('@font-face', array('font-family' => 'my-font', 'src' => 'url( ' . dirname(__FILE__) . '/../files/fonts/font.ttf )')))); $renderer = new ezcDocumentPdfMainRenderer(new ezcDocumentPdfSvgDriver(), $style); $pdf = $renderer->render($docbook, new ezcDocumentPdfDefaultHyphenator()); $this->assertPdfDocumentsSimilar($pdf, __CLASS__ . '_' . __FUNCTION__); }