HTML/Compiler.pm

filter: Statementstmt 94.1% 81 / 86 Branchbran 85.0% 17 / 20 Conditioncond 61.1% 11 / 18 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 92.8% 13 / 14 Podpod 25.0% 2 / 8 totaltotal 82.0% 128 / 156 CC 18 SCAR 18.0
CC
18
Cov
88%
process_element
12.4
find_delimiter
3.3
build_attr
3.0
CRAP 6.1
« HTML/STL.pm ↑ Summary HTML.pm »
1 package DSL::HTML::Compiler;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Exporter::Declare qw/import default_exports/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 default_exports qw{
12 compile_from_html
13 compile_from_file
14 };
15  
16 10 ▶sub delimiters {
Subroutine delimiters: called CC=1 SCAR=0.0
Pod undocumented
17 10 return qw{
18 '
19 "
20 `
21 /
22 |
23 :
24 ~
25 !
26 -
27 ⌁
28 }
29 }
30  
31 0 ▶sub compile_from_file {
Subroutine compile_from_file: not called CC=1 SCAR=6.9
Pod documented
32 0 my ($name, $file) = @_;
33 0 my $tree = HTML::TreeBuilder->new;
34 0 $tree->parse_file($file);
35 0 return compile_from_tree($name, $tree);
36 }
37  
38 1 ▶sub compile_from_html {
Subroutine compile_from_html: called CC=1 SCAR=0.0
Pod documented
39 1 my ($name, $text) = @_;
40 1 my $tree = HTML::TreeBuilder->new;
41 1 $tree->parse($text);
42 1 $tree->eof;
43 1 return compile_from_tree($name, $tree);
44 }
45  
46 1 ▶sub compile_from_tree {
Subroutine compile_from_tree: called CC=1 SCAR=0.0
Pod undocumented
47 1 my ($name, $tree) = @_;
48 1 $tree->elementify;
49  
50 1 my $out;
51  
52 1 $out = "use strict;\nuse warnings;\nuse utf8;\nuse DSL\::HTML;\n\ntemplate $name {\n";
53 1 process_element( \$out, $tree, 1 );
54 1 $out .= "}\n\n1;\n";
55  
56 1 return $out;
57 }
58  
59 10 ▶sub find_delimiter {
Subroutine find_delimiter: called CC=3 SCAR=12.0
Pod undocumented
60 10 my ($content) = @_;
61 10 for my $d ( delimiters(), 'a' .. 'z', 'A' .. 'Z' ) {
62 10 ▶ return $d unless $content =~ m/$d/;
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ /$d/ 10 0 10
63 }
64  
65 0 croak "Could not find a usable delimiter";
66 }
67  
68 13 ▶sub process_element {
Subroutine process_element: called CC=12 SCAR=25.2
Pod undocumented
69 13 my ($out, $elem, $indent) = @_;
70  
71 13 my $prefix = " " x $indent;
72  
73 13 ▶ unless (ref $elem) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $elem) 4 9 13
74 4 my $d = find_delimiter($elem);
75 4 $$out .= "${prefix}text q${d}${elem}${d};\n";
76 4 return;
77 }
78  
79 9 my $has_content = $elem->content_list;
80  
81 9 ▶ if ( !$has_content && $elem->tag =~ m/^script$/i && $elem->attr('src')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $has_content and $elem->tag =~ /^script$/i and $elem->attr("src")) 1 8 9
Condition83%5/6
not $has_content and $elem->tag =~ /^script$/i and $elem->attr("src")
!ll&&!rl&&r
801
not $has_content and $elem->tag =~ /^script$/i
!ll&&!rl&&r
711
MC/DC66%2/3
not $has_content and $elem->tag =~ /^script$/i and $elem->attr("src")
not $has_content $elem->tag =~ /^script$/i $elem->attr("src")
Truth table75%3/4
not(A) and B and C
A= not $has_contentB= $elem->tag =~ /^script$/iC= $elem->attr("src")
ABCresult
0XX0
10X0
1100
1111
82 1 my $src = $elem->attr('src');
83 1 my $d = find_delimiter($src);
84 1 $$out .= qq|${prefix}js q${d}${src}${d};\n|;
85 1 return;
86 }
87  
88 8 ▶ if ( !$has_content && $elem->tag =~ m/^link$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $has_content and $elem->tag =~ /^link$/i) 1 7 8
Condition66%2/3
not $has_content and $elem->tag =~ /^link$/i
!ll&&!rl&&r
701
MC/DC50%1/2
not $has_content and $elem->tag =~ /^link$/i
not $has_content $elem->tag =~ /^link$/i
Truth table66%2/3
not(A) and B
A= not $has_contentB= $elem->tag =~ /^link$/i
ABresult
0X0
100
111
89 1 my %attr = $elem->all_attr;
90 1 ▶ if ($attr{rel} && $attr{href} && $attr{type} eq 'text/css') {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr{"rel"} and $attr{"href"} and $attr{"type"} eq "text/css") 1 0 1
Condition33%2/6
$attr{"rel"} and $attr{"href"} and $attr{"type"} eq "text/css"
!ll&&!rl&&r
001
$attr{"rel"} and $attr{"href"}
!ll&&!rl&&r
001
MC/DC0%0/3
$attr{"rel"} and $attr{"href"} and $attr{"type"} eq "text/css"
$attr{"rel"} $attr{"href"} $attr{"type"} eq "text/css"
Truth table25%1/4
A and B and C
A= $attr{"rel"}B= $attr{"href"}C= $attr{"type"} eq "text/css"
ABCresult
0XX0
10X0
1100
1111
91 1 my $href = $attr{href};
92 1 my $d = find_delimiter($href);
93 1 $$out .= qq|${prefix}css q${d}${href}${d};\n|;
94 1 return;
95 }
96 }
97  
98 7 ▶ unless ($elem->tag =~ m/^(html|body)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($elem->tag =~ /^(html|body)$/i) 5 2 7
99 5 my $tag = $elem->tag;
100 5 my $attr = build_attr($elem);
101 5 $$out .= "${prefix}tag ${tag}${attr} {";
102 5 ▶ if ($has_content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_content) 5 0 5
103 5 $$out .= "\n";
104 5 process_children($out, $elem, $indent + 1);
105 5 $$out .= "${prefix}";
106 }
107 5 $$out .= "}\n";
108 5 return;
109 }
110  
111 2 process_children($out, $elem, $indent);
112 }
113  
114 7 ▶sub process_children {
Subroutine process_children: called CC=3 SCAR=11.0
Pod undocumented
115 7 my ($out, $elem, $indent) = @_;
116 7 my @children = $elem->content_list;
117 7 ▶ for ( my $i = 0; $i < @children; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @children) 12 7 19
118 12 process_element($out, $children[$i], $indent);
119 12 ▶ $$out .= "\n" unless $i == (@children - 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $i == @children - 1 5 7 12
120 }
121 }
122  
123 5 ▶sub build_attr {
Subroutine build_attr: called CC=3 SCAR=11.0
Pod undocumented
124 5 my ($elem) = @_;
125 5 my %attr = $elem->all_attr;
126 5 ▶ my @keys = sort grep { $_ !~ m/^_/ && $_ ne '/' } keys %attr;
Condition66%2/3
!($_ =~ /^_/) && $_ ne '/'
!ll&&!rl&&r
1504
MC/DC50%1/2
!($_ =~ /^_/) && $_ ne '/'
!($_ =~ /^_/) $_ ne '/'
Truth table66%2/3
not(A) && B
A= !($_ =~ /^_/)B= $_ ne '/'
ABresult
0X0
100
111
127  
128 5 ▶ return "" unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 3 2 5
129  
130 my $text = join ", " => map {
131 2 my $v = $attr{$_};
132 4 my $d = find_delimiter($v);
133 4 qq|'$_' => q${d}${v}${d}|
134 } @keys;
135  
136 2 return "($text)";
137 }
138  
139 1 1;
140  
141 __END__
« HTML/STL.pm ↑ Summary HTML.pm »