home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / POD2HTML.pm < prev    next >
Encoding:
Perl POD Document  |  2002-06-15  |  5.6 KB  |  240 lines

  1. package DocSet::Doc::POD2HTML;
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use File::Spec::Functions;
  7. use File::Basename ();
  8.  
  9. use DocSet::Util;
  10. use DocSet::RunTime;
  11.  
  12. require Pod::POM;
  13. my $view_mode = 'DocSet::Doc::POD2HTML::View::HTML';
  14.  
  15. use DocSet::Doc::Common ();
  16. *fetch_pdf_doc_ver = \&DocSet::Doc::Common::fetch_pdf_doc_ver;
  17. *fetch_src_doc_ver = \&DocSet::Doc::Common::fetch_src_doc_ver;
  18.  
  19. use vars qw(@ISA);
  20. require DocSet::Source::POD;
  21. @ISA = qw(DocSet::Source::POD);
  22.  
  23. my %split_by = map {"head".$_ => 1} 1..4;
  24.  
  25. sub convert {
  26.     my($self) = @_;
  27.  
  28.     set_render_obj($self);
  29.  
  30.     my $pom = $self->{parsed_tree};
  31.  
  32.     my @sections = $pom->content();
  33.     shift @sections; # skip the title
  34.  
  35. #    my @body = ();
  36. #    foreach my $node (@sections) {
  37. ##    my $type = $node->type();
  38. ##        print "$type\n";
  39. #    push @body, $node->present($view_mode);
  40. #    }
  41.  
  42.     
  43.     #dumper $sections[$#sections];
  44.  
  45.     my @body = slice_by_head(@sections);
  46.  
  47.     my $vars = {
  48.                 meta => $self->{meta},
  49.                 toc  => $self->{toc},
  50.                 body => \@body,
  51.                 headers => {},
  52.                 dir  => $self->{dir},
  53.                 nav  => $self->{nav},
  54.                 last_modified => $self->{timestamp},
  55.                 pdf_doc  => $self->fetch_pdf_doc_ver,
  56.                 src_doc  => $self->fetch_src_doc_ver,
  57.                };
  58.  
  59.     my $tmpl_file = 'page';
  60.     my $mode = $self->{tmpl_mode};
  61.     my $tmpl_root = $self->{tmpl_root};
  62.     $self->{output} = proc_tmpl($tmpl_root, $tmpl_file, $mode, {doc => $vars} );
  63.  
  64.     unset_render_obj();
  65.  
  66. }
  67.  
  68.  
  69. sub slice_by_head {
  70.     my @sections = @_;
  71.     my @body = ();
  72.     for my $node (@sections) {
  73.         my @next = ();
  74.         # assumption, after the first 'headX' section, there can only
  75.         # be other 'headX' sections
  76.         my $count = scalar $node->content;
  77.         my $id = -1;
  78.         for ($node->content) {
  79.             $id++;
  80.             next unless exists $split_by{ $_->type };
  81.             @next = splice @{$node->content}, $id;
  82.             last;
  83.         }
  84.         push @body, $node->present($view_mode), slice_by_head(@next);
  85.     }
  86.     return @body;
  87. }
  88.  
  89.  
  90. 1;
  91.  
  92.  
  93. package DocSet::Doc::POD2HTML::View::HTML;
  94.  
  95. use vars qw(@ISA);
  96. require Pod::POM::View::HTML;
  97. @ISA = qw( Pod::POM::View::HTML);
  98.  
  99. use DocSet::RunTime;
  100.  
  101. use File::Spec::Functions;
  102. use File::Basename;
  103.  
  104. sub view_head1 {
  105.     my ($self, $head1) = @_;
  106.     return "<h1>" . $self->anchor($head1->title) . "</h1>\n\n" .
  107.         $head1->content->present($self);
  108. }
  109.  
  110. sub view_head2 {
  111.     my ($self, $head2) = @_;
  112.     return "<h2>" . $self->anchor($head2->title) . "</h2>\n\n" .
  113.         $head2->content->present($self);
  114. }
  115.  
  116. sub view_head3 {
  117.     my ($self, $head3) = @_;
  118.     return "<h3>" . $self->anchor($head3->title) . "</h3>\n\n" .
  119.         $head3->content->present($self);
  120. }
  121.  
  122. sub view_head4 {
  123.     my ($self, $head4) = @_;
  124.     return "<h4>" . $self->anchor($head4->title) . "</h4>\n\n" .
  125.         $head4->content->present($self);
  126. }
  127.  
  128. sub view_seq_file {
  129.     my ($self, $path) = @_;
  130.     my $doc_obj = get_render_obj();
  131.     my $base_dir = dirname catfile $doc_obj->{src_root}, $doc_obj->{src_uri};
  132.     my $file = catfile $base_dir, $path;
  133.     #warn "file: $file";
  134.  
  135.     # XXX: may need to test the location at dest_path, not src, to
  136.     # make sure that the file actually gets copied
  137.     return -e $file ? qq{<a href="$path">$path</a>} : qq{<i>$path</i>};
  138. }
  139.  
  140. # the <pre> section uses class "pre-section", which allows to use a custom
  141. # look-n-feel via the CSS
  142. sub view_verbatim {
  143.     my ($self, $text) = @_;
  144.     for ($text) {
  145.         s/&/&/g;
  146.         s/</</g;
  147.         s/>/>/g;
  148.     }
  149.  
  150.     return qq{<pre class="pre-section">$text</pre>\n};
  151. }
  152.  
  153.  
  154. #sub view_for {
  155. #    my $self = shift;
  156. #    my ($for) = @_;
  157. #    return $self->SUPER::view_for(@_) if $for->format() =~ /\bhtml\b/;
  158. #    if ($for->format() =~ /\btt2\b/) {
  159. #        my $text = $for->text();
  160. #print "$text\n";
  161. #return $text;
  162. ##        "WHOOOOOOOOOOOOOOOOOOOOOOO";   
  163. ##        $self->parse_sequence($text);
  164. #    }
  165. #}
  166.  
  167. *anchor        = \&DocSet::Doc::Common::pod_pom_html_anchor;
  168. *view_seq_link_transform_path = \&DocSet::Doc::Common::pod_pom_html_view_seq_link_transform_path;
  169.  
  170. #*view_seq_link = \&DocSet::Doc::Common::pod_pom_html_view_seq_link;
  171.  
  172. #use DocSet::Util;
  173. ## META: temp override
  174. ## the one in superclass screws up URLs in L<>: L<http://foo.bar.com>
  175. ## should view_seq_text be called at all on these parts?
  176. #sub view_seq_text {
  177. #    my ($self, $text) = @_;
  178. #dumper $self;
  179. ##print $self->[CMD];
  180. #    return $text;
  181. #}
  182.  
  183. 1;
  184.  
  185.  
  186.  
  187. __END__
  188.  
  189. =head1 NAME
  190.  
  191. C<DocSet::Doc::POD2HTML> - POD source to HTML target converter
  192.  
  193. =head1 SYNOPSIS
  194.  
  195.  
  196.  
  197. =head1 DESCRIPTION
  198.  
  199. Implements an C<DocSet::Doc> sub-class which converts a source
  200. document in POD, into an output document in HTML.
  201.  
  202. =head1 METHODS
  203.  
  204. For the rest of the super class methods see C<DocSet::Doc>.
  205.  
  206. =over
  207.  
  208. =item * convert
  209.  
  210. =back
  211.  
  212. =head1 Rendering Class
  213.  
  214. documents using this class are rendered via
  215. C<DocSet::Doc::POD2HTML::View::HTML>, which is a subclass of
  216. C<Pod::POM::View::HTML>.
  217.  
  218. C<view_head{1-4}()> are overridden to add the E<lt>a nameE<gt> anchors
  219. next to the headers for proper hyperlinking.
  220.  
  221. view_seq_file() is overriden too. Here we search for the file relative
  222. to the location of the document and if we find it we link to it
  223. otherwise the default behaviour applies (the file path is turned into
  224. italics).
  225.  
  226. view_verbatim() is overriden: renders the
  227. E<lt>preE<gt>...E<lt>/preE<gt> html, but defines a CSS class
  228. C<pre-section> so the look-n-feel can be adjusted.
  229.  
  230. The following rendering methods: anchor() and
  231. view_seq_link_transform_path() are defined in the
  232. C<DocSet::Doc::Common> class and documented there.
  233.  
  234. =head1 AUTHORS
  235.  
  236. Stas Bekman E<lt>stas (at) stason.orgE<gt>
  237.  
  238. =cut
  239.  
  240.