about summary refs log tree commit diff stats
path: root/scripts/collapse-br
blob: a3378b5ecc34ddca466bd6ba8c1748fc91fe5ea1 (plain) (blame)
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
#!/usr/bin/env perl
# $LynxId: collapse-br,v 1.8 2017/07/04 19:35:45 tom Exp $
# Generate a series of HTML files containing a mixture of text and <br> tags,
# comparing dumps of those to w3m and elinks.

use warnings;
use strict;
use diagnostics;

$| = 1;

use Getopt::Std;
use File::Temp qw/ tempdir /;

our ( $opt_C, $opt_T, $opt_e, $opt_l, $opt_p, $opt_t, $opt_w );
our $tempdir = tempdir( CLEANUP => 1 );

sub dumpit($$) {
    my $prog = shift;
    my $html = shift;
    my $opts = "-dump";
    $html =
        "<html>"
      . "<head><title>T</title></head>"
      . "<body>$html</body>"
      . "</html>"
      if ($opt_t);
    my @result;
    if ( $prog =~ /lynx$/ ) {
        my $name = "$tempdir/lynx.cfg";
        $opts .= " -cfg=$name";
        open my $fh, ">$name";
        printf $fh "collapse_br_tags:%s\n", $opt_C ? "false" : "true";
        printf $fh "trim_blank_lines:%s\n", $opt_T ? "false" : "true";
        close $fh;
    }
    if ($opt_p) {
        $opts .= " -stdin"       if ( $prog =~ /lynx$/ );
        $opts .= " -force-html"  if ( $prog =~ /elinks$/ );
        $opts .= " -T text/html" if ( $prog =~ /w3m$/ );
        if ( open my $fh, "echo '$html' | $prog $opts |" ) {
            @result = <$fh>;
            close $fh;
        }
    }
    else {
        my $name = "$tempdir/foobar.html";
        open my $fh, ">$name";
        printf $fh "%s", $html;
        close $fh;

        $opts .= " $name";
        if ( open my $fh, "$prog $opts |" ) {
            @result = <$fh>;
            close $fh;
        }
    }
    for my $n ( 0 .. $#result ) {
        chomp $result[$n];
    }

    if ( open my $fh, "echo '$html' | $prog $opts |" ) {
        @result = <$fh>;
        close $fh;
        for my $n ( 0 .. $#result ) {
            chomp $result[$n];
        }
    }
    $result[0] = "OOPS" unless ( $#result >= 0 );
    return @result;
}

sub header($) {
    my @cols = @{ $_[0] };
    my $text = "";
    for my $c ( 0 .. $#cols ) {
        $text .= sprintf "%-8s", $cols[$c];
    }
    printf "\t    %s\n", $text;
}

sub doit() {
    my $length = 1;
    my $state  = -1;

    my @tokens;
    $tokens[0] = " ";
    $tokens[1] = "X";
    $tokens[2] = "<br>";
    my $tokens = $#tokens + 1;

    my @progs;
    $progs[ $#progs + 1 ] = "lynx";

    $progs[ $#progs + 1 ] = "w3m"    if ($opt_w);
    $progs[ $#progs + 1 ] = "elinks" if ($opt_e);
    $progs[ $#progs + 1 ] = "./lynx" if ( -f "./lynx" );

    while ( $length <= $opt_l ) {
        my $bits  = "";
        my $html  = "";
        my $value = ++$state;
        $length = 0;
        while ( $value >= 0 ) {
            my $digit  = $value % $tokens;
            my $update = ( $value - $digit ) / $tokens;
            last if ( ( $update <= 0 ) and ( $value <= 0 ) and $length > 0 );
            $bits .= sprintf "%d", $digit;
            $length++;
            $html .= $tokens[$digit];
            $value = $update;
        }

        # skip the non-interesting cases
        next if ( $bits =~ /00/ );
        next if ( $bits =~ /11/ );
        next unless ( $bits =~ /2/ );
        printf "%-*s '%s'\n", $opt_l, $bits, $html;
        my @listing;
        for my $p ( 0 .. $#progs ) {
            my @q = &dumpit( $progs[$p], $html );
            my $l = $p * 8;
            for my $r ( 0 .. $#q ) {

                $listing[$r] = "" unless ( $listing[$r] );
                $listing[$r] = sprintf "%-*s", $l, $listing[$r] if ( $l > 0 );
                $listing[$r] .= sprintf "|%s",
                  substr( $q[$r] . "........", 0, 7 );
            }
        }
        &header( \@progs );
        for my $r ( 0 .. $#listing ) {
            printf "\t%2d %s|\n", $r, $listing[$r];
        }
    }
}

sub main::HELP_MESSAGE() {
    printf STDERR <<EOF
Usage: $0 [options]

Options:
 -C      do not collapse BR-tags (lynx only)
 -T      do not trim blank lines (lynx only)
 -e      compare with elinks
 -l NUM  generate test-cases up to this length (default: 3)
 -p      pipe HTML to the program rather than reading a file
 -t      add dummy title
 -w      compare with w3m

EOF
      ;
    exit;
}

&getopts('CTel:ptw') || main::HELP_MESSAGE;

$opt_l = 3 unless ($opt_l);

&doit;

1;