line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::FormatText; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Format HTML as plaintext |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
552
|
use 5.006_001; |
|
1
|
|
|
|
|
3
|
|
7
|
1
|
|
|
1
|
|
3
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
17
|
|
8
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# We now use Smart::Comments in place of the old DEBUG framework. |
11
|
|
|
|
|
|
|
# this should be commented out in release versions.... |
12
|
|
|
|
|
|
|
##use Smart::Comments; |
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
4
|
use base 'HTML::Formatter'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
490
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '2.15'; # TRIAL VERSION |
17
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:NIGELM'; # AUTHORITY |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
20
|
|
|
|
|
|
|
sub default_values { |
21
|
12
|
|
|
12
|
0
|
56
|
( shift->SUPER::default_values(), |
22
|
|
|
|
|
|
|
lm => 3, # left margin |
23
|
|
|
|
|
|
|
rm => 72, # right margin (actually, maximum text width) |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
28
|
|
|
|
|
|
|
sub configure { |
29
|
2
|
|
|
2
|
0
|
3
|
my ( $self, $hash ) = @_; |
30
|
|
|
|
|
|
|
|
31
|
2
|
|
|
|
|
8
|
my $lm = $self->{lm}; |
32
|
2
|
|
|
|
|
2
|
my $rm = $self->{rm}; |
33
|
|
|
|
|
|
|
|
34
|
2
|
50
|
|
|
|
7
|
$lm = delete $hash->{lm} if exists $hash->{lm}; |
35
|
2
|
50
|
|
|
|
7
|
$lm = delete $hash->{leftmargin} if exists $hash->{leftmargin}; |
36
|
2
|
50
|
|
|
|
4
|
$rm = delete $hash->{rm} if exists $hash->{rm}; |
37
|
2
|
50
|
|
|
|
5
|
$rm = delete $hash->{rightmargin} if exists $hash->{rightmargin}; |
38
|
|
|
|
|
|
|
|
39
|
2
|
|
|
|
|
4
|
my $width = $rm - $lm; |
40
|
2
|
50
|
|
|
|
4
|
if ( $width < 1 ) { |
41
|
0
|
0
|
|
|
|
0
|
warn "Bad margins, ignored" if $^W; |
42
|
0
|
|
|
|
|
0
|
return; |
43
|
|
|
|
|
|
|
} |
44
|
2
|
50
|
|
|
|
4
|
if ( $width < 20 ) { |
45
|
0
|
0
|
|
|
|
0
|
warn "Page probably too narrow" if $^W; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
2
|
|
|
|
|
7
|
for ( keys %$hash ) { |
49
|
0
|
0
|
|
|
|
0
|
warn "Unknown configure option '$_'" if $^W; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
2
|
|
|
|
|
3
|
$self->{lm} = $lm; |
53
|
2
|
|
|
|
|
3
|
$self->{rm} = $rm; |
54
|
2
|
|
|
|
|
4
|
$self; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
58
|
|
|
|
|
|
|
sub begin { |
59
|
10
|
|
|
10
|
0
|
10
|
my $self = shift; |
60
|
|
|
|
|
|
|
|
61
|
10
|
|
|
|
|
25
|
$self->SUPER::begin; |
62
|
10
|
|
|
|
|
9
|
$self->{curpos} = 0; # current output position. |
63
|
10
|
|
|
|
|
12
|
$self->{maxpos} = 0; # highest value of $pos (used by header underliner) |
64
|
10
|
|
|
|
|
21
|
$self->{hspace} = 0; # horizontal space pending flag |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
68
|
|
|
|
|
|
|
sub end { |
69
|
10
|
|
|
10
|
0
|
20
|
shift->collect("\n"); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
73
|
|
|
|
|
|
|
sub header_start { |
74
|
2
|
|
|
2
|
0
|
3
|
my ( $self, $level ) = @_; |
75
|
|
|
|
|
|
|
|
76
|
2
|
|
|
|
|
24
|
$self->vspace( 1 + ( 6 - $level ) * 0.4 ); |
77
|
2
|
|
|
|
|
3
|
$self->{maxpos} = 0; |
78
|
2
|
|
|
|
|
3
|
1; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
82
|
|
|
|
|
|
|
sub header_end { |
83
|
2
|
|
|
2
|
0
|
3
|
my ( $self, $level ) = @_; |
84
|
|
|
|
|
|
|
|
85
|
2
|
50
|
|
|
|
6
|
if ( $level <= 2 ) { |
86
|
2
|
|
|
|
|
1
|
my $line; |
87
|
2
|
100
|
|
|
|
5
|
$line = '=' if $level == 1; |
88
|
2
|
100
|
|
|
|
4
|
$line = '-' if $level == 2; |
89
|
2
|
|
|
|
|
6
|
$self->vspace(0); |
90
|
2
|
|
|
|
|
6
|
$self->out( $line x ( $self->{maxpos} - $self->{lm} ) ); |
91
|
|
|
|
|
|
|
} |
92
|
2
|
|
|
|
|
5
|
$self->vspace(1); |
93
|
2
|
|
|
|
|
4
|
1; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
97
|
|
|
|
|
|
|
sub bullet { |
98
|
10
|
|
|
10
|
0
|
99
|
my $self = shift; |
99
|
|
|
|
|
|
|
|
100
|
10
|
|
|
|
|
29
|
$self->SUPER::bullet( $_[0] . ' ' ); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
104
|
|
|
|
|
|
|
sub hr_start { |
105
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
106
|
|
|
|
|
|
|
|
107
|
1
|
|
|
|
|
2
|
$self->vspace(1); |
108
|
1
|
|
|
|
|
7
|
$self->out( '-' x ( $self->{rm} - $self->{lm} ) ); |
109
|
1
|
|
|
|
|
3
|
$self->vspace(1); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
113
|
|
|
|
|
|
|
sub pre_out { |
114
|
1
|
|
|
1
|
0
|
1
|
my $self = shift; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# should really handle bold/italic etc. |
117
|
1
|
50
|
|
|
|
3
|
if ( defined $self->{vspace} ) { |
118
|
1
|
50
|
|
|
|
3
|
if ( $self->{out} ) { |
119
|
1
|
|
|
|
|
5
|
$self->nl() while $self->{vspace}-- >= 0; |
120
|
1
|
|
|
|
|
1
|
$self->{vspace} = undef; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
} |
123
|
1
|
|
|
|
|
3
|
my $indent = ' ' x $self->{lm}; |
124
|
1
|
|
|
|
|
1
|
my $pre = shift; |
125
|
1
|
|
|
|
|
6
|
$pre =~ s/^/$indent/mg; |
126
|
1
|
|
|
|
|
3
|
$self->collect($pre); |
127
|
1
|
|
|
|
|
3
|
$self->{out}++; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
131
|
|
|
|
|
|
|
sub out { |
132
|
945
|
|
|
945
|
0
|
742
|
my $self = shift; |
133
|
945
|
|
|
|
|
790
|
my $text = shift; |
134
|
|
|
|
|
|
|
|
135
|
945
|
100
|
|
|
|
1823
|
if ( $text =~ /^\s*$/ ) { |
136
|
468
|
|
|
|
|
410
|
$self->{hspace} = 1; |
137
|
468
|
|
|
|
|
693
|
return; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
477
|
100
|
|
|
|
670
|
if ( defined $self->{vspace} ) { |
141
|
41
|
100
|
|
|
|
67
|
if ( $self->{out} ) { |
142
|
33
|
|
|
|
|
99
|
$self->nl while $self->{vspace}-- >= 0; |
143
|
|
|
|
|
|
|
} |
144
|
41
|
|
|
|
|
57
|
$self->goto_lm; |
145
|
41
|
|
|
|
|
47
|
$self->{vspace} = undef; |
146
|
41
|
|
|
|
|
40
|
$self->{hspace} = 0; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
477
|
100
|
|
|
|
662
|
if ( $self->{hspace} ) { |
150
|
428
|
100
|
|
|
|
570
|
if ( $self->{curpos} + length($text) > $self->{rm} ) { |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# word will not fit on line; do a line break |
153
|
50
|
|
|
|
|
59
|
$self->nl; |
154
|
50
|
|
|
|
|
55
|
$self->goto_lm; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
else { |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# word fits on line; use a space |
159
|
378
|
|
|
|
|
585
|
$self->collect(' '); |
160
|
378
|
|
|
|
|
358
|
++$self->{curpos}; |
161
|
|
|
|
|
|
|
} |
162
|
428
|
|
|
|
|
402
|
$self->{hspace} = 0; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
477
|
|
|
|
|
675
|
$self->collect($text); |
166
|
477
|
|
|
|
|
451
|
my $pos = $self->{curpos} += length $text; |
167
|
477
|
100
|
|
|
|
717
|
$self->{maxpos} = $pos if $self->{maxpos} < $pos; |
168
|
477
|
|
|
|
|
729
|
$self->{'out'}++; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
172
|
|
|
|
|
|
|
sub goto_lm { |
173
|
121
|
|
|
121
|
0
|
100
|
my $self = shift; |
174
|
|
|
|
|
|
|
|
175
|
121
|
|
|
|
|
109
|
my $pos = $self->{curpos}; |
176
|
121
|
|
|
|
|
85
|
my $lm = $self->{lm}; |
177
|
121
|
100
|
|
|
|
220
|
if ( $pos < $lm ) { |
178
|
91
|
|
|
|
|
72
|
$self->{curpos} = $lm; |
179
|
91
|
|
|
|
|
212
|
$self->collect( " " x ( $lm - $pos ) ); |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
184
|
|
|
|
|
|
|
sub nl { |
185
|
115
|
|
|
115
|
0
|
86
|
my $self = shift; |
186
|
|
|
|
|
|
|
|
187
|
115
|
|
|
|
|
98
|
$self->{'out'}++; |
188
|
115
|
|
|
|
|
92
|
$self->{curpos} = 0; |
189
|
115
|
|
|
|
|
176
|
$self->collect("\n"); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
193
|
|
|
|
|
|
|
sub adjust_lm { |
194
|
30
|
|
|
30
|
0
|
21
|
my $self = shift; |
195
|
|
|
|
|
|
|
|
196
|
30
|
|
|
|
|
31
|
$self->{lm} += $_[0]; |
197
|
30
|
|
|
|
|
37
|
$self->goto_lm; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
201
|
|
|
|
|
|
|
sub adjust_rm { |
202
|
2
|
|
|
2
|
0
|
4
|
shift->{rm} += $_[0]; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
1; |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
__END__ |