| 1 | #! /bin/false | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>, | ||||||||||||||||||||||||||||
| 4 | # all rights reserved. | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # This program is free software: you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 7 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||
| 8 | # the Free Software Foundation; either version 3 of the License, or | ||||||||||||||||||||||||||||
| 9 | # (at your option) any later version. | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||
| 14 | # GNU General Public License for more details. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | package Qgoda::Processor::HTMLFilter; | ||||||||||||||||||||||||||||
| 20 | 2 | $Qgoda::Processor::HTMLFilter::VERSION = 'v0.9.2'; | |||||||||||||||||||||||||||
| 21 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use Locale::TextDomain qw(qgoda); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use HTML::Parser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use Qgoda::Util qw(empty perl_class class2module); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | use base qw(Qgoda::Processor); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub new { | ||||||||||||||||||||||||||||
| 32 | 2 | ▶ | my ($class, @plug_ins) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=14 SCAR=27.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | my %handlers = ( | |||||||||||||||||||||||||||
| 35 | declaration => [], | ||||||||||||||||||||||||||||
| 36 | start => [], | ||||||||||||||||||||||||||||
| 37 | text => [], | ||||||||||||||||||||||||||||
| 38 | end => [], | ||||||||||||||||||||||||||||
| 39 | comment => [], | ||||||||||||||||||||||||||||
| 40 | process => [], | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | my $count = 0; | |||||||||||||||||||||||||||
| 44 | 2 | foreach my $spec (@plug_ins) { | |||||||||||||||||||||||||||
| 45 | 8 | ++$count; | |||||||||||||||||||||||||||
| 46 | 8 | ▶ | if (!ref $spec || 'ARRAY' ne reftype $spec) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $spec or "ARRAY" ne builtin::reftype($spec)
MC/DC50%1/2
not ref $spec or "ARRAY" ne builtin::reftype($spec)
not ref $spec
"ARRAY" ne builtin::reftype($spec)
Truth table66%2/3
not(A) or B
A= not ref $specB= "ARRAY" ne builtin::reftype($spec)
| |||||||||||||||||||||||||||||
| 47 | 6 | $spec = [$spec]; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 8 | my ($name, @args) = @{$spec}; | |||||||||||||||||||||||||||
| 51 | 8 | ▶ | if (empty $name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | die __x("{class}: filter specification #{count}:" | |||||||||||||||||||||||||||
| 53 | . " empty plug-in name", | ||||||||||||||||||||||||||||
| 54 | class => $class, count => $count); | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 8 | ▶ | if (!perl_class $name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | die __x("{class}: filter specification #{count}:" | |||||||||||||||||||||||||||
| 59 | . " illegal plug-in name '{name}'", | ||||||||||||||||||||||||||||
| 60 | class => $class, count => $count, | ||||||||||||||||||||||||||||
| 61 | name => $name); | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 8 | $name = 'Qgoda::HTMLFilter::' . $name; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 8 | my $module = class2module $name; | |||||||||||||||||||||||||||
| 67 | 8 | require $module; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 8 | my $plug_in = $name->new(@args); | |||||||||||||||||||||||||||
| 70 | 8 | ▶ | push @{$handlers{declaration}}, $plug_in | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | if $plug_in->can('declaration'); | ||||||||||||||||||||||||||||
| 72 | 8 | ▶ | push @{$handlers{start}}, $plug_in | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | if $plug_in->can('start'); | ||||||||||||||||||||||||||||
| 74 | 8 | ▶ | push @{$handlers{text}}, $plug_in | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | if $plug_in->can('text'); | ||||||||||||||||||||||||||||
| 76 | 8 | ▶ | push @{$handlers{end}}, $plug_in | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | if $plug_in->can('end'); | ||||||||||||||||||||||||||||
| 78 | 8 | ▶ | push @{$handlers{comment}}, $plug_in | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | if $plug_in->can('comment'); | ||||||||||||||||||||||||||||
| 80 | 8 | ▶ | push @{$handlers{process}}, $plug_in | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | if $plug_in->can('process'); | ||||||||||||||||||||||||||||
| 82 | 8 | ▶ | push @{$handlers{start_document}}, $plug_in | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | if $plug_in->can('start_document'); | ||||||||||||||||||||||||||||
| 84 | 8 | ▶ | push @{$handlers{end_document}}, $plug_in | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | if $plug_in->can('end_document'); | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | my $self = { | |||||||||||||||||||||||||||
| 89 | __handlers => \%handlers, | ||||||||||||||||||||||||||||
| 90 | }; | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | bless $self, $class; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub process { | ||||||||||||||||||||||||||||
| 96 | 3 | ▶ | my ($self, $content, $asset) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 3 | my $output = ''; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | my $handler = sub { | ||||||||||||||||||||||||||||
| 101 | 129 | ▶ | my ($event, $text, $tagname, $attr, $attrseq, $is_cdata) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 129 | my $chunk; | |||||||||||||||||||||||||||
| 104 | 129 | ▶ | if ('start_document' eq $event || 'end_document' eq $event) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
"start_document" eq $event or "end_document" eq $event
MC/DC100%2/2
"start_document" eq $event or "end_document" eq $event
"start_document" eq $event
"end_document" eq $event
Truth table100%3/3
A or B
A= "start_document" eq $eventB= "end_document" eq $event
| |||||||||||||||||||||||||||||
| 105 | 6 | $chunk = $output; | |||||||||||||||||||||||||||
| 106 | 6 | $output = ''; | |||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||
| 108 | 123 | $chunk = $text; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 129 | foreach my $plug_in (@{$self->{__handlers}->{$event}}) { | |||||||||||||||||||||||||||
| 112 | 124 | $chunk = $plug_in->$event( | |||||||||||||||||||||||||||
| 113 | $chunk, | ||||||||||||||||||||||||||||
| 114 | event => $event, | ||||||||||||||||||||||||||||
| 115 | text => $text, | ||||||||||||||||||||||||||||
| 116 | output => \$output, | ||||||||||||||||||||||||||||
| 117 | text => $text, | ||||||||||||||||||||||||||||
| 118 | tagname => $tagname, | ||||||||||||||||||||||||||||
| 119 | attr => $attr, | ||||||||||||||||||||||||||||
| 120 | attrseq => $attrseq, | ||||||||||||||||||||||||||||
| 121 | is_cdata => $is_cdata, | ||||||||||||||||||||||||||||
| 122 | asset => $asset, | ||||||||||||||||||||||||||||
| 123 | ); | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 129 | $output .= $chunk; | |||||||||||||||||||||||||||
| 127 | 3 | }; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 3 | my $parser = HTML::Parser->new( | |||||||||||||||||||||||||||
| 130 | comment_h => [$handler, 'event, text'], | ||||||||||||||||||||||||||||
| 131 | declaration_h => [$handler, 'event, text'], | ||||||||||||||||||||||||||||
| 132 | start_h => [$handler, 'event, text, tagname, attr, attrseq, is_cdata'], | ||||||||||||||||||||||||||||
| 133 | end_h => [$handler, 'event, text, tagname'], | ||||||||||||||||||||||||||||
| 134 | process_h => [$handler, 'event, text'], | ||||||||||||||||||||||||||||
| 135 | text_h => [$handler, 'event, text'], | ||||||||||||||||||||||||||||
| 136 | start_document_h => [$handler, 'event'], | ||||||||||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||||||||||
| 138 | 3 | $parser->empty_element_tags(1); | |||||||||||||||||||||||||||
| 139 | 3 | $parser->parse($content); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # FIXME! Why can that handler not be registered like the others? | ||||||||||||||||||||||||||||
| 142 | 3 | $handler->('end_document', $output); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 3 | return $output; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 2 | 1; | |||||||||||||||||||||||||||