For RST a conversion shortcut has been implemented, so that you don't need to
convert the RST to docbook and the docbook to XHTML. This saves conversion time
and enables you to prevent from information loss during multiple conversions:
The default XHTML compiler generates complete XHTML documents, including header
and meta-data in the header. If you want to in-line the result, you may specify
another XHTML compiler, which just creates a XHTML block level element, which
can be embedded in your source code:
You can of course also use the predefined and custom directives for XHTML
rendering. The directives used during XHTML generation also need to implement
the interface ezcDocumentRstXhtmlDirective.
You can modify the generated output of the XHTML visitor by creating a custom
visitor for the RST AST. The easiest way probably is to extend from one of the
existing XHTML visitors and reusing it. For example you may want to fill the
type attribute in bullet lists, like known from HTML, which isn't valid XHTML,
though:
class myDocumentRstXhtmlVisitor extends ezcDocumentRstXhtmlVisitor
{
protected function visitBulletList( DOMNode $root, ezcDocumentRstNode $node )
{
$list = $this->document->createElement( 'ul' );
$root->appendChild( $list );
$listTypes = array(
'*' => 'circle',
'+' => 'disc',
'-' => 'square',
"\xe2\x80\xa2" => 'disc',
"\xe2\x80\xa3" => 'circle',
"\xe2\x81\x83" => 'square',
);
// Not allowed in XHTML strict
$list->setAttribute( 'type', $listTypes[$node->token->content] );
// Decoratre blockquote contents
foreach ( $node->nodes as $child )
{
$this->visitNode( $list, $child );
}
}
}
The structure, which is not enforced for visitors, but used in the docbook and
XHTML visitors, is to call special methods for each node type in the AST to
decorate the AST recursively. This method will be called for all bullet list
nodes in the AST which contain the actual list items. As the first parameter
the current position in the XHTML DOM tree is also provided to the method.
To create the XHTML we can now just create a new list node (<ul>) in the
current DOMNode, set the new attribute, and recursively decorate all
descendants using the general visitor dispatching method visitNode() for all
children in the AST. For the AST childs being also rendered as children in the XML
tree, we pass the just created DOMNode (<ul>) as the new root node to the
visitNode() method.
After defining such a class, you could use the custom visitor like shown
above:
- <?php
- $document = new ezcDocumentRst();
- $document->options->xhtmlVisitor = 'myDocumentRstXhtmlVisitor';
- $document->loadFile( $from );
-
- $xhtml = $document->getAsXhtml();
- $xml = $xhtml->save();
- ?>
Now the lists in the generated XHTML will also the type attribute set.