package Perl::Tidy::HtmlWriter;
use strict;
use warnings;
-our $VERSION = '20181120';
+our $VERSION = '20220613';
+use English qw( -no_match_vars );
use File::Basename;
+use constant EMPTY_STRING => q{};
+use constant SPACE => q{ };
+
# class variables
use vars qw{
%html_color
BEGIN {
if ( !eval { require HTML::Entities; 1 } ) {
- $missing_html_entities = $@ ? $@ : 1;
+ $missing_html_entities = $EVAL_ERROR ? $EVAL_ERROR : 1;
}
if ( !eval { require Pod::Html; 1 } ) {
- $missing_pod_html = $@ ? $@ : 1;
+ $missing_pod_html = $EVAL_ERROR ? $EVAL_ERROR : 1;
}
}
+sub AUTOLOAD {
+
+ # Catch any undefined sub calls so that we are sure to get
+ # some diagnostic information. This sub should never be called
+ # except for a programming error.
+ our $AUTOLOAD;
+ return if ( $AUTOLOAD =~ /\bDESTROY$/ );
+ my ( $pkg, $fname, $lno ) = caller();
+ my $my_package = __PACKAGE__;
+ print STDERR <<EOM;
+======================================================================
+Error detected in package '$my_package', version $VERSION
+Received unexpected AUTOLOAD call for sub '$AUTOLOAD'
+Called from package: '$pkg'
+Called from File '$fname' at line '$lno'
+This error is probably due to a recent programming change
+======================================================================
+EOM
+ exit 1;
+}
+
+sub DESTROY {
+
+ # required to avoid call to AUTOLOAD in some versions of perl
+}
+
sub new {
- my ( $class, $input_file, $html_file, $extension, $html_toc_extension,
- $html_src_extension )
- = @_;
+ my ( $class, @args ) = @_;
+
+ my %defaults = (
+ input_file => undef,
+ html_file => undef,
+ extension => undef,
+ html_toc_extension => undef,
+ html_src_extension => undef,
+ );
+ my %args = ( %defaults, @args );
+
+ my $input_file = $args{input_file};
+ my $html_file = $args{html_file};
+ my $extension = $args{extension};
+ my $html_toc_extension = $args{html_toc_extension};
+ my $html_src_extension = $args{html_src_extension};
my $html_file_opened = 0;
my $html_fh;
( $html_fh, my $html_filename ) =
Perl::Tidy::streamhandle( $html_file, 'w' );
unless ($html_fh) {
- Perl::Tidy::Warn("can't open $html_file: $!\n");
+ Perl::Tidy::Warn("can't open $html_file: $ERRNO\n");
return;
}
$html_file_opened = 1;
( $title, my $path ) = fileparse($input_file);
}
my $toc_item_count = 0;
- my $in_toc_package = "";
+ my $in_toc_package = EMPTY_STRING;
my $last_level = 0;
return bless {
_input_file => $input_file, # name of input file
my $end_package_list = sub {
if ( ${$rin_toc_package} ) {
$html_toc_fh->print("</ul>\n</li>\n");
- ${$rin_toc_package} = "";
+ ${$rin_toc_package} = EMPTY_STRING;
}
+ return;
};
my $start_package_list = sub {
<ul>
EOM
${$rin_toc_package} = $package;
+ return;
};
# start the table of contents on the first item
# check for conflict
if ( $css_linkname && $rOpts->{'nohtml-style-sheets'} ) {
$rOpts->{'nohtml-style-sheets'} = 0;
- warning("You can't specify both -css and -nss; -nss ignored\n");
+ Perl::Tidy::Warn(
+ "You can't specify both -css and -nss; -nss ignored\n");
}
# write a style sheet file if necessary
my $css_filename = shift;
my $fh;
unless ( $fh = IO::File->new("> $css_filename") ) {
- Perl::Tidy::Die("can't open $css_filename: $!\n");
+ Perl::Tidy::Die("can't open $css_filename: $ERRNO\n");
}
write_style_sheet_data($fh);
close_object($fh);
my $long_name = $short_to_long_names{$short_name};
my $abbrev = '.' . $short_name;
- if ( length($short_name) == 1 ) { $abbrev .= ' ' } # for alignment
+ if ( length($short_name) == 1 ) { $abbrev .= SPACE } # for alignment
my $color = $html_color{$short_name};
if ( !defined($color) ) { $color = $text_color }
$fh->print("$abbrev \{ color: $color;");
set_default_color( "html-color-$short_to_long_names{$short_name}", $color );
my $key;
- $key = "html-bold-$short_to_long_names{$short_name}";
+ $key = "html-bold-$short_to_long_names{$short_name}";
$rOpts->{$key} = ( defined $rOpts->{$key} ) ? $rOpts->{$key} : $bold;
- $key = "html-italic-$short_to_long_names{$short_name}";
+ $key = "html-italic-$short_to_long_names{$short_name}";
$rOpts->{$key} = ( defined $rOpts->{$key} ) ? $rOpts->{$key} : $italic;
return;
}
# "header!", "index!", "recurse!", "quiet!", "verbose!"
foreach my $kw (qw(podheader podindex podrecurse podquiet podverbose)) {
my $kwd = $kw; # allows us to strip 'pod'
- if ( $rOpts->{$kw} ) { $kwd =~ s/^pod//; push @args, "--$kwd" }
+ if ( $rOpts->{$kw} ) { $kwd =~ s/^pod//; push @args, "--$kwd" }
elsif ( defined( $rOpts->{$kw} ) ) {
$kwd =~ s/^pod//;
push @args, "--no$kwd";
Perl::Tidy::Die( $_[0] );
};
- pod2html(@args);
+ Pod::Html::pod2html(@args);
}
$fh_tmp = IO::File->new( $tmpfile, 'r' );
unless ($fh_tmp) {
# This routine will write the html selectively and store the toc
my $html_print = sub {
- foreach (@_) {
- $html_fh->print($_) unless ($no_print);
- if ($in_toc) { push @toc, $_ }
+ foreach my $line (@_) {
+ $html_fh->print($line) unless ($no_print);
+ if ($in_toc) { push @toc, $line }
}
+ return;
};
# loop over lines of html output from pod2html and merge in
# the necessary perltidy html sections
my ( $saw_body, $saw_index, $saw_body_end );
- my $timestamp = "";
+ my $timestamp = EMPTY_STRING;
if ( $rOpts->{'timestamp'} ) {
my $date = localtime;
$timestamp = "on $date";
$html_print->("<hr />\n") if $rOpts->{'frames'};
$html_print->("<h2>Code Index:</h2>\n");
##my @toc = map { $_ .= "\n" } split /\n/, $toc_string;
- my @toc = map { $_ . "\n" } split /\n/, $toc_string;
- $html_print->(@toc);
+ my @toc_st = map { $_ . "\n" } split /\n/, $toc_string;
+ $html_print->(@toc_st);
}
- $in_toc = "";
+ $in_toc = EMPTY_STRING;
$no_print = 0;
}
$html_print->("<hr />\n") if $rOpts->{'frames'};
$html_print->("<h2>Code Index:</h2>\n");
##my @toc = map { $_ .= "\n" } split /\n/, $toc_string;
- my @toc = map { $_ . "\n" } split /\n/, $toc_string;
- $html_print->(@toc);
+ my @toc_st = map { $_ . "\n" } split /\n/, $toc_string;
+ $html_print->(@toc_st);
}
- $in_toc = "";
+ $in_toc = EMPTY_STRING;
$ul_level = 0;
$no_print = 0;
}
# because the tmpfile may be one of the names used for frames
if ( -e $tmpfile ) {
unless ( unlink($tmpfile) ) {
- Perl::Tidy::Warn("couldn't unlink temporary file $tmpfile: $!\n");
+ Perl::Tidy::Warn(
+ "couldn't unlink temporary file $tmpfile: $ERRNO\n");
$success_flag = 0;
}
}
$title = escape_html($title);
# FUTURE input parameter:
- my $top_basename = "";
+ my $top_basename = EMPTY_STRING;
# We need to produce 3 html files:
# 1. - the table of contents
# 2. The current .html filename is renamed to be the contents panel
rename( $html_filename, $src_filename )
- or Perl::Tidy::Die("Cannot rename $html_filename to $src_filename:$!\n");
+ or Perl::Tidy::Die(
+ "Cannot rename $html_filename to $src_filename: $ERRNO\n");
# 3. Then use the original html filename for the frame
write_frame_html(
# write a separate html table of contents file for frames
my ( $title, $toc_filename, $src_basename, $rtoc, $src_frame_name ) = @_;
my $fh = IO::File->new( $toc_filename, 'w' )
- or Perl::Tidy::Die("Cannot open $toc_filename:$!\n");
+ or Perl::Tidy::Die("Cannot open $toc_filename: $ERRNO\n");
$fh->print(<<EOM);
<html>
<head>
my $first_anchor =
change_anchor_names( $rtoc, $src_basename, "$src_frame_name" );
- $fh->print( join "", @{$rtoc} );
+ $fh->print( join EMPTY_STRING, @{$rtoc} );
$fh->print(<<EOM);
</body>
) = @_;
my $fh = IO::File->new( $frame_filename, 'w' )
- or Perl::Tidy::Die("Cannot open $toc_basename:$!\n");
+ or Perl::Tidy::Die("Cannot open $toc_basename: $ERRNO\n");
$fh->print(<<EOM);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
# --------------------------------------------------
my $input_file = $self->{_input_file};
my $title = escape_html($input_file);
- my $timestamp = "";
+ my $timestamp = EMPTY_STRING;
if ( $rOpts->{'timestamp'} ) {
my $date = localtime;
$timestamp = "on $date";
my $rlast_level = $self->{_rlast_level};
my $rpackage_stack = $self->{_rpackage_stack};
- for ( my $j = 0 ; $j < @{$rtoken_type} ; $j++ ) {
+ foreach my $j ( 0 .. @{$rtoken_type} - 1 ) {
$type = $rtoken_type->[$j];
$token = $rtokens->[$j];
$level = $rlevels->[$j];
$type = 'M';
# but don't include sub declarations in the toc;
- # these wlll have leading token types 'i;'
- my $signature = join "", @{$rtoken_type};
+ # these will have leading token types 'i;'
+ my $signature = join EMPTY_STRING, @{$rtoken_type};
unless ( $signature =~ /^i;/ ) {
my $subname = $token;
$subname =~ s/[\s\(].*$//; # remove any attributes and prototype
$html_line = $1;
}
else {
- $html_line = "";
+ $html_line = EMPTY_STRING;
}
my ($rcolored_tokens) =
$self->markup_tokens( $rtokens, $rtoken_type, $rlevels );
- $html_line .= join '', @{$rcolored_tokens};
+ $html_line .= join EMPTY_STRING, @{$rcolored_tokens};
}
# markup line of non-code..
elsif ( $line_type eq 'HERE_END' ) { $line_character = 'h' }
elsif ( $line_type eq 'FORMAT' ) { $line_character = 'H' }
elsif ( $line_type eq 'FORMAT_END' ) { $line_character = 'h' }
+ elsif ( $line_type eq 'SKIP' ) { $line_character = 'H' }
+ elsif ( $line_type eq 'SKIP_END' ) { $line_character = 'h' }
elsif ( $line_type eq 'SYSTEM' ) { $line_character = 'c' }
elsif ( $line_type eq 'END_START' ) {
$line_character = 'k';
# otherwise, just clear the current string and start
# over
else {
- ${$rpre_string} = "";
+ ${$rpre_string} = EMPTY_STRING;
$html_pod_fh->print("\n");
}
}
# add the line number if requested
if ( $rOpts->{'html-line-numbers'} ) {
my $extra_space =
- ( $line_number < 10 ) ? " "
- : ( $line_number < 100 ) ? " "
- : ( $line_number < 1000 ) ? " "
- : "";
- $html_line = $extra_space . $line_number . " " . $html_line;
+ ( $line_number < 10 ) ? SPACE x 3
+ : ( $line_number < 100 ) ? SPACE x 2
+ : ( $line_number < 1000 ) ? SPACE
+ : EMPTY_STRING;
+ $html_line = $extra_space . $line_number . SPACE . $html_line;
}
# write the line
return;
}
1;
-