| 1 |
#!/usr/bin/env perl |
| 2 |
# $LynxId: collapse-br,v 1.8 2017/07/04 19:35:45 tom Exp $ |
| 3 |
# Generate a series of HTML files containing a mixture of text and <br> tags, |
| 4 |
# comparing dumps of those to w3m and elinks. |
| 5 |
|
| 6 |
use warnings; |
| 7 |
use strict; |
| 8 |
use diagnostics; |
| 9 |
|
| 10 |
$| = 1; |
| 11 |
|
| 12 |
use Getopt::Std; |
| 13 |
use File::Temp qw/ tempdir /; |
| 14 |
|
| 15 |
our ( $opt_C, $opt_T, $opt_e, $opt_l, $opt_p, $opt_t, $opt_w ); |
| 16 |
our $tempdir = tempdir( CLEANUP => 1 ); |
| 17 |
|
| 18 |
sub dumpit($$) { |
| 19 |
my $prog = shift; |
| 20 |
my $html = shift; |
| 21 |
my $opts = "-dump"; |
| 22 |
$html = |
| 23 |
"<html>" |
| 24 |
. "<head><title>T</title></head>" |
| 25 |
. "<body>$html</body>" |
| 26 |
. "</html>" |
| 27 |
if ($opt_t); |
| 28 |
my @result; |
| 29 |
if ( $prog =~ /lynx$/ ) { |
| 30 |
my $name = "$tempdir/lynx.cfg"; |
| 31 |
$opts .= " -cfg=$name"; |
| 32 |
open my $fh, ">$name"; |
| 33 |
printf $fh "collapse_br_tags:%s\n", $opt_C ? "false" : "true"; |
| 34 |
printf $fh "trim_blank_lines:%s\n", $opt_T ? "false" : "true"; |
| 35 |
close $fh; |
| 36 |
} |
| 37 |
if ($opt_p) { |
| 38 |
$opts .= " -stdin" if ( $prog =~ /lynx$/ ); |
| 39 |
$opts .= " -force-html" if ( $prog =~ /elinks$/ ); |
| 40 |
$opts .= " -T text/html" if ( $prog =~ /w3m$/ ); |
| 41 |
if ( open my $fh, "echo '$html' | $prog $opts |" ) { |
| 42 |
@result = <$fh>; |
| 43 |
close $fh; |
| 44 |
} |
| 45 |
} |
| 46 |
else { |
| 47 |
my $name = "$tempdir/foobar.html"; |
| 48 |
open my $fh, ">$name"; |
| 49 |
printf $fh "%s", $html; |
| 50 |
close $fh; |
| 51 |
|
| 52 |
$opts .= " $name"; |
| 53 |
if ( open my $fh, "$prog $opts |" ) { |
| 54 |
@result = <$fh>; |
| 55 |
close $fh; |
| 56 |
} |
| 57 |
} |
| 58 |
for my $n ( 0 .. $#result ) { |
| 59 |
chomp $result[$n]; |
| 60 |
} |
| 61 |
|
| 62 |
if ( open my $fh, "echo '$html' | $prog $opts |" ) { |
| 63 |
@result = <$fh>; |
| 64 |
close $fh; |
| 65 |
for my $n ( 0 .. $#result ) { |
| 66 |
chomp $result[$n]; |
| 67 |
} |
| 68 |
} |
| 69 |
$result[0] = "OOPS" unless ( $#result >= 0 ); |
| 70 |
return @result; |
| 71 |
} |
| 72 |
|
| 73 |
sub header($) { |
| 74 |
my @cols = @{ $_[0] }; |
| 75 |
my $text = ""; |
| 76 |
for my $c ( 0 .. $#cols ) { |
| 77 |
$text .= sprintf "%-8s", $cols[$c]; |
| 78 |
} |
| 79 |
printf "\t %s\n", $text; |
| 80 |
} |
| 81 |
|
| 82 |
sub doit() { |
| 83 |
my $length = 1; |
| 84 |
my $state = -1; |
| 85 |
|
| 86 |
my @tokens; |
| 87 |
$tokens[0] = " "; |
| 88 |
$tokens[1] = "X"; |
| 89 |
$tokens[2] = "<br>"; |
| 90 |
my $tokens = $#tokens + 1; |
| 91 |
|
| 92 |
my @progs; |
| 93 |
$progs[ $#progs + 1 ] = "lynx"; |
| 94 |
|
| 95 |
$progs[ $#progs + 1 ] = "w3m" if ($opt_w); |
| 96 |
$progs[ $#progs + 1 ] = "elinks" if ($opt_e); |
| 97 |
$progs[ $#progs + 1 ] = "./lynx" if ( -f "./lynx" ); |
| 98 |
|
| 99 |
while ( $length <= $opt_l ) { |
| 100 |
my $bits = ""; |
| 101 |
my $html = ""; |
| 102 |
my $value = ++$state; |
| 103 |
$length = 0; |
| 104 |
while ( $value >= 0 ) { |
| 105 |
my $digit = $value % $tokens; |
| 106 |
my $update = ( $value - $digit ) / $tokens; |
| 107 |
last if ( ( $update <= 0 ) and ( $value <= 0 ) and $length > 0 ); |
| 108 |
$bits .= sprintf "%d", $digit; |
| 109 |
$length++; |
| 110 |
$html .= $tokens[$digit]; |
| 111 |
$value = $update; |
| 112 |
} |
| 113 |
|
| 114 |
# skip the non-interesting cases |
| 115 |
next if ( $bits =~ /00/ ); |
| 116 |
next if ( $bits =~ /11/ ); |
| 117 |
next unless ( $bits =~ /2/ ); |
| 118 |
printf "%-*s '%s'\n", $opt_l, $bits, $html; |
| 119 |
my @listing; |
| 120 |
for my $p ( 0 .. $#progs ) { |
| 121 |
my @q = &dumpit( $progs[$p], $html ); |
| 122 |
my $l = $p * 8; |
| 123 |
for my $r ( 0 .. $#q ) { |
| 124 |
|
| 125 |
$listing[$r] = "" unless ( $listing[$r] ); |
| 126 |
$listing[$r] = sprintf "%-*s", $l, $listing[$r] if ( $l > 0 ); |
| 127 |
$listing[$r] .= sprintf "|%s", |
| 128 |
substr( $q[$r] . "........", 0, 7 ); |
| 129 |
} |
| 130 |
} |
| 131 |
&header( \@progs ); |
| 132 |
for my $r ( 0 .. $#listing ) { |
| 133 |
printf "\t%2d %s|\n", $r, $listing[$r]; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
sub main::HELP_MESSAGE() { |
| 139 |
printf STDERR <<EOF |
| 140 |
Usage: $0 [options] |
| 141 |
|
| 142 |
Options: |
| 143 |
-C do not collapse BR-tags (lynx only) |
| 144 |
-T do not trim blank lines (lynx only) |
| 145 |
-e compare with elinks |
| 146 |
-l NUM generate test-cases up to this length (default: 3) |
| 147 |
-p pipe HTML to the program rather than reading a file |
| 148 |
-t add dummy title |
| 149 |
-w compare with w3m |
| 150 |
|
| 151 |
EOF |
| 152 |
; |
| 153 |
exit; |
| 154 |
} |
| 155 |
|
| 156 |
&getopts('CTel:ptw') || main::HELP_MESSAGE; |
| 157 |
|
| 158 |
$opt_l = 3 unless ($opt_l); |
| 159 |
|
| 160 |
&doit; |
| 161 |
|
| 162 |
1; |