lib/ChordPro/Output/HTML.pm

filter: Statementstmt 62.5% 194 / 310 Branchbran 41.4% 63 / 152 Conditioncond 40.0% 24 / 60 MC/DCmcdc 11.7% 4 / 34 Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 5 totaltotal 52.6% 307 / 583 CC 111 SCAR 62.6
CC
111
Cov
54%
generate_song
1084.4
render
69.3
generate_songbook
25.6
CRAP 525.5
« blib/lib/ChordPro/Delegate/Program.pm ↑ Summary lib/ChordPro/Output/PDF.pm »
1 #!/usr/bin/perl
2  
3 package ChordPro::Output::HTML;
4  
5 # Produce nice viewable HMTL output.
6 #
7 # You should be able to print it using a decent browser (notexisting)
8 # or a formatting tool like weasyprint.
9  
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use ChordPro::Files;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use ChordPro::Paths;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use ChordPro::Output::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use ChordPro::Utils qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use ChordPro::Assets;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶sub generate_songbook {
Subroutine generate_songbook: called CC=17 SCAR=32.4
Pod undocumented
20 3 my ( $self, $sb ) = @_;
21  
22 3 my @book;
23 3 ▶ my $cfg = $::config->{html} // {};
Condition50%1/2
$main::config->{"html"} // {}
l!l
30
MC/DC0%0/1
$main::config->{"html"} // {}
$main::config->{"html"}
Truth table50%1/2
A
A= $main::config->{"html"}
Aresult
11
00
24 3 ▶ $cfg->{styles}->{default} //= "chordpro.css";
Condition50%1/2
$cfg->{"styles"}{"default"} //= "chordpro.css"
l!l
30
MC/DC0%0/1
$cfg->{"styles"}{"default"} //= "chordpro.css"
$cfg->{"styles"}{"default"}
Truth table50%1/2
A
A= $cfg->{"styles"}{"default"}
Aresult
11
00
25 3 ▶ $cfg->{styles}->{screen} //= $cfg->{styles}->{display} // "";
Condition25%1/4
$cfg->{"styles"}{"screen"} //= $cfg->{"styles"}{"display"} // ""
l!l
30
$cfg->{"styles"}{"display"} // ""
l!l
00
MC/DC0%0/1
$cfg->{"styles"}{"screen"} //= $cfg->{"styles"}{"display"} // ""
$cfg->{"styles"}{"screen"}
Truth table50%1/2
A
A= $cfg->{"styles"}{"screen"}
Aresult
11
00
26 3 ▶ $cfg->{styles}->{print} //= "chordpro_print.css";
Condition50%1/2
$cfg->{"styles"}{"print"} //= "chordpro_print.css"
l!l
30
MC/DC0%0/1
$cfg->{"styles"}{"print"} //= "chordpro_print.css"
$cfg->{"styles"}{"print"}
Truth table50%1/2
A
A= $cfg->{"styles"}{"print"}
Aresult
11
00
27  
28 3 my %styles = %{ $cfg->{styles} };
29 3 my @styles = ( "default" );
30 3 for ( sort keys %styles ) {
31 15 ▶ next if /^(?:default|embed)$/ ;
Branch: 1 branch
BranchTrueFalseTotal
if /^(?:default|embed)$/ 6 9 15
32 9 ▶ next unless $styles{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $styles{$_} 6 3 9
33 3 push( @styles, $_ );
34 }
35  
36 3 push( @book,
37 '<html>',
38 '<head>',
39 '<meta charset="utf-8">' );
40  
41 3 ▶ if ( delete $styles{embed} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $styles{"embed"}) 0 3 3
42 2 ▶ use File::LoadLines;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 0 foreach my $class ( @styles ) {
44 0 my $style = CP->findres( $styles{$class}, class => "styles" );
45 0 ▶ next unless fs_test( 'rf', $style );
Branch: 1 branch
BranchTrueFalseTotal
unless fs_test("rf", $style) 0 0 0
46 0 my @lines = loadlines($style, { fail => 'hard' } );
47 0 ▶ push( @book, "<style type=\"text/css\" ".
Branch: 1 branch
BranchTrueFalseTotal
$class eq 'default' ? : 0 0 0
48 ($class eq "default" ? "" : "media=\"$class\"").">",
49 @lines, "</style>" );
50 0 delete $styles{$class};
51 }
52 }
53 3 foreach ( @styles ) {
54 6 ▶ next unless $styles{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $styles{$_} 0 6 6
55 push( @book,
56 6 ▶ '<link rel="stylesheet" href="'.$styles{$_}.'"'.
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^(default)$/ ? : 3 3 6
57 ( $_ =~ /^(default)$/ ? "" : qq{ media="$_"} ).
58 '>' );
59 }
60 3 push( @book, '</head>',
61 '<body>',
62 );
63  
64 3 foreach my $song ( @{$sb->{songs}} ) {
65 3 push( @book, @{ generate_song($song) } );
66 }
67  
68 3 push( @book, "</body>", "</html>" );
69 3 \@book;
70 }
71  
72 2 my $config;
73 2 my $single_space = 0; # suppress chords line when empty
74 2 my $lyrics_only = 0; # suppress all chords lines
75 2 my $layout;
76  
77 3 ▶sub generate_song {
Subroutine generate_song: called CC=62 SCAR=69.9
Pod undocumented
78 3 my ( $s ) = @_;
79  
80 3 my $tidy = $::options->{tidy};
81 3 $single_space = $::options->{'single-space'};
82 3 ▶ $config = dclone( $s->{config} // $::config );
Condition33%1/3
$s->{"config"} // $main::config
l!l&&r!l&&!r
300
MC/DC0%0/2
$s->{"config"} // $main::config
$s->{"config"} $main::config
Truth table33%1/3
A // B
A= $s->{"config"}B= $main::config
ABresult
1X1
011
000
83 3 $lyrics_only = $config->{settings}->{'lyrics-only'};
84 3 $s->structurize;
85 3 prepare_assets($s);
86 3 ▶ $s->dump(0) if $config->{debug}->{song};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"}{"song"} 0 3 3
87 3 $layout = Text::Layout::HTML->new;
88 3 ▶ while ( my($k,$v) = each( %{$config->{markup}->{shortcodes}}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$config{"markup"}{"shortcodes"};}) 0 3 3
89 0 ▶ unless ( $layout->can("register_shortcode") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($layout->can("register_shortcode")) 0 0 0
90 0 warn("Cannot register shortcodes, upgrade Text::Layout module\n");
91 0 last;
92 }
93 0 $layout->register_shortcode( $k, $v );
94 }
95  
96 3 my @s;
97  
98 3 ▶ for ( $s->{title} // "Untitled" ) {
Condition50%1/2
$s->{"title"} // "Untitled"
l!l
30
MC/DC0%0/1
$s->{"title"} // "Untitled"
$s->{"title"}
Truth table50%1/2
A
A= $s->{"title"}
Aresult
11
00
99 3 push( @s,
100 '<div class="song">',
101 '<style>',
102 '@page {',
103 ' @top-center {',
104 ' content: counter(song) ". ' . $_ . '";',
105 ' }',
106 '}',
107 '</style>',
108 '<div class="title">' . nhtml($_) . '</div>',
109 );
110  
111 }
112 3 ▶ if ( defined $s->{subtitle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->{"subtitle"}) 3 0 3
113 push( @s,
114 3 map { '<div class="subtitle">' . nhtml($_) . '</div>' }
115 3 @{$s->{subtitle}} );
116 }
117  
118 3 ▶ push(@s, "") if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 3 3
119  
120 3 my @elts = @{$s->{body}};
121 3 ▶ while ( @elts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@elts) 30 3 33
122 30 my $elt = shift(@elts);
123  
124 30 ▶ if ( $elt->{type} eq "empty" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "empty") 0 30 30
125 0 push(@s, "***SHOULD NOT HAPPEN***");
126 0 next;
127 }
128  
129 30 ▶ if ( $elt->{type} eq "colb" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "colb") 3 27 30
130 # push(@s, "{column_break}");
131 3 next;
132 }
133  
134 27 ▶ if ( $elt->{type} eq "newpage" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "newpage") 3 24 27
135 # push(@s, "{new_page}");
136 3 next;
137 }
138  
139 24 ▶ if ( $elt->{type} eq "songline" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "songline") 0 24 24
140 0 push(@s, songline( $s, $elt ));
141 0 next;
142 }
143  
144 24 ▶ if ( $elt->{type} eq "tab" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "tab") 0 24 24
145 0 my $p = '<div class="tab">';
146 0 foreach ( @{ $elt->{body} } ) {
147 0 ▶ next if $_->{type} eq "set";
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"type"} eq "set" 0 0 0
148 0 push( @s, $p . html($_->{text}) );
149 0 $p = "";
150 }
151 0 push( @s, $p . '</div>' );
152 0 ▶ push( @s, "") if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 0 0
153 0 next;
154 }
155  
156 24 ▶ if ( exists $elt->{body} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $elt->{"body"}) 12 12 24
157 12 push( @s, '<div class="' . $elt->{type} . '">' );
158 12 my @elts = @{$elt->{body}};
159 12 ▶ while ( @elts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@elts) 51 12 63
160 51 my $e = shift(@elts);
161 51 ▶ if ( $e->{type} eq "empty" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "empty") 0 51 51
162 0 push( @s, "<!-- ***SHOULD NOT HAPPEN*** -->" );
163 0 next;
164 }
165 51 ▶ if ( $e->{type} eq "songline" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "songline") 48 3 51
166 48 push( @s, songline( $s, $e ) );
167 48 next;
168 }
169 3 ▶ if ( $e->{type} =~ /^comment(_\w+)?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} =~ /^comment(_\w+)?$/) 0 3 3
170 push( @s,
171 '<div class="' . $e->{type} . '">' .
172 0 '<span>' . nhtml($e->{text}) . '</span></div>' );
173 0 next;
174 }
175 3 ▶ if ( $e->{type} eq "set" && $e->{name} eq "label" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "set" and $e->{"name"} eq "label") 0 3 3
Condition33%1/3
$e->{"type"} eq "set" and $e->{"name"} eq "label"
!ll&&!rl&&r
030
MC/DC0%0/2
$e->{"type"} eq "set" and $e->{"name"} eq "label"
$e->{"type"} eq "set" $e->{"name"} eq "label"
Truth table33%1/3
A and B
A= $e->{"type"} eq "set"B= $e->{"name"} eq "label"
ABresult
0X0
100
111
176 push( @s,
177 0 '<div class="label">' . nhtml($e->{value}) . '</div>'
178 );
179 0 next;
180 }
181 3 ▶ if ( $e->{type} eq "delegate"
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "delegate" and $e->{"subtype"} =~ /^image(?:-(\w+))?$/) 0 3 3
Condition33%1/3
$e->{"type"} eq "delegate" and $e->{"subtype"} =~ /^image(?:-(\w+))?$/
!ll&&!rl&&r
300
MC/DC0%0/2
$e->{"type"} eq "delegate" and $e->{"subtype"} =~ /^image(?:-(\w+))?$/
$e->{"type"} eq "delegate" $e->{"subtype"} =~ /^image(?:-(\w+))?$/
Truth table33%1/3
A and B
A= $e->{"type"} eq "delegate"B= $e->{"subtype"} =~ /^image(?:-(\w+))?$/
ABresult
0X0
100
111
182 && $e->{subtype} =~ /^image(?:-(\w+))?$/ ) {
183 0 ▶ my $delegate = $1 // $e->{delegate};
Condition0%0/3
$1 // $e->{"delegate"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$1 // $e->{"delegate"}
$1 $e->{"delegate"}
Truth table0%0/3
A // B
A= $1B= $e->{"delegate"}
ABresult
1X1
011
000
184 0 my $pkg = __PACKAGE__;
185 0 $pkg =~ s/::Output::\w+$/::Delegate::$delegate/;
186 0 ▶ eval "require $pkg" || die($@);
Branch: 1 branch
BranchTrueFalseTotal
eval "require $pkg" or die($@) 0 0 0
187 0 ▶ my $hd = $pkg->can($e->{handler}) //
Condition0%0/3
$pkg->can($e->{"handler"}) // die(qq[HTML: Missing delegate handler ${pkg}::$e->{"handler"}\n])
l!l&&r!l&&!r
000
MC/DC0%0/2
$pkg->can($e->{"handler"}) // die(qq[HTML: Missing delegate handler ${pkg}::$e->{"handler"}\n])
$pkg->can($e->{"handler"}) die(qq[HTML: Missing delegate handler ${pkg}::$e->{"handler"}\n])
Truth table0%0/3
A // B
A= $pkg->can($e->{"handler"})B= die(qq[HTML: Missing delegate handler ${pkg}::$e->{"handler"}\n])
ABresult
1X1
011
000
188 die("HTML: Missing delegate handler ${pkg}::$e->{handler}\n");
189 0 my $res = $hd->( $s, 0, $e );
190 0 ▶ next unless $res; # assume errors have been given
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
191 0 unshift( @elts, @$res );
192 0 next;
193 }
194 3 ▶ if ( $e->{type} eq "svg" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "svg") 0 3 3
195 0 push( @s, '<div class="' . $e->{type} . '">' );
196 0 push( @s, fs_load( $e->{uri} ) );
197 0 push( @s, "</div>" );
198 0 ▶ push( @s, "" ) if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 0 0
199 0 next;
200 }
201 3 ▶ if ( $e->{type} eq "image" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "image") 0 3 3
202 2 ▶ use ChordPro::Output::Common qw(mimedata);
Subroutine BEGIN: called CC=1 SCAR=0.0
203 0 my @args;
204 0 ▶ while ( my($k,$v) = each( %{ $e->{opts} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$e{"opts"};}) 0 0 0
205 0 push( @args, "$k=\"$v\"" );
206 }
207  
208 0 my $asset = $s->{assets}->{$e->{id}};
209 0 ▶ $e->{uri} //= $asset->{uri};
Condition0%0/3
$e->{"uri"} //= $asset->{"uri"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$e->{"uri"} //= $asset->{"uri"}
$e->{"uri"} $asset->{"uri"}
Truth table0%0/3
A //= B
A= $e->{"uri"}B= $asset->{"uri"}
ABresult
1X1
011
000
210  
211 0 my @images;
212 0 ▶ if ( $s->{assets}->{$e->{id}}->{data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{'assets'}{$e->{'id'}}{'data'}) { } 0 0 0
213 # mimedata will return one or more image sources.
214 0 @images = mimedata( \join("\n",@{$asset->{data}}) );
215 }
216 else {
217 # Presumably a single image source.
218 0 @images = mimedata($e->{uri});
219 }
220 0 for ( @images ) {
221 push( @s,
222 0 '<div class="' . $e->{type} . '">' .
223 qq{<img src="$_" } .
224 "@args" . "/>" .
225 '</div>' );
226 0 ▶ push( @s, "" ) if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 0 0
227 }
228 0 next;
229 }
230  
231 3 ▶ if ( $e->{type} eq "html" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "html") 0 3 3
232 0 my @args;
233 0 ▶ while ( my($k,$v) = each( %{ $e->{opts} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$e{"opts"};}) 0 0 0
234 0 push( @args, "$k=\"$v\"" );
235 }
236 2 ▶ use ChordPro::Delegate::TextBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
237 0 my $pkg = 'ChordPro::Delegate::'. $e->{delegate};
238 0 my $hnd = $e->{handler};
239 0 my $res = $pkg->can($hnd)->( $s, elt => $e );
240 push( @s, '<div class="' . lc($hnd) . '">' . $_ . '</div>' )
241 0 for @{$res->{data}};
242 0 ▶ push( @s, "" ) if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 0 0
243 0 next;
244 }
245 }
246 12 push( @s, '</div>' );
247 12 ▶ push( @s, "" ) if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 12 12
248 12 next;
249 }
250  
251 12 ▶ if ( $elt->{type} eq "comment" || $elt->{type} eq "comment_italic" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "comment" or $elt->{"type"} eq "comment_italic") 12 0 12
Condition66%2/3
$elt->{"type"} eq "comment" or $elt->{"type"} eq "comment_italic"
l!l&&r!l&&!r
930
MC/DC0%0/2
$elt->{"type"} eq "comment" or $elt->{"type"} eq "comment_italic"
$elt->{"type"} eq "comment" $elt->{"type"} eq "comment_italic"
Truth table66%2/3
A or B
A= $elt->{"type"} eq "comment"B= $elt->{"type"} eq "comment_italic"
ABresult
1X1
011
000
252 12 ▶ if ( $elt->{chords} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{'chords'}) { } 0 12 12
253 0 my $t = "";
254 0 ▶ for ( my $i=0; $i < @{$elt->{chords}}; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$elt->{"chords"};}) 0 0 0
255 $t .= $s->{chordsinfo}->{$elt->{chords}->[$i]->key}->name
256 0 ▶ if $elt->{chords}->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $elt->{"chords"}[$i] 0 0 0
257 0 $t .= $elt->{phrases}->[$i];
258 }
259 0 push( @s, '<div class="' . $elt->{type} . '"><span>' .
260 nhtml($t) . '</span></div>' );
261  
262 }
263 else {
264 push( @s,
265 '<div class="' . $elt->{type} . '">' .
266 12 '<span>' . nhtml(fmt_subst($s,$elt->{text})) . '</span></div>' );
267 }
268 12 ▶ push( @s, "" ) if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 12 12
269 12 next;
270 }
271  
272 0 ▶ if ( $elt->{type} eq "image" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "image") 0 0 0
273 2 ▶ use ChordPro::Output::Common qw(mimedata);
Subroutine BEGIN: called CC=1 SCAR=0.0
274 0 my @args;
275 0 ▶ while ( my($k,$v) = each( %{ $elt->{opts} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$elt{"opts"};}) 0 0 0
276 0 push( @args, "$k=\"$v\"" );
277 }
278  
279 0 my $asset = $s->{assets}->{$elt->{id}};
280 0 ▶ $elt->{uri} //= $asset->{uri};
Condition0%0/3
$elt->{"uri"} //= $asset->{"uri"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$elt->{"uri"} //= $asset->{"uri"}
$elt->{"uri"} $asset->{"uri"}
Truth table0%0/3
A //= B
A= $elt->{"uri"}B= $asset->{"uri"}
ABresult
1X1
011
000
281  
282 0 my @images;
283 0 ▶ if ( $s->{assets}->{$elt->{id}}->{data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{'assets'}{$elt->{'id'}}{'data'}) { } 0 0 0
284 0 @images = mimedata( \$asset->{data} );
285 }
286 else {
287 0 @images = mimedata($elt->{uri});
288 }
289 0 for ( @images ) {
290 push( @s,
291 0 '<div class="' . $elt->{type} . '">' .
292 qq{<img src="$_" } .
293 "@args" . "/>" .
294 '</div>' );
295 0 ▶ push( @s, "" ) if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 0 0
296 }
297 0 next;
298 }
299  
300 0 ▶ if ( $elt->{type} eq "html" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "html") 0 0 0
301 0 my @args;
302 0 ▶ while ( my($k,$v) = each( %{ $elt->{opts} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$elt{"opts"};}) 0 0 0
303 0 push( @args, "$k=\"$v\"" );
304 }
305  
306 0 my $pkg = 'ChordPro::Delegate::'. $elt->{delegate};
307 0 my $hnd = $elt->{handler};
308 0 my $html = $pkg->can($hnd)->( $s, elt => $elt );
309 0 push( @s, '<div class="' . lc($hnd) . '">' . $html . '</div>' );
310 0 ▶ push( @s, "" ) if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 0 0
311 0 next;
312 }
313  
314 0 ▶ if ( $elt->{type} eq "control" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "control") 0 0 0
315 0 ▶ if ( $elt->{name} eq "lyrics-only" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"name"} eq "lyrics-only") 0 0 0
316 $lyrics_only = $elt->{value}
317 0 ▶ unless $lyrics_only > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $lyrics_only > 1 0 0 0
318 }
319 }
320 }
321  
322 3 push( @s, '</div>' ); # song
323 3 \@s;
324 }
325  
326 48 ▶sub songline {
Subroutine songline: called CC=12 SCAR=25.0
Pod undocumented
327 48 my ( $song, $elt ) = @_;
328  
329 48 my $t_line = "";
330  
331 48 ▶ $elt->{chords} //= [ '' ];
Condition100%2/2
$elt->{"chords"} //= [""]
l!l
2424
MC/DC100%1/1
$elt->{"chords"} //= [""]
$elt->{"chords"}
Truth table100%2/2
A
A= $elt->{"chords"}
Aresult
11
00
332 my @c = map {
333 111 ▶ $_ eq "" ? "" : $song->{chordsinfo}->{$_->key }->name
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '' ? : 48 63 111
334 48 } @{ $elt->{chords} };
335  
336 48 ▶ if ( $lyrics_only
Branch: 1 branch
BranchTrueFalseTotal
if ($lyrics_only or $single_space and not $elt->{"chords"} && join("", @c) =~ /\S/) 24 24 48
Condition88%8/9
$lyrics_only or $single_space and not $elt->{"chords"} && join("", @c) =~ /\S/
l!l&&r!l&&!r
16824
$single_space and not $elt->{"chords"} && join("", @c) =~ /\S/
!ll&&!rl&&r
1688
$elt->{'chords'} && join('', @c) =~ /\S/
!ll&&!rl&&r
088
MC/DC75%3/4
$lyrics_only or $single_space and not $elt->{"chords"} && join("", @c) =~ /\S/
$lyrics_only $single_space $elt->{'chords'} join('', @c) =~ /\S/
Truth table80%4/5
A or B and not(C && D)
A= $lyrics_onlyB= $single_spaceC= $elt->{'chords'}D= join('', @c) =~ /\S/
ABCDresult
1XXX1
010X1
01101
00XX0
01110
337 or
338 $single_space && ! ( $elt->{chords} && join( "", @c ) =~ /\S/ )
339 ) {
340 24 $t_line = join( "", @{ $elt->{phrases} } );
341 24 $t_line =~ s/\s+$//;
342 24 return ( '<table class="songline">',
343 ' <tr class="lyrics">',
344 ' <td>' . nhtml($t_line) . '</td>',
345 ' </tr>',
346 '</table>' );
347 }
348  
349 24 my $cr = ""; # chords row
350 24 for ( @{$elt->{chords}} ) {
351 66 ▶ if ( $_ eq "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "") 24 42 66
352 24 $cr .= "<td></td>";
353 24 next;
354 }
355 42 my $c = $song->{chordsinfo}->{$_->key};
356 42 ▶ if ( $c->isa("ChordPro::Chord::Annotation") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->isa('ChordPro::Chord::Annotation')) { } 0 42 42
357 0 $cr .= "<td class=\"annotation\">" . $c->{text} . "</td>";
358 }
359 else {
360 42 $cr .= "<td>" . $c->name . "</td>";
361 }
362 }
363  
364 my $pr = join( '', # phrases row
365 66 ▶ map { ( $_ =~ s/^\s+// ? '<td class="indent">' : '<td>' ) . nhtml($_) . '</td>' }
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ s/^\s+// ? : 12 54 66
366 24 ( @{ $elt->{phrases} } ) );
367  
368 24 ▶ if ( $config->{settings}->{'chords-under'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"settings"}{"chords-under"}) 0 24 24
369 0 return ( '<table class="songline">',
370 ' <tr class="lyrics">', ' ' . $pr, ' </tr>',
371 ' <tr class="chords">', ' ' . $cr, ' </tr>',
372 '</table>' );
373 }
374 24 return ( '<table class="songline">',
375 ' <tr class="chords">', ' ' . $cr, ' </tr>',
376 ' <tr class="lyrics">', ' ' . $pr, ' </tr>',
377 '</table>' );
378 }
379  
380 =for later
381  
382 sub gridline {
383 }
384  
385 <style>
386 div.grid_2_4x4_1 {
387 display: grid;
388 grid-template-columns: 5.263% 5.263% 5.263% 5.263% 5.263% 5.263% 5.263% 5.263% 5.263% 5.263% 5.263% 5.263% 5.263% 5.263% 5.263% 5.263% 5.263% 5.263% 5.263%;
389 }
390 div.grid_2_4x4_1 div { padding: 4px }
391 div.grid_2_4x4_1 div.A { grid-column: span 2 }
392 div.grid_2_4x4_1 div.L { border-left: 1px solid black }
393 div.grid_2_4x4_1 div.M { border-left: 1px solid #e0e0e0 }
394 div.grid_2_4x4_1 div.R { border-right: 1px solid black }
395 div.grid_2_4x4_1 div.Z { grid-column: span 1 }
396 </style>
397  
398 <div class="grid_2_4x4_1">
399  
400 <div class="A">intro</div>
401 <div class="L">a1</div>
402 <div class="M">a2</div>
403 <div class="M">a3</div>
404 <div class="M">a4</div>
405 <div class="L">b1</div>
406 <div class="M">b2</div>
407 <div class="M">b3</div>
408 <div class="M">b4</div>
409 <div class="L">c1</div>
410 <div class="M">c2</div>
411 <div class="M">c3</div>
412 <div class="M">c4</div>
413 <div class="L">d1</div>
414 <div class="M">d2</div>
415 <div class="M">d3</div>
416 <div class="M R">d4 d4</div>
417 <div class="Z"></div>
418  
419 <div class="A"></div>
420 <div class="L">a1</div>
421 <div class="M">a2</div>
422 <div class="M">a3</div>
423 <div class="M">a4</div>
424 <div class="L">b1</div>
425 <div class="M">b2</div>
426 <div class="M">b3</div>
427 <div class="M">b4</div>
428 <div class="L">c1</div>
429 <div class="M">c2</div>
430 <div class="M">c3</div>
431 <div class="M">c4</div>
432 <div class="L">d1</div>
433 <div class="M">d2</div>
434 <div class="M">d3</div>
435 <div class="M R">d4</div>
436 <div class="Z">2x</div>
437  
438 </div>
439  
440 =cut
441  
442 108 ▶sub nhtml {
Subroutine nhtml: called CC=2 SCAR=7.1
Pod undocumented
443 108 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 108 108
444 108 $layout->set_markup(shift);
445 108 $layout->render;
446 }
447  
448 102 ▶sub html {
Subroutine html: called CC=1 SCAR=0.0
Pod undocumented
449 102 my $t = shift;
450 102 $t =~ s/&/&amp;/g;
451 102 $t =~ s/</&lt;/g;
452 102 $t =~ s/>/&gt;/g;
453 102 $t;
454 }
455  
456 # Temporary. Eventually we'll have a decent HTML backend for Text::Layout.
457  
458 package Text::Layout::HTML;
459  
460 2 ▶use parent 'Text::Layout';
Subroutine BEGIN: called CC=1 SCAR=0.0
461  
462 2 ▶use ChordPro::Utils qw(fq);
Subroutine BEGIN: called CC=1 SCAR=0.0
463  
464 # Eliminate warning when HTML backend is loaded together with Text backend.
465 2 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
466  
467 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
468 3 my ( $pkg, @data ) = @_;
469 3 my $self = $pkg->SUPER::new;
470 3 $self->{_currentfont} = { family => 'default',
471 style => 'normal',
472 weight => 'normal' };
473 3 $self->{_currentcolor} = 'black';
474 3 $self->{_currentsize} = 12;
475 3 $self;
476 }
477  
478 2 *html = \&ChordPro::Output::HTML::html;
479  
480 108 ▶sub render {
Subroutine render: called CC=22 SCAR=42.4
481 108 my ( $self ) = @_;
482 108 my $res = "";
483 108 foreach my $fragment ( @{ $self->{_content} } ) {
484 102 ▶ if ( $fragment->{type} eq 'strut' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment->{"type"} eq "strut") 0 102 102
485 0 ▶ next unless length($fragment->{label}//"");
Branch: 1 branch
BranchTrueFalseTotal
unless length($fragment->{"label"} // "") 0 0 0
Condition0%0/2
$fragment->{"label"} // ""
l!l
00
MC/DC0%0/1
$fragment->{"label"} // ""
$fragment->{"label"}
Truth table0%0/2
A
A= $fragment->{"label"}
Aresult
11
00
486 0 $res .= "<span id=\"".$fragment->{label}."\"/>";
487 0 next;
488 }
489 102 ▶ next unless length($fragment->{text});
Branch: 1 branch
BranchTrueFalseTotal
unless length $fragment->{"text"} 0 102 102
490 102 ▶ my $f = $fragment->{font} || $self->{_currentfont};
Condition33%1/3
$fragment->{'font'} || $self->{'_currentfont'}
l!l&&r!l&&!r
10200
MC/DC0%0/2
$fragment->{'font'} || $self->{'_currentfont'}
$fragment->{'font'} $self->{'_currentfont'}
Truth table33%1/3
A || B
A= $fragment->{'font'}B= $self->{'_currentfont'}
ABresult
1X1
011
000
491 102 my @c; # styles
492 my @d; # decorations
493 102 ▶ if ( $f->{style} eq "italic" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->{"style"} eq "italic") 0 102 102
494 0 push( @c, q{font-style:italic} );
495 }
496 102 ▶ if ( $f->{weight} eq "bold" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->{"weight"} eq "bold") 0 102 102
497 0 push( @c, q{font-weight:bold} );
498 }
499 102 ▶ if ( $fragment->{color} && $fragment->{color} ne $self->{_currentcolor} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment->{"color"} and $fragment->{"color"} ne $self->{"_currentcolor"}) 0 102 102
Condition33%1/3
$fragment->{"color"} and $fragment->{"color"} ne $self->{"_currentcolor"}
!ll&&!rl&&r
01020
MC/DC0%0/2
$fragment->{"color"} and $fragment->{"color"} ne $self->{"_currentcolor"}
$fragment->{"color"} $fragment->{"color"} ne $self->{"_currentcolor"}
Truth table33%1/3
A and B
A= $fragment->{"color"}B= $fragment->{"color"} ne $self->{"_currentcolor"}
ABresult
0X0
100
111
500 0 push( @c, join(":","color",$fragment->{color}) );
501 }
502 102 ▶ if ( $fragment->{size} && $fragment->{size} ne $self->{_currentsize} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment->{"size"} and $fragment->{"size"} ne $self->{"_currentsize"}) 0 102 102
Condition33%1/3
$fragment->{"size"} and $fragment->{"size"} ne $self->{"_currentsize"}
!ll&&!rl&&r
01020
MC/DC0%0/2
$fragment->{"size"} and $fragment->{"size"} ne $self->{"_currentsize"}
$fragment->{"size"} $fragment->{"size"} ne $self->{"_currentsize"}
Truth table33%1/3
A and B
A= $fragment->{"size"}B= $fragment->{"size"} ne $self->{"_currentsize"}
ABresult
0X0
100
111
503 0 push( @c, join(":","font-size",$fragment->{size}) );
504 }
505 102 ▶ if ( $fragment->{bgcolor} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment->{"bgcolor"}) 0 102 102
506 0 push( @c, join(":","background-color",$fragment->{bgcolor}) );
507 }
508 102 ▶ if ( $fragment->{underline} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment->{"underline"}) 0 102 102
509 0 push( @d, q{underline} );
510 }
511 102 ▶ if ( $fragment->{strikethrough} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragment->{"strikethrough"}) 0 102 102
512 0 push( @d, q{line-through} );
513 }
514 102 ▶ push( @c, "text-decoration-line:@d" ) if @d;
Branch: 1 branch
BranchTrueFalseTotal
if @d 0 102 102
515 102 ▶ my $href = $fragment->{href} // "";
Condition50%1/2
$fragment->{"href"} // ""
l!l
0102
MC/DC0%0/1
$fragment->{"href"} // ""
$fragment->{"href"}
Truth table50%1/2
A
A= $fragment->{"href"}
Aresult
11
00
516 102 ▶ $res .= "<a href=\"".html($href)."\">" if length($href);
Branch: 1 branch
BranchTrueFalseTotal
if length $href 0 102 102
517 102 ▶ $res .= "<span$href style=\"" . join(";",@c) . "\">" if @c;
Branch: 1 branch
BranchTrueFalseTotal
if @c 0 102 102
518 102 $res .= html(fq($fragment->{text}));
519 102 ▶ $res .= "</span>" if @c;
Branch: 1 branch
BranchTrueFalseTotal
if @c 0 102 102
520 102 ▶ $res .= "</a>" if length($href);
Branch: 1 branch
BranchTrueFalseTotal
if length $href 0 102 102
521 }
522 108 $res;
523 }
524  
525 2 1;
« blib/lib/ChordPro/Delegate/Program.pm ↑ Summary lib/ChordPro/Output/PDF.pm »