1 #####################################################################
3 # The Perl::Tidy::HtmlWriter class writes a copy of the input stream in html
5 #####################################################################
7 package Perl::Tidy::HtmlWriter;
10 our $VERSION = '20220217';
24 $missing_html_entities
28 # replace unsafe characters with HTML entity representation if HTML::Entities
30 #{ eval "use HTML::Entities"; $missing_html_entities = $@; }
33 if ( !eval { require HTML::Entities; 1 } ) {
34 $missing_html_entities = $@ ? $@ : 1;
36 if ( !eval { require Pod::Html; 1 } ) {
37 $missing_pod_html = $@ ? $@ : 1;
43 # Catch any undefined sub calls so that we are sure to get
44 # some diagnostic information. This sub should never be called
45 # except for a programming error.
47 return if ( $AUTOLOAD =~ /\bDESTROY$/ );
48 my ( $pkg, $fname, $lno ) = caller();
49 my $my_package = __PACKAGE__;
51 ======================================================================
52 Error detected in package '$my_package', version $VERSION
53 Received unexpected AUTOLOAD call for sub '$AUTOLOAD'
54 Called from package: '$pkg'
55 Called from File '$fname' at line '$lno'
56 This error is probably due to a recent programming change
57 ======================================================================
64 # required to avoid call to AUTOLOAD in some versions of perl
69 my ( $class, @args ) = @_;
75 html_toc_extension => undef,
76 html_src_extension => undef,
78 my %args = ( %defaults, @args );
80 my $input_file = $args{input_file};
81 my $html_file = $args{html_file};
82 my $extension = $args{extension};
83 my $html_toc_extension = $args{html_toc_extension};
84 my $html_src_extension = $args{html_src_extension};
86 my $html_file_opened = 0;
88 ( $html_fh, my $html_filename ) =
89 Perl::Tidy::streamhandle( $html_file, 'w' );
91 Perl::Tidy::Warn("can't open $html_file: $!\n");
94 $html_file_opened = 1;
96 if ( !$input_file || $input_file eq '-' || ref($input_file) ) {
97 $input_file = "NONAME";
100 # write the table of contents to a string
102 my $html_toc_fh = Perl::Tidy::IOScalar->new( \$toc_string, 'w' );
105 my @pre_string_stack;
106 if ( $rOpts->{'html-pre-only'} ) {
108 # pre section goes directly to the output stream
109 $html_pre_fh = $html_fh;
110 $html_pre_fh->print( <<"PRE_END");
116 # pre section go out to a temporary string
118 $html_pre_fh = Perl::Tidy::IOScalar->new( \$pre_string, 'w' );
119 push @pre_string_stack, \$pre_string;
122 # pod text gets diverted if the 'pod2html' is used
125 if ( $rOpts->{'pod2html'} ) {
126 if ( $rOpts->{'html-pre-only'} ) {
127 undef $rOpts->{'pod2html'};
130 ##eval "use Pod::Html";
132 if ($missing_pod_html) {
134 "unable to find Pod::Html; cannot use pod2html\n-npod disables this message\n"
136 undef $rOpts->{'pod2html'};
139 $html_pod_fh = Perl::Tidy::IOScalar->new( \$pod_string, 'w' );
146 if ( $rOpts->{'frames'} ) {
147 unless ($extension) {
149 "cannot use frames without a specified output extension; ignoring -frm\n"
151 undef $rOpts->{'frames'};
154 $toc_filename = $input_file . $html_toc_extension . $extension;
155 $src_filename = $input_file . $html_src_extension . $extension;
159 # ----------------------------------------------------------
160 # Output is now directed as follows:
161 # html_toc_fh <-- table of contents items
162 # html_pre_fh <-- the <pre> section of formatted code, except:
163 # html_pod_fh <-- pod goes here with the pod2html option
164 # ----------------------------------------------------------
166 my $title = $rOpts->{'title'};
168 ( $title, my $path ) = fileparse($input_file);
170 my $toc_item_count = 0;
171 my $in_toc_package = "";
174 _input_file => $input_file, # name of input file
175 _title => $title, # title, unescaped
176 _html_file => $html_file, # name of .html output file
177 _toc_filename => $toc_filename, # for frames option
178 _src_filename => $src_filename, # for frames option
179 _html_file_opened => $html_file_opened, # a flag
180 _html_fh => $html_fh, # the output stream
181 _html_pre_fh => $html_pre_fh, # pre section goes here
182 _rpre_string_stack => \@pre_string_stack, # stack of pre sections
183 _html_pod_fh => $html_pod_fh, # pod goes here if pod2html
184 _rpod_string => \$pod_string, # string holding pod
185 _pod_cut_count => 0, # how many =cut's?
186 _html_toc_fh => $html_toc_fh, # fh for table of contents
187 _rtoc_string => \$toc_string, # string holding toc
188 _rtoc_item_count => \$toc_item_count, # how many toc items
189 _rin_toc_package => \$in_toc_package, # package name
190 _rtoc_name_count => {}, # hash to track unique names
191 _rpackage_stack => [], # stack to check for package
193 _rlast_level => \$last_level, # brace indentation level
200 # returns true if close works, false if not
201 # failure probably means there is no close method
202 return eval { $object->close(); 1 };
207 # Add an item to the html table of contents.
208 # This is called even if no table of contents is written,
209 # because we still want to put the anchors in the <pre> text.
210 # We are given an anchor name and its type; types are:
211 # 'package', 'sub', '__END__', '__DATA__', 'EOF'
212 # There must be an 'EOF' call at the end to wrap things up.
213 my ( $self, $name, $type ) = @_;
214 my $html_toc_fh = $self->{_html_toc_fh};
215 my $html_pre_fh = $self->{_html_pre_fh};
216 my $rtoc_name_count = $self->{_rtoc_name_count};
217 my $rtoc_item_count = $self->{_rtoc_item_count};
218 my $rlast_level = $self->{_rlast_level};
219 my $rin_toc_package = $self->{_rin_toc_package};
220 my $rpackage_stack = $self->{_rpackage_stack};
222 # packages contain sublists of subs, so to avoid errors all package
223 # items are written and finished with the following routines
224 my $end_package_list = sub {
225 if ( ${$rin_toc_package} ) {
226 $html_toc_fh->print("</ul>\n</li>\n");
227 ${$rin_toc_package} = "";
232 my $start_package_list = sub {
233 my ( $unique_name, $package ) = @_;
234 if ( ${$rin_toc_package} ) { $end_package_list->() }
235 $html_toc_fh->print(<<EOM);
236 <li><a href=\"#$unique_name\">package $package</a>
239 ${$rin_toc_package} = $package;
243 # start the table of contents on the first item
244 unless ( ${$rtoc_item_count} ) {
246 # but just quit if we hit EOF without any other entries
247 # in this case, there will be no toc
248 return if ( $type eq 'EOF' );
249 $html_toc_fh->print( <<"TOC_END");
250 <!-- BEGIN CODE INDEX --><a name="code-index"></a>
254 ${$rtoc_item_count}++;
256 # make a unique anchor name for this location:
257 # - packages get a 'package-' prefix
258 # - subs use their names
259 my $unique_name = $name;
260 if ( $type eq 'package' ) { $unique_name = "package-$name" }
262 # append '-1', '-2', etc if necessary to make unique; this will
263 # be unique because subs and packages cannot have a '-'
264 if ( my $count = $rtoc_name_count->{ lc $unique_name }++ ) {
265 $unique_name .= "-$count";
268 # - all names get terminal '-' if pod2html is used, to avoid
269 # conflicts with anchor names created by pod2html
270 if ( $rOpts->{'pod2html'} ) { $unique_name .= '-' }
272 # start/stop lists of subs
273 if ( $type eq 'sub' ) {
274 my $package = $rpackage_stack->[ ${$rlast_level} ];
275 unless ($package) { $package = 'main' }
277 # if we're already in a package/sub list, be sure its the right
278 # package or else close it
279 if ( ${$rin_toc_package} && ${$rin_toc_package} ne $package ) {
280 $end_package_list->();
283 # start a package/sub list if necessary
284 unless ( ${$rin_toc_package} ) {
285 $start_package_list->( $unique_name, $package );
289 # now write an entry in the toc for this item
290 if ( $type eq 'package' ) {
291 $start_package_list->( $unique_name, $name );
293 elsif ( $type eq 'sub' ) {
294 $html_toc_fh->print("<li><a href=\"#$unique_name\">$name</a></li>\n");
297 $end_package_list->();
298 $html_toc_fh->print("<li><a href=\"#$unique_name\">$name</a></li>\n");
301 # write the anchor in the <pre> section
302 $html_pre_fh->print("<a name=\"$unique_name\"></a>");
304 # end the table of contents, if any, on the end of file
305 if ( $type eq 'EOF' ) {
306 $html_toc_fh->print( <<"TOC_END");
308 <!-- END CODE INDEX -->
316 # This is the official list of tokens which may be identified by the
317 # user. Long names are used as getopt keys. Short names are
318 # convenient short abbreviations for specifying input. Short names
319 # somewhat resemble token type characters, but are often different
320 # because they may only be alphanumeric, to allow command line
321 # input. Also, note that because of case insensitivity of html,
322 # this table must be in a single case only (I've chosen to use all
324 # When adding NEW_TOKENS: update this hash table
325 # short names => long names
326 %short_to_long_names = (
336 'pu' => 'punctuation',
339 'h' => 'here-doc-target',
340 'hh' => 'here-doc-text',
347 # Now we have to map actual token types into one of the above short
348 # names; any token types not mapped will get 'punctuation'
351 # The values of this hash table correspond to the keys of the
352 # previous hash table.
353 # The keys of this hash table are token types and can be seen
354 # by running with --dump-token-types (-dtt).
356 # When adding NEW_TOKENS: update this hash table
357 # $type => $short_name
358 %token_short_names = (
383 # These token types will all be called identifiers for now
384 # FIXME: could separate user defined modules as separate type
385 my @identifier = qw< i t U C Y Z G :: CORE::>;
386 @token_short_names{@identifier} = ('i') x scalar(@identifier);
388 # These token types will be called 'structure'
389 my @structure = qw< { } >;
390 @token_short_names{@structure} = ('s') x scalar(@structure);
392 # OLD NOTES: save for reference
393 # Any of these could be added later if it would be useful.
394 # For now, they will by default become punctuation
395 # my @list = qw< L R [ ] >;
396 # @token_long_names{@list} = ('non-structure') x scalar(@list);
399 # / /= * *= ** **= + += - -= % %= = ++ -- << <<= >> >>= pp p m mm
401 # @token_long_names{@list} = ('math') x scalar(@list);
403 # my @list = qw" & &= ~ ~= ^ ^= | |= ";
404 # @token_long_names{@list} = ('bit') x scalar(@list);
406 # my @list = qw" == != < > <= <=> ";
407 # @token_long_names{@list} = ('numerical-comparison') x scalar(@list);
409 # my @list = qw" && || ! &&= ||= //= ";
410 # @token_long_names{@list} = ('logical') x scalar(@list);
412 # my @list = qw" . .= =~ !~ x x= ";
413 # @token_long_names{@list} = ('string-operators') x scalar(@list);
416 # my @list = qw" .. -> <> ... \ ? ";
417 # @token_long_names{@list} = ('misc-operators') x scalar(@list);
421 sub make_getopt_long_names {
422 my ( $class, $rgetopt_names ) = @_;
423 while ( my ( $short_name, $name ) = each %short_to_long_names ) {
424 push @{$rgetopt_names}, "html-color-$name=s";
425 push @{$rgetopt_names}, "html-italic-$name!";
426 push @{$rgetopt_names}, "html-bold-$name!";
428 push @{$rgetopt_names}, "html-color-background=s";
429 push @{$rgetopt_names}, "html-linked-style-sheet=s";
430 push @{$rgetopt_names}, "nohtml-style-sheets";
431 push @{$rgetopt_names}, "html-pre-only";
432 push @{$rgetopt_names}, "html-line-numbers";
433 push @{$rgetopt_names}, "html-entities!";
434 push @{$rgetopt_names}, "stylesheet";
435 push @{$rgetopt_names}, "html-table-of-contents!";
436 push @{$rgetopt_names}, "pod2html!";
437 push @{$rgetopt_names}, "frames!";
438 push @{$rgetopt_names}, "html-toc-extension=s";
439 push @{$rgetopt_names}, "html-src-extension=s";
441 # Pod::Html parameters:
442 push @{$rgetopt_names}, "backlink=s";
443 push @{$rgetopt_names}, "cachedir=s";
444 push @{$rgetopt_names}, "htmlroot=s";
445 push @{$rgetopt_names}, "libpods=s";
446 push @{$rgetopt_names}, "podpath=s";
447 push @{$rgetopt_names}, "podroot=s";
448 push @{$rgetopt_names}, "title=s";
450 # Pod::Html parameters with leading 'pod' which will be removed
451 # before the call to Pod::Html
452 push @{$rgetopt_names}, "podquiet!";
453 push @{$rgetopt_names}, "podverbose!";
454 push @{$rgetopt_names}, "podrecurse!";
455 push @{$rgetopt_names}, "podflush";
456 push @{$rgetopt_names}, "podheader!";
457 push @{$rgetopt_names}, "podindex!";
461 sub make_abbreviated_names {
463 # We're appending things like this to the expansion list:
464 # 'hcc' => [qw(html-color-comment)],
465 # 'hck' => [qw(html-color-keyword)],
467 my ( $class, $rexpansion ) = @_;
469 # abbreviations for color/bold/italic properties
470 while ( my ( $short_name, $long_name ) = each %short_to_long_names ) {
471 ${$rexpansion}{"hc$short_name"} = ["html-color-$long_name"];
472 ${$rexpansion}{"hb$short_name"} = ["html-bold-$long_name"];
473 ${$rexpansion}{"hi$short_name"} = ["html-italic-$long_name"];
474 ${$rexpansion}{"nhb$short_name"} = ["nohtml-bold-$long_name"];
475 ${$rexpansion}{"nhi$short_name"} = ["nohtml-italic-$long_name"];
478 # abbreviations for all other html options
479 ${$rexpansion}{"hcbg"} = ["html-color-background"];
480 ${$rexpansion}{"pre"} = ["html-pre-only"];
481 ${$rexpansion}{"toc"} = ["html-table-of-contents"];
482 ${$rexpansion}{"ntoc"} = ["nohtml-table-of-contents"];
483 ${$rexpansion}{"nnn"} = ["html-line-numbers"];
484 ${$rexpansion}{"hent"} = ["html-entities"];
485 ${$rexpansion}{"nhent"} = ["nohtml-entities"];
486 ${$rexpansion}{"css"} = ["html-linked-style-sheet"];
487 ${$rexpansion}{"nss"} = ["nohtml-style-sheets"];
488 ${$rexpansion}{"ss"} = ["stylesheet"];
489 ${$rexpansion}{"pod"} = ["pod2html"];
490 ${$rexpansion}{"npod"} = ["nopod2html"];
491 ${$rexpansion}{"frm"} = ["frames"];
492 ${$rexpansion}{"nfrm"} = ["noframes"];
493 ${$rexpansion}{"text"} = ["html-toc-extension"];
494 ${$rexpansion}{"sext"} = ["html-src-extension"];
500 # This will be called once after options have been parsed
501 # Note that we are defining the package variable $rOpts here:
502 ( my $class, $rOpts ) = @_;
504 # X11 color names for default settings that seemed to look ok
505 # (these color names are only used for programming clarity; the hex
506 # numbers are actually written)
507 use constant ForestGreen => "#228B22";
508 use constant SaddleBrown => "#8B4513";
509 use constant magenta4 => "#8B008B";
510 use constant IndianRed3 => "#CD5555";
511 use constant DeepSkyBlue4 => "#00688B";
512 use constant MediumOrchid3 => "#B452CD";
513 use constant black => "#000000";
514 use constant white => "#FFFFFF";
515 use constant red => "#FF0000";
517 # set default color, bold, italic properties
518 # anything not listed here will be given the default (punctuation) color --
519 # these types currently not listed and get default: ws pu s sc cm co p
520 # When adding NEW_TOKENS: add an entry here if you don't want defaults
522 # set_default_properties( $short_name, default_color, bold?, italic? );
523 set_default_properties( 'c', ForestGreen, 0, 0 );
524 set_default_properties( 'pd', ForestGreen, 0, 1 );
525 set_default_properties( 'k', magenta4, 1, 0 ); # was SaddleBrown
526 set_default_properties( 'q', IndianRed3, 0, 0 );
527 set_default_properties( 'hh', IndianRed3, 0, 1 );
528 set_default_properties( 'h', IndianRed3, 1, 0 );
529 set_default_properties( 'i', DeepSkyBlue4, 0, 0 );
530 set_default_properties( 'w', black, 0, 0 );
531 set_default_properties( 'n', MediumOrchid3, 0, 0 );
532 set_default_properties( 'v', MediumOrchid3, 0, 0 );
533 set_default_properties( 'j', IndianRed3, 1, 0 );
534 set_default_properties( 'm', red, 1, 0 );
536 set_default_color( 'html-color-background', white );
537 set_default_color( 'html-color-punctuation', black );
539 # setup property lookup tables for tokens based on their short names
540 # every token type has a short name, and will use these tables
541 # to do the html markup
542 while ( my ( $short_name, $long_name ) = each %short_to_long_names ) {
543 $html_color{$short_name} = $rOpts->{"html-color-$long_name"};
544 $html_bold{$short_name} = $rOpts->{"html-bold-$long_name"};
545 $html_italic{$short_name} = $rOpts->{"html-italic-$long_name"};
548 # write style sheet to STDOUT and die if requested
549 if ( defined( $rOpts->{'stylesheet'} ) ) {
550 write_style_sheet_file('-');
554 # make sure user gives a file name after -css
555 if ( defined( $rOpts->{'html-linked-style-sheet'} ) ) {
556 $css_linkname = $rOpts->{'html-linked-style-sheet'};
557 if ( $css_linkname =~ /^-/ ) {
558 Perl::Tidy::Die("You must specify a valid filename after -css\n");
563 if ( $css_linkname && $rOpts->{'nohtml-style-sheets'} ) {
564 $rOpts->{'nohtml-style-sheets'} = 0;
566 "You can't specify both -css and -nss; -nss ignored\n");
569 # write a style sheet file if necessary
572 # if the selected filename exists, don't write, because user may
573 # have done some work by hand to create it; use backup name instead
574 # Also, this will avoid a potential disaster in which the user
575 # forgets to specify the style sheet, like this:
576 # perltidy -html -css myfile1.pl myfile2.pl
577 # This would cause myfile1.pl to parsed as the style sheet by GetOpts
578 my $css_filename = $css_linkname;
579 unless ( -e $css_filename ) {
580 write_style_sheet_file($css_filename);
583 $missing_html_entities = 1 unless $rOpts->{'html-entities'};
587 sub write_style_sheet_file {
589 my $css_filename = shift;
591 unless ( $fh = IO::File->new("> $css_filename") ) {
592 Perl::Tidy::Die("can't open $css_filename: $!\n");
594 write_style_sheet_data($fh);
599 sub write_style_sheet_data {
601 # write the style sheet data to an open file handle
604 my $bg_color = $rOpts->{'html-color-background'};
605 my $text_color = $rOpts->{'html-color-punctuation'};
607 # pre-bgcolor is new, and may not be defined
608 my $pre_bg_color = $rOpts->{'html-pre-color-background'};
609 $pre_bg_color = $bg_color unless $pre_bg_color;
612 /* default style sheet generated by perltidy */
613 body {background: $bg_color; color: $text_color}
614 pre { color: $text_color;
615 background: $pre_bg_color;
616 font-family: courier;
621 foreach my $short_name ( sort keys %short_to_long_names ) {
622 my $long_name = $short_to_long_names{$short_name};
624 my $abbrev = '.' . $short_name;
625 if ( length($short_name) == 1 ) { $abbrev .= ' ' } # for alignment
626 my $color = $html_color{$short_name};
627 if ( !defined($color) ) { $color = $text_color }
628 $fh->print("$abbrev \{ color: $color;");
630 if ( $html_bold{$short_name} ) {
631 $fh->print(" font-weight:bold;");
634 if ( $html_italic{$short_name} ) {
635 $fh->print(" font-style:italic;");
637 $fh->print("} /* $long_name */\n");
642 sub set_default_color {
644 # make sure that options hash $rOpts->{$key} contains a valid color
645 my ( $key, $color ) = @_;
646 if ( $rOpts->{$key} ) { $color = $rOpts->{$key} }
647 $rOpts->{$key} = check_RGB($color);
653 # if color is a 6 digit hex RGB value, prepend a #, otherwise
654 # assume that it is a valid ascii color name
656 if ( $color =~ /^[0-9a-fA-F]{6,6}$/ ) { $color = "#$color" }
660 sub set_default_properties {
661 my ( $short_name, $color, $bold, $italic ) = @_;
663 set_default_color( "html-color-$short_to_long_names{$short_name}", $color );
665 $key = "html-bold-$short_to_long_names{$short_name}";
666 $rOpts->{$key} = ( defined $rOpts->{$key} ) ? $rOpts->{$key} : $bold;
667 $key = "html-italic-$short_to_long_names{$short_name}";
668 $rOpts->{$key} = ( defined $rOpts->{$key} ) ? $rOpts->{$key} : $italic;
674 # Use Pod::Html to process the pod and make the page
675 # then merge the perltidy code sections into it.
676 # return 1 if success, 0 otherwise
677 my ( $self, $pod_string, $css_string, $toc_string, $rpre_string_stack ) =
679 my $input_file = $self->{_input_file};
680 my $title = $self->{_title};
681 my $success_flag = 0;
683 # don't try to use pod2html if no pod
684 unless ($pod_string) {
685 return $success_flag;
688 # Pod::Html requires a real temporary filename
689 my ( $fh_tmp, $tmpfile ) = File::Temp::tempfile();
692 "unable to open temporary file $tmpfile; cannot use pod2html\n");
693 return $success_flag;
696 #------------------------------------------------------------------
697 # Warning: a temporary file is open; we have to clean up if
698 # things go bad. From here on all returns should be by going to
699 # RETURN so that the temporary file gets unlinked.
700 #------------------------------------------------------------------
702 # write the pod text to the temporary file
703 $fh_tmp->print($pod_string);
706 # Hand off the pod to pod2html.
707 # Note that we can use the same temporary filename for input and output
708 # because of the way pod2html works.
712 push @args, "--infile=$tmpfile", "--outfile=$tmpfile", "--title=$title";
714 # Flags with string args:
715 # "backlink=s", "cachedir=s", "htmlroot=s", "libpods=s",
716 # "podpath=s", "podroot=s"
717 # Note: -css=s is handled by perltidy itself
718 foreach my $kw (qw(backlink cachedir htmlroot libpods podpath podroot))
720 if ( $rOpts->{$kw} ) { push @args, "--$kw=$rOpts->{$kw}" }
723 # Toggle switches; these have extra leading 'pod'
724 # "header!", "index!", "recurse!", "quiet!", "verbose!"
725 foreach my $kw (qw(podheader podindex podrecurse podquiet podverbose)) {
726 my $kwd = $kw; # allows us to strip 'pod'
727 if ( $rOpts->{$kw} ) { $kwd =~ s/^pod//; push @args, "--$kwd" }
728 elsif ( defined( $rOpts->{$kw} ) ) {
730 push @args, "--no$kwd";
736 if ( $rOpts->{$kw} ) { $kw =~ s/^pod//; push @args, "--$kw" }
738 # Must clean up if pod2html dies (it can);
739 # Be careful not to overwrite callers __DIE__ routine
740 local $SIG{__DIE__} = sub {
741 unlink $tmpfile if -e $tmpfile;
742 Perl::Tidy::Die( $_[0] );
745 Pod::Html::pod2html(@args);
747 $fh_tmp = IO::File->new( $tmpfile, 'r' );
750 # this error shouldn't happen ... we just used this filename
752 "unable to open temporary file $tmpfile; cannot use pod2html\n");
756 my $html_fh = $self->{_html_fh};
762 # This routine will write the html selectively and store the toc
763 my $html_print = sub {
765 $html_fh->print($_) unless ($no_print);
766 if ($in_toc) { push @toc, $_ }
771 # loop over lines of html output from pod2html and merge in
772 # the necessary perltidy html sections
773 my ( $saw_body, $saw_index, $saw_body_end );
776 if ( $rOpts->{'timestamp'} ) {
777 my $date = localtime;
778 $timestamp = "on $date";
780 while ( my $line = $fh_tmp->getline() ) {
782 if ( $line =~ /^\s*<html>\s*$/i ) {
783 ##my $date = localtime;
784 ##$html_print->("<!-- Generated by perltidy on $date -->\n");
785 $html_print->("<!-- Generated by perltidy $timestamp -->\n");
786 $html_print->($line);
789 # Copy the perltidy css, if any, after <body> tag
790 elsif ( $line =~ /^\s*<body.*>\s*$/i ) {
792 $html_print->($css_string) if $css_string;
793 $html_print->($line);
795 # add a top anchor and heading
796 $html_print->("<a name=\"-top-\"></a>\n");
797 $title = escape_html($title);
798 $html_print->("<h1>$title</h1>\n");
801 # check for start of index, old pod2html
802 # before Pod::Html VERSION 1.15_02 it is delimited by comments as:
803 # <!-- INDEX BEGIN -->
809 elsif ( $line =~ /^\s*<!-- INDEX BEGIN -->\s*$/i ) {
812 # when frames are used, an extra table of contents in the
813 # contents panel is confusing, so don't print it
814 $no_print = $rOpts->{'frames'}
815 || !$rOpts->{'html-table-of-contents'};
816 $html_print->("<h2>Doc Index:</h2>\n") if $rOpts->{'frames'};
817 $html_print->($line);
820 # check for start of index, new pod2html
821 # After Pod::Html VERSION 1.15_02 it is delimited as:
825 elsif ( $line =~ /^\s*<ul\s+id="index">/i ) {
829 # when frames are used, an extra table of contents in the
830 # contents panel is confusing, so don't print it
831 $no_print = $rOpts->{'frames'}
832 || !$rOpts->{'html-table-of-contents'};
833 $html_print->("<h2>Doc Index:</h2>\n") if $rOpts->{'frames'};
834 $html_print->($line);
837 # Check for end of index, old pod2html
838 elsif ( $line =~ /^\s*<!-- INDEX END -->\s*$/i ) {
840 $html_print->($line);
842 # Copy the perltidy toc, if any, after the Pod::Html toc
844 $html_print->("<hr />\n") if $rOpts->{'frames'};
845 $html_print->("<h2>Code Index:</h2>\n");
846 ##my @toc = map { $_ .= "\n" } split /\n/, $toc_string;
847 my @toc = map { $_ . "\n" } split /\n/, $toc_string;
854 # must track <ul> depth level for new pod2html
855 elsif ( $line =~ /\s*<ul>\s*$/i && $in_toc eq 'UL' ) {
857 $html_print->($line);
860 # Check for end of index, for new pod2html
861 elsif ( $line =~ /\s*<\/ul>/i && $in_toc eq 'UL' ) {
863 $html_print->($line);
865 # Copy the perltidy toc, if any, after the Pod::Html toc
866 if ( $ul_level <= 0 ) {
869 $html_print->("<hr />\n") if $rOpts->{'frames'};
870 $html_print->("<h2>Code Index:</h2>\n");
871 ##my @toc = map { $_ .= "\n" } split /\n/, $toc_string;
872 my @toc = map { $_ . "\n" } split /\n/, $toc_string;
881 # Copy one perltidy section after each marker
882 elsif ( $line =~ /^(.*)<!-- pERLTIDY sECTION -->(.*)$/ ) {
884 $html_print->($1) if $1;
886 # Intermingle code and pod sections if we saw multiple =cut's.
887 if ( $self->{_pod_cut_count} > 1 ) {
888 my $rpre_string = shift( @{$rpre_string_stack} );
889 if ( ${$rpre_string} ) {
890 $html_print->('<pre>');
891 $html_print->( ${$rpre_string} );
892 $html_print->('</pre>');
896 # shouldn't happen: we stored a string before writing
899 "Problem merging html stream with pod2html; order may be wrong\n"
902 $html_print->($line);
905 # If didn't see multiple =cut lines, we'll put the pod out first
906 # and then the code, because it's less confusing.
909 # since we are not intermixing code and pod, we don't need
910 # or want any <hr> lines which separated pod and code
911 $html_print->($line) unless ( $line =~ /^\s*<hr>\s*$/i );
915 # Copy any remaining code section before the </body> tag
916 elsif ( $line =~ /^\s*<\/body>\s*$/i ) {
918 if ( @{$rpre_string_stack} ) {
919 unless ( $self->{_pod_cut_count} > 1 ) {
920 $html_print->('<hr />');
922 while ( my $rpre_string = shift( @{$rpre_string_stack} ) ) {
923 $html_print->('<pre>');
924 $html_print->( ${$rpre_string} );
925 $html_print->('</pre>');
928 $html_print->($line);
931 $html_print->($line);
937 Perl::Tidy::Warn("Did not see <body> in pod2html output\n");
940 unless ($saw_body_end) {
941 Perl::Tidy::Warn("Did not see </body> in pod2html output\n");
944 unless ($saw_index) {
945 Perl::Tidy::Warn("Did not find INDEX END in pod2html output\n");
950 close_object($html_fh);
952 # note that we have to unlink tmpfile before making frames
953 # because the tmpfile may be one of the names used for frames
955 unless ( unlink($tmpfile) ) {
956 Perl::Tidy::Warn("couldn't unlink temporary file $tmpfile: $!\n");
961 if ( $success_flag && $rOpts->{'frames'} ) {
962 $self->make_frame( \@toc );
964 return $success_flag;
969 # Make a frame with table of contents in the left panel
970 # and the text in the right panel.
972 # $html_filename contains the no-frames html output
973 # $rtoc is a reference to an array with the table of contents
974 my ( $self, $rtoc ) = @_;
975 my $input_file = $self->{_input_file};
976 my $html_filename = $self->{_html_file};
977 my $toc_filename = $self->{_toc_filename};
978 my $src_filename = $self->{_src_filename};
979 my $title = $self->{_title};
980 $title = escape_html($title);
982 # FUTURE input parameter:
983 my $top_basename = "";
985 # We need to produce 3 html files:
986 # 1. - the table of contents
987 # 2. - the contents (source code) itself
988 # 3. - the frame which contains them
990 # get basenames for relative links
991 my ( $toc_basename, $toc_path ) = fileparse($toc_filename);
992 my ( $src_basename, $src_path ) = fileparse($src_filename);
994 # 1. Make the table of contents panel, with appropriate changes
995 # to the anchor names
996 my $src_frame_name = 'SRC';
998 write_toc_html( $title, $toc_filename, $src_basename, $rtoc,
1001 # 2. The current .html filename is renamed to be the contents panel
1002 rename( $html_filename, $src_filename )
1003 or Perl::Tidy::Die("Cannot rename $html_filename to $src_filename:$!\n");
1005 # 3. Then use the original html filename for the frame
1007 $title, $html_filename, $top_basename,
1008 $toc_basename, $src_basename, $src_frame_name
1013 sub write_toc_html {
1015 # write a separate html table of contents file for frames
1016 my ( $title, $toc_filename, $src_basename, $rtoc, $src_frame_name ) = @_;
1017 my $fh = IO::File->new( $toc_filename, 'w' )
1018 or Perl::Tidy::Die("Cannot open $toc_filename:$!\n");
1022 <title>$title</title>
1025 <h1><a href=\"$src_basename#-top-" target="$src_frame_name">$title</a></h1>
1029 change_anchor_names( $rtoc, $src_basename, "$src_frame_name" );
1030 $fh->print( join "", @{$rtoc} );
1040 sub write_frame_html {
1042 # write an html file to be the table of contents frame
1044 $title, $frame_filename, $top_basename,
1045 $toc_basename, $src_basename, $src_frame_name
1048 my $fh = IO::File->new( $frame_filename, 'w' )
1049 or Perl::Tidy::Die("Cannot open $toc_basename:$!\n");
1052 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
1053 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
1054 <?xml version="1.0" encoding="iso-8859-1" ?>
1055 <html xmlns="http://www.w3.org/1999/xhtml">
1057 <title>$title</title>
1061 # two left panels, one right, if master index file
1062 if ($top_basename) {
1064 <frameset cols="20%,80%">
1065 <frameset rows="30%,70%">
1066 <frame src = "$top_basename" />
1067 <frame src = "$toc_basename" />
1072 # one left panels, one right, if no master index file
1075 <frameset cols="20%,*">
1076 <frame src = "$toc_basename" />
1080 <frame src = "$src_basename" name = "$src_frame_name" />
1083 <p>If you see this message, you are using a non-frame-capable web client.</p>
1084 <p>This document contains:</p>
1086 <li><a href="$toc_basename">A table of contents</a></li>
1087 <li><a href="$src_basename">The source code</a></li>
1097 sub change_anchor_names {
1099 # add a filename and target to anchors
1100 # also return the first anchor
1101 my ( $rlines, $filename, $target ) = @_;
1103 foreach my $line ( @{$rlines} ) {
1105 # We're looking for lines like this:
1106 # <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
1107 # ---- - -------- -----------------
1109 if ( $line =~ /^(.*)<a(.*)href\s*=\s*"([^#]*)#([^"]+)"[^>]*>(.*)$/i ) {
1113 my $href = "$filename#$name";
1114 $line = "$pre<a href=\"$href\" target=\"$target\">$post\n";
1115 unless ($first_anchor) { $first_anchor = $href }
1118 return $first_anchor;
1121 sub close_html_file {
1123 return unless $self->{_html_file_opened};
1125 my $html_fh = $self->{_html_fh};
1126 my $rtoc_string = $self->{_rtoc_string};
1128 # There are 3 basic paths to html output...
1130 # ---------------------------------
1131 # Path 1: finish up if in -pre mode
1132 # ---------------------------------
1133 if ( $rOpts->{'html-pre-only'} ) {
1134 $html_fh->print( <<"PRE_END");
1137 close_object($html_fh);
1142 $self->add_toc_item( 'EOF', 'EOF' );
1144 my $rpre_string_stack = $self->{_rpre_string_stack};
1146 # Patch to darken the <pre> background color in case of pod2html and
1147 # interleaved code/documentation. Otherwise, the distinction
1148 # between code and documentation is blurred.
1149 if ( $rOpts->{pod2html}
1150 && $self->{_pod_cut_count} >= 1
1151 && $rOpts->{'html-color-background'} eq '#FFFFFF' )
1153 $rOpts->{'html-pre-color-background'} = '#F0F0F0';
1156 # put the css or its link into a string, if used
1158 my $fh_css = Perl::Tidy::IOScalar->new( \$css_string, 'w' );
1160 # use css linked to another file
1161 if ( $rOpts->{'html-linked-style-sheet'} ) {
1163 qq(<link rel="stylesheet" href="$css_linkname" type="text/css" />));
1166 # use css embedded in this file
1167 elsif ( !$rOpts->{'nohtml-style-sheets'} ) {
1168 $fh_css->print( <<'ENDCSS');
1169 <style type="text/css">
1172 write_style_sheet_data($fh_css);
1173 $fh_css->print( <<"ENDCSS");
1179 # -----------------------------------------------------------
1180 # path 2: use pod2html if requested
1181 # If we fail for some reason, continue on to path 3
1182 # -----------------------------------------------------------
1183 if ( $rOpts->{'pod2html'} ) {
1184 my $rpod_string = $self->{_rpod_string};
1186 ${$rpod_string}, $css_string,
1187 ${$rtoc_string}, $rpre_string_stack
1191 # --------------------------------------------------
1192 # path 3: write code in html, with pod only in italics
1193 # --------------------------------------------------
1194 my $input_file = $self->{_input_file};
1195 my $title = escape_html($input_file);
1197 if ( $rOpts->{'timestamp'} ) {
1198 my $date = localtime;
1199 $timestamp = "on $date";
1201 $html_fh->print( <<"HTML_START");
1202 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
1203 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1204 <!-- Generated by perltidy $timestamp -->
1205 <html xmlns="http://www.w3.org/1999/xhtml">
1207 <title>$title</title>
1210 # output the css, if used
1212 $html_fh->print($css_string);
1213 $html_fh->print( <<"ENDCSS");
1220 $html_fh->print( <<"HTML_START");
1222 <body bgcolor=\"$rOpts->{'html-color-background'}\" text=\"$rOpts->{'html-color-punctuation'}\">
1226 $html_fh->print("<a name=\"-top-\"></a>\n");
1227 $html_fh->print( <<"EOM");
1231 # copy the table of contents
1232 if ( ${$rtoc_string}
1233 && !$rOpts->{'frames'}
1234 && $rOpts->{'html-table-of-contents'} )
1236 $html_fh->print( ${$rtoc_string} );
1239 # copy the pre section(s)
1240 my $fname_comment = $input_file;
1241 $fname_comment =~ s/--+/-/g; # protect HTML comment tags
1242 $html_fh->print( <<"END_PRE");
1244 <!-- contents of filename: $fname_comment -->
1248 foreach my $rpre_string ( @{$rpre_string_stack} ) {
1249 $html_fh->print( ${$rpre_string} );
1252 # and finish the html page
1253 $html_fh->print( <<"HTML_END");
1258 close_object($html_fh);
1260 if ( $rOpts->{'frames'} ) {
1261 ##my @toc = map { $_ .= "\n" } split /\n/, ${$rtoc_string};
1262 my @toc = map { $_ . "\n" } split /\n/, ${$rtoc_string};
1263 $self->make_frame( \@toc );
1269 my ( $self, $rtokens, $rtoken_type, $rlevels ) = @_;
1270 my ( @colored_tokens, $type, $token, $level );
1271 my $rlast_level = $self->{_rlast_level};
1272 my $rpackage_stack = $self->{_rpackage_stack};
1274 for ( my $j = 0 ; $j < @{$rtoken_type} ; $j++ ) {
1275 $type = $rtoken_type->[$j];
1276 $token = $rtokens->[$j];
1277 $level = $rlevels->[$j];
1278 $level = 0 if ( $level < 0 );
1280 #-------------------------------------------------------
1281 # Update the package stack. The package stack is needed to keep
1282 # the toc correct because some packages may be declared within
1283 # blocks and go out of scope when we leave the block.
1284 #-------------------------------------------------------
1285 if ( $level > ${$rlast_level} ) {
1286 unless ( $rpackage_stack->[ $level - 1 ] ) {
1287 $rpackage_stack->[ $level - 1 ] = 'main';
1289 $rpackage_stack->[$level] = $rpackage_stack->[ $level - 1 ];
1291 elsif ( $level < ${$rlast_level} ) {
1292 my $package = $rpackage_stack->[$level];
1293 unless ($package) { $package = 'main' }
1295 # if we change packages due to a nesting change, we
1296 # have to make an entry in the toc
1297 if ( $package ne $rpackage_stack->[ $level + 1 ] ) {
1298 $self->add_toc_item( $package, 'package' );
1301 ${$rlast_level} = $level;
1303 #-------------------------------------------------------
1304 # Intercept a sub name here; split it
1305 # into keyword 'sub' and sub name; and add an
1307 #-------------------------------------------------------
1308 if ( $type eq 'i' && $token =~ /^(sub\s+)(\w.*)$/ ) {
1309 $token = $self->markup_html_element( $1, 'k' );
1310 push @colored_tokens, $token;
1314 # but don't include sub declarations in the toc;
1315 # these wlll have leading token types 'i;'
1316 my $signature = join "", @{$rtoken_type};
1317 unless ( $signature =~ /^i;/ ) {
1318 my $subname = $token;
1319 $subname =~ s/[\s\(].*$//; # remove any attributes and prototype
1320 $self->add_toc_item( $subname, 'sub' );
1324 #-------------------------------------------------------
1325 # Intercept a package name here; split it
1326 # into keyword 'package' and name; add to the toc,
1327 # and update the package stack
1328 #-------------------------------------------------------
1329 if ( $type eq 'i' && $token =~ /^(package\s+)(\w.*)$/ ) {
1330 $token = $self->markup_html_element( $1, 'k' );
1331 push @colored_tokens, $token;
1334 $self->add_toc_item( "$token", 'package' );
1335 $rpackage_stack->[$level] = $token;
1338 $token = $self->markup_html_element( $token, $type );
1339 push @colored_tokens, $token;
1341 return ( \@colored_tokens );
1344 sub markup_html_element {
1345 my ( $self, $token, $type ) = @_;
1347 return $token if ( $type eq 'b' ); # skip a blank token
1348 return $token if ( $token =~ /^\s*$/ ); # skip a blank line
1349 $token = escape_html($token);
1351 # get the short abbreviation for this token type
1352 my $short_name = $token_short_names{$type};
1353 if ( !defined($short_name) ) {
1354 $short_name = "pu"; # punctuation is default
1357 # handle style sheets..
1358 if ( !$rOpts->{'nohtml-style-sheets'} ) {
1359 if ( $short_name ne 'pu' ) {
1360 $token = qq(<span class="$short_name">) . $token . "</span>";
1364 # handle no style sheets..
1366 my $color = $html_color{$short_name};
1368 if ( $color && ( $color ne $rOpts->{'html-color-punctuation'} ) ) {
1369 $token = qq(<font color="$color">) . $token . "</font>";
1371 if ( $html_italic{$short_name} ) { $token = "<i>$token</i>" }
1372 if ( $html_bold{$short_name} ) { $token = "<b>$token</b>" }
1380 if ($missing_html_entities) {
1381 $token =~ s/\&/&/g;
1382 $token =~ s/\</</g;
1383 $token =~ s/\>/>/g;
1384 $token =~ s/\"/"/g;
1387 HTML::Entities::encode_entities($token);
1392 sub finish_formatting {
1394 # called after last line
1396 $self->close_html_file();
1402 my ( $self, $line_of_tokens ) = @_;
1403 return unless $self->{_html_file_opened};
1404 my $html_pre_fh = $self->{_html_pre_fh};
1405 my $line_type = $line_of_tokens->{_line_type};
1406 my $input_line = $line_of_tokens->{_line_text};
1407 my $line_number = $line_of_tokens->{_line_number};
1410 # markup line of code..
1412 if ( $line_type eq 'CODE' ) {
1413 my $rtoken_type = $line_of_tokens->{_rtoken_type};
1414 my $rtokens = $line_of_tokens->{_rtokens};
1415 my $rlevels = $line_of_tokens->{_rlevels};
1417 if ( $input_line =~ /(^\s*)/ ) {
1423 my ($rcolored_tokens) =
1424 $self->markup_tokens( $rtokens, $rtoken_type, $rlevels );
1425 $html_line .= join '', @{$rcolored_tokens};
1428 # markup line of non-code..
1431 if ( $line_type eq 'HERE' ) { $line_character = 'H' }
1432 elsif ( $line_type eq 'HERE_END' ) { $line_character = 'h' }
1433 elsif ( $line_type eq 'FORMAT' ) { $line_character = 'H' }
1434 elsif ( $line_type eq 'FORMAT_END' ) { $line_character = 'h' }
1435 elsif ( $line_type eq 'SKIP' ) { $line_character = 'H' }
1436 elsif ( $line_type eq 'SKIP_END' ) { $line_character = 'h' }
1437 elsif ( $line_type eq 'SYSTEM' ) { $line_character = 'c' }
1438 elsif ( $line_type eq 'END_START' ) {
1439 $line_character = 'k';
1440 $self->add_toc_item( '__END__', '__END__' );
1442 elsif ( $line_type eq 'DATA_START' ) {
1443 $line_character = 'k';
1444 $self->add_toc_item( '__DATA__', '__DATA__' );
1446 elsif ( $line_type =~ /^POD/ ) {
1447 $line_character = 'P';
1448 if ( $rOpts->{'pod2html'} ) {
1449 my $html_pod_fh = $self->{_html_pod_fh};
1450 if ( $line_type eq 'POD_START' ) {
1452 my $rpre_string_stack = $self->{_rpre_string_stack};
1453 my $rpre_string = $rpre_string_stack->[-1];
1455 # if we have written any non-blank lines to the
1456 # current pre section, start writing to a new output
1458 if ( ${$rpre_string} =~ /\S/ ) {
1461 Perl::Tidy::IOScalar->new( \$pre_string, 'w' );
1462 $self->{_html_pre_fh} = $html_pre_fh;
1463 push @{$rpre_string_stack}, \$pre_string;
1465 # leave a marker in the pod stream so we know
1466 # where to put the pre section we just
1468 my $for_html = '=for html'; # don't confuse pod utils
1469 $html_pod_fh->print(<<EOM);
1472 <!-- pERLTIDY sECTION -->
1477 # otherwise, just clear the current string and start
1480 ${$rpre_string} = "";
1481 $html_pod_fh->print("\n");
1484 $html_pod_fh->print( $input_line . "\n" );
1485 if ( $line_type eq 'POD_END' ) {
1486 $self->{_pod_cut_count}++;
1487 $html_pod_fh->print("\n");
1492 else { $line_character = 'Q' }
1493 $html_line = $self->markup_html_element( $input_line, $line_character );
1496 # add the line number if requested
1497 if ( $rOpts->{'html-line-numbers'} ) {
1499 ( $line_number < 10 ) ? " "
1500 : ( $line_number < 100 ) ? " "
1501 : ( $line_number < 1000 ) ? " "
1503 $html_line = $extra_space . $line_number . " " . $html_line;
1507 $html_pre_fh->print("$html_line\n");