1 #============================================================= -*-Perl-*-
6 # HTML view of a Pod Object Model.
9 # Andy Wardley <abw@kfs.org>
12 # Copyright (C) 2000 Andy Wardley. All Rights Reserved.
14 # This module is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
18 # $Id: HTML.pm 84 2009-08-20 21:07:00Z ford $
20 #========================================================================
22 package BASIS::Pod::POM::View::HTML;
27 use BASIS::Pod::POM::View;
28 use parent qw( BASIS::Pod::POM::View );
29 use vars qw( $VERSION $DEBUG $ERROR $AUTOLOAD );
32 $VERSION = sprintf("%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/);
33 $DEBUG = 0 unless defined $DEBUG;
39 my $self = $class->SUPER::new(@_)
42 # initalise stack for maintaining info for nested lists
50 my ($self, $type, $item) = @_;
52 if ($type =~ s/^seq_//) {
55 elsif (UNIVERSAL::isa($item, 'HASH')) {
56 if (defined $item->{ content }) {
57 return $item->{ content }->present($self);
59 elsif (defined $item->{ text }) {
60 my $text = $item->{ text };
61 return ref $text ? $text->present($self) : $text;
77 my ($self, $pod) = @_;
78 return "<html>\n<body bgcolor=\"#ffffff\">\n"
79 . $pod->content->present($self)
80 . "</body>\n</html>\n";
85 my ($self, $head1) = @_;
86 my $title = $head1->title->present($self);
87 return "<h1>$title</h1>\n\n"
88 . $head1->content->present($self);
93 my ($self, $head2) = @_;
94 my $title = $head2->title->present($self);
95 return "<h2>$title</h2>\n"
96 . $head2->content->present($self);
101 my ($self, $head3) = @_;
102 my $title = $head3->title->present($self);
103 return "<h3>$title</h3>\n"
104 . $head3->content->present($self);
109 my ($self, $head4) = @_;
110 my $title = $head4->title->present($self);
111 return "<h4>$title</h4>\n"
112 . $head4->content->present($self);
117 my ($self, $over) = @_;
118 my ($start, $end, $strip);
119 my $items = $over->item();
123 my $first_title = $items->[0]->title();
125 if ($first_title =~ /^\s*\*\s*/) {
129 $strip = qr/^\s*\*\s*/;
131 elsif ($first_title =~ /^\s*\d+\.?\s*/) {
132 # '=item 1.' or '=item 1 ' => <ol>
135 $strip = qr/^\s*\d+\.?\s*/;
143 my $overstack = ref $self ? $self->{ OVER } : \@OVER;
144 push(@$overstack, $strip);
145 my $content = $over->content->present($self);
153 return "<blockquote>\n"
154 . $over->content->present($self)
161 my ($self, $item) = @_;
163 my $over = ref $self ? $self->{ OVER } : \@OVER;
164 my $title = $item->title();
165 my $strip = $over->[-1];
167 if (defined $title) {
168 $title = $title->present($self) if ref $title;
169 $title =~ s/$strip// if $strip;
172 $anchor =~ s/^\s*|\s*$//g; # strip leading and closing spaces
174 $title = qq{<a name="item_$anchor"></a><b>$title</b>};
180 . $item->content->present($self)
186 my ($self, $for) = @_;
187 return '' unless $for->format() =~ /\bhtml\b/;
194 my ($self, $begin) = @_;
195 return '' unless $begin->format() =~ /\bhtml\b/;
197 my $output = $begin->content->present($self);
204 my ($self, $text) = @_;
205 return $HTML_PROTECT ? "$text\n" : "<p>$text</p>\n";
210 my ($self, $text) = @_;
216 return "<pre>$text</pre>\n\n";
221 my ($self, $text) = @_;
222 return "<b>$text</b>";
226 sub view_seq_italic {
227 my ($self, $text) = @_;
228 return "<i>$text</i>";
233 my ($self, $text) = @_;
234 return "<code>$text</code>";
238 my ($self, $text) = @_;
239 return "<i>$text</i>";
243 my ($self, $text) = @_;
244 $text =~ s/\s/ /g;
249 sub view_seq_entity {
250 my ($self, $entity) = @_;
261 my ($self, $link) = @_;
263 # view_seq_text has already taken care of L<http://example.com/>
264 if ($link =~ /^<a href=/ ) {
268 # full-blown URL's are emitted as-is
269 if ($link =~ m{^\w+://}s ) {
270 return make_href($link);
273 $link =~ s/\n/ /g; # undo line-wrapped tags
275 my $orig_link = $link;
277 # strip the sub-title and the following '|' char
278 if ( $link =~ s/^ ([^|]+) \| //x ) {
282 # make sure sections start with a /
287 if ($link =~ m|^ (.*?) / "? (.*?) "? $|x) { # [name]/"section"
288 ($page, $section) = ($1, $2);
290 elsif ($link =~ /\s/) { # this must be a section with missing quotes
291 ($page, $section) = ('', $link);
294 ($page, $section) = ($link, '');
297 # warning; show some text.
298 $linktext = $orig_link unless defined $linktext;
301 if (defined $page && length $page) {
302 $url = $self->view_seq_link_transform_path($page);
305 # append the #section if exists
306 $url .= "#$section" if defined $url and
307 defined $section and length $section;
309 return make_href($url, $linktext);
313 # should be sub-classed if extra transformations are needed
315 # for example a sub-class may search for the given page and return a
316 # relative path to it.
318 # META: where this functionality should be documented? This module
319 # doesn't have docs section
321 sub view_seq_link_transform_path {
322 my($self, $page) = @_;
324 # right now the default transform doesn't check whether the link
325 # is not dead (i.e. whether there is a corresponding file.
326 # therefore we don't link L<>'s other than L<http://>
327 # subclass to change the default (and of course add validation)
329 # this is the minimal transformation that will be required if enabled
330 # $page = "$page.html";
332 #print "page $page\n";
338 my($url, $title) = @_;
341 return defined $title ? "<i>$title</i>" : '';
344 $title = $url unless defined $title;
345 #print "$url, $title\n";
346 return qq{<a href="$url">$title</a>};
352 # this code has been borrowed from Pod::Html
353 my $urls = '(' . join ('|',
365 my $gunk = '/#~:.?+=&%@!\-';
366 my $punc = '.:!?\-;';
367 my $any = "${ltrs}${gunk}${punc}";
370 my ($self, $text) = @_;
372 unless ($HTML_PROTECT) {
381 \b # start at word boundary
383 $urls : # need resource and a colon
384 (?!:) # Ignore File::, among others.
385 [$any] +? # followed by one or more of any valid
386 # character, but be conservative and
387 # take only what you need to....
389 (?= # look-ahead non-consumptive assertion
390 [$punc]* # either 0 or more punctuation followed
392 [^$any] # by a non-url char
394 $ # end of the string
397 $ # then end of the string
399 }{<a href="$1">$1</a>}igox;
405 my($self,$text) = @_;
407 return Encode::encode("ascii",$text,Encode::FB_XMLCREF());
418 HTML view of a Pod Object Model.
424 =item C<view($self, $type, $item)>
426 =item C<view_pod($self, $pod)>
428 =item C<view_head1($self, $head1)>
430 =item C<view_head2($self, $head2)>
432 =item C<view_head3($self, $head3)>
434 =item C<view_head4($self, $head4)>
436 =item C<view_over($self, $over)>
438 =item C<view_item($self, $item)>
440 =item C<view_for($self, $for)>
442 =item C<view_begin($self, $begin)>
444 =item C<view_textblock($self, $textblock)>
446 =item C<view_verbatim($self, $verbatim)>
448 =item C<view_meta($self, $meta)>
450 =item C<view_seq_bold($self, $text)>
452 Returns the text of a C<BE<lt>E<gt>> sequence enclosed in a C<E<lt>b<E<gt>> element.
454 =item C<view_seq_italic($self, $text)>
456 Returns the text of a C<IE<lt>E<gt>> sequence enclosed in a C<E<lt>i<E<gt>> element.
458 =item C<view_seq_code($self, $text)>
460 Returns the text of a C<CE<lt>E<gt>> sequence enclosed in a C<E<lt>code<E<gt>> element.
462 =item C<view_seq_file($self, $text)>
464 =item C<view_seq_entity($self, $text)>
466 =item C<view_seq_index($self, $text)>
468 Returns an empty string. Index sequences are suppressed in HTML view.
470 =item C<view_seq_link($self, $text)>
476 Andy Wardley E<lt>abw@kfs.orgE<gt>
478 =head1 COPYRIGHT AND LICENSE
480 Copyright (C) 2000 Andy Wardley. All Rights Reserved.
482 This module is free software; you can redistribute it and/or
483 modify it under the same terms as Perl itself.