lib/ChordPro/Output/Markdown.pm

filter: Statementstmt 83.6% 200 / 239 Branchbran 72.0% 49 / 68 Conditioncond 55.5% 10 / 18 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 84.6% 22 / 26 Podpod 0.0% 0 / 22 totaltotal 74.6% 286 / 383 CC 52 SCAR 23.4
CC
52
Cov
80%
line_songline
30.1
line_set
14.4
generate_song
9.0
CRAP 10.4
« lib/ChordPro/Output/Debug.pm ↑ Summary blib/lib/ChordPro/Wx/SongbookExportPanel_wxg.pm »
1 #!/usr/bin/perl
2 package main;
3  
4 1 our $options;
5 1 our $config;
6  
7 package ChordPro::Output::Markdown;
8 # Author: Johannes Rumpf / 2022
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use ChordPro::Output::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Text::Layout::Markdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 my $single_space = 0; # suppress chords line when empty
16 1 my $lyrics_only = 0; # suppress all chords lines
17 1 my $chords_under = 0; # chords under lyrics
18 1 my $text_layout = Text::Layout::Markdown->new; # Text::Layout::Text->new;
19 1 my %line_routines = ();
20 1 my $tidy;
21 my $rechorus; # not implemented @todo
22 1 my $act_song;
23 1 my $cp = "\t"; # Chord-Prefix // Verbatim / Code line in Markdown
24  
25 10 ▶sub upd_config {
Subroutine upd_config: called CC=1 SCAR=0.0
Pod undocumented
26 10 $lyrics_only = $config->{settings}->{'lyrics-only'};
27 10 $chords_under = $config->{settings}->{'chords-under'};
28 10 $rechorus = $config->{text}->{chorus}->{recall};
29 }
30  
31 10 ▶sub generate_songbook {
Subroutine generate_songbook: called CC=7 SCAR=19.7
Pod undocumented
32 10 my ( $self, $sb ) = @_;
33 10 my @book;
34 # push(@book, "[TOC]"); # maybe https://metacpan.org/release/IMAGO/Markdown-TOC-0.01 to create a TOC?
35  
36 10 foreach my $song ( @{$sb->{songs}} ) {
37 10 ▶ if ( @book ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@book) 0 10 10
38 0 ▶ push(@book, "") if $options->{'backend-option'}->{tidy};
Branch: 1 branch
BranchTrueFalseTotal
if $main::options->{"backend-option"}{"tidy"} 0 0 0
39 }
40 10 push(@book, @{generate_song($song)});
41 10 push(@book, "--------------- \n"); #Horizontal line between each song
42 }
43  
44 10 push( @book, "");
45  
46 # remove all double empty lines
47 10 my @new;
48 10 my $count = 0;
49 10 foreach (@book){
50 256 ▶ if ($_ =~ /.{1,}/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /.{1,}/) { } 173 83 256
51 173 push(@new, $_);
52 173 $count = 0
53 } else {
54 83 ▶ push(@new, $_) if $count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 65 18 83
55 83 $count++;
56 }
57 }
58 10 \@new;
59 }
60  
61 10 ▶sub generate_song {
Subroutine generate_song: called CC=9 SCAR=22.0
Pod undocumented
62 10 my ( $s ) = @_;
63 10 $act_song = $s;
64 10 $tidy = $options->{'backend-option'}->{tidy};
65 10 $single_space = $options->{'single-space'};
66  
67 10 upd_config();
68  
69 # asume songline a verse when no context is applied. # check https://github.com/ChordPro/chordpro/pull/211
70 10 foreach my $item ( @{ $s->{body} } ) {
71 151 ▶ if ( $item->{type} eq "songline" && $item->{context} eq '' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"type"} eq "songline" and $item->{"context"} eq "") 55 96 151
Condition100%3/3
$item->{"type"} eq "songline" and $item->{"context"} eq ""
!ll&&!rl&&r
841255
MC/DC100%2/2
$item->{"type"} eq "songline" and $item->{"context"} eq ""
$item->{"type"} eq "songline" $item->{"context"} eq ""
Truth table100%3/3
A and B
A= $item->{"type"} eq "songline"B= $item->{"context"} eq ""
ABresult
0X0
100
111
72 55 $item->{context} = 'verse';
73 }} # end of pull --
74
75 10 $s->structurize;
76 10 my @s;
77 10 ▶ push(@s, "# " . $s->{title}) if defined $s->{title};
Branch: 1 branch
BranchTrueFalseTotal
if defined $s->{"title"} 10 0 10
78 10 ▶ if ( defined $s->{subtitle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->{"subtitle"}) 3 7 10
79 3 push(@s, map { +"## $_" } @{$s->{subtitle}});
80 }
81  
82 10 ▶ if ( !$lyrics_only ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($lyrics_only) 10 0 10
83 10 my $all_chords = "";
84 # https://chordgenerator.net/D.png?p=xx0212&s=2 # reuse of other projects (https://github.com/einaregilsson/ChordImageGenerator)?
85 # generate png-out of this project? // fingers also possible - but not set in basics.
86 10 foreach my $mchord (@{$s->{chords}->{chords}}){
87 # replace -1 with 'x' - alternative '-'
88 31 ▶ my $frets = join("", map { if($_ eq '-1'){ $_ = 'x'; } +"$_"} @{$s->{chordsinfo}->{$mchord}->{frets}});
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq -1) 27 159 186
89 31 $all_chords .= "![$mchord](https://chordgenerator.net/$mchord.png?p=$frets&s=2) ";
90
91 }
92 10 push(@s, $all_chords);
93 10 push(@s, "");
94 }
95 10 push(@s, elt_handler($s->{body}));
96 10 return \@s;
97 }
98  
99 8 ▶sub line_default {
Subroutine line_default: called CC=1 SCAR=0.0
Pod undocumented
100 8 my ( $lineobject, $ref_lineobjects ) = @_;
101 8 return "";
102 }
103 1 $line_routines{line_default} = \&line_default;
104  
105 102 ▶sub chord {
Subroutine chord: called CC=3 SCAR=11.0
Pod undocumented
106 102 my ( $c ) = @_;
107 102 ▶ return "" unless length($c);
Branch: 1 branch
BranchTrueFalseTotal
unless length $c 25 77 102
108 77 ▶ return $c->key if $c->info->is_annotation;
Branch: 1 branch
BranchTrueFalseTotal
if $c->info->is_annotation 1 76 77
109 76 $text_layout->set_markup($c->chord_display);
110 76 return $text_layout->render;
111 }
112  
113 32 ▶sub md_textline{
Subroutine md_textline: called CC=2 SCAR=6.9
Pod undocumented
114 32 my ( $songline ) = @_;
115 32 my $empty = $songline;
116 32 my $textline = $songline;
117 32 my $nbsp = "\x{00A0}"; #unicode for nbsp sign
118 32 ▶ if($empty =~ /^\s+/){ # starts with spaces
Branch: 1 branch
BranchTrueFalseTotal
if ($empty =~ /^\s+/) 14 18 32
119 14 $empty =~ s/^(\s+).*$/$1/; # not the elegant solution - but working - replace all spaces in the beginning of a line
120 14 my $replaces = $empty; #with a nbsp symbol as the intend tend to be intentional
121 14 $replaces =~ s/\s/$nbsp/g;
122 14 $textline =~ s/$empty/$replaces/;
123 }
124 32 $textline = $textline." "; # append two spaces to force linebreak in Markdown
125 32 return $textline;
126 }
127  
128 67 ▶sub line_songline {
Subroutine line_songline: called CC=15 SCAR=34.1
Pod undocumented
129 67 my ( $elt ) = @_;
130 67 my $t_line = "";
131 134 my @phrases = map { $text_layout->set_markup($_); $text_layout->render }
132 67 @{ $elt->{phrases} };
133  
134 67 ▶ if ( $lyrics_only or
Branch: 1 branch
BranchTrueFalseTotal
if ($lyrics_only or $single_space and not $elt->{"chords"} && join("", map({$_->raw;} @{$$elt{"chords"};})) =~ /\S/) 0 67 67
Condition22%2/9
$lyrics_only or $single_space and not $elt->{"chords"} && join("", map({$_->raw;} @{$$elt{"chords"};})) =~ /\S/
l!l&&r!l&&!r
0067
$single_space and not $elt->{"chords"} && join("", map({$_->raw;} @{$$elt{"chords"};})) =~ /\S/
!ll&&!rl&&r
6700
$elt->{'chords'} && join('', map({$_->raw;} @{$$elt{'chords'};})) =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/4
$lyrics_only or $single_space and not $elt->{"chords"} && join("", map({$_->raw;} @{$$elt{"chords"};})) =~ /\S/
$lyrics_only $single_space $elt->{'chords'} join('', map({$_->raw;} @{$$elt{'chords'};})) =~ /\S/
Truth table20%1/5
A or B and not(C && D)
A= $lyrics_onlyB= $single_spaceC= $elt->{'chords'}D= join('', map({$_->raw;} @{$$elt{'chords'};})) =~ /\S/
ABCDresult
1XXX1
010X1
01101
00XX0
01110
135 0 $single_space && ! ( $elt->{chords} && join( "", map { $_->raw } @{ $elt->{chords} } ) =~ /\S/ )
136 ) {
137 0 $t_line = join( "", @phrases );
138 0 return md_textline($cp.$t_line);
139 }
140  
141 67 ▶ unless ( $elt->{chords} ) { # i guess we have a line with no chords now...
Branch: 1 branch
BranchTrueFalseTotal
unless ($elt->{"chords"}) 32 35 67
142 32 return ($cp. md_textline( join( " ", @phrases )) );
143 }
144
145 35 ▶ if ( my $f = $::config->{settings}->{'inline-chords'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $main::config->{"settings"}{"inline-chords"}) 0 35 35
146 0 ▶ $f = '[%s]' unless $f =~ /^[^%]*\%s[^%]*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $f =~ /^[^%]*\%s[^%]*$/ 0 0 0
147 0 $f .= '%s';
148 0 foreach ( 0..$#{$elt->{chords}} ) {
149 $t_line .= sprintf( $f,
150 0 chord( $elt->{chords}->[$_]->raw ),
151 $phrases[$_] );
152 }
153 0 return ( md_textline($cp.$t_line) );
154 }
155  
156 35 my $c_line = "";
157 35 foreach ( 0..$#{$elt->{chords}} ) {
158 102 $c_line .= chord( $elt->{chords}->[$_] ) . " ";
159 102 $t_line .= $phrases[$_];
160 102 my $d = length($c_line) - length($t_line);
161 102 ▶ $t_line .= "-" x $d if $d > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $d > 0 4 98 102
162 102 ▶ $c_line .= " " x -$d if $d < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $d < 0 90 12 102
163 } # this looks like setting the chords above the words.
164  
165 35 s/\s+$// for ( $t_line, $c_line );
166  
167 # main problem in markdown - a fixed position is only available in "Code escapes" so weather to set
168 # a tab or a double backticks (``) - i tend to the tab - so all lines with tabs are "together"
169 35 ▶ if ($c_line ne ""){ # Block-lines are not replacing initial spaces - as the are "code"
Branch: 1 branch
BranchTrueFalseTotal
if ($c_line ne '') { } 35 0 35
170 35 $t_line = $cp.$t_line." ";
171 35 $c_line = $cp.$c_line." ";
172 }
173 else{
174 0 $t_line = md_textline($cp.$t_line);
175 }
176 35 ▶ return $chords_under
Branch: 1 branch
BranchTrueFalseTotal
$chords_under ? : 0 35 35
177 ? ( $t_line, $c_line )
178 : ( $c_line, $t_line );
179 }
180 1 $line_routines{line_songline} = \&line_songline;
181  
182 3 ▶sub line_newpage {
Subroutine line_newpage: called CC=1 SCAR=0.0
Pod undocumented
183 3 return "--------------- \n";
184 }
185 1 $line_routines{line_newpage} = \&line_newpage;
186  
187 0 ▶sub line_empty {
Subroutine line_empty: not called CC=1 SCAR=6.9
Pod undocumented
188 0 return "$cp";
189 }
190 1 $line_routines{line_empty} = \&line_empty;
191  
192 19 ▶sub line_comment {
Subroutine line_comment: called CC=5 SCAR=21.6
Pod undocumented
193 19 my ( $elt ) = @_; # Template for comment?
194 19 my @s;
195 19 my $text = $elt->{text};
196 19 ▶ if ( $elt->{chords} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"chords"}) 0 19 19
197 0 $text = "";
198 0 for ( 0..$#{ $elt->{chords} } ) {
199 $text .= "[" . $elt->{chords}->[$_]->raw . "]"
200 0 ▶ if $elt->{chords}->[$_] ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $elt->{"chords"}[$_] ne "" 0 0 0
201 0 $text .= $elt->{phrases}->[$_];
202 }}
203 19 ▶ if ($elt->{type} =~ /italic$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} =~ /italic$/) 0 19 19
204 0 $text = "*" . $text . "* ";
205 }
206 19 push(@s, "> $text ");
207 19 return @s;
208 }
209 1 $line_routines{line_comment} = \&line_comment;
210  
211 3 ▶sub line_comment_italic {
Subroutine line_comment_italic: called CC=1 SCAR=0.0
Pod undocumented
212 3 my ( $lineobject ) = @_; # Template for comment?
213 3 return "> *". $lineobject->{text} ."*";;
214 }
215 1 $line_routines{line_comment_italic} = \&line_comment_italic;
216  
217  
218 0 ▶sub line_image {
Subroutine line_image: not called CC=1 SCAR=6.9
Pod undocumented
219 0 my ( $elt ) = @_;
220 0 return "![](".$elt->{uri}.")";
221 }
222 1 $line_routines{line_image} = \&line_image;
223  
224 3 ▶sub line_colb {
Subroutine line_colb: called CC=1 SCAR=0.0
Pod undocumented
225 3 return "\n\n\n";
226 }
227 1 $line_routines{line_colb} = \&line_colb;
228  
229 23 ▶sub body_has_chords{
Subroutine body_has_chords: called CC=5 SCAR=16.1
Pod undocumented
230 23 my ( $elts ) = @_; # reference to array
231 23 my $has_chord = 0; # default false has no chords
232 23 foreach my $elt (@{ $elts }) {
233 48 ▶ if ($elt->{type} eq 'songline'){
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "songline") 47 1 48
234 47 ▶ if ((defined $elt->{chords}) && (scalar @{$elt->{chords}} > 0 )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $elt->{"chords"} and scalar @{$elt->{"chords"};} > 0) 15 32 47
Condition66%2/3
defined $elt->{"chords"} and scalar @{$elt->{"chords"};} > 0
!ll&&!rl&&r
32015
MC/DC50%1/2
defined $elt->{"chords"} and scalar @{$elt->{"chords"};} > 0
defined $elt->{"chords"} scalar @{$elt->{"chords"};} > 0
Truth table66%2/3
A and B
A= defined $elt->{"chords"}B= scalar @{$elt->{"chords"};} > 0
ABresult
0X0
100
111
235 15 $has_chord = 1;
236 15 return $has_chord;
237 }}
238 }
239 8 return $has_chord;
240 }
241 3 ▶sub line_chorus {
Subroutine line_chorus: called CC=2 SCAR=6.9
Pod undocumented
242 3 my ( $lineobject ) = @_; #
243 3 my @s;
244 3 ▶ $cp = (body_has_chords($lineobject->{body})) ? "\t" : ""; # Verbatim on Verse/Chorus because Chords are present
Branch: 1 branch
BranchTrueFalseTotal
body_has_chords($lineobject->{'body'}) ? : 2 1 3
245 3 push(@s, "**Chorus**");
246 3 push(@s, "");
247 3 push(@s, elt_handler($lineobject->{body}));
248 # push(@s, "\x{00A0} "); # nbsp
249 3 push(@s, "--------------- \n");
250 3 return @s;
251 }
252 1 $line_routines{line_chorus} = \&line_chorus;
253  
254 20 ▶sub line_verse {
Subroutine line_verse: called CC=2 SCAR=6.9
Pod undocumented
255 20 my ( $lineobject ) = @_; #
256 20 my @s;
257 20 ▶ $cp = (body_has_chords($lineobject->{body})) ? "\t" : ""; # Verbatim on Verse/Chorus because Chords are present
Branch: 1 branch
BranchTrueFalseTotal
body_has_chords($lineobject->{'body'}) ? : 13 7 20
258 20 push(@s, elt_handler($lineobject->{body}));
259 20 push(@s, "");
260 # push(@s, "\x{00A0} "); # nbsp
261 20 return @s;
262 }
263 1 $line_routines{line_verse} = \&line_verse;
264  
265 11 ▶sub line_set { # potential comments in fe. Chorus or verse or .... complicated handling - potential contextsensitiv.
Subroutine line_set: called CC=5 SCAR=26.7
Pod undocumented
266 11 my ( $elt ) = @_;
267 11 ▶ if ( $elt->{name} eq "lyrics-only" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elt->{'name'} eq 'lyrics-only') { } 0 11 11
elsif ($elt->{'name'} =~ /^(text\..+)/) { } 0 11 11
268 $lyrics_only = $elt->{value}
269 0 ▶ unless $lyrics_only > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $lyrics_only > 1 0 0 0
270 }
271 # Arbitrary config values.
272 elsif ( $elt->{name} =~ /^(text\..+)/ ) {
273 0 my @k = split( /[.]/, $1 );
274 0 my $cc = {};
275 0 my $c = \$cc;
276 0 foreach ( @k ) {
277 0 $c = \($$c->{$_});
278 }
279 0 $$c = $elt->{value};
280 0 $config->augment($cc);
281 0 upd_config();
282 }
283 11 return "";
284 }
285 1 $line_routines{line_set} = \&line_set;
286  
287 0 ▶sub line_tabline {
Subroutine line_tabline: not called CC=1 SCAR=6.9
Pod undocumented
288 0 my ( $lineobject ) = @_;
289 0 return "\t".$lineobject->{text};
290 }
291 1 $line_routines{line_tabline} = \&line_tabline;
292  
293 0 ▶sub line_tab {
Subroutine line_tab: not called CC=1 SCAR=6.9
Pod undocumented
294 0 my ( $lineobject ) = @_;
295 0 my @s;
296 0 push(@s, "**Tabulatur** "); #@todo
297 0 push(@s, "");
298 0 push(@s, map { "\t".$_ } elt_handler($lineobject->{body}) ); #maybe this need to go for code markup as well´?
299 0 return @s;
300 }
301 1 $line_routines{line_tab} = \&line_tab;
302  
303 2 ▶sub line_grid {
Subroutine line_grid: called CC=1 SCAR=0.0
Pod undocumented
304 2 my ( $lineobject ) = @_;
305 2 my @s;
306 2 push(@s, "**Grid** ");
307 2 push(@s, "");
308 2 push(@s, elt_handler($lineobject->{body}));
309 # push(@s, "\x{00A0} ");
310 2 push(@s, "");
311 2 return @s;
312 }
313 1 $line_routines{line_grid} = \&line_grid;
314  
315 6 ▶sub line_gridline {
Subroutine line_gridline: called CC=2 SCAR=6.9
Pod undocumented
316 6 my ( $elt ) = @_;
317 6 my @a = @{ $elt->{tokens} };
318 6 @a = map { $_->{class} eq 'chord'
319 ? $_->{chord}->raw
320 78 ▶ : $_->{symbol} } @a;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'class'} eq 'chord' ? : 16 62 78
321 6 return "\t".join("", @a);
322 }
323 1 $line_routines{line_gridline} = \&line_gridline;
324  
325 35 ▶sub elt_handler {
Subroutine elt_handler: called CC=6 SCAR=17.9
Pod undocumented
326 35 my ( $elts ) = @_; # reference to array
327 35 my $cref; #command reference to subroutine
328 35 my $init_context = 1;
329 35 my $ctx = "";
330  
331 35 my @lines;
332 35 my $last_type='';
333 35 foreach my $elt (@{ $elts }) {
334 150 ▶ if ( $elt->{type} =~ /^(ignore|meta)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} =~ /^(ignore|meta)$/) 5 145 150
335 5 next;
336 }
337 145 ▶ if (($elt->{type} eq 'verse') && ($last_type =~ /comment/)){
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "verse" and $last_type =~ /comment/) 13 132 145
Condition100%3/3
$elt->{"type"} eq "verse" and $last_type =~ /comment/
!ll&&!rl&&r
125713
MC/DC100%2/2
$elt->{"type"} eq "verse" and $last_type =~ /comment/
$elt->{"type"} eq "verse" $last_type =~ /comment/
Truth table100%3/3
A and B
A= $elt->{"type"} eq "verse"B= $last_type =~ /comment/
ABresult
0X0
100
111
338 13 push(@lines, "");
339 }
340 # Gang of Four-Style - sort of command pattern
341 145 my $sub_type = "line_".$elt->{type}; # build command "line_<linetype>"
342 145 ▶ if (defined $line_routines{$sub_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line_routines{$sub_type}) { } 137 8 145
343 137 $cref = $line_routines{$sub_type}; #\&$sub_type; # due to use strict - we need to get an reference to the command
344 137 push(@lines, &$cref($elt)); # call line with actual line-object
345 }
346 else {
347 8 push(@lines, line_default($elt)); # default = empty line
348 }
349 145 $last_type = $elt->{type};
350 }
351 35 return @lines;
352 }
353  
354 #################
355  
356 # package Text::Layout::Text;
357  
358 # use parent 'Text::Layout';
359  
360 # # Eliminate warning when HTML backend is loaded together with Text backend.
361 # no warnings 'redefine';
362  
363 # sub new {
364 # my ( $pkg, @data ) = @_;
365 # my $self = $pkg->SUPER::new;
366 # $self;
367 # }
368  
369 # sub render {
370 # my ( $self ) = @_;
371 # my $res = "";
372 # foreach my $fragment ( @{ $self->{_content} } ) {
373 # next unless length($fragment->{text});
374 # $res .= $fragment->{text};
375 # }
376 # $res;
377 # }
378  
379  
380 1 1;
381 # @todo
382 # sub line_rechorus {
383 # my ( $lineobject ) = @_;
384 # if ( $rechorus->{quote} ) {
385 # unshift( @elts, @{ $elt->{chorus} } );
386 # }
387 # elsif ( $rechorus->{type} && $rechorus->{tag} ) {
388 # push( @s, "{".$rechorus->{type}.": ".$rechorus->{tag}."}" );
389 # }
390 # else {
391 # push( @s, "{chorus}" );
392 # }
393 # }
394  
395 # sub line_control {
396 # my ( $lineobject ) = @_;
397 # }
« lib/ChordPro/Output/Debug.pm ↑ Summary blib/lib/ChordPro/Wx/SongbookExportPanel_wxg.pm »