| 1 | ## no critic: InputOutput::RequireBriefOpen | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package App::htmlgrep; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 6 | 1 | our $DATE = '2023-02-10'; # DATE | |||||||||||||||||||||||||||
| 7 | 1 | our $DIST = 'App-HTMLUtils-Grep'; # DIST | |||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.002'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Log::ger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use AppBase::Grep; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use IPC::System::Options qw(system); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Perinci::Sub::Util qw(gen_modified_sub); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | our %SPEC; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub _browser_dump { | ||||||||||||||||||||||||||
|
Subroutine
_browser_dump: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 22 | 0 | my ($input_path, $output_path, $args) = @_; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | if ($args->{browser} eq 'links') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | system({shell=>1}, "links", "-force-html", "-dump", $input_path, \">", $output_path); | |||||||||||||||||||||||||||
| 26 | } else { | ||||||||||||||||||||||||||||
| 27 | 0 | die "Browser not set or unknown"; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | gen_modified_sub( | ||||||||||||||||||||||||||||
| 32 | output_name => 'htmlgrep', | ||||||||||||||||||||||||||||
| 33 | base_name => 'AppBase::Grep::grep', | ||||||||||||||||||||||||||||
| 34 | summary => 'Print lines matching text in HTML files', | ||||||||||||||||||||||||||||
| 35 | description => <<'_', | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | This is a wrapper for 'lynx -dump' (or equivalent in links and w3m) + grep-like | ||||||||||||||||||||||||||||
| 38 | utility that is based on <pm:AppBase::Grep>. The unique features include | ||||||||||||||||||||||||||||
| 39 | multiple patterns and `--dash-prefix-inverts`. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | _ | ||||||||||||||||||||||||||||
| 42 | add_args => { | ||||||||||||||||||||||||||||
| 43 | files => { | ||||||||||||||||||||||||||||
| 44 | description => <<'_', | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | If not specified, will search for all HTML files recursively from the current | ||||||||||||||||||||||||||||
| 47 | directory. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | _ | ||||||||||||||||||||||||||||
| 50 | 'x.name.is_plural' => 1, | ||||||||||||||||||||||||||||
| 51 | 'x.name.singular' => 'file', | ||||||||||||||||||||||||||||
| 52 | schema => ['array*', of=>'filename*'], | ||||||||||||||||||||||||||||
| 53 | pos => 1, | ||||||||||||||||||||||||||||
| 54 | slurpy => 1, | ||||||||||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||||||||||
| 56 | browser => { | ||||||||||||||||||||||||||||
| 57 | schema => ['str*', in=>[qw/lynx links w3m/]], | ||||||||||||||||||||||||||||
| 58 | default => 'links', | ||||||||||||||||||||||||||||
| 59 | }, | ||||||||||||||||||||||||||||
| 60 | # XXX recursive (-r) | ||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||
| 62 | 1 | ▶ | modify_meta => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 1 | my $meta = shift; | |||||||||||||||||||||||||||
| 64 | $meta->{examples} = [ | ||||||||||||||||||||||||||||
| 65 | 1 | ]; | |||||||||||||||||||||||||||
| 66 | $meta->{links} = [ | ||||||||||||||||||||||||||||
| 67 | 1 | ]; | |||||||||||||||||||||||||||
| 68 | $meta->{deps} = { | ||||||||||||||||||||||||||||
| 69 | 1 | }; | |||||||||||||||||||||||||||
| 70 | }, | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | output_code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 72 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 73 | 0 | my ($tempdir, $fh, $file); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my @files = @{ $args{files} // [] }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"files"} // []
MC/DC0%0/1
$args{"files"} // []
$args{"files"}
Truth table0%0/2
A
A= $args{"files"}
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($args{regexps} && @{ $args{regexps} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args{"regexps"} and @{$args{"regexps"};}
MC/DC0%0/2
$args{"regexps"} and @{$args{"regexps"};}
$args{"regexps"}
@{$args{"regexps"};}
Truth table0%0/3
A and B
A= $args{"regexps"}B= @{$args{"regexps"};}
| |||||||||||||||||||||||||||||
| 77 | 0 | unshift @files, delete $args{pattern}; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | unless (@files) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | require File::Find::Rule; | |||||||||||||||||||||||||||
| 81 | 0 | @files = File::Find::Rule->new->file->name("*.htm", "*.html", "*.HTM", "*.HTML")->in("."); | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | unless (@files) { return [200, "No HTML files to search against"] } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | my $show_label = @files > 1 ? 1:0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | $args{_source} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 88 | READ_LINE: | ||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if (!defined $fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | return unless @files; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | unless (defined $tempdir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | require File::Temp; | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | $tempdir = File::Temp::tempdir(CLEANUP=>$ENV{DEBUG} ? 0:1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | $file = shift @files; | |||||||||||||||||||||||||||
| 99 | 0 | require File::Basename; | |||||||||||||||||||||||||||
| 100 | 0 | my $tempfile = File::Basename::basename($file) . ".txt"; | |||||||||||||||||||||||||||
| 101 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 102 | 0 | while (1) { | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | my $tempfile2 = $tempfile . ($i ? ".$i" : ""); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | do { $tempfile = $tempfile2; last } unless -e "$tempdir/$tempfile2"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | $i++; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | log_trace "Running browser dump $file $tempdir/$tempfile ..."; | |||||||||||||||||||||||||||
| 109 | 0 | _browser_dump($file, "$tempdir/$tempfile", \%args); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | open $fh, "<", "$tempdir/$tempfile" or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | warn "htmlgrep: Can't open '$tempdir/$tempfile': $!, skipped\n"; | |||||||||||||||||||||||||||
| 113 | 0 | undef $fh; | |||||||||||||||||||||||||||
| 114 | }; | ||||||||||||||||||||||||||||
| 115 | 0 | redo READ_LINE; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my $line = <$fh>; | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | if (defined $line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return ($line, $show_label ? $file : undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||||||||||
| 122 | 0 | undef $fh; | |||||||||||||||||||||||||||
| 123 | 0 | redo READ_LINE; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 0 | }; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | AppBase::Grep::grep(%args); | |||||||||||||||||||||||||||
| 129 | }, | ||||||||||||||||||||||||||||
| 130 | 1 | ); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 1 | 1; | |||||||||||||||||||||||||||
| 133 | # ABSTRACT: Print lines matching text in HTML files | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||