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