blib/lib/EBook/Ishmael/TextBrowserDump.pm

filter: Statementstmt 67.2% 41 / 61 Branchbran 18.7% 3 / 16 Conditioncond 6.6% 1 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 90.0% 9 / 10 Podpod 100% 1 / 1 totaltotal 49.1% 55 / 112 CC 12 SCAR 45.0
CC
12
Cov
49%
browser_dump
156.0
BEGIN
1.0
BEGIN
1.0
CRAP 89.6
↑ Summary blib/lib/EBook/Ishmael/EBook/CHM.pm »
1 package EBook::Ishmael::TextBrowserDump;
2 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 our $VERSION = '2.03';
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our @EXPORT = qw(browser_dump);
9  
10 2 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use EBook::Ishmael::ShellQuote qw(safe_qx);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 our $CAN_DUMP = 0;
18  
19 # TODO: Is there some sort of post-processing we can do to get rid of <img>s?
20  
21 # From links
22 2 my $WIDTH_MAX = 512;
23 2 my $WIDTH_MIN = 10;
24  
25 2 my @ORDER = qw(
26 lynx
27 links
28 elinks
29 w3m
30 chawan
31 pandoc
32 queequeg
33 );
34  
35 2 my %Browsers = (
36 'lynx' => {
37 Bins => [ qw(lynx) ],
38 Bin => undef,
39 Opts => [ qw(-dump -force_html -nolist -display_charset=utf8) ],
40 Width => '-width %d',
41 Xhtml => [ qw(-xhtml_parsing) ],
42 },
43 'links' => {
44 Bins => [ qw(links links2) ],
45 Bin => undef,
46 Opts => [ qw(-dump -force-htm -codepage utf8) ],
47 Width => '-width %d',
48 Xhtml => [],
49 },
50 'elinks' => {
51 Bins => [ qw(elinks) ],
52 Bin => undef,
53 Opts => [ qw(-dump -force-html -no-home -no-references -no-numbering
54 -dump-charset utf8) ],
55 Width => '-dump-width %d',
56 Xhtml => [],
57 },
58 'w3m' => {
59 Bins => [ qw(w3m) ],
60 Bin => undef,
61 Opts => [ qw(-dump -T text/html -O utf8) ],
62 Width => '-cols %d',
63 Xhtml => [],
64 },
65 'chawan' => {
66 Bins => [ qw(cha) ],
67 Bin => undef,
68 Opts => [ qw(-d -I utf8 -O utf8 -T text/html) ],
69 Width => "-o 'display.columns=%d'",
70 Xhtml => [],
71 },
72 'pandoc' => {
73 Bins => [ qw(pandoc) ],
74 Bin => undef,
75 Opts => [ qw(-f html -t plain -o -) ],
76 Width => "--columns %d",
77 Xhtml => [],
78 },
79 'queequeg' => {
80 Bins => [ qw(queequeg) ],
81 Bin => undef,
82 Opts => [ qw(-e utf8) ],
83 Width => '-w %d',
84 Xhtml => [],
85 },
86 );
87  
88 2 my $Default = undef;
89 2 for my $k (@ORDER) {
90 16 ▶ my $bin = first { which $_ } @{ $Browsers{ $k }->{Bins} };
Subroutine __ANON__: called CC=1 SCAR=0.0
91 14 ▶ next unless defined $bin;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bin 12 2 14
92 2 $Browsers{ $k }->{Bin} = $bin;
93 2 ▶ $Default //= $k;
Condition33%1/3
$Default //= $k
l!l&&r!l&&!r
020
MC/DC0%0/2
$Default //= $k
$Default $k
Truth table33%1/3
A //= B
A= $DefaultB= $k
ABresult
1X1
011
000
94 }
95  
96 2 $CAN_DUMP = defined $Default;
97  
98 2 ▶unless ($CAN_DUMP) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($EBook::Ishmael::TextBrowserDump::CAN_DUMP) 0 2 2
99 0 warn "No valid text browser was found installed on your system, you " .
100 "will be unable to use any feature requiring a text browser\n";
101 }
102  
103 sub browser_dump {
104  
105 0 ▶ unless (defined $Default) {
Subroutine browser_dump: not called CC=12 SCAR=50.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Default) 0 0 0
106 0 die "Cannot format HTML; no valid program was found on your system\n";
107 }
108  
109 0 my $file = shift;
110 0 ▶ my $param = shift // {};
Condition0%0/2
shift() // {}
l!l
00
MC/DC0%0/1
shift() // {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
111  
112 0 ▶ my $browser = $param->{browser} // $Default;
Condition0%0/3
$param->{"browser"} // $Default
l!l&&r!l&&!r
000
MC/DC0%0/2
$param->{"browser"} // $Default
$param->{"browser"} $Default
Truth table0%0/3
A // B
A= $param->{"browser"}B= $Default
ABresult
1X1
011
000
113 0 ▶ my $xhtml = $param->{xhtml} // 0;
Condition0%0/2
$param->{"xhtml"} // 0
l!l
00
MC/DC0%0/1
$param->{"xhtml"} // 0
$param->{"xhtml"}
Truth table0%0/2
A
A= $param->{"xhtml"}
Aresult
11
00
114 0 ▶ my $width = $param->{width} // 80;
Condition0%0/2
$param->{"width"} // 80
l!l
00
MC/DC0%0/1
$param->{"width"} // 80
$param->{"width"}
Truth table0%0/2
A
A= $param->{"width"}
Aresult
11
00
115  
116 0 ▶ unless (exists $Browsers{ $browser }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Browsers{$browser}) 0 0 0
117 0 die <<"HERE";
118 '$browser' is not a valid program; the following is a list of valid programs:
119 lynx
120 links
121 elinks
122 w3m
123 chawan
124 pandoc
125 queequeg
126 HERE
127 }
128  
129 0 ▶ unless (defined $Browsers{ $browser }->{Bin}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Browsers{$browser}{"Bin"}) 0 0 0
130 0 die "'$browser' is not installed on your system\n";
131 }
132  
133 0 ▶ unless ($width >= $WIDTH_MIN and $width <= $WIDTH_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($width >= $WIDTH_MIN and $width <= $WIDTH_MAX) 0 0 0
Condition0%0/3
$width >= $WIDTH_MIN and $width <= $WIDTH_MAX
!ll&&!rl&&r
000
MC/DC0%0/2
$width >= $WIDTH_MIN and $width <= $WIDTH_MAX
$width >= $WIDTH_MIN $width <= $WIDTH_MAX
Truth table0%0/3
A and B
A= $width >= $WIDTH_MINB= $width <= $WIDTH_MAX
ABresult
0X0
100
111
134 0 die "Width cannot be greater than $WIDTH_MAX or less than $WIDTH_MIN\n";
135 }
136  
137 my $dump = safe_qx(
138 $Browsers{ $browser }->{Bin},
139 split(/\s+/, sprintf($Browsers{ $browser }->{Width}, $width)),
140 0 ▶ @{ $Browsers{ $browser }->{Opts} },
Branch: 1 branch
BranchTrueFalseTotal
$xhtml ? : 0 0 0
141 0 ($xhtml ? join(" ", @{ $Browsers{ $browser }->{Xhtml} }) : ()),
142 $file
143 );
144 0 ▶ unless ($? >> 8 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? >> 8 == 0) 0 0 0
145 0 die "Failed to dump $file with $Browsers{ $browser }->{Bin}\n";
146 }
147  
148 # We can't use 'open IN => ":encoding(UTF-8)"' because it was broken prior
149 # to 5.34, so we must manually decode :-/.
150 0 return decode('UTF-8', $dump);
151  
152 }
153  
154 2 1;
155  
156 =head1 NAME
157  
158 EBook::Ishmael::TextBrowserDump - Format HTML through different installed programs
159  
160 =head1 SYNOPSIS
161  
162 use EBook::Ishmael::TextBrowserDump;
163  
164 my $dump = browser_dump($file);
165  
166 =head1 DESCRIPTION
167  
168 B<EBook::Ishmael::TextBrowserDump> is a module for dumping the contents of
169 HTML files to formatted text, via programs like L<lynx(1)>. For
170 L<ishmael> user documentation, you should consult its manual (this is
171 developer documentation).
172  
173 B<EBook::Ishmael::TextBrowserDump> requires at least one of the following
174 programs to be installed:
175  
176 =over 4
177  
178 =item L<elinks(1)>
179  
180 =item L<links(1)>
181  
182 =item L<lynx(1)>
183  
184 =item L<w3m(1)>
185  
186 =item chawan
187  
188 =item L<pandoc(1)>
189  
190 =item L<queequeg(1)>
191  
192 =back
193  
194 =head1 SUBROUTINES
195  
196 =head2 $dump = browser_dump($file, $opt_ref)
197  
198 Subroutine that dumps the formatted text contents of C<$file> via a text
199 web browser.
200  
201 C<browser_dump()> can also be given a hash ref of options.
202  
203 =over 4
204  
205 =item browser
206  
207 The specific browser you would like to use for the dumping. See above for a list
208 of valid browsers. If not specified, defaults to the first browser
209 C<browser_dump()> finds installed on your system.
210  
211 =item xhtml
212  
213 Bool specifying whether the input file is XHTML or not. Defaults to C<0>.
214  
215 =item width
216  
217 Specify the width of the formatted text. Defaults to C<80>.
218  
219 =back
220  
221 =head1 GLOBAL VARIABLES
222  
223 =head2 $EBook::Ishmael::TextBrowserDump::CAN_DUMP
224  
225 Bool stating whether this module is able to dump or not (is a valid
226 text browser installed or not).
227  
228 =head1 AUTHOR
229  
230 Written by Samuel Young, E<lt>samyoung12788@gmail.comE<gt>.
231  
232 This project's source can be found on its
233 L<Codeberg Page|https://codeberg.org/1-1sam/ishmael>. Comments and pull
234 requests are welcome!
235  
236 =head1 COPYRIGHT
237  
238 Copyright (C) 2025-2026 Samuel Young
239  
240 This program is free software: you can redistribute it and/or modify
241 it under the terms of the GNU General Public License as published by
242 the Free Software Foundation, either version 3 of the License, or
243 (at your option) any later version.
244  
245 =head1 SEE ALSO
246  
247 L<queequeg(1)>, L<elinks(1)>, L<links(1)>, L<lynx(1)>, L<w3m(1)>, L<cha(1)>,
248 L<pandoc(1)>,
249  
250 =cut
↑ Summary blib/lib/EBook/Ishmael/EBook/CHM.pm »