lib/ChordPro/lib/SVGPDF/Parser.pm

filter: Statementstmt 17.1% 22 / 128 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 55.5% 5 / 9 Podpod 0.0% 0 / 2 totaltotal 9.9% 27 / 272 CC 52 SCAR 74.1
CC
52
Cov
9%
_parse
2162.0
__ANON__
30.0
parse_file
6.0
CRAP 1660.5
« lib/ChordPro/lib/SVGPDF/Text.pm ↑ Summary lib/ChordPro/Output/ChordPro.pm »
1 #! perl
2  
3 12 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # SVG Parser, based on a modified version of XML::Tiny.
8  
9 class SVGPDF::Parser;
10  
11 12 ▶use File::LoadLines;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 12 field $debug;
15  
16 0 ▶method parse_file ( $fname, %args ) {
Subroutine parse_file: not called CC=2 SCAR=17.9
Pod undocumented
17 0 ▶ $debug = $args{debug} if defined $args{debug};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"debug"} 0 0 0
18 0 my $data = loadlines( $fname, { split => 0, chomp => 0 } );
19 0 $self->parse( $data, %args );
20 12 }
21  
22 0 ▶method parse ( $data, %args ) {
Subroutine parse: not called CC=2 SCAR=17.9
Pod undocumented
23 0 ▶ if ( $debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
24 # Make it easier to read/write long lines and disable parts.
25 0 $data =~ s/^#.*//mg;
26 0 $data =~ s/\\[\n\r]+\s*//g;
27 }
28 0 $self->_parse( $data, %args );
29 12 }
30  
31 # The _parse method is a modified version of XML::Tiny. All comments
32 # and restrictions of L<XML::Tiny> are applicable.
33 # Main modification is to allow whitespace elements in <text> elements.
34 # These are significant in SVG.
35 # Since we're aiming at SVG parsing, and SVG is strict XML but often
36 # wrapped in an (X)HTML document, the parser functionality is set
37 # to no fatal_declarations and strict_entity_parsing.
38  
39 12 field $re_name;
40 12 field %emap;
41  
42 0 ▶method _parse ( $data, %params) {
Subroutine _parse: not called CC=46 SCAR=76.8
43 0 my $elem = { content => [] };
44  
45 # TODO: Accept whitespace tokens by default within <text> elements.
46 0 my $whitespace_tokens = $params{whitespace_tokens};
47  
48 0 ▶ $re_name //= '[:_a-z][\\w:\\.-]*';
Condition0%0/2
$re_name //= "[:_a-z][\\w:\\.-]*"
l!l
00
MC/DC0%0/1
$re_name //= "[:_a-z][\\w:\\.-]*"
$re_name
Truth table0%0/2
A
A= $re_name
Aresult
11
00
49 0 %emap = qw( lt < gt > amp & quot " apos ' );
50  
51 0 ▶ my $fixent = sub ( $e ) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
52 0 ▶ $e =~ s/&#(\d+);/chr($1)/ge && return $e;
Branch: 1 branch
BranchTrueFalseTotal
$e =~ s/&#(\d+);/chr $1;/egu and return $e 0 0 0
53 0 ▶ $e =~ s/&#(x[0-9a-f]+);/chr(hex($1))/gie && return $e;
Branch: 1 branch
BranchTrueFalseTotal
$e =~ s/&#(x[0-9a-f]+);/chr hex $1;/egiu and return $e 0 0 0
54 0 ▶ $e =~ s/&(lt|gt|quot|apos|amp);/$emap{$1}/ge && return $e;
Branch: 1 branch
BranchTrueFalseTotal
$e =~ s/&(lt|gt|quot|apos|amp);/$emap{$1};/egu and return $e 0 0 0
55 0 ▶ croak( "SVG Parser: Illegal ampersand or entity \"$1\"" )
Branch: 1 branch
BranchTrueFalseTotal
if $e =~ /(&[^;]{0,10})/u 0 0 0
56 if $e =~ /(&[^;]{0,10})/;
57 0 $e;
58 0 };
59  
60 0 ▶ croak( "SVG Parser: No elements" ) if !defined($data) || $data !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data and $data =~ /\S/u 0 0 0
Condition0%0/3
defined $data and $data =~ /\S/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data and $data =~ /\S/u
defined $data $data =~ /\S/u
Truth table0%0/3
A and B
A= defined $dataB= $data =~ /\S/u
ABresult
0X0
100
111
61  
62 # Illegal low-ASCII chars.
63 0 ▶ croak( "SVG Parser: Not well-formed (illegal low-ASCII chars)" )
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ /[\x00-\x08\x0b\x0c\x0e-\x1f]/u 0 0 0
64 if $data =~ /[\x00-\x08\x0b\x0c\x0e-\x1f]/;
65  
66 # Turn CDATA into PCDATA.
67 0 $data =~ s{<!\[CDATA\[(.*?)]]>}{
68 0 $_ = $1.chr(0); # this makes sure that empty CDATAs become
69 0 s/([&<>'"])/ # the empty string and aren't just thrown away.
70 0 ▶ $1 eq '&' ? '&amp;' :
Branch: 4 branches
BranchTrueFalseTotal
$1 eq '&' ? : 0 0 0
$1 eq '<' ? : 0 0 0
$1 eq '"' ? : 0 0 0
$1 eq q['] ? : 0 0 0
71 $1 eq '<' ? '&lt;' :
72 $1 eq '"' ? '&quot;' :
73 $1 eq "'" ? '&apos;' :
74 '&gt;'
75 /eg;
76 0 $_;
77 }egs;
78  
79 croak( "SVG Parser: Not well-formed (CDATA not delimited or bad comment)" )
80 if $data =~ /]]>/ # ]]> not delimiting CDATA
81 || $data =~ /<!--(.*?)--->/s # ---> can't end a comment
82 0 ▶ || grep { $_ && /--/ }
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ /]]>/u or $data =~ /<!--(.*?)--->/su or grep {$_ and /--/u;} $data =~ /^\s+|<!--(.*?)-->|\s+$/gsu 0 0 0
Condition0%0/9
$data =~ /]]>/u or $data =~ /<!--(.*?)--->/su or grep {$_ and /--/u;} $data =~ /^\s+|<!--(.*?)-->|\s+$/gsu
l!l&&r!l&&!r
000
$data =~ /]]>/u or $data =~ /<!--(.*?)--->/su
l!l&&r!l&&!r
000
$_ && /--/u
!ll&&!rl&&r
000
MC/DC0%0/3
$data =~ /]]>/u or $data =~ /<!--(.*?)--->/su or grep {$_ and /--/u;} $data =~ /^\s+|<!--(.*?)-->|\s+$/gsu
$data =~ /]]>/u $data =~ /<!--(.*?)--->/su grep {$_ and /--/u;} $data =~ /^\s+|<!--(.*?)-->|\s+$/gsu
MC/DC0%0/2
$_ && /--/u
$_ /--/u
Truth table0%0/4
A or B or C
A= $data =~ /]]>/uB= $data =~ /<!--(.*?)--->/suC= grep {$_ and /--/u;} $data =~ /^\s+|<!--(.*?)-->|\s+$/gsu
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/3
A && B
A= $_B= /--/u
ABresult
0X0
100
111
83 ( $data =~ /^\s+|<!--(.*?)-->|\s+$/gs); # -- in comm
84  
85 # Strip leading/trailing whitespace and comments (which don't nest - phew!).
86 0 $data =~ s/^\s+|<!--(.*?)-->|\s+$//gs;
87
88 # Turn quoted > in attribs into &gt;.
89 # Double- and single-quoted attrib values get done seperately.
90 0 ▶ while ( $data =~ s/($re_name\s*=\s*"[^"]*)>([^"]*")/$1&gt;$2/gsi ) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ s/($re_name\s*=\s*"[^"]*)>([^"]*")/$1&gt;$2/gisu) 0 0 0
91 0 ▶ while ( $data =~ s/($re_name\s*=\s*'[^']*)>([^']*')/$1&gt;$2/gsi ) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ s/($re_name\s*=\s*'[^']*)>([^']*')/$1&gt;$2/gisu) 0 0 0
92  
93 0 ▶ if ( $params{fatal_declarations} && $data =~ /<!(ENTITY|DOCTYPE)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"fatal_declarations"} and $data =~ /<!(ENTITY|DOCTYPE)/u) 0 0 0
Condition0%0/3
$params{"fatal_declarations"} and $data =~ /<!(ENTITY|DOCTYPE)/u
!ll&&!rl&&r
000
MC/DC0%0/2
$params{"fatal_declarations"} and $data =~ /<!(ENTITY|DOCTYPE)/u
$params{"fatal_declarations"} $data =~ /<!(ENTITY|DOCTYPE)/u
Truth table0%0/3
A and B
A= $params{"fatal_declarations"}B= $data =~ /<!(ENTITY|DOCTYPE)/u
ABresult
0X0
100
111
94 0 croak( "SVG Parser: Unexpected \"$1\"" );
95 }
96  
97 # The abc2svg generator forgets the close the body. Fix it.
98 0 ▶ if ( $data =~ /\<meta\s+name="generator"\s+content="abc2svg/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /\<meta\s+name="generator"\s+content="abc2svg/u) 0 0 0
99 0 $data =~ s;</div>\s*</html>;</div></body></html>;;
100 0 $whitespace_tokens++;
101 }
102  
103 # Ignore empty tokens/whitespace tokens.
104 0 foreach my $token ( grep { length }
105 split( /(<[^>]+>)/, $data ) ) {
106 0 ▶ next if $token =~ /^\s+$/s && !$whitespace_tokens;
Branch: 1 branch
BranchTrueFalseTotal
if $token =~ /^\s+$/su and not $whitespace_tokens 0 0 0
Condition0%0/3
$token =~ /^\s+$/su and not $whitespace_tokens
!ll&&!rl&&r
000
MC/DC0%0/2
$token =~ /^\s+$/su and not $whitespace_tokens
$token =~ /^\s+$/su not $whitespace_tokens
Truth table0%0/3
A and not(B)
A= $token =~ /^\s+$/suB= not $whitespace_tokens
ABresult
0X0
100
111
107 0 ▶ next if $token =~ /<\?$re_name.*?\?>/is
Branch: 1 branch
BranchTrueFalseTotal
if $token =~ /<\?$re_name.*?\?>/isu or $token =~ /^<!(ENTITY|DOCTYPE)/iu 0 0 0
Condition0%0/3
$token =~ /<\?$re_name.*?\?>/isu or $token =~ /^<!(ENTITY|DOCTYPE)/iu
l!l&&r!l&&!r
000
MC/DC0%0/2
$token =~ /<\?$re_name.*?\?>/isu or $token =~ /^<!(ENTITY|DOCTYPE)/iu
$token =~ /<\?$re_name.*?\?>/isu $token =~ /^<!(ENTITY|DOCTYPE)/iu
Truth table0%0/3
A or B
A= $token =~ /<\?$re_name.*?\?>/isuB= $token =~ /^<!(ENTITY|DOCTYPE)/iu
ABresult
1X1
011
000
108 || $token =~ /^<!(ENTITY|DOCTYPE)/i;
109  
110 0 ▶ if ( $token =~ m!^</($re_name)\s*>!i ) { # close tag
Branch: 3 branches
BranchTrueFalseTotal
if ($token =~ m[^</($re_name)\s*>]iu) { } 0 0 0
elsif ($token =~ m[^<$re_name(\s[^>]*)*(\s*/)?>]isu) { } 0 0 0
elsif ($token =~ /^</u) { } 0 0 0
111 croak( "SVG Parser: Not well-formed (at \"$token\")" )
112 0 ▶ if $elem->{name} ne $1;
Branch: 1 branch
BranchTrueFalseTotal
if $elem->{"name"} ne $1 0 0 0
113 0 $elem = delete $elem->{parent};
114 }
115 elsif ( $token =~ /^<$re_name(\s[^>]*)*(\s*\/)?>/is ) { # open tag
116 0 my ( $tagname, $attribs_raw ) =
117 ( $token =~ m!<(\S*)(.*?)(\s*/)?>!s );
118 # First make attribs into a list so we can spot duplicate keys.
119 0 my $attrib = [
120 # Do double- and single- quoted attribs seperately.
121 $attribs_raw =~ /\s($re_name)\s*=\s*"([^"]*?)"/gi,
122 $attribs_raw =~ /\s($re_name)\s*=\s*'([^']*?)'/gi
123 ];
124 0 ▶ if ( @{$attrib} == 2 * keys %{{@{$attrib}}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$attrib;} == 2 * keys(%{+{@{$attrib;}};})) { } 0 0 0
125 0 $attrib = { @{$attrib} }
126 }
127 else {
128 0 croak( "SVG Parser: Not well-formed (duplicate attribute)" );
129 }
130  
131 # Now trash any attribs that we *did* manage to parse and see
132 # if there's anything left.
133 0 $attribs_raw =~ s/\s($re_name)\s*=\s*"([^"]*?)"//gi;
134 0 $attribs_raw =~ s/\s($re_name)\s*=\s*'([^']*?)'//gi;
135 croak( "SVG Parser: Not well-formed ($attribs_raw)" )
136 0 ▶ if $attribs_raw =~ /\S/ || grep { /</ } values %{$attrib};
Branch: 1 branch
BranchTrueFalseTotal
if $attribs_raw =~ /\S/u or grep {/</u;} values %{$attrib;} 0 0 0
Condition0%0/3
$attribs_raw =~ /\S/u or grep {/</u;} values %{$attrib;}
l!l&&r!l&&!r
000
MC/DC0%0/2
$attribs_raw =~ /\S/u or grep {/</u;} values %{$attrib;}
$attribs_raw =~ /\S/u grep {/</u;} values %{$attrib;}
Truth table0%0/3
A or B
A= $attribs_raw =~ /\S/uB= grep {/</u;} values %{$attrib;}
ABresult
1X1
011
000
137  
138 0 ▶ unless ( $params{no_entity_parsing} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"no_entity_parsing"}) 0 0 0
139 0 foreach my $key ( keys %{$attrib} ) {
140 0 ($attrib->{$key} = $fixent->($attrib->{$key})) =~
141 s/\x00//g; # get rid of CDATA marker
142 }
143 }
144 # We have an element. Push it.
145 0 $elem = { content => [],
146 name => $tagname,
147 type => 'e',
148 attrib => $attrib,
149 parent => $elem
150 };
151 0 push( @{ $elem->{parent}->{content} }, $elem );
152  
153 # Handle self-closing tags.
154 0 ▶ if ( $token =~ /\s*\/>$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ m[\s*/>$]u) 0 0 0
155 0 $elem->{name} =~ s/\/$//;
156 0 $elem = delete( $elem->{parent} );
157 }
158 }
159 elsif ( $token =~ /^</ ) { # some token taggish thing
160 0 croak( "SVG Parser: Unexpected \"$token\"" );
161 }
162 else { # ordinary content
163 0 $token =~ s/\x00//g; # get rid of our CDATA marker
164 0 ▶ unless ( $params{no_entity_parsing} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"no_entity_parsing"}) 0 0 0
165 0 $token = $fixent->($token);
166 }
167 0 push( @{$elem->{content}},
168 { content => $token, type => 't' } );
169 }
170 }
171 croak( "SVG Parser: Not well-formed (", $elem->{name}, " duplicated parent)" )
172 0 ▶ if exists($elem->{parent});
Branch: 1 branch
BranchTrueFalseTotal
if exists $elem->{"parent"} 0 0 0
173  
174 0 ▶ if ( $whitespace_tokens ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($whitespace_tokens) 0 0 0
175 0 ▶ while ( @{$elem->{content}} > 1
Branch: 1 branch
BranchTrueFalseTotal
if (@{$elem->{"content"};} > 1 and $elem->{"content"}[0]{"type"} eq "t" and not $elem->{"content"}[0]{"content"} =~ /\S/u) 0 0 0
Condition0%0/6
@{$elem->{"content"};} > 1 and $elem->{"content"}[0]{"type"} eq "t" and not $elem->{"content"}[0]{"content"} =~ /\S/u
!ll&&!rl&&r
000
@{$elem->{"content"};} > 1 and $elem->{"content"}[0]{"type"} eq "t"
!ll&&!rl&&r
000
MC/DC0%0/3
@{$elem->{"content"};} > 1 and $elem->{"content"}[0]{"type"} eq "t" and not $elem->{"content"}[0]{"content"} =~ /\S/u
@{$elem->{"content"};} > 1 $elem->{"content"}[0]{"type"} eq "t" not $elem->{"content"}[0]{"content"} =~ /\S/u
Truth table0%0/4
A and B and not(C)
A= @{$elem->{"content"};} > 1B= $elem->{"content"}[0]{"type"} eq "t"C= not $elem->{"content"}[0]{"content"} =~ /\S/u
ABCresult
0XX0
10X0
1100
1111
176 && $elem->{content}->[0]->{type} eq 't'
177 && $elem->{content}->[0]->{content} !~ /\S/
178 )
179 {
180 0 shift( @{$elem->{content}} );
181 }
182 0 ▶ while ( @{$elem->{content}} > 1
Branch: 1 branch
BranchTrueFalseTotal
if (@{$elem->{"content"};} > 1 and $elem->{"content"}[-1]{"type"} eq "t" and not $elem->{"content"}[-1]{"content"} =~ /\S/u) 0 0 0
Condition0%0/6
@{$elem->{"content"};} > 1 and $elem->{"content"}[-1]{"type"} eq "t" and not $elem->{"content"}[-1]{"content"} =~ /\S/u
!ll&&!rl&&r
000
@{$elem->{"content"};} > 1 and $elem->{"content"}[-1]{"type"} eq "t"
!ll&&!rl&&r
000
MC/DC0%0/3
@{$elem->{"content"};} > 1 and $elem->{"content"}[-1]{"type"} eq "t" and not $elem->{"content"}[-1]{"content"} =~ /\S/u
@{$elem->{"content"};} > 1 $elem->{"content"}[-1]{"type"} eq "t" not $elem->{"content"}[-1]{"content"} =~ /\S/u
Truth table0%0/4
A and B and not(C)
A= @{$elem->{"content"};} > 1B= $elem->{"content"}[-1]{"type"} eq "t"C= not $elem->{"content"}[-1]{"content"} =~ /\S/u
ABCresult
0XX0
10X0
1100
1111
183 && $elem->{content}->[-1]->{type} eq 't'
184 && $elem->{content}->[-1]->{content} !~ /\S/
185 )
186 {
187 0 pop( @{$elem->{content}} );
188 }
189 }
190 croak( "SVG Parser: No elements?" )
191 0 ▶ if @{$elem->{content}} == 0 || $elem->{content}->[0]->{type} ne 'e';
Branch: 1 branch
BranchTrueFalseTotal
if @{$elem->{"content"};} == 0 or $elem->{"content"}[0]{"type"} ne "e" 0 0 0
Condition0%0/3
@{$elem->{"content"};} == 0 or $elem->{"content"}[0]{"type"} ne "e"
l!l&&r!l&&!r
000
MC/DC0%0/2
@{$elem->{"content"};} == 0 or $elem->{"content"}[0]{"type"} ne "e"
@{$elem->{"content"};} == 0 $elem->{"content"}[0]{"type"} ne "e"
Truth table0%0/3
A or B
A= @{$elem->{"content"};} == 0B= $elem->{"content"}[0]{"type"} ne "e"
ABresult
1X1
011
000
192  
193 # If multiple, they must be the same elements.
194 0 my $e = $elem->{content}->[0]->{name};
195 0 for ( @{$elem->{content}} ) {
196 croak( "SVG Parser: Junk in or after end of document" )
197 0 ▶ unless $_->{name} eq $e;
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"name"} eq $e 0 0 0
198 }
199  
200 0 return $elem->{content};
201 12 }
202  
203 12 1;
« lib/ChordPro/lib/SVGPDF/Text.pm ↑ Summary lib/ChordPro/Output/ChordPro.pm »