| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
not $has_content and $elem->tag =~ /^script$/i and $elem->attr("src")
not $has_content and $elem->tag =~ /^script$/i
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not $has_content and $elem->tag =~ /^link$/i
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
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | my %attr = $elem->all_attr; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | if ($attr{rel} && $attr{href} && $attr{type} eq 'text/css') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$attr{"rel"} and $attr{"href"} and $attr{"type"} eq "text/css"
$attr{"rel"} and $attr{"href"}
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 12 | process_element($out, $children[$i], $indent); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 12 | ▶ | $$out .= "\n" unless $i == (@children - 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 '/'
MC/DC50%1/2
!($_ =~ /^_/) && $_ ne '/'
!($_ =~ /^_/)
$_ ne '/'
Truth table66%2/3
not(A) && B
A= !($_ =~ /^_/)B= $_ ne '/'
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 5 | ▶ | return "" unless @keys; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||