Perl6::Perldoc::To::Xhtml - Add a to_xhtml() method to Perl6::Perldoc::Parser
This document describes Perl6::Perldoc::To::Xhtml version 0.0.6
use Perl6::Perldoc::Parser; use Perl6::Perldoc::To::Xhtml; # All Perl6::Perldoc::Parser DOM classes now have a to_xhtml() method
This module adds a method named to_xhtml() to each of the classes in the Perl6::Perldoc::Root hierarchy, enabling them all to produce an XHTML representation of themselves and their nested components.
to_xhtml()
Perl6::Perldoc::Root
The module also adds a to_xhtml() method to the Perl6::Perldoc::ReturnVal object returned by Perl6::Perldoc::Parser::parse(), so that perldoc-to-xhtml translation can be performed in a single statement:
Perl6::Perldoc::ReturnVal
Perl6::Perldoc::Parser::parse()
use Perl6::Perldoc::Parser; use Perl6::Perldoc::To::Xhtml; print Perl6::Perldoc::Parser->parse($file) ->report_errors() ->to_xhtml();
Loading the module automatically installs the necessary to_xhtml() methods in every Perl6::Perldoc subclass.
Perl6::Perldoc
Each to_xhtml() method takes a reference to a hash containing options, and returns a string containing an XHTML representation of the object to which the method was applied.
The options currently supported are:
full_doc => \%suboptions
If this option is specified, the to_xhtml() method generates a complete XHTML document (including a DTD, <head>, <title>, and <body> tags), rather than just the body contents.
<head>
<title>
<body>
By default the DTD used is "XHTML 1.0 Transitional//EN", but you can make it "XHTML 1.0 Strict//EN" instead by specifying the 'DTD' suboption:
"XHTML 1.0 Transitional//EN"
"XHTML 1.0 Strict//EN"
'DTD'
$perldoc->to_xhtml({ full_doc => {DTD=>'strict'} });
Alternatively, you can specify the full DTD explicitly:
$perldoc->to_xhtml({ full_doc => { DTD => '-//W3C//DTD XHTML 1.0 Frameset//EN' } });
By default, the name of the file from which the Perldoc object was generated is used as the title of the documents (i.e. in its <title>..</title> tags).
</title>
However, you can specify another title using the 'title' suboption:
'title'
$perldoc->to_xhtml({ full_doc => {title => 'Synopsis 26'} });
It's also possible to embed CSS into the generated XHTML document, using the 'style' suboption:
'style'
my $CSS = <<'END_CSS'; a:link { color: #ff8080 } a:visited { color: #ff0000 } a:active { color: #a05050 } p:first-line { margin-left: 5% } h1, h2, blockquote { background: #000080 } END_CSS $perldoc->to_xhtml({ full_doc => {style => $CSS} });
If the value of the 'style' option doesn't start with a <style..., then the style tags (and the usual backwards-compatibility <!-- --> comment) are added automatically. However, you can provide the tags explictly if you need to specify special properties:
<style...
<!-- -->
my $CSS = <<'END_CSS'; <style type="text/css" media="holodeck"> a:link { aroma: citrus } a:visited { aroma: floral } a:active { aroma: sweaty } </style> END_CSS $perldoc->to_xhtml({ full_doc => {style => $CSS} });
Alternatively, the 'style' option can also contain a link to an external style-sheet:
my $CSS = <<'END_CSS'; <link href="mystyle.css" rel="stylesheet" type="text/css"> END_CSS $perldoc->to_xhtml({ full_doc => {style => $CSS} });
Note that generating a full XHTML document is not the default behaviour of to_xhtml() because defaulting to generating unencapsuated body contents makes it easy to generate XHTML markup from separate Perldoc documents and then concatentate them into a single body:
my @body_parts = map { $_->to_xhtml() } @perldoc_reps; print <<"END_XHTML" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"> <html> <head> <title>My compound document</title> </head> <body> @body_parts </body> </html> END_XHTML
notes_prefix => HTML
Annotations are appended at the end of the document, prefixed by default with <h1>Notes</h1>\n. This option allows to change this prefix to something else, for instance:
<h1>Notes</h1>\n
$perldoc->to_xhtml({ notes_prefix => "<hr>" });
text_to_entities => sub {...}
Normally the module automatically converts instances of &, < and > to the entities &, < and >, but does not insert any other entities into the generated XHTML. This option allows you to control precisely how entities are introduced into the final XHTML source.
&
<
>
&
<
>
If this option is present, its value must be a subroutine reference. The subroutine will be passed two arguments:
a reference to the parent DOM object that the specific text is from
the raw text itself
The subroutine is expected to return XHTML text, presumably with some entity substitutions. Note that no other entity processing is performed if the 'text_to_entities' option is specified, so the translation subroutine will almost certainly need to handle the standard entities as well:
'text_to_entities'
text_to_entities => sub { my ($parent, $content) = @_; $content =~ s{&}{&}g; $content =~ s{<}{<}g; $content =~ s{>}{>}g; if (! $parent->is_verbatim) { # Other entity processing here } return $content; }
In addition to the diagnostics of Perl6::Perldoc::Parser:
The 'text_to_entities' option expects a reference to a subroutine that processes text strings. You passed it something else.
Perl6::Perldoc::To::Xhtml requires no configuration files or environment variables.
Perl6::Perldoc::Parser
None reported.
The translator does not expand P<> formatting codes (it represents them as ordinary links, rather than pulling the contents of the link into the document). This approach is permitted under the Perldoc definition, but not the desired behaviour.
P<>
No bugs have been reported.
Please report any bugs or feature requests to bug-perldoctotext@rt.cpan.org, or through the web interface at http://rt.cpan.org.
bug-perldoctotext@rt.cpan.org
Damian Conway <DCONWAY@cpan.org>
<DCONWAY@cpan.org>
Copyright (c) 2006, Damian Conway <DCONWAY@cpan.org>. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
To install Perl6::Perldoc, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Perl6::Perldoc
CPAN shell
perl -MCPAN -e shell install Perl6::Perldoc
For more information on module installation, please visit the detailed CPAN module installation guide.