Renderer
A Renderer transforms the AST into a desired output format.
The most common renderers handle each document separately. For example the HtmlRenderer renders the AST into HTML.
Each renderer must implement TypeRenderer .
Basic document
type renderers like the HTML or Latex renderer can extend
the BaseTypeRenderer
. The
BaseTypeRenderer
creates a
RenderDocumentCommand
for each
document in the document tree. The RenderDocumentCommand
passes the
rendering to the NodeRenders which do the actual rendering.
All renderers must be registered in the ContainerConfigurator of the extension
with the tag 'phpdoc.renderer.typerenderer'
and additional format information.
Example: a plaintext renderer
Create a class called PlaintextRenderer
which just extends
BaseTypeRenderer
.
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Renderer;
use phpDocumentor\Guides\Renderer\BaseTypeRenderer;
final class PlaintextRenderer extends BaseTypeRenderer
{
}
Register the new renderer in the container:
<?php
use MyVendor\MyExtension\Renderer\PlaintextRenderer;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $container): void {
$container->services()
->defaults()->autowire()
// ...
->set(PlaintextRenderer::class)
->tag(
'phpdoc.renderer.typerenderer',
[
'noderender_tag' => 'phpdoc.guides.noderenderer.txt',
'format' => 'txt',
],
);
};
You can now configure your project to also generate output in plaintext:
<?xml version="1.0" encoding="UTF-8" ?>
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
>
<extension class="MyVendor\MyExtension"/>
<output-format>txt</output-format>
</guides>
You can now define custom templates or even custom NodeRenderer for the new output format.