blob: e3ca90bad6f0bcaa6369d6376d3cac269e090da8 (
plain)
- #!/usr/bin/perl
- # normalize HTML
- #
- # TODO: settle on a specific order of header levels
- # TODO: handle more subheaders
- use Getopt::Long;
- use File::Slurp;
- use strict;
- use warnings;
- my $force;
- GetOptions ("force|f");
- my $stem = shift;
- my $_ = read_file( $stem . '.htm' );
- # whitespace
- s/ / /mg;
- s{<P\b[^>]*>\s*</P>\s*}{}mg;
- # page header
- s{<DIV\b[^>]*>\s*\K(?:<P\b[^>]*;top:1\d{3}px;[^>]*>(?:<[bi]>)?[^<]+(?:</[bi]>)?</P>\s*)+}{}mg;
- # footnote
- s{<P\b[^>]*>\h+</P>\s*(?:<P\b[^>]*><b>\S+</b></P>\s*<P\b[^>]*>((?:(?!</P\b).)+.)</P>\s*)+(?=</DIV>)}{}mg;
- foreach my $class ( map /(?<=\.)(ft\d+)(?={font-size:7px;)/mg, $_ ) {
- s{\(</P>\s*<P\s[^>]*\sclass="$class">\d+</P>\s*<P\s[^>]*>\)}{}mg;
- };
- # document headers
- s{<HEAD>.*?</HEAD>\s*}{}msg;
- s{</DIV>\n</BODY>\n</HTML>.*?<DIV\b[^>]*>\s*}{}msg;
- # unwrap similarly styled bolded paragraphs
- s{<P\b[^>]*class="([^"]+)"[^>]*><b>[^<]+\K</b></P>\s*<P\b[^>]*class="\1"[^>]*><b>}{ }mg;
- # headline
- s{<P\b[^>]*>(TITLE \S+)</P>\s*<P\b[^>]*><b>((?:(?!</P\b).)+.)</b></P>}{<H1>$1</H1>\n<H2>$2</H2>}mg;
- s{<P\b[^>]*><i>(SECTION \S+)</i></P>\s*<P\b[^>]*><i><b>((?:(?!</P\b).)+.)</b></i></P>}{<H1>$1</H1>\n<H2>$2</H2>}mg;
- s{<P\b[^>]*><i>((?:Article|ANNEX) \S+)</i></P>\s*<P\b[^>]*><b>((?:(?!</P\b).)+.)\s*</b></P>}{<H1>$1</H1>\n<H2>$2</H2>}mg;
- s{<P\b[^>]*><i>((?:Article|ANNEX) \S+)\s*</i></P>\s*<P\b[^>]*><i><b>((?:(?!</P\b).)+?.)\s*</b>\s*</i></P>}{<H1>$1</H1>\n<H2>$2</H2>}mg;
- s{<P\b[^>]*>(\d+)\.</P>\s*}{<H3>$1</H3>\n}mg;
- s{(<P\b[^>]*>)(\d+)\. }{<H3>$2</H3>\n$1}mg;
- s{<P\b[^>]*>\(([a-z])\)</P>}{<H4>$1</H4>}mg;
- s{(<P\b[^>]*>)\(([ivx]+)\) }{<H5>$2</H5>\n$1}mg;
- s{<P\b[^>]*>\(([ivx]+)\)</P>}{<H5>$1</H5>}mg;
- # unwrap
- s{(?<=[[:lower:]])-(?:<br/>|</P>\s*<P\b[^>]*>)(?=[[:lower:]])}{}mg;
- #s{<P\b[^>]*class="([^"]+)"[^>]*>[^<]+\K\s*</P>\s*<P\b[^>]*class="\1"[^>]*>}{ }mg;
- s{(?<=class="(ft\d{1})">)[^<]+?\K\s*</P>\s*<P\b[^>]*class="\1"[^>]*>}{ }mg;
- s{(?<=class="(ft\d{2})">)[^<]+?\K\s*</P>\s*<P\b[^>]*class="\1"[^>]*>}{ }mg;
- s{(?<=class="(ft\d{3})">)[^<]+?\K\s*</P>\s*<P\b[^>]*class="\1"[^>]*>}{ }mg;
- #s{<P\b[^>]*class="([^"]+)"[^>]*>([^<]+?)\s*</P>\s*(?=<P\b[^>]*class="\1"[^>]*>)}{$2 }mg;
- s{\s*<br/>\s*}{ }mg;
- # styling
- s{<P\b[^>]*>}{<P>}mg;
- write_file( $stem . '.html', $_ );
- print "DONE: $0 stem $stem\n";
|