1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
#
# $Id$
#
use Text::Wrap;
use Data::Dumper;
$Text::Wrap::columns = 76;
$Text::Wrap::unexpand = 0;
$MAGIC_NBSP = "\xff";
sub unentity
{
my $html = shift;
die ( "INTERNAL ERROR: magic nbsp code found in html" ) if ( $html =~ /$MAGIC_NBSP/ );
$html =~ s/ /$MAGIC_NBSP/g;
$html =~ s/</</g;
$html =~ s/>/>/g;
$html =~ s/“/<</g;
$html =~ s/”/>>/g;
$html =~ s/©/(c)/g;
$html =~ s/&/&/g;
return $html;
};
sub linify
{
my $text = shift;
$text =~ s/\n/ /g;
$text =~ s/\t/ /g;
$text =~ s/\s{2,}/ /g;
$text =~ s/^\s+//ms;
$text =~ s/\s+$//ms;
$text =~ s/\s+(\w)\s+/ \1$MAGIC_NBSP/g; # glue 1-letter words with nbsp's
return $text;
};
sub in
{
my ($a,$v) = @_;
foreach my $e ( @$a )
{
return 1 if $e eq $v;
}
return 0;
};
###############
## entry point
###############
undef $/;
$html = <>;
$html =~ s/.*?<body.*?>//ms;
$html =~ s/<\/body.*/\n--eof--\n/ms;
$res = "";
$acc = "";
$hdr = "";
$left = 0;
$red = "";
@lists = "";
$li = 0;
$pre = 0;
sub max ($$) { $_[$_[0] < $_[1]] }
sub pad
{
my ($s, $l) = @_;
$s .= " " x ( $l - length($s) );
return $s;
}
while ( $html =~ s/^(.*?)<([\/]*\w+)(\s+.*?)*>//ms )
{
$text = $1;
$tag = lc $2;
$attrs = lc $3;
if ( $tag eq "table" )
{
@table = ();
@row = ();
next;
}
if ( $tag eq "/table" )
{
# compute column lengths
@lens = ();
@wlens = ();
foreach $row ( @table )
{
$n = 0;
foreach $col ( @{$row} ) { $lens[$n++] = max($lens[$n], length($col)); }
}
$len = 0;
for $l ( @lens ) { $len += $l; }
$k = ( $Text::Wrap::columns - 2 - 2*length(@lens) ) / $len;
$save = $Text::Wrap::columns;
for ( $n=0; $n<=$#lens; $n++ )
{
$clen = $lens[$n]; # target column length
$clen = int(1+$k*$clen) if ( $clen>10 ); # only wrap columns under 10 chars
$lens[$n] = $clen;
}
# wrap and print
$sep = "+";
for ( $j=0; $j<$n; $j++ )
{
$sep .= "-" x $lens[$j] . "--+";
}
$sep .= "\n";
$res .= $sep;
$r = 0;
foreach $row ( @table )
{
$n = 0;
@rowtext = (); # 1st index text lines, 2nd index columns
$maxline = 0;
foreach $col ( @{$row} )
{
$Text::Wrap::columns = $lens[$n] + 1;
@clines = split /\n/, wrap ( "", "", $col );
$rline = 0;
for $cline ( @clines )
{
$rowtext[$rline][$n] = $cline;
$rline++;
$maxline = max($maxline, $rline);
}
$n++;
}
for ( $i=0; $i<$maxline; $i++ )
{
$res .= "|";
for ( $j=0; $j<$n; $j++ )
{
$res .= " " . pad ( $rowtext[$i][$j], $lens[$j] ) . " |";
}
$res .= "\n";
}
$res .= $sep if ( ++$r==1 );
}
$res .= $sep;
$Text::Wrap::columns = $save;
next;
}
if ( in ( [ "/th", "/td" ], $tag ) )
{
push ( @row, $text );
next;
}
if ( $tag eq "/tr" )
{
push @table, [@row];
@row = ();
next;
}
$acc .= $text;
next if ( in ( [ "a","/a","i","/i","p","code","/code","b","/b","span","/span",
"strong","/strong","br","em","/em", "colgroup", "/colgroup", "col", "/col",
"thead", "/thead", "tr", "th", "tbody", "/tbody", "td" ], $tag ) );
if ( in ( [ "div","/div","/h1","/h2","/h3","/h4","/h5","/p","hr","h5",
"dl","ol","ul","dt","dd","/dl","/ol","/ul","/dt","/dd",
"li","/li","pre","/pre" ], $tag ) )
{
# fixme! handle userinput instead of literallayout
if ( !$pre )
{
$acc = linify(unentity($acc));
$acc = wrap ( (" " x $left).$red, " "x($left+length($red)), $acc );
die if ( $acc =~ /\t/ );
} else
{
my @lines = split ( /\n/, unentity($acc) );
$acc = "";
foreach $line ( @lines )
{
next if ( !$acc && !$line );
$acc .= ( " " x $left ) . "| $line\n";
}
$acc = "\n$acc\n";
}
$red = " "x length($red);
if ( $acc )
{
if ( length($hdr) )
{
$res .= "$acc\n" . $hdr x length($acc) . "\n\n";
$hdr = "";
} else
{
$res .= "$acc\n";
}
}
$acc = "";
$res .= "\n" if ( in ( [ "div","/div","/h1","/h2","/h3","/p","hr" ], $tag ) );
$res .= "\n" if ( $#lists<=2 && in ( ["/dl","/ol","/ul" ], $tag ) );
if ( $tag eq "hr" )
{
$res .= "-" x $Text::Wrap::columns . "\n\n";
}
if ( in ( [ "dd","ol","ul" ], $tag ) )
{
$left += 3;
$li = 1 if ( $tag eq "ol" ); # fixme! allow nested ol
push ( @lists, $tag );
next;
}
if ( in ( [ "/dd","/ol","/ul" ], $tag ) )
{
$left -= 3;
pop @lists;
$red = ""; # just in case
$res .= "\n";
next;
}
if ( $tag eq "li" )
{
my $list = $lists[$#lists];
if ( $list eq "ol" ) { $red = "$li. "; $li++; }
elsif ( $list eq "ul" ) { $red = "* "; }
else { print Dumper(@lists);
die ( "INTERNAL ERROR: 'li' in unknown list '$list'" ); }
next;
}
if ( $tag eq "pre" || ( $tag eq "div" && $attrs =~ /literallayout/ ) )
{
$pre = 1;
$left += 3;
next;
}
if ( $pre && ( $tag eq "/pre" || $tag eq "/div" ) )
{
$pre = 0;
$left -= 3;
next;
}
next;
}
if ( $tag eq "h1" ) { $hdr = "="; next; }
if ( $tag eq "h2" ) { $hdr = "="; next; }
if ( $tag eq "h3" ) { $hdr = "-"; next; }
if ( $tag eq "h4" ) { $hdr = ""; next; }
die ( "unknown tag=[$tag] attrs=[$attrs] text=[$text]\n" );
}
$res .= "\n\n--eof--\n";
$res =~ s/^\n+//;
$res =~ s/\n{3,}/\n\n/gms;
$res =~ s/$MAGIC_NBSP/ /g;
print $res;
#
# $Id$
#
|