Pod::Elemental::Transformer::SynHi - a role for transforming code into syntax highlighted HTML regions
Pod::Elemental::Transformer::SynHi is a role to be included by transformers that replace parts of the Pod document with
presumably to be consumed by a downstream Pod-to-HTML transformer.
If your class composes this role,
you only need to write one method,
It will be called like this:
That will use the mythical Some::Syntax::Highlighter to turn the given content into HTML, acting on blocks like:
You'll probably also want to specify a default format name indicating what regions to transform by doing this:
has '+format_name' => (default => 'js');
With that done, the transformer will look for
=begin js or
=for js regions, or verbatim paragraphs beginning with
#!js and feed them to the syntax highlighter.
This role provides a
transform_node method. It will call
synhi_params_for_para for each paragraph under the node. If that method returns false, nothing happens. If it returns a true value, that value will be passed to the
build_html method, which should return HTML to be placed in an
html region and used to replace the node that was found.
build_html is the one method you must write for yourself!
SynHi transformers have a
format_name attribute. The default
synhi_params_for_para will look for begin/end or for regions with that format name, or for verbatim paragraphs that start with
!#formatname. Any text following the format name will be passed to
parse_synhi_param and the result will be passed as the
$param argument seen above. The rest of the content (excluding the shebang line, if one was used) will be the
parse_synhi_param will raise an exception if the param string is not empty.
All the documentation of attributes and methods below will be of use primarily if you are trying to do something more complex than described above.
This is the format name used to mark regions for syntax highlighting. It must be a valid format name and must be provided. Classes composing this role are expected (but not required) to provide a default.
This boolean, which defaults to true, controls whether the output of a SynHi transformer's
build_html method is automatically wrapped with
my $maybe_result = $xformer->synhi_params_for_para($pod_para);
This method is called for each paragraph the transformer considers. It should return either false or an arrayref in the form:
[ $content_string, $parameters ]
The behavior of the default
synhi_params_for_para is described above: it looks for regions with the proper format name or verbatim paragraphs starting with shebang lines. It parses post-format-name line content with the
parse_synhi_param method below.
In the example lines:
=begin formatname parameter string #!formatname parameter string
The string "parameter string" can be any arbitrary string that may alter the way the SynHi tranformer will work. This method parses that string and returns the result. This will usually be done by individual syntax highlighting classes. The default method provided will return an empty hashref if the parameter string is empty and will raise an exception otherwise.
synhi_params_for_para method returns true, this method is called with the result (array-dereferenced) and the result of this method is used to replace the original paragraph. The default implementation of this method is probably suitable for everyone: it passes its parameters along to the
build_html method, constructs a
html region containing the resultant string, and returns that.
my $html = $xform->standard_code_block( $in_html );
Given a hunk of HTML representing the syntax highlighted code, this rips the HTML apart and re-wraps it in a table with line numbers. It assumes the code's actual lines are broken by newlines or
The standard code block emitted by this role is table with the class
code-listing. It will have one row with two cells; the first has class
line-numbers and the second has class
code. The table is used to make it easy to copy only the code without the line numbers.
Some other minor changes are made, and these may change over time, to make the code blocks "better" displayed. If your needs are very specific, replace this method.
Ricardo SIGNES <email@example.com>
This software is copyright (c) 2013 by Ricardo SIGNES.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.