lib/ChordPro/Output/Text.pm

filter: Statementstmt 70.7% 150 / 212 Branchbran 52.3% 66 / 126 Conditioncond 52.6% 10 / 19 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 5 totaltotal 62.8% 242 / 385 CC 81 SCAR 55.0
CC
81
Cov
63%
generate_song
392.8
songline
23.1
generate_songbook
4.6
CRAP 245.7
« lib/ChordPro/lib/SVGPDF/Element.pm ↑ Summary lib/ChordPro/lib/JSON/Relaxed/Parser.pm »
1 #!/usr/bin/perl
2  
3 package main;
4  
5 5 our $options;
6 5 our $config;
7  
8 package ChordPro::Output::Text;
9  
10 5 ▶use ChordPro::Output::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 21 ▶sub generate_songbook {
Subroutine generate_songbook: called CC=4 SCAR=15.2
Pod undocumented
16 21 my ( $self, $sb ) = @_;
17 21 my @book;
18  
19 21 foreach my $song ( @{$sb->{songs}} ) {
20 21 ▶ if ( @book ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@book) 0 21 21
21 0 ▶ push(@book, "") if $options->{'backend-option'}->{tidy};
Branch: 1 branch
BranchTrueFalseTotal
if $main::options->{"backend-option"}{"tidy"} 0 0 0
22 0 push(@book, "-- New song");
23 }
24 21 push(@book, @{generate_song($song)});
25 }
26  
27 21 push( @book, "");
28 21 \@book;
29 }
30  
31 5 my $single_space = 0; # suppress chords line when empty
32 5 my $lyrics_only = 0; # suppress all chords lines
33 5 my $chords_under = 0; # chords under lyrics
34 5 my $layout = Text::Layout::Text->new;
35 5 my $rechorus;
36  
37 24 ▶sub upd_config {
Subroutine upd_config: called CC=1 SCAR=0.0
Pod undocumented
38 24 $lyrics_only = $config->{settings}->{'lyrics-only'};
39 24 $chords_under = $config->{settings}->{'chords-under'};
40 24 $rechorus = $config->{text}->{chorus}->{recall};
41 }
42  
43 21 ▶sub generate_song {
Subroutine generate_song: called CC=57 SCAR=59.7
Pod undocumented
44 21 my ( $s ) = @_;
45 21 $config = $s->{config};
46  
47 21 my $tidy = $options->{'backend-option'}->{tidy};
48 21 $single_space = $options->{'single-space'};
49 21 upd_config();
50  
51 $s->structurize
52 21 ▶ if ( $options->{'backend-option'}->{structure} // '' ) eq 'structured';
Branch: 1 branch
BranchTrueFalseTotal
if ($main::options->{"backend-option"}{"structure"} // "") eq "structured" 0 21 21
Condition50%1/2
$main::options->{"backend-option"}{"structure"} // ""
l!l
021
MC/DC0%0/1
$main::options->{"backend-option"}{"structure"} // ""
$main::options->{"backend-option"}{"structure"}
Truth table50%1/2
A
A= $main::options->{"backend-option"}{"structure"}
Aresult
11
00
53  
54 21 my @s;
55  
56 push(@s, "-- Title: " . $s->{title})
57 21 ▶ if defined $s->{title};
Branch: 1 branch
BranchTrueFalseTotal
if defined $s->{"title"} 21 0 21
58 21 ▶ if ( defined $s->{subtitle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->{"subtitle"}) 6 15 21
59 6 push(@s, map { +"-- Subtitle: $_" } @{$s->{subtitle}});
60 }
61  
62 21 ▶ push(@s, "") if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 21 21
63  
64 21 my $ctx = "";
65 21 my @elts = @{$s->{body}};
66 21 ▶ while ( @elts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@elts) 321 21 342
67 321 my $elt = shift(@elts);
68  
69 321 ▶ if ( $elt->{context} ne $ctx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"context"} ne $ctx) 54 267 321
70 54 ▶ push(@s, "-- End of $ctx") if $ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx 24 30 54
71 54 ▶ push(@s, "-- Start of $ctx") if $ctx = $elt->{context};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx = $elt->{"context"} 30 24 54
72 }
73  
74 321 ▶ if ( $elt->{type} eq "empty" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "empty") 81 240 321
75 push(@s, "***SHOULD NOT HAPPEN***")
76 81 ▶ if $s->{structure} eq 'structured';
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"structure"} eq "structured" 0 81 81
77 81 push(@s, "");
78 81 next;
79 }
80  
81 240 ▶ if ( $elt->{type} eq "colb" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "colb") 3 237 240
82 3 push(@s, "-- Column break");
83 3 next;
84 }
85  
86 237 ▶ if ( $elt->{type} eq "newpage" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "newpage") 3 234 237
87 3 push(@s, "-- New page");
88 3 next;
89 }
90  
91 234 ▶ if ( $elt->{type} eq "songline" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "songline") 105 129 234
92 105 push(@s, songline( $s, $elt ));
93 105 next;
94 }
95  
96 129 ▶ if ( $elt->{type} eq "tabline" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "tabline") 24 105 129
97 24 push(@s, $elt->{text});
98 24 next;
99 }
100  
101 105 ▶ if ( $elt->{type} eq "chorus" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "chorus") 0 105 105
102 0 ▶ push(@s, "") if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 0 0
103 0 push(@s, "-- Start of chorus*");
104 0 foreach my $e ( @{$elt->{body}} ) {
105 0 ▶ if ( $e->{type} eq "empty" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "empty") 0 0 0
106 0 push(@s, "");
107 0 next;
108 }
109 0 ▶ if ( $e->{type} eq "songline" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "songline") 0 0 0
110 0 push(@s, songline( $s, $e ));
111 0 next;
112 }
113 }
114 0 push(@s, "-- End of chorus*");
115 0 ▶ push(@s, "") if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 0 0
116 0 next;
117 }
118  
119 105 ▶ if ( $elt->{type} eq "rechorus" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "rechorus") 15 90 105
120 15 ▶ if ( $rechorus->{quote} && $elt->{chorus} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rechorus->{'quote'} and $elt->{'chorus'}) { } 15 0 15
elsif ($rechorus->{'type'} and $rechorus->{'tag'}) { } 0 0 0
Condition33%1/3
$rechorus->{"quote"} and $elt->{"chorus"}
!ll&&!rl&&r
0015
MC/DC0%0/2
$rechorus->{"quote"} and $elt->{"chorus"}
$rechorus->{"quote"} $elt->{"chorus"}
Truth table33%1/3
A and B
A= $rechorus->{"quote"}B= $elt->{"chorus"}
ABresult
0X0
100
111
121 15 unshift( @elts, @{ $elt->{chorus} } );
122 }
123 ▶ elsif ( $rechorus->{type} && $rechorus->{tag} ) {
Condition0%0/3
$rechorus->{"type"} and $rechorus->{"tag"}
!ll&&!rl&&r
000
MC/DC0%0/2
$rechorus->{"type"} and $rechorus->{"tag"}
$rechorus->{"type"} $rechorus->{"tag"}
Truth table0%0/3
A and B
A= $rechorus->{"type"}B= $rechorus->{"tag"}
ABresult
0X0
100
111
124 0 push( @s, "{".$rechorus->{type}.": ".$rechorus->{tag}."}" );
125 }
126 else {
127 0 push( @s, "{chorus}" );
128 }
129 15 next;
130 }
131  
132 90 ▶ if ( $elt->{type} eq "tab" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "tab") 0 90 90
133 0 ▶ push(@s, "") if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 0 0
134 0 push(@s, "-- Start of tab");
135 0 push(@s, map { $_->{text} } @{$elt->{body}} );
136 0 push(@s, "-- End of tab");
137 0 ▶ push(@s, "") if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 0 0
138 0 next;
139 }
140  
141 90 ▶ if ( $elt->{type} eq "verse" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "verse") 0 90 90
142 0 ▶ push(@s, "") if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 0 0
143 0 push(@s, "-- Start of verse");
144 0 foreach my $e ( @{$elt->{body}} ) {
145 0 ▶ if ( $e->{type} eq "empty" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "empty") 0 0 0
146 push(@s, "***SHOULD NOT HAPPEN***")
147 0 ▶ if $s->{structure} eq 'structured';
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"structure"} eq "structured" 0 0 0
148 0 next;
149 }
150 0 ▶ if ( $e->{type} eq "songline" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "songline") 0 0 0
151 0 push(@s, songline( $s, $e ));
152 0 next;
153 }
154 0 ▶ if ( $e->{type} eq "comment" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "comment") 0 0 0
155 0 push(@s, "-c- " . $e->{text});
156 0 next;
157 }
158 0 ▶ if ( $e->{type} eq "comment_italic" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "comment_italic") 0 0 0
159 0 push(@s, "-i- " . $e->{text});
160 0 next;
161 }
162 }
163 0 push(@s, "-- End of verse");
164 0 ▶ push(@s, "") if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 0 0
165 0 next;
166 }
167  
168 90 ▶ if ( $elt->{type} =~ /^comment(?:_italic|_box)?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} =~ /^comment(?:_italic|_box)?$/) 42 48 90
169 42 ▶ push(@s, "") if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 42 42
170 42 my $text = $elt->{text};
171 42 ▶ if ( $elt->{chords} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"chords"}) 12 30 42
172 12 $text = "";
173 12 for ( 0..$#{ $elt->{chords} } ) {
174 $text .= "[" . $elt->{chords}->[$_]->key . "]"
175 24 ▶ if $elt->{chords}->[$_] ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $elt->{"chords"}[$_] ne "" 12 12 24
176 24 $text .= $elt->{phrases}->[$_];
177 }
178 }
179 # $text = fmt_subst( $s, $text );
180 42 push(@s, "-- $text");
181 42 ▶ push(@s, "") if $tidy;
Branch: 1 branch
BranchTrueFalseTotal
if $tidy 0 42 42
182 42 next;
183 }
184  
185 48 ▶ if ( $elt->{type} eq "image" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "image") 0 48 48
186 0 ▶ my @args = ( "image:", $elt->{uri} // "<none>" );
Condition0%0/2
$elt->{"uri"} // "<none>"
l!l
00
MC/DC0%0/1
$elt->{"uri"} // "<none>"
$elt->{"uri"}
Truth table0%0/2
A
A= $elt->{"uri"}
Aresult
11
00
187 0 ▶ while ( my($k,$v) = each( %{ $elt->{opts} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$elt{"opts"};}) 0 0 0
188 0 push( @args, "$k=$v" );
189 }
190 0 foreach ( @args ) {
191 0 ▶ next unless /\s/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\s/ 0 0 0
192 0 $_ = '"' . $_ . '"';
193 }
194 0 push( @s, "+ @args" );
195 0 next;
196 }
197  
198 48 ▶ if ( $elt->{type} eq "set" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "set") 33 15 48
199 33 ▶ if ( $elt->{name} eq "lyrics-only" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elt->{'name'} eq 'lyrics-only') { } 0 33 33
elsif ($elt->{'name'} =~ /^(text\..+)/) { } 3 30 33
200 $lyrics_only = $elt->{value}
201 0 ▶ unless $lyrics_only > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $lyrics_only > 1 0 0 0
202 }
203 # Arbitrary config values.
204 elsif ( $elt->{name} =~ /^(text\..+)/ ) {
205 3 my @k = split( /[.]/, $1 );
206 3 my $cc = {};
207 3 my $c = \$cc;
208 3 foreach ( @k ) {
209 12 $c = \($$c->{$_});
210 }
211 3 $$c = $elt->{value};
212 3 $config->augment($cc);
213 3 upd_config();
214 }
215 33 next;
216 }
217  
218 15 ▶ if ( $elt->{type} eq "control" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "control") 0 15 15
219 }
220 }
221 21 ▶ push(@s, "-- End of $ctx") if $ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx 6 15 21
222  
223 21 \@s;
224 }
225  
226 105 ▶sub songline {
Subroutine songline: called CC=18 SCAR=31.4
Pod undocumented
227 105 my ( $song, $elt ) = @_;
228  
229 105 my $t_line = "";
230 330 my @phrases = map { $layout->set_markup($_); $layout->render }
231 105 @{ $elt->{phrases} };
232  
233 105 ▶ if ( $lyrics_only
Branch: 1 branch
BranchTrueFalseTotal
if ($lyrics_only or $single_space and not $elt->{"chords"} && join("", map({$_ ? $_->key : "";} @{$$elt{"chords"};})) =~ /\S/) 43 62 105
Condition88%8/9
$lyrics_only or $single_space and not $elt->{"chords"} && join("", map({$_ ? $_->key : "";} @{$$elt{"chords"};})) =~ /\S/
l!l&&r!l&&!r
35862
$single_space and not $elt->{"chords"} && join("", map({$_ ? $_->key : "";} @{$$elt{"chords"};})) =~ /\S/
!ll&&!rl&&r
35278
$elt->{'chords'} && join('', map({$_ ? $_->key : '';} @{$$elt{'chords'};})) =~ /\S/
!ll&&!rl&&r
8027
MC/DC75%3/4
$lyrics_only or $single_space and not $elt->{"chords"} && join("", map({$_ ? $_->key : "";} @{$$elt{"chords"};})) =~ /\S/
$lyrics_only $single_space $elt->{'chords'} join('', map({$_ ? $_->key : '';} @{$$elt{'chords'};})) =~ /\S/
Truth table80%4/5
A or B and not(C && D)
A= $lyrics_onlyB= $single_spaceC= $elt->{'chords'}D= join('', map({$_ ? $_->key : '';} @{$$elt{'chords'};})) =~ /\S/
ABCDresult
1XXX1
010X1
01101
00XX0
01110
234 or
235 102 ▶ $single_space && ! ( $elt->{chords} && join( "", map { $_?$_->key:"" } @{ $elt->{chords} } ) =~ /\S/ )
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 75 27 102
236 ) {
237 43 $t_line = join( "", @phrases );
238 43 $t_line =~ s/\s+$//;
239 43 return $t_line;
240 }
241  
242 62 ▶ unless ( $elt->{chords} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($elt->{"chords"}) 8 54 62
243 8 ▶ return ( $config->{settings}->{'inline-chords'} ? () : "",
Branch: 1 branch
BranchTrueFalseTotal
$main::config->{'settings'}{'inline-chords'} ? : 0 8 8
244 join( " ", @phrases ) );
245 }
246  
247 54 ▶ if ( my $f = $config->{settings}->{'inline-chords'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $main::config->{"settings"}{"inline-chords"}) 0 54 54
248 0 ▶ $f = '[%s]' unless $f =~ /^[^%]*\%s[^%]*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $f =~ /^[^%]*\%s[^%]*$/ 0 0 0
249 0 $f .= '%s';
250 0 foreach ( 0..$#{$elt->{chords}} ) {
251 0 ▶ if ( $elt->{chords}->[$_] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{'chords'}[$_]) { } 0 0 0
252 $t_line .= sprintf( $f,
253 0 chord( $song, $elt->{chords}->[$_] ),
254 $phrases[$_] );
255 }
256 else {
257 0 $t_line .= $phrases[$_];
258 }
259 }
260 0 return ( $t_line );
261 }
262  
263 54 my $c_line = "";
264 54 foreach my $c ( 0..$#{$elt->{chords}} ) {
265 $c_line .= chord( $song, $elt->{chords}->[$c] ) . " "
266 204 ▶ if ref $elt->{chords}->[$c];
Branch: 1 branch
BranchTrueFalseTotal
if ref $elt->{"chords"}[$c] 150 54 204
267 204 $t_line .= $phrases[$c];
268 204 my $d = length($c_line) - length($t_line);
269 204 ▶ $t_line .= "-" x $d if $d > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $d > 0 4 200 204
270 204 ▶ $c_line .= " " x -$d if $d < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $d < 0 198 6 204
271 }
272 54 s/\s+$// for ( $t_line, $c_line );
273 54 ▶ return $chords_under
Branch: 1 branch
BranchTrueFalseTotal
$chords_under ? : 0 54 54
274 ? ( $t_line, $c_line )
275 : ( $c_line, $t_line )
276 }
277  
278 150 ▶sub chord {
Subroutine chord: called CC=3 SCAR=11.3
Pod undocumented
279 150 my ( $s, $c ) = @_;
280 150 ▶ return "" unless length($c);
Branch: 1 branch
BranchTrueFalseTotal
unless length $c 0 150 150
281 150 $layout->set_markup($c->chord_display);
282 150 my $t = $layout->render;
283 150 ▶ return $c->info->is_annotation ? "*$t" : $t;
Branch: 1 branch
BranchTrueFalseTotal
$c->info->is_annotation ? : 0 150 150
284 }
285  
286 # Temporary. Eventually we'll have a decent HTML backend for Text::Layout.
287  
288 package Text::Layout::Text;
289  
290 5 ▶use parent 'Text::Layout';
Subroutine BEGIN: called CC=1 SCAR=0.0
291 5 ▶use ChordPro::Utils qw( fq );
Subroutine BEGIN: called CC=1 SCAR=0.0
292  
293 # Eliminate warning when HTML backend is loaded together with Text backend.
294 5 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
295  
296 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
297 5 my ( $pkg, @data ) = @_;
298 5 my $self = $pkg->SUPER::new;
299 5 $self;
300 }
301  
302 480 ▶sub render {
Subroutine render: called CC=3 SCAR=11.0
303 480 my ( $self ) = @_;
304 480 my $res = "";
305 480 foreach my $fragment ( @{ $self->{_content} } ) {
306 474 ▶ next unless length($fragment->{text});
Branch: 1 branch
BranchTrueFalseTotal
unless length $fragment->{"text"} 0 474 474
307 474 $res .= fq($fragment->{text});
308 }
309 480 $res;
310 }
311  
312 5 1;
« lib/ChordPro/lib/SVGPDF/Element.pm ↑ Summary lib/ChordPro/lib/JSON/Relaxed/Parser.pm »