blib/lib/Markapl/FromHTML.pm

filter: Statementstmt 21.4% 29 / 135 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 57.1% 8 / 14 Podpod n/a totaltotal 15.2% 37 / 243 CC 39 SCAR 56.1
CC
39
Cov
14%
__ANON__
373.6
convert
373.6
__ANON__
5.0
CRAP 273.7
↑ Summary
1 package Markapl::FromHTML;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Rubyish;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use HTML::PullParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 # use Data::Dump qw(pp);
9  
10 6 our $VERSION = '0.03';
11  
12 6 my $indent_offset = 4;
13  
14 0 ▶def load($html) {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine load: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
15 0 $self->{html} = $html;
16 0 $self;
17 6 }
18  
19 0 ▶def dump {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine __ANON__: not called CC=2 SCAR=16.1
Subroutine dump: not called CC=2 SCAR=16.1
20 0 ▶ return $_ if (defined($_ = $self->{markapl}));
Branch: 2 branches
BranchTrueFalseTotal
if defined($_ = $self->{"markapl"}) 0 0 0
if defined($_ = $self->{"markapl"}) 0 0 0
21 0 return $self->convert;
22 6 }
23  
24 0 ▶def convert {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine convert: not called CC=19 SCAR=59.2
Subroutine __ANON__: not called CC=19 SCAR=59.2
25 0 ▶ return "" unless $self->{html};
Branch: 2 branches
BranchTrueFalseTotal
unless $self->{"html"} 0 0 0
unless $self->{"html"} 0 0 0
26  
27 my $p = HTML::PullParser->new(
28 doc => $self->{html},
29 0 start => '"S", tagname, @attr',
30 text => '"T", text',
31 end => '"E", tagname',
32 );
33  
34 0 my $current_tag = "";
35 0 my @stack = ();
36 0 my $indent = 0;
37 0 ▶ while(my $token = $p->get_token) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $token = $p->get_token) 0 0 0
if (my $token = $p->get_token) 0 0 0
38 # warn $token->[0],"\n";;
39 0 ▶ if ($token->[0] eq 'S') {
Branch: 6 branches
BranchTrueFalseTotal
if ($token->[0] eq 'S') { } 0 0 0
elsif ($token->[0] eq 'T') { } 0 0 0
elsif ($token->[0] eq 'E') { } 0 0 0
if ($token->[0] eq 'S') { } 0 0 0
elsif ($token->[0] eq 'T') { } 0 0 0
elsif ($token->[0] eq 'E') { } 0 0 0
40 0 push @stack, { tag => $token->[1], attr => [@$token[2..$#$token]]};
41 0 $indent += 1;
42 }
43 elsif ($token->[0] eq 'T') {
44 0 ▶ unless($token->[1] =~ /^\s*$/s ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($token->[1] =~ /^\s*$/s) 0 0 0
unless ($token->[1] =~ /^\s*$/s) 0 0 0
45 0 push @stack, { text => $token->[1] }
46 }
47 }
48 elsif ($token->[0] eq 'E') {
49 # pp $token;
50 0 my @content;
51 0 my $content = pop @stack;
52 0 ▶ while (!$content->{tag} || $content->{tag} ne $token->[1]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $content->{"tag"} or $content->{"tag"} ne $token->[1]) 0 0 0
if (not $content->{"tag"} or $content->{"tag"} ne $token->[1]) 0 0 0
Condition0%0/6
not $content->{"tag"} or $content->{"tag"} ne $token->[1]
l!l&&r!l&&!r
000
not $content->{"tag"} or $content->{"tag"} ne $token->[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not $content->{"tag"} or $content->{"tag"} ne $token->[1]
not $content->{"tag"} $content->{"tag"} ne $token->[1]
MC/DC0%0/2
not $content->{"tag"} or $content->{"tag"} ne $token->[1]
not $content->{"tag"} $content->{"tag"} ne $token->[1]
Truth table0%0/3
not(A) or B
A= not $content->{"tag"}B= $content->{"tag"} ne $token->[1]
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not $content->{"tag"}B= $content->{"tag"} ne $token->[1]
ABresult
1X1
011
000
53 0 push @content, $content;
54 0 $content = pop @stack;
55 }
56  
57 0 my $start_tag = $content;
58  
59 0 my $indent_str = " " x ($indent * $indent_offset);
60 0 my $indent_str2 = " " x ( ($indent + 1) * $indent_offset);
61  
62 0 my $attr = "";
63 0 my @attr = @{$start_tag->{attr}};
64 0 ▶ if (@attr) {
Branch: 2 branches
BranchTrueFalseTotal
if (@attr) 0 0 0
if (@attr) 0 0 0
65 0 ▶ while (my ($k, $v) = splice(@attr, 0, 2)) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = splice(@attr, 0, 2)) 0 0 0
if (() = splice(@attr, 0, 2)) 0 0 0
66 0 $attr .= qq{ $k => "$v"};
67 }
68 0 $attr = "($attr )";
69 }
70  
71 0 ▶ if (@content == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@content == 1) { } 0 0 0
if (@content == 1) { } 0 0 0
72 0 my $content_text = $content[0]->{code};
73 0 ▶ if (!$content_text && $content[0]->{text}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $content_text and $content[0]{"text"}) 0 0 0
if (not $content_text and $content[0]{"text"}) 0 0 0
Condition0%0/6
not $content_text and $content[0]{"text"}
!ll&&!rl&&r
000
not $content_text and $content[0]{"text"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $content_text and $content[0]{"text"}
not $content_text $content[0]{"text"}
MC/DC0%0/2
not $content_text and $content[0]{"text"}
not $content_text $content[0]{"text"}
Truth table0%0/3
not(A) and B
A= not $content_textB= $content[0]{"text"}
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not $content_textB= $content[0]{"text"}
ABresult
0X0
100
111
74 0 $content_text = "\"$content[0]->{text}\""
75 }
76 0 ▶ $content_text ||= '';
Condition0%0/4
$content_text ||= ""
l!l
00
$content_text ||= ""
l!l
00
MC/DC0%0/1
$content_text ||= ""
$content_text
MC/DC0%0/1
$content_text ||= ""
$content_text
Truth table0%0/2
A
A= $content_text
Aresult
11
00
Truth table0%0/2
A
A= $content_text
Aresult
11
00
77 0 push @stack, {
78 code => "\n${indent_str}$start_tag->{tag}${attr} {\n${indent_str2}$content_text\n${indent_str}};\n"
79 };
80 }
81 else {
82 0 for (@content) {
83 0 ▶ if ($_->{text}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_->{"text"}) 0 0 0
if ($_->{"text"}) 0 0 0
84 0 $_->{code} = "outs \"$_->{text}\";";
85 0 $_->{text} = undef;
86 }
87 }
88 0 ▶ my $content_code = join "\n", map { $_->{code}||"" } reverse @content;
Condition0%0/4
$_->{'code'} || ''
l!l
00
$_->{'code'} || ''
l!l
00
MC/DC0%0/1
$_->{'code'} || ''
$_->{'code'}
MC/DC0%0/1
$_->{'code'} || ''
$_->{'code'}
Truth table0%0/2
A
A= $_->{'code'}
Aresult
11
00
Truth table0%0/2
A
A= $_->{'code'}
Aresult
11
00
89 # pp $start_tag->{tag}, $start_tag->{indent};
90 0 push @stack, {
91 code => "\n${indent_str}$start_tag->{tag}${attr} {\n${indent_str2}$content_code\n${indent_str}};\n"
92 };
93 }
94  
95 0 $indent -= 1;
96 }
97 }
98  
99 0 ▶ my $ret = join "\n", "sub {", (map { $_->{code} || $_->{text} } @stack), "\n}\n";
Condition0%0/6
$_->{'code'} || $_->{'text'}
l!l&&r!l&&!r
000
$_->{'code'} || $_->{'text'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{'code'} || $_->{'text'}
$_->{'code'} $_->{'text'}
MC/DC0%0/2
$_->{'code'} || $_->{'text'}
$_->{'code'} $_->{'text'}
Truth table0%0/3
A || B
A= $_->{'code'}B= $_->{'text'}
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $_->{'code'}B= $_->{'text'}
ABresult
1X1
011
000
100  
101 # Squeeze empty lines.
102 0 $ret =~ s/\n\s*\n/\n/g;
103 0 $ret =~ s/\{\n\s+\}(;?)\n/{}$1\n/g;
104  
105 # Re-org all text only blocks to a single line.
106 0 $ret =~ s/\{\n\s+(".+")\n\s+\}(;?)\n/{ $1 }$2\n/g;
107  
108 0 return $ret;
109 6 }
110  
111 6 1;
112 __END__
↑ Summary