Advertisement
rplantiko

Pimp My HTML

Jun 28th, 2016
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.93 KB | None | 0 0
  1. #!\perl\bin\perl
  2.  
  3. # Durchsucht einen Verzeichnisbaum nach *.html, *.htm oder *.jsp Files
  4. # Verschönert den HTML-Code dieser Files in zwei Durchgängen
  5. # 1. Durchgang: HTML Tidy
  6. #   - DOCTYPE HTML 4.01 transitional
  7. #   - kleingeschriebene Element- und Attributnamen, Attribute in ""
  8. #   - Ausgabe in UTF-8
  9. #   - Umlaute nicht als Entität, sondern direkt (Text bleibt somit lesbar)
  10. #   - Zeilenumbrüche einheitlich
  11. # 2. Durchgang: HTML::Parser
  12. #   - <body> ersetzen durch <body><div id="content"> (passend für CSS)
  13. #   - Umbrüche in Kommentaren bereinigen (macht Tidy nicht)
  14. #   - <meta name="generator"> (auch mehrfache) entfernen
  15.  
  16. use strict;
  17. use warnings;
  18.  
  19. use HTML::Parser;
  20. use File::Iterator;
  21.  
  22. iterate('\astrotexte\sources_bak', \&pimp );
  23. #iterate('\astrotexte\sources', sub {print "$1\n" if (shift =~ /\\astrotexte\\sources\\(.*)/) ; } );
  24.  
  25. sub iterate {
  26.   my ($startdir,$do) = @_;
  27.   my ($it,$file);
  28.   $it = new File::Iterator(
  29.              DIR     => $startdir,
  30.              RECURSE => 0,
  31.              FILTER  => sub { $_[0] =~ /Spec\.(html?|jsp)$/ },
  32.           );
  33.  
  34.   while ($file = $it->next()) {
  35.    &$do($file);
  36.    }
  37.  
  38. }
  39.  
  40. sub pimp {
  41.  
  42.   my $source = shift;
  43.   my $target = $source;
  44.   $target =~ s/sources_bak/sources/;
  45.   my $tmpfile = "\\astrotexte\\sources\\_temp";
  46.   my $options;
  47.  
  48.   print "$source";
  49.  
  50. # Erster Durchlauf: Mit HTML Tidy
  51.  
  52.   ( $options = <<OPTIONS ) =~ s/\s*(#[^\n]*)?\n/ /gm;
  53. -o "$tmpfile"      # In temporäres File schreiben
  54. -i                 # indent = Einrückungen
  55. -w 80              # Line wrap bei ca. Position 80
  56. --doctype loose    # HTML 4.01, Tidy erzeugt keine URL der DTD (ist aber OK)
  57. --join-classes 1   # Mehrere CSS-Class-Attributes zu einem zusammenfassen
  58. --output-encoding UTF8  # Ausgabe in Unicode UTF-8
  59. --quiet 1          # Nicht schwätzen
  60. OPTIONS
  61.  
  62.   `\\tidy\\tidy $options "$source" 2>NUL`;
  63.  
  64.   print ".";
  65.  
  66. # Zweiter Durchlauf: Nun mit HTML::Parser
  67.  
  68. # Immer nur <body> oder <body onload="">, alle anderen Attribute entfernen
  69. # <body>  -->  <body><div id="content">
  70. # </body> -->  </div></body>
  71. # <meta name="generator"> (auch mehrfache) entfernen
  72.  
  73.  
  74.   open(RESULT, ">:utf8", $target)  or die "$! - Can't open $target for write";
  75.  
  76.   my $p = HTML::Parser->new(
  77.     default_h => [ \&handle_default, 'text'],
  78.     comment_h => [ \&handle_comment, 'text'],
  79.     start_h   => [ \&handle_start,   'tagname,attr,text'],
  80.     end_h     => [ \&handle_end,     'tagname'],
  81.     text_h    => [ \&handle_text,    'text'],
  82.     );
  83.  
  84.  
  85.   open(my $fh, "<:utf8", $tmpfile ) || die $!;
  86.   $p->parse_file($fh) || die $!;
  87.   close $fh;
  88.   close RESULT;
  89.  
  90.   print ".\n";
  91.  
  92. }
  93.  
  94. {
  95.  
  96. # Flags mit eingeschränktem Geltungsbereich
  97. my $body = 0;  
  98.  
  99. sub handle_start {
  100.  
  101.   my ($tagname,$attr,$text) = @_;
  102.   my $att ="";
  103.  
  104.   if ($tagname eq "body") {
  105.     $att =qq(onload="$attr->{onload}") if $attr->{onload};
  106.     print RESULT qq(<body $att><div id="content">);
  107.     $body = 1;
  108.     }
  109.   elsif ($tagname eq "meta"
  110.            and $attr->{name}
  111.            and $attr->{name} =~ /generator/i) {
  112.     return;  # Generator-Tag unterdrücken
  113.     }
  114.   elsif ($tagname eq "script") {
  115.     handle_default( $text );
  116.     print RESULT "</script>";
  117.     }  
  118.   else {
  119.     handle_default( $text );
  120.     }
  121.  
  122.   }
  123.  
  124. sub handle_end {
  125.   my ($tagname) = @_;
  126.   if ($tagname eq 'body') {
  127.     print RESULT qq(</div></body>);
  128.     $body = 0;
  129.     }
  130.   elsif ($tagname eq 'script') {
  131.     return;  # Machen wir schon in handle_start
  132.     }
  133.   else {
  134.     print RESULT qq(</$tagname>);
  135.     }
  136.   }
  137.  
  138. sub handle_text {
  139.   my $text = shift;
  140.  
  141. # Im Headerbereich mehrfache Zeilenumbrüche entfernen  
  142.   $text =~ s/(  )?\n(  )?\n/\n/gm unless $body;
  143.  
  144.   print RESULT $text;
  145.  
  146.   }
  147.  
  148. sub handle_default {
  149.   print RESULT shift;
  150.   }
  151.  
  152. # Tidy bearbeitet keine 0D/0A-Probleme innerhalb von Comments
  153. sub handle_comment {
  154.   my $text = shift;
  155.   $text =~ s/\r\n/\n/gm;
  156.   $text =~ s/\n\n/\n/gm;
  157.   print RESULT $text;
  158.   }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement