|
NAMEPod::POM::View::HTML::Filter - Use filters on sections of your pod documentsSYNOPSISIn your POD:Some coloured Perl code: =begin filter perl # now in full colour! $A++; =end filter =for filter=perl $A++; # this works too This should read C<bar bar bar>: =begin filter foo bar foo bar =end filter In your code: my $view = Pod::POM::View::HTML::Filter->new; $view->add( foo => { code => sub { my $s = shift; $s =~ s/foo/bar/gm; $s }, # other options are available } ); my $pom = Pod::POM->parse_file( '/my/pod/file' ); $pom->present($view); DESCRIPTIONThis module is a subclass of "Pod::POM::View::HTML" that support the "filter" extension. This can be used in "=begin" / "=end" and "=for" pod blocks.Please note that since the view maintains an internal state, only an instance of the view can be used to present the POM object. Either use: my $view = Pod::POM::View::HTML::Filter->new; $pom->present( $view ); or $Pod::POM::DEFAULT_VIEW = Pod::POM::View::HTML::Filter->new; $pom->present; Even though the module was specifically designed for use with "Perl::Tidy", you can write your own filters quite easily (see "Writing your own filters"). FILTERING POD?The whole idea of this module is to take advantage of all the syntax colouring modules that exist (actually, "Perl::Tidy" was my first target) to produce colourful code examples in a POD document (after conversion to HTML).Filters can be used in two different POD constructs:
OptionsSome filters may accept options that alter their behaviour. Options are separated by whitespace, and appear after the name of the filter. For example, the following code will be rendered in colour and with line numbers:=begin filter perl -nnn $a = 123; $b = 3; print $a * $b; # prints 369 print $a x $b; # prints 123123123 =end filter "=for" filters can also accept options, but the syntax is less clear. (This is because "=for" expects the formatname to match "\S+".) The syntax is the following: =for filter=html:nnn=1 <center><img src="camel.png" /> A camel</center> In summary, options are separated by space for "=begin" blocks and by colons for "=for" paragraphs. The options and their paramater depend on the filter, but they cannot contain the pipe ("|") or colon (":") character, for obvious reasons. PipesHaving filter to modify a block of text is usefule, but what's more useful (and fun) than a filter? Answer: a stack of filters piped together!Take the imaginary filters "foo" (which does a simple "s/foo/bar/g") and "bang" (which does an even simpler "tr/r/!/"). The following block =begin filter foo|bar foo bar baz =end will become "ba! ba! baz". And naturally, =for filter=bar|foo foo bar baz will return "bar ba! baz". A note on verbatim and text blocksNote: The fact that I mention verbatim and paragraph in this section is due to an old bug in "Pod::POM", which parses the content of "begin"/"end" sections as the usual POD paragraph and verbatim blocks. This is a bug in "Pod::POM", around which "Pod::POM::View::HTML::Filter" tries to work around.As from version 0.06, "Pod::POM::View::HTML::Filter" gets to the original text contained in the "=begin" / "=end" block (it was easier than I thought, actually) and put that string throught all the filters. If any filter in the stack is defined as "verbatim", or if "Pod::POM" detect any block in the "=begin" / "=end" block as verbatim, then the output will be produced between "<pre>" and "</pre>" tags. Otherwise, no special tags will be added (his is left to the formatter). ExamplesAn example of the power of pipes can be seen in the following example. Take a bit of Perl code to colour:=begin filter perl "hot cross buns" =~ /cross/; print "Matched: <$`> $& <$'>\n"; # Matched: <hot > cross < buns> print "Left: <$`>\n"; # Left: <hot > print "Match: <$&>\n"; # Match: <cross> print "Right: <$'>\n"; # Right: < buns> =end This will produce the following HTML code: <pre> <span class="q">"hot cross buns"</span> =~ <span class="q">/cross/</span><span class="sc">;</span> <span class="k">print</span> <span class="q">"Matched: <$`> $& <$'>\n"</span><span class="sc">;</span> <span class="c"># Matched: <hot > cross < buns></span> <span class="k">print</span> <span class="q">"Left: <$`>\n"</span><span class="sc">;</span> <span class="c"># Left: <hot ></span> <span class="k">print</span> <span class="q">"Match: <$&>\n"</span><span class="sc">;</span> <span class="c"># Match: <cross></span> <span class="k">print</span> <span class="q">"Right: <$'>\n"</span><span class="sc">;</span> <span class="c"># Right: < buns></span></pre> Now if you want to colour and number the HTML code produced, it's as simple as tackling the "html" on top of the "perl" filter: =begin filter perl | html nnn=1 "hot cross buns" =~ /cross/; print "Matched: <$`> $& <$'>\n"; # Matched: <hot > cross < buns> print "Left: <$`>\n"; # Left: <hot > print "Match: <$&>\n"; # Match: <cross> print "Right: <$'>\n"; # Right: < buns> =end Which produces the rather unreadable piece of HTML: <pre><span class="h-lno"> 1</span> <span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"q</span>"<span class="h-ab">></span><span class="h-ent">&quot;</span>hot cross buns<span class="h-ent">&quot;</span><span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span> =~ <span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"q</span>"<span class="h-ab">></span>/cross/<span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span><span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"sc</span>"<span class="h-ab">></span>;<span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span> <span class="h-lno"> 2</span> <span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"k</span>"<span class="h-ab">></span>print<span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span> <span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"q</span>"<span class="h-ab">></span><span class="h-ent">&quot;</span>Matched: <span class="h-ent">&lt;</span>$`<span class="h-ent">&gt;</span> $<span class="h-ent">&amp;</span> <span class="h-ent">&lt;</span>$'<span class="h-ent">&gt;</span>\n<span class="h-ent">&quot;</span><span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span><span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"sc</span>"<span class="h-ab">></span>;<span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span> <span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"c</span>"<span class="h-ab">></span># Matched: <span class="h-ent">&lt;</span>hot <span class="h-ent">&gt;</span> cross <span class="h-ent">&lt;</span> buns<span class="h-ent">&gt;</span><span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span> <span class="h-lno"> 3</span> <span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"k</span>"<span class="h-ab">></span>print<span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span> <span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"q</span>"<span class="h-ab">></span><span class="h-ent">&quot;</span>Left: <span class="h-ent">&lt;</span>$`<span class="h-ent">&gt;</span>\n<span class="h-ent">&quot;</span><span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span><span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"sc</span>"<span class="h-ab">></span>;<span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span> <span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"c</span>"<span class="h-ab">></span># Left: <span class="h-ent">&lt;</span>hot <span class="h-ent">&gt;</span><span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span> <span class="h-lno"> 4</span> <span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"k</span>"<span class="h-ab">></span>print<span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span> <span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"q</span>"<span class="h-ab">></span><span class="h-ent">&quot;</span>Match: <span class="h-ent">&lt;</span>$<span class="h-ent">&amp;</span><span class="h-ent">&gt;</span>\n<span class="h-ent">&quot;</span><span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span><span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"sc</span>"<span class="h-ab">></span>;<span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span> <span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"c</span>"<span class="h-ab">></span># Match: <span class="h-ent">&lt;</span>cross<span class="h-ent">&gt;</span><span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span> <span class="h-lno"> 5</span> <span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"k</span>"<span class="h-ab">></span>print<span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span> <span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"q</span>"<span class="h-ab">></span><span class="h-ent">&quot;</span>Right: <span class="h-ent">&lt;</span>$'<span class="h-ent">&gt;</span>\n<span class="h-ent">&quot;</span><span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span><span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"sc</span>"<span class="h-ab">></span>;<span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span> <span class="h-ab"><</span><span class="h-tag">span</span> <span class="h-attr">class</span>=<span class="h-attv">"c</span>"<span class="h-ab">></span># Right: <span class="h-ent">&lt;</span> buns<span class="h-ent">&gt;</span><span class="h-ab"></</span><span class="h-tag">span</span><span class="h-ab">></span></pre> CaveatsThere were a few things to keep in mind when mixing verbatim and text paragraphs in a "=begin" block. These problems do not exist any more as from version 0.06.
METHODSPublic methodsThe following methods are available:
Overloaded methodsThe following "Pod::POM::View::HTML" methods are overridden in "Pod::POM::View::HTML::Filter":
FILTERSBuilt-in filters"Pod::POM::View::HTML::Filter" is shipped with a few built-in filters.The name for the filter is obtained by removing "_filter" from the names listed below (except for "default"):
Writing your own filtersWrite a filter is quite easy: a filter is a subroutine that takes two arguments (text to parse and option string) and returns the filtered string.The filter is added to "Pod::POM::View::HTML::Filter"'s internal filter list with the "add()" method: $view->add( foo => { code => \&foo_filter, requires => [], } ); When presenting the following piece of pod, =begin filter foo bar baz Some text to filter. =end filter the "foo_filter()" routine will be called with two arguments, like this: foo_filter( "Some text to filter.", "bar baz" ); If you have a complex set of options, your routine will have to parse the option string by itself. Please note that in a "=for" construct, whitespace in the option string must be replaced with colons: =for filter=foo:bar:baz Some text to filter. The "foo_filter()" routine will be called with the same two arguments as before. BUILT-IN FILTERS CSS STYLESEach filter uses its own CSS classes, so that one can define their favourite colours in a custom CSS file."perl" filter"Perl::Tidy"'s HTML code looks like:<span class="i">$A</span>++<span class="sc">;</span> Here are the classes used by "Perl::Tidy": n numeric p paren q quote s structure c comment v v-string cm comma w bareword co colon pu punctuation i identifier j label h here-doc-target hh here-doc-text k keyword sc semicolon m subroutine pd pod-text "ppi" filter"PPI::HTML" uses the following CSS classes:comment double heredoc_content interpolate keyword for language keywords (my, use line_number number operator for language operators pragma for pragmatas (strict, warnings) single structure for syntaxic symbols substitute symbol word for module, function and method names words match "html" filter"Syntax::Highlight::HTML" makes use of the following classes:h-decl declaration # declaration <!DOCTYPE ...> h-pi process # process instruction <?xml ...?> h-com comment # comment <!-- ... --> h-ab angle_bracket # the characters '<' and '>' as tag delimiters h-tag tag_name # the tag name of an element h-attr attr_name # the attribute name h-attv attr_value # the attribute value h-ent entity # any entities: é « "shell" filter"Syntax::Highlight::Shell" makes use of the following classes:s-key # shell keywords (like if, for, while, do...) s-blt # the builtins commands s-cmd # the external commands s-arg # the command arguments s-mta # shell metacharacters (|, >, \, &) s-quo # the single (') and double (") quotes s-var # expanded variables: $VARIABLE s-avr # assigned variables: VARIABLE=value s-val # shell values (inside quotes) s-cmt # shell comments "kate" filterOutput formatted with "Syntax::Highlight::Engine::Kate" makes use of the following classes:k-alert # Alert k-basen # BaseN k-bstring # BString k-char # Char k-comment # Comment k-datatype # DataType k-decval # DecVal k-error # Error k-float # Float k-function # Function k-istring # IString k-keyword # Keyword k-normal # Normal k-operator # Operator k-others # Others k-regionmarker # RegionMarker k-reserved # Reserved k-string # String k-variable # Variable k-warning # Warning HISTORYThe goal behind this module was to produce nice looking HTML pages from the articles the French Perl Mongers are writing for the French magazine GNU/Linux Magazine France (<http://www.linuxmag-france.org/>).The resulting web pages can be seen at <http://articles.mongueurs.net/magazines/>. AUTHORPhilippe "BooK" Bruhat, "<book@cpan.org>"THANKSMany thanks to Sébastien Aperghis-Tramoni (Maddingue), who helped debugging the module and wrote "Syntax::Highlight::HTML" and "Syntax::Highlight::Shell" so that I could ship PPVHF with more than one filter. He also pointed me to "Syntax::Highlight::Engine::Kate", which led me to clean up PPVHF before adding support for SHEK.Perl code examples where borrowed in Amelia, aka Programming Perl, 3rd edition. TODOThere are a few other syntax highlighting modules on CPAN, which I should try to add support for in "Pod::POM::View::HTML::Filter":
BUGSPlease report any bugs or feature requests to "bug-pod-pom-view-html-filter@rt.cpan.org", or through the web interface at <http://rt.cpan.org>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.COPYRIGHT & LICENSECopyright 2004 Philippe "BooK" Bruhat, All Rights Reserved.This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. POD ERRORSHey! The above document had some coding errors, which are explained below:
Visit the GSP FreeBSD Man Page Interface. |