FormatExternal.pm

filter: Statementstmt 25.0% 35 / 140 Branchbran 5.0% 3 / 60 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 55.5% 10 / 18 Podpod 100% 4 / 4 totaltotal 18.7% 52 / 277 CC 43 SCAR 50.5
CC
43
Cov
16%
format_file
380.0
_base_prefix
132.0
format_string
72.0
CRAP 155.7
↑ Summary FormatText/Lynx.pm »
1 # Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2015 Kevin Ryde
2  
3 # HTML-FormatExternal is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License as published
5 # by the Free Software Foundation; either version 3, or (at your option) any
6 # later version.
7 #
8 # HTML-FormatExternal is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 # for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with HTML-FormatExternal. If not, see <http://www.gnu.org/licenses/>.
15  
16  
17  
18 # Maybe:
19 # capture error output
20 # errors_to => \$var
21 # combine error messages
22 #
23  
24  
25 package HTML::FormatExternal;
26 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use File::Spec 0.80; # version 0.80 of perl 5.6.0 or thereabouts for devnull()
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use IPC::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # uncomment this to run the ### lines
34 # use Smart::Comments;
35  
36 5 our $VERSION = 26;
37  
38 14 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
39 14 my ($class, %self) = @_;
40 14 return bless \%self, $class;
41 }
42 0 ▶sub format {
Subroutine format: not called CC=2 SCAR=17.9
Pod documented
43 0 my ($self, $html) = @_;
44 0 ▶ if (ref $html) { $html = $html->as_HTML; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $html) 0 0 0
45 0 return $self->format_string ($html, %$self);
46 }
47  
48 5 ▶use constant _WIDE_INPUT_CHARSET => 'UTF-8';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 5 ▶use constant _WIDE_OUTPUT_CHARSET => 'UTF-8';
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 # format_string() takes the easy approach of putting the string in a temp
52 # file and letting format_file() do the real work. The formatter programs
53 # can generally read stdin and write stdout, so might do that with select()
54 # to simultaneously write and read back.
55 #
56 0 ▶sub format_string {
Subroutine format_string: not called CC=8 SCAR=42.8
Pod documented
57 0 my ($class, $html_str, %options) = @_;
58  
59 0 my $fh = _tempfile();
60 0 my $input_wide = eval { utf8::is_utf8($html_str) };
61 0 _output_wide(\%options, $input_wide);
62  
63 # insert <base> while in wide chars
64 0 ▶ if (defined $options{'base'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"base"}) 0 0 0
65 0 $html_str = _base_prefix(\%options, $html_str, $input_wide);
66 }
67  
68 0 ▶ if ($input_wide) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_wide) 0 0 0
69 0 ▶ if (! $options{'input_charset'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"input_charset"}) 0 0 0
70 0 $options{'input_charset'} = $class->_WIDE_INPUT_CHARSET;
71 }
72 ### input_charset for wide: $options{'input_charset'}
73 0 ▶ if ($options{'input_charset'} eq 'entitize') {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'input_charset'} eq 'entitize') { } 0 0 0
74 0 $html_str = _entitize($html_str);
75 0 delete $options{'input_charset'};
76 } else {
77 0 my $layer = ":encoding($options{'input_charset'})";
78 0 ▶ binmode ($fh, $layer) or die 'Cannot add layer ',$layer;
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, $layer or die "Cannot add layer ", $layer 0 0 0
79 }
80 }
81  
82 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
do { print $fh $html_str and close $fh } or die "Cannot write temp file: ", $! 0 0 0
83 0 ▶ print $fh $html_str
Condition0%0/3
print($fh $html_str) && close $fh
!ll&&!rl&&r
000
MC/DC0%0/2
print($fh $html_str) && close $fh
print($fh $html_str) close $fh
Truth table0%0/3
A && B
A= print($fh $html_str)B= close $fh
ABresult
0X0
100
111
84 and close($fh)
85 } || die 'Cannot write temp file: ',$!;
86  
87 0 return $class->format_file ($fh->filename, %options);
88 }
89  
90 # Left margin is synthesized by adding spaces afterwards because the various
91 # programs have pretty variable support for a specified margin.
92 # * w3m doesn't seem to have a left margin option at all
93 # * lynx has one but it's too well hidden in its style sheet or something
94 # * elinks has document.browse.margin_width but it's limited to 8 or so
95 # * netrik doesn't seem to have one at all
96 # * vilistextum has a "spaces" internally for lists etc but no apparent
97 # way to initialize from the command line
98 #
99 0 ▶sub format_file {
Subroutine format_file: not called CC=19 SCAR=59.4
Pod documented
100 0 my ($class, $filename, %options) = @_;
101  
102 # If neither leftmargin nor rightmargin are specified then '_width' is
103 # unset and the _make_run() funcs leave it to the program defaults.
104 #
105 # If either leftmargin or rightmargin are set then '_width' is established
106 # and the _make_run() funcs use it and and zero left margin, then the
107 # actual left margin is applied below.
108 #
109 # The DEFAULT_LEFTMARGIN and DEFAULT_RIGHTMARGIN establish the defaults
110 # when just one of the two is set. Not good hard coding those values,
111 # but the programs don't have anything to set one but not the other.
112 #
113 0 my $leftmargin = $options{'leftmargin'};
114 0 my $rightmargin = $options{'rightmargin'};
115 0 ▶ if (defined $leftmargin || defined $rightmargin) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $leftmargin or defined $rightmargin) 0 0 0
Condition0%0/3
defined $leftmargin or defined $rightmargin
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $leftmargin or defined $rightmargin
defined $leftmargin defined $rightmargin
Truth table0%0/3
A or B
A= defined $leftmarginB= defined $rightmargin
ABresult
1X1
011
000
116 0 ▶ if (! defined $leftmargin) { $leftmargin = $class->DEFAULT_LEFTMARGIN; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $leftmargin) 0 0 0
117 0 ▶ if (! defined $rightmargin) { $rightmargin = $class->DEFAULT_RIGHTMARGIN; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rightmargin) 0 0 0
118 0 $options{'_width'} = $rightmargin - $leftmargin;
119 }
120  
121 0 _output_wide(\%options, 0); # file input is reckoned as not wide
122 0 ▶ if ($options{'output_wide'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"output_wide"}) 0 0 0
123 0 ▶ $options{'output_charset'} ||= $class->_WIDE_OUTPUT_CHARSET;
Condition0%0/3
$options{"output_charset"} ||= $class->_WIDE_OUTPUT_CHARSET
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"output_charset"} ||= $class->_WIDE_OUTPUT_CHARSET
$options{"output_charset"} $class->_WIDE_OUTPUT_CHARSET
Truth table0%0/3
A ||= B
A= $options{"output_charset"}B= $class->_WIDE_OUTPUT_CHARSET
ABresult
1X1
011
000
124 }
125  
126 0 my $tempfh;
127 0 ▶ if (defined $options{'base'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"base"}) 0 0 0
128 # insert <base> by copying to a temp file
129  
130 # File::Copy rudely calls eq() to compare $from and $to. Need either
131 # File::Temp 0.18 to have that work on $tempfh, or File::Copy 2.??? for
132 # it to check an overload method exists first. Newer File::Temp is
133 # available from cpan, where File::Copy may not be, so ask for
134 # File::Temp 0.18.
135 0 require File::Temp;
136 0 File::Temp->VERSION(0.18);
137  
138 # must sysread()/syswrite() because that's what File::Copy does (as of
139 # its version 2.30) so anything held in the perl buffering by the normal
140 # read() is lost.
141  
142 0 my $initial;
143 my $fh;
144 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
do { open $fh, "<", $filename and binmode $fh and defined sysread($fh, $initial, 4) } or croak("Cannot open ${filename}: $!") 0 0 0
145 0 ▶ open $fh, '<', $filename
Condition0%0/6
open($fh, '<', $filename) && binmode($fh) && defined sysread($fh, $initial, 4)
!ll&&!rl&&r
000
open $fh, "<", $filename and binmode $fh
!ll&&!rl&&r
000
MC/DC0%0/3
open($fh, '<', $filename) && binmode($fh) && defined sysread($fh, $initial, 4)
open $fh, "<", $filename binmode $fh defined sysread($fh, $initial, 4)
Truth table0%0/4
A and B && C
A= open $fh, "<", $filenameB= binmode $fhC= defined sysread($fh, $initial, 4)
ABCresult
0XX0
10X0
1100
1111
146 and binmode $fh
147 and defined (sysread $fh, $initial, 4)
148 } || croak "Cannot open $filename: $!";
149 ### $initial
150  
151 0 $initial = _base_prefix(\%options, $initial, 0);
152  
153 0 $tempfh = _tempfile();
154 0 $tempfh->autoflush(1);
155 0 require File::Copy;
156 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
do { defined syswrite($tempfh, $initial) and File::Copy::copy($fh, $tempfh) and close $tempfh and close $fh } or croak("Cannot copy $filename to temp file: $!") 0 0 0
157 0 ▶ defined(syswrite($tempfh, $initial))
Condition0%0/9
defined syswrite($tempfh, $initial) && File::Copy::copy($fh, $tempfh) && close $tempfh && close $fh
!ll&&!rl&&r
000
defined syswrite($tempfh, $initial) and File::Copy::copy($fh, $tempfh) and close $tempfh
!ll&&!rl&&r
000
defined syswrite($tempfh, $initial) and File::Copy::copy($fh, $tempfh)
!ll&&!rl&&r
000
MC/DC0%0/4
defined syswrite($tempfh, $initial) && File::Copy::copy($fh, $tempfh) && close $tempfh && close $fh
defined syswrite($tempfh, $initial) File::Copy::copy($fh, $tempfh) close $tempfh close $fh
Truth table0%0/5
A and B and C && D
A= defined syswrite($tempfh, $initial)B= File::Copy::copy($fh, $tempfh)C= close $tempfhD= close $fh
ABCDresult
0XXX0
10XX0
110X0
11100
11111
158 and File::Copy::copy($fh, $tempfh)
159 and close $tempfh
160 and close $fh
161 } || croak "Cannot copy $filename to temp file: $!";
162  
163  
164 0 $filename = $tempfh->filename;
165 }
166  
167 # # dump the file being crunched
168 # print "Bytes passed to program:\n";
169 # IPC::Run::run(['hd'], '<',$filename, '|',['cat']);
170  
171 # _make_run() can set $options{'ENV'} too
172 0 my ($command_aref, @run) = $class->_make_run($filename, \%options);
173 0 ▶ my $env = $options{'ENV'} || {};
Condition0%0/2
$options{'ENV'} || {}
l!l
00
MC/DC0%0/1
$options{'ENV'} || {}
$options{'ENV'}
Truth table0%0/2
A
A= $options{'ENV'}
Aresult
11
00
174  
175 ### $command_aref
176 ### @run
177 ### $env
178  
179 0 ▶ if (! @run) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@run) 0 0 0
180 0 push @run, '<', File::Spec->devnull;
181 }
182  
183 0 my $str;
184 {
185 0 local %ENV = (%ENV, %$env); # overrides from _make_command()
186 0 eval { IPC::Run::run($command_aref,
187 @run,
188 '>', \$str,
189 # FIXME: what to do with stderr ?
190 # '2>', File::Spec->devnull,
191 ) };
192 }
193 0 _die_on_insecure();
194 ### $str
195  
196 ### final output_wide: $options{'output_wide'}
197 0 ▶ if ($options{'output_wide'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"output_wide"}) 0 0 0
198 0 require Encode;
199 0 $str = Encode::decode ($options{'output_charset'}, $str);
200 }
201  
202 0 ▶ if ($leftmargin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leftmargin) 0 0 0
203 0 my $fill = ' ' x $leftmargin;
204 0 $str =~ s/^(.)/$fill$1/mg; # non-empty lines only
205 }
206 0 return $str;
207 }
208  
209 # most program running errors are quietly ignored for now, but re-throw
210 # "Insecure $ENV{PATH}" when cannot run due to taintedness.
211 0 ▶sub _die_on_insecure {
Subroutine _die_on_insecure: not called CC=2 SCAR=17.9
212 0 ▶ if ($@ =~ /^Insecure/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /^Insecure/) 0 0 0
213 0 die $@;
214 }
215 }
216  
217 39 ▶sub _run_version {
Subroutine _run_version: called CC=3 SCAR=11.1
218 39 my ($self_or_class, $command_aref, @ipc_options) = @_;
219 ### _run_version() ...
220 ### $command_aref
221 ### @ipc_options
222  
223 39 ▶ if (! @ipc_options) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@ipc_options) 29 10 39
224 29 @ipc_options = ('2>', File::Spec->devnull);
225 }
226  
227 39 my $version; # left undef if any exec/slurp problem
228 39 eval { IPC::Run::run($command_aref,
229 '<', File::Spec->devnull,
230 '>', \$version,
231 @ipc_options) };
232  
233 # strip blank lines at end of lynx, maybe others
234 0 ▶ if (defined $version) { $version =~ s/\n+$/\n/s; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $version) 0 39 39
235 39 return $version;
236 }
237  
238 # return a File::Temp filehandle object
239 0 ▶sub _tempfile {
Subroutine _tempfile: not called CC=2 SCAR=17.9
240 0 require File::Temp;
241 0 my $fh = File::Temp->new (TEMPLATE => 'HTML-FormatExternal-XXXXXX',
242 SUFFIX => '.html',
243 TMPDIR => 1);
244 0 ▶ binmode($fh) or die 'Oops, cannot set binmode() on temp file';
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh or die "Oops, cannot set binmode() on temp file" 0 0 0
245  
246 ### tempfile: $fh->filename
247 # $fh->unlink_on_destroy(0); # to preserve for debugging ...
248  
249 0 return $fh;
250 }
251  
252 0 ▶sub _output_wide {
Subroutine _output_wide: not called CC=3 SCAR=24.8
253 0 my ($options, $input_wide) = @_;
254 0 ▶ if (! defined $options->{'output_wide'}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $options->{"output_wide"} or $options->{"output_wide"} eq "as_input") 0 0 0
Condition0%0/3
not defined $options->{"output_wide"} or $options->{"output_wide"} eq "as_input"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $options->{"output_wide"} or $options->{"output_wide"} eq "as_input"
not defined $options->{"output_wide"} $options->{"output_wide"} eq "as_input"
Truth table0%0/3
not(A) or B
A= not defined $options->{"output_wide"}B= $options->{"output_wide"} eq "as_input"
ABresult
1X1
011
000
255 || $options->{'output_wide'} eq 'as_input') {
256 0 $options->{'output_wide'} = $input_wide;
257 }
258 }
259  
260 # $str is HTML or some initial bytes.
261 # Return a new string with <base> at the start.
262 #
263 0 ▶sub _base_prefix {
Subroutine _base_prefix: not called CC=11 SCAR=48.8
264 0 my ($options, $str, $input_wide) = @_;
265 0 my $base = delete $options->{'base'};
266 ### _base_prefix: $base
267  
268 0 $base = "$base"; # stringize possible URI object
269 0 $base = _entitize($base); # probably shouldn't be any non-ascii in a url
270 0 $base = "<base href=\"$base\">\n";
271  
272 0 my $pos = 0;
273 0 ▶ unless ($input_wide) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($input_wide) 0 0 0
274 # encode $base in the input_charset, and possibly after a BOM.
275 #
276 # Lynx recognises a BOM, if it doesn't have other -assume_charset. It
277 # recognises it only at the start of the file, so must insert <base>
278 # after it here to preserve that feature of Lynx.
279 #
280 # If input_charset is utf-32 or utf-16 then it seems reasonable to step
281 # over any BOM. But Lynx for some reason doesn't like a BOM together
282 # with utf-32 or utf-16 specified. Dunno if that's a bug or a feature
283 # on its part.
284  
285 0 my $input_charset = $options->{'input_charset'};
286 0 ▶ if (! defined $input_charset || lc($input_charset) eq 'utf-32') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $input_charset or lc $input_charset eq "utf-32") 0 0 0
Condition0%0/3
not defined $input_charset or lc $input_charset eq "utf-32"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $input_charset or lc $input_charset eq "utf-32"
not defined $input_charset lc $input_charset eq "utf-32"
Truth table0%0/3
not(A) or B
A= not defined $input_charsetB= lc $input_charset eq "utf-32"
ABresult
1X1
011
000
287 0 ▶ if ($str =~ /^\000\000\376\377/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /^\000\000\376\377/) { } 0 0 0
elsif ($str =~ /^\377\376\000\000/) { } 0 0 0
288 0 $input_charset = 'utf-32be';
289 0 $pos = 4;
290 } elsif ($str =~ /^\377\376\000\000/) {
291 0 $input_charset = 'utf-32le';
292 0 $pos = 4;
293 }
294 }
295 0 ▶ if (! defined $input_charset || lc($input_charset) eq 'utf-16') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $input_charset or lc $input_charset eq "utf-16") 0 0 0
Condition0%0/3
not defined $input_charset or lc $input_charset eq "utf-16"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $input_charset or lc $input_charset eq "utf-16"
not defined $input_charset lc $input_charset eq "utf-16"
Truth table0%0/3
not(A) or B
A= not defined $input_charsetB= lc $input_charset eq "utf-16"
ABresult
1X1
011
000
296 0 ▶ if ($str =~ /^\376\377/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /^\376\377/) { } 0 0 0
elsif ($str =~ /^\377\376/) { } 0 0 0
297 0 $input_charset = 'utf-16be';
298 0 $pos = 4;
299 } elsif ($str =~ /^\377\376/) {
300 0 $input_charset = 'utf-16le';
301 0 $pos = 2;
302 }
303 }
304 0 ▶ if (defined $input_charset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_charset) 0 0 0
305 # encode() errors out if unknown charset, and doesn't exist for older
306 # Perl, in which case leave $base as ascii. May not be right, but
307 # ought to work with the various ASCII superset encodings.
308 0 eval {
309 0 require Encode;
310 0 $base = Encode::encode ($input_charset, $base);
311 };
312 }
313 }
314 0 substr($str, $pos,0, $base); # insert $base at $pos
315 0 return $str;
316 }
317  
318 # return $str with non-ascii replaced by &#123; entities
319 0 ▶sub _entitize {
Subroutine _entitize: not called CC=1 SCAR=6.9
320 0 my ($str) = @_;
321 0 $str =~ s{([^\x20-\x7E])}{'&#'.ord($1).';'}eg;
322 ### $str
323 0 return $str;
324 }
325  
326 5 1;
327 __END__
↑ Summary FormatText/Lynx.pm »