/** * Docarate RST AST * * Visit the RST abstract syntax tree. * * @param ezcDocumentRstDocumentNode $ast * @return mixed */ public function visit(ezcDocumentRstDocumentNode $ast) { parent::visit($ast); // Create article from AST $imp = new DOMImplementation(); $dtd = $imp->createDocumentType('article', '-//OASIS//DTD DocBook XML V4.5//EN', 'http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd'); $this->document = $imp->createDocument('http://docbook.org/ns/docbook', '', $dtd); $this->document->formatOutput = true; // $root = $this->document->createElement( 'article' ); $root = $this->document->createElementNs('http://docbook.org/ns/docbook', 'article'); $this->document->appendChild($root); // Visit all childs of the AST root node. foreach ($ast->nodes as $node) { $this->visitNode($root, $node); } return $this->document; }
/** * Parse directive token list with RST parser * * This method is intended to parse the token list, provided for the RST * contents using the standard RST parser. It will be visited afterwards by * the provided RST-visitor implementation. * * The method returns the created document as a DOMDocument. You normally * need to use DOMDocument::importNode to embed the conatined nodes in your * target document. * * @param array $tokens * @param ezcDocumentRstVisitor $visitor * @return DOMDocument */ protected function parseTokens(array $tokens, ezcDocumentRstVisitor $visitor) { $parser = new ezcDocumentRstParser(); $ast = $parser->parse($tokens); $doc = $visitor->visit($ast, $this->path); return $doc; }
/** * Docarate RST AST * * Visit the RST abstract syntax tree. * * @param ezcDocumentRstDocumentNode $ast * @return mixed */ public function visit(ezcDocumentRstDocumentNode $ast) { parent::visit($ast); // Create article from AST $imp = new DOMImplementation(); $dtd = $imp->createDocumentType('html', '-//W3C//DTD XHTML 1.0 Transitional//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'); $this->document = $imp->createDocument('http://www.w3.org/1999/xhtml', '', $dtd); $root = $this->document->createElementNs('http://www.w3.org/1999/xhtml', 'html'); $this->document->appendChild($root); $this->head = $this->document->createElement('head'); $root->appendChild($this->head); // Append generator $generator = $this->document->createElement('meta'); $generator->setAttribute('name', 'generator'); $generator->setAttribute('content', 'eZ Components; http://ezcomponents.org'); $this->head->appendChild($generator); // Set content type and encoding $type = $this->document->createElement('meta'); $type->setAttribute('http-equiv', 'Content-Type'); $type->setAttribute('content', 'text/html; charset=utf-8'); $this->head->appendChild($type); $this->addStylesheets($this->head); $body = $this->document->createElement('body'); $root->appendChild($body); // Visit all childs of the AST root node. foreach ($ast->nodes as $node) { $this->visitNode($body, $node); } // Visit all footnotes at the document body foreach ($this->footnotes as $footnotes) { ksort($footnotes); $footnoteList = $this->document->createElement('ul'); $footnoteList->setAttribute('class', 'footnotes'); $body->appendChild($footnoteList); foreach ($footnotes as $footnote) { $this->visitFootnote($footnoteList, $footnote); } } // Check that all required elements for a valid XHTML document exist if ($this->head->getElementsByTagName('title')->length < 1) { $title = $this->document->createElement('title', 'Empty document'); $this->head->appendChild($title); } return $this->document; }