]> git.donarmstrong.com Git - perltidy.git/blobdiff - lib/Perl/Tidy/HtmlWriter.pm
New upstream version 20220613
[perltidy.git] / lib / Perl / Tidy / HtmlWriter.pm
index eba6335a47cfead7ab7a0090c1880a508f737689..ac1abb2ba25cd0bf6ea1953069e9693fc6a3d449 100644 (file)
@@ -7,10 +7,14 @@
 package Perl::Tidy::HtmlWriter;
 use strict;
 use warnings;
-our $VERSION = '20220217';
+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
@@ -31,10 +35,10 @@ use vars qw{
 
 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;
     }
 }
 
@@ -88,7 +92,7 @@ sub new {
     ( $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;
@@ -168,7 +172,7 @@ PRE_END
         ( $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
@@ -224,7 +228,7 @@ sub add_toc_item {
     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;
     };
@@ -589,7 +593,7 @@ sub write_style_sheet_file {
     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);
@@ -622,7 +626,7 @@ EOM
         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;");
@@ -761,9 +765,9 @@ sub pod_to_html {
 
     # 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;
     };
@@ -772,7 +776,7 @@ sub pod_to_html {
     # 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";
@@ -844,10 +848,10 @@ sub pod_to_html {
                 $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;
         }
 
@@ -869,10 +873,10 @@ sub pod_to_html {
                     $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;
             }
@@ -953,7 +957,8 @@ sub pod_to_html {
     # 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;
         }
     }
@@ -980,7 +985,7 @@ sub make_frame {
     $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
@@ -1000,7 +1005,8 @@ sub make_frame {
 
     # 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(
@@ -1015,7 +1021,7 @@ sub write_toc_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>
@@ -1027,7 +1033,7 @@ EOM
 
     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>
@@ -1046,7 +1052,7 @@ sub write_frame_html {
     ) = @_;
 
     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"
@@ -1193,7 +1199,7 @@ ENDCSS
     # --------------------------------------------------
     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";
@@ -1271,7 +1277,7 @@ sub markup_tokens {
     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];
@@ -1312,8 +1318,8 @@ sub markup_tokens {
             $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
@@ -1418,11 +1424,11 @@ sub write_line {
             $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..
@@ -1477,7 +1483,7 @@ EOM
                     # otherwise, just clear the current string and start
                     # over
                     else {
-                        ${$rpre_string} = "";
+                        ${$rpre_string} = EMPTY_STRING;
                         $html_pod_fh->print("\n");
                     }
                 }
@@ -1496,11 +1502,11 @@ EOM
     # 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