.htmlgen/FunctionalPerl/Htmlgen/Htmlparse.pm

filter: Statementstmt 100% 62 / 62 Branchbran 66.6% 4 / 6 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 3 totaltotal 92.2% 83 / 90 CC 6 SCAR 9.0
CC
6
Cov
96%
htmlmap
6.1
BEGIN
1.0
BEGIN
1.0
CRAP 2.5
« examples/primes ↑ Summary meta/FunctionalPerl/Indexing.pm »
1 #
2 # Copyright (c) 2014-2019 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FunctionalPerl::Htmlgen::Htmlparse
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FunctionalPerl::Htmlgen::Htmlparse qw(htmlparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 my $b = htmlparse '<p>hi</p> <p>there!', 'body';
18 is ref($b), 'PXML::_::XHTML';
19 is $b->string, '<body><p>hi</p><p>there!</p></body>';
20  
21 =head1 DESCRIPTION
22  
23  
24 =head1 NOTE
25  
26 This is alpha software! Read the status section in the package README
27 or on the L<website|http://functional-perl.org/>.
28  
29 =cut
30  
31 package FunctionalPerl::Htmlgen::Htmlparse;
32 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use experimental "signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 3 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 3 our @EXPORT = qw();
40 3 our @EXPORT_OK = qw(htmlparse);
41 3 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
42  
43 3 ▶use FP::Docstring;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 #use PXML::Element;
47 3 ▶use PXML::XHTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 4 ▶sub htmlparse_raw ($htmlstr, $whichtag) {
Subroutine htmlparse_raw: called CC=1 SCAR=0.0
Pod undocumented
51 4 my $t = HTML::TreeBuilder->new;
52 4 $t->ignore_unknown(0); # allow <with_toc> elements
53 4 $t->parse_content($htmlstr);
54 4 my $e = $t->elementify;
55  
56 # (^ actually mutates $t into the HTML::Element object already, ugh)
57 4 $e->find_by_tag_name($whichtag)
58 }
59  
60 3 my $attsubname_re = qr/\w[\w-]*/; # XX OK?
61 3 my $attname_re
62 = qr/$attsubname_re(?::$attsubname_re)?/; # with namespace, optionally
63  
64 15 ▶sub htmlmap($e) {
Subroutine htmlmap: called CC=6 SCAR=18.0
Pod undocumented
65 15 __ '(HTML::Element) -> PXML::_::XHTML '
66 . '-- convert output from HTML::TreeBuilder to PXML::XHTML (PXML::Element)';
67 15 my $name = lc($e->tag);
68 15 my $atts = {};
69 15 for ($e->all_external_attr_names) {
70 1 ▶ next if $_ eq "/";
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "/" 0 1 1
71  
72 # HACK: accept namespaces in attribute names
73 1 ▶ die "invalid attribute name string '$_'" unless /^$attname_re\z/s;
Branch: 1 branch
BranchTrueFalseTotal
unless /^$attname_re\z/s 0 1 1
74 1 $$atts{ lc $_ } = $e->attr($_);
75 }
76  
77 # XX unsafe, if we don't check that a corresponding constructor
78 # exists! Could fall back to just PXML::Element (which
79 # PXML::_::XHTML is):
80 PXML::_::XHTML->new(
81 $name, $atts,
82 [
83 map {
84 22 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 11 11 22
85  
86 # another HTML::Element
87 3 ▶ no warnings "recursion"; # XX should rather sanitize input?
Subroutine BEGIN: called CC=1 SCAR=0.0
88 11 htmlmap($_)
89 } else {
90  
91 # a string
92 11 $_
93 }
94 15 ▶ } @{ $e->content || [] }
Condition50%1/2
$e->content || []
l!l
150
MC/DC0%0/1
$e->content || []
$e->content
Truth table50%1/2
A
A= $e->content
Aresult
11
00
95 ]
96 );
97 }
98  
99 4 ▶sub htmlparse ($str, $whichtag) {
Subroutine htmlparse: called CC=1 SCAR=0.0
Pod undocumented
100 4 __ '($str,$whichtag) -> PXML::Element '
101 . '-- parse HTML string to PXML; $whichtag is passed to'
102 . ' find_by_tag_name from HTML::TreeBuilder';
103 4 htmlmap(htmlparse_raw($str, $whichtag))
104 }
105  
106 # TEST{ htmlparse ('<with_toc><p>abc</p><p>foo</p></with_toc>', "body")
107 # ->string }
108 # '<body><with_toc><p>abc</p><p>foo</p></with_toc></body>';
109 # HTML::TreeBuilder VERSION 5.02 drops with_toc here.
110  
111 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
112 1 htmlparse('x<with_toc><p>abc</p><p>foo</p></with_toc>', "body")->string
113 }
114 3 '<body>x<with_toc><p>abc</p><p>foo</p></with_toc></body>';
115  
116 # interestingly here it doesn't.
117  
118 # But perhaps it's best to do like:
119 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
120 1 htmlparse('<body><with_toc><p>abc</p><p>foo</p></with_toc></body>', "body")
121 ->string
122 }
123 3 '<body><with_toc><p>abc</p><p>foo</p></with_toc></body>';
124  
125 3 1
« examples/primes ↑ Summary meta/FunctionalPerl/Indexing.pm »