| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package ChordPro::Output::LaTeX; | ||
| 4 | # Author: Johannes Rumpf / 2022 | ||
| 5 | # relevant Latex packages - still using the template module would make it possible | ||
| 6 | # to create any form of textual output. | ||
| 7 | # delivered example will work with songs-package - any other package needed to be | ||
| 8 | # evaluated / tested. But should work | ||
| 9 | # http://songs.sourceforge.net/songsdoc/songs.html | ||
| 10 | # https://www.ctan.org/pkg/songs | ||
| 11 | # https://www.ctan.org/pkg/guitar | ||
| 12 | # https://www.ctan.org/pkg/songbook | ||
| 13 | # https://www.ctan.org/pkg/gchords | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use warnings; | ||
| 17 | use ChordPro::Paths; | ||
| 18 | use ChordPro::Output::Common; | ||
| 19 | use Template; | ||
| 20 | use LaTeX::Encode; | ||
| 21 | |||
| 22 | our $CHORDPRO_LIBRARY; | ||
| 23 | |||
| 24 | my $single_space = 0; # suppress chords line when empty | ||
| 25 | my $lyrics_only = 0; # suppress all chords lines | ||
| 26 | my %line_routines = (); | ||
| 27 | my $gtemplate; | ||
| 28 | my $gcfg; | ||
| 29 | |||
| 30 | my $newpage_tag = "[% newpage_tag %]" ; | ||
| 31 | my $emptyline_tag = "[% emptyline_tag %]"; | ||
| 32 | my $columnbreak_tag = "[% columnbreak_tag %]"; | ||
| 33 | my $beginchorus_tag = "[% beginchorus_tag %]"; | ||
| 34 | my $endchorus_tag = "[% endchorus_tag %]"; | ||
| 35 | my $beginverse_tag = "[% beginverse_tag %]"; | ||
| 36 | my $endverse_tag = "[% endverse_tag %]"; | ||
| 37 | my $beginabc_tag = "[% beginabc_tag %]"; | ||
| 38 | my $endabc_tag = "[% endabc_tag %]"; | ||
| 39 | my $beginlilypond_tag = "[% beginlilypond_tag %]"; | ||
| 40 | my $endlilypond_tag = "[% endlilypond_tag %]"; | ||
| 41 | my $begingrid_tag = "[% begingrid_tag %]"; | ||
| 42 | my $endgrid_tag = "[% endgrid_tag %]"; | ||
| 43 | my $begintab_tag = "[% begintab_tag %]"; | ||
| 44 | my $endtab_tag = "[% endtab_tag %]"; | ||
| 45 | my $gchordstart_tag = "[% gchordstart_tag %]"; | ||
| 46 | my $gchordend_tag = "[% gchordend_tag %]"; | ||
| 47 | my $chorded_line = "[% chorded_line %]"; | ||
| 48 | my $unchorded_line = "[% unchorded_line %]"; | ||
| 49 | my $start_spaces_songline = "[% start_spaces_songline %]"; | ||
| 50 | my $eol = "[% eol %]"; | ||
| 51 | |||
| 52 | sub generate_songbook { | ||
| 53 | my ( $self, $sb ) = @_; | ||
| 54 | my @songs; | ||
| 55 | $gcfg = $::config->{latex}; | ||
| 56 | $gtemplate = Template->new | ||
| 57 | ({ INCLUDE_PATH => [@{$gcfg->{template_include_path}}, | ||
| 58 | CP->findres("templates"), $CHORDPRO_LIBRARY], | ||
| 59 | INTERPOLATE => 1, | ||
| 60 | }) || die "$Template::ERROR\n"; | ||
| 61 | |||
| 62 | foreach my $song ( @{$sb->{songs}} ) { | ||
| 63 | push( @songs, generate_song($song) ); | ||
| 64 | } | ||
| 65 | my $songbook = ''; | ||
| 66 | my %vars = (); | ||
| 67 | $vars{songs} = [@songs] ; | ||
| 68 | $gtemplate->process($gcfg->{templates}->{songbook}, \%vars, $::options->{output} ) | ||
| 69 | || die $gtemplate->error(); | ||
| 70 | # i like it more to handle output through template module - but its possible to result it as array. | ||
| 71 | # return split(/\n/, $songbook); | ||
| 72 | $::options->{output} = '-'; | ||
| 73 | return []; | ||
| 74 | } | ||
| 75 | |||
| 76 | # some not implemented feature is requested. will be removed. | ||
| 77 | sub line_default { | ||
| 78 | my ( $lineobject, $ref_lineobjects ) = @_; | ||
| 79 | return ""; | ||
| 80 | } | ||
| 81 | $line_routines{line_default} = \&line_default; | ||
| 82 | |||
| 83 | sub get_firstphrase{ | ||
| 84 | my ( $elts ) = @_; # reference to array | ||
| 85 | my $line = ""; | ||
| 86 | foreach my $elt (@{ $elts }) { | ||
| 87 | if($elt->{type} eq 'songline'){ | ||
| 88 | foreach my $phrase (@{$elt->{phrases}}){ | ||
| 89 | $line .= $phrase; | ||
| 90 | } | ||
| 91 | return my_latex_encode($line); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | sub line_songline { | ||
| 97 | my ( $lineobject ) = @_; | ||
| 98 | my $index = 0; | ||
| 99 | my $line = ""; | ||
| 100 | my $chord = ""; | ||
| 101 | my $has_chord = 0; | ||
| 102 | foreach my $phrase (@{$lineobject->{phrases}}){ | ||
| 103 | if(defined $lineobject->{chords}){ | ||
| 104 | if (@{$lineobject->{chords}}[$index] ne '' ){ | ||
| 105 | $chord = $gchordstart_tag.@{$lineobject->{chords}}[$index]->key .$gchordend_tag; #songbook format \\[chord] | ||
| 106 | $has_chord = 1; | ||
| 107 | }} | ||
| 108 | $line .= $chord . latex_encode($phrase); | ||
| 109 | $index += 1; | ||
| 110 | $chord = ""; | ||
| 111 | } | ||
| 112 | |||
| 113 | my $empty = $line; | ||
| 114 | my $textline = $line; | ||
| 115 | my $nbsp = $start_spaces_songline; #unicode for nbsp sign # start_spaces_songline | ||
| 116 | if($empty =~ /^\s+/){ # starts with spaces | ||
| 117 | $empty =~ s/^(\s+).*$/$1/; # not the elegant solution - but working - replace all spaces in the beginning of a line | ||
| 118 | my $replaces = $empty; #with a nbsp symbol as the intend tend to be intentional | ||
| 119 | $replaces =~ s/\s+/$nbsp/g; | ||
| 120 | $textline =~ s/$empty/$replaces/; | ||
| 121 | } | ||
| 122 | $line = $textline; | ||
| 123 | if ($has_chord) { $line = $chorded_line . $line; } else { $line = $unchorded_line . $line; } | ||
| 124 | return $line.$eol; | ||
| 125 | } | ||
| 126 | $line_routines{line_songline} = \&line_songline; | ||
| 127 | |||
| 128 | sub line_newpage { | ||
| 129 | my ( $lineobject ) = @_; | ||
| 130 | return $newpage_tag; | ||
| 131 | } | ||
| 132 | $line_routines{line_newpage} = \&line_newpage; | ||
| 133 | |||
| 134 | sub line_empty { | ||
| 135 | my ( $lineobject ) = @_; | ||
| 136 | return $emptyline_tag; | ||
| 137 | } | ||
| 138 | $line_routines{line_empty} = \&line_empty; | ||
| 139 | |||
| 140 | sub line_comment { | ||
| 141 | my ( $lineobject ) = @_; # Template for comment? | ||
| 142 | my $vars = { | ||
| 143 | comment => latex_encode($lineobject->{text}) | ||
| 144 | }; | ||
| 145 | my $comment = ''; | ||
| 146 | $gtemplate->process($gcfg->{templates}->{comment}, $vars, \$comment) || die $gtemplate->error(); | ||
| 147 | return $comment ; | ||
| 148 | } | ||
| 149 | $line_routines{line_comment} = \&line_comment; | ||
| 150 | |||
| 151 | sub line_comment_italic { | ||
| 152 | my ( $lineobject ) = @_; # Template for comment? | ||
| 153 | my $vars = { | ||
| 154 | comment => "\\textit{". latex_encode($lineobject->{text}) ."}" | ||
| 155 | }; | ||
| 156 | my $comment = ''; | ||
| 157 | $gtemplate->process($gcfg->{templates}->{comment}, $vars, \$comment) || die $gtemplate->error(); | ||
| 158 | return $comment; | ||
| 159 | } | ||
| 160 | $line_routines{line_comment_italic} = \&line_comment_italic; | ||
| 161 | |||
| 162 | sub line_image { | ||
| 163 | my ( $lineobject ) = @_; | ||
| 164 | my $image = ''; | ||
| 165 | $gtemplate->process($gcfg->{templates}->{image}, $lineobject, \$image)|| die $gtemplate->error(); | ||
| 166 | return $image; | ||
| 167 | } | ||
| 168 | $line_routines{line_image} = \&line_image; | ||
| 169 | |||
| 170 | sub line_colb { | ||
| 171 | my ( $lineobject ) = @_; # Template for comment? | ||
| 172 | return $columnbreak_tag; | ||
| 173 | } | ||
| 174 | $line_routines{line_colb} = \&line_colb; | ||
| 175 | |||
| 176 | sub line_chorus { | ||
| 177 | my ( $lineobject ) = @_; # | ||
| 178 | return $beginchorus_tag ."\n". | ||
| 179 | elt_handler($lineobject->{body}) . | ||
| 180 | $endchorus_tag . "\n"; | ||
| 181 | } | ||
| 182 | $line_routines{line_chorus} = \&line_chorus; | ||
| 183 | |||
| 184 | sub line_verse { | ||
| 185 | my ( $lineobject ) = @_; # | ||
| 186 | return $beginverse_tag ."\n". | ||
| 187 | elt_handler($lineobject->{body}) | ||
| 188 | .$endverse_tag ."\n"; | ||
| 189 | } | ||
| 190 | $line_routines{line_verse} = \&line_verse; | ||
| 191 | |||
| 192 | sub line_set { # potential comments in fe. Chorus or verse or .... complicated handling - potential contextsensitiv. | ||
| 193 | my ( $lineobject ) = @_; | ||
| 194 | return ''; | ||
| 195 | } | ||
| 196 | $line_routines{line_set} = \&line_set; | ||
| 197 | |||
| 198 | sub line_tabline { | ||
| 199 | my ( $lineobject ) = @_; | ||
| 200 | return $lineobject->{text}.$eol; | ||
| 201 | } | ||
| 202 | $line_routines{line_tabline} = \&line_tabline; | ||
| 203 | |||
| 204 | sub line_tab { | ||
| 205 | my ( $lineobject ) = @_; | ||
| 206 | return $begintab_tag."\n". | ||
| 207 | elt_handler($lineobject->{body}) . | ||
| 208 | $endtab_tag ."\n"; | ||
| 209 | } | ||
| 210 | $line_routines{line_tab} = \&line_tab; | ||
| 211 | |||
| 212 | sub line_grid { | ||
| 213 | my ( $lineobject ) = @_; | ||
| 214 | return $begingrid_tag."\n". | ||
| 215 | elt_handler($lineobject->{body}) | ||
| 216 | .$endgrid_tag ."\n"; | ||
| 217 | } | ||
| 218 | $line_routines{line_grid} = \&line_grid; | ||
| 219 | |||
| 220 | sub line_gridline { | ||
| 221 | my ( $lineobject ) = @_; | ||
| 222 | my $line = ''; | ||
| 223 | if(defined $lineobject->{margin}){ | ||
| 224 | $line .= $lineobject->{margin}->{text} . "\t"; | ||
| 225 | } | ||
| 226 | else { | ||
| 227 | $line .= "\t\t"; | ||
| 228 | } | ||
| 229 | foreach my $token (@{ $lineobject->{tokens} }){ | ||
| 230 | if ($token->{class} eq 'chord'){ | ||
| 231 | $line .= $token->{chord}->key; | ||
| 232 | } | ||
| 233 | else { | ||
| 234 | $line .= $token->{symbol}; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | if(defined $lineobject->{comment}){ | ||
| 238 | $line .= $lineobject->{comment}->{text}; | ||
| 239 | } | ||
| 240 | return $line. $eol; | ||
| 241 | } | ||
| 242 | $line_routines{line_gridline} = \&line_gridline; | ||
| 243 | |||
| 244 | sub elt_handler { | ||
| 245 | my ( $elts ) = @_; # reference to array | ||
| 246 | my $cref; #command reference to subroutine | ||
| 247 | |||
| 248 | my $lines = ""; | ||
| 249 | foreach my $elt (@{ $elts }) { | ||
| 250 | # Gang of Four-Style - sort of command pattern | ||
| 251 | my $sub_type = "line_".$elt->{type}; # build command "line_<linetype>" | ||
| 252 | # if (exists &{$sub_type}) { #check if sub is implemented / maybe hash is -would be- faster... | ||
| 253 | if (defined $line_routines{$sub_type}) { | ||
| 254 | $cref = $line_routines{$sub_type}; #\&$sub_type; # due to use strict - we need to get an reference to the command | ||
| 255 | $lines .= &$cref($elt); # call line with actual line-object | ||
| 256 | } | ||
| 257 | else { | ||
| 258 | $lines .= line_default($elt); # default = empty line | ||
| 259 | |||
| 260 | } | ||
| 261 | } | ||
| 262 | return $lines; | ||
| 263 | } | ||
| 264 | |||
| 265 | sub my_latex_encode{ | ||
| 266 | my ( $val ) = @_; | ||
| 267 | if ((ref($val) eq 'SCALAR') or ( ref($val) eq '' )) { return latex_encode($val); } | ||
| 268 | if (ref($val) eq 'ARRAY'){ | ||
| 269 | my @array_return; | ||
| 270 | foreach my $array_val (@{$val}){ | ||
| 271 | push(@array_return, my_latex_encode($array_val)); | ||
| 272 | } | ||
| 273 | return \@array_return; | ||
| 274 | } | ||
| 275 | if (ref($val) eq 'HASH'){ | ||
| 276 | my %hash_return = (); | ||
| 277 | foreach my $hash_key (keys( % {$val } )){ | ||
| 278 | $hash_return{$hash_key} = my_latex_encode( $val->{$hash_key} ); | ||
| 279 | } | ||
| 280 | return \%hash_return; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | sub generate_song { | ||
| 285 | my ( $s ) = @_; | ||
| 286 | my %gtemplatatevar = (); | ||
| 287 | |||
| 288 | if ( defined $s->{meta} ) { | ||
| 289 | $gtemplatatevar{meta} = my_latex_encode($s->{meta}); | ||
| 290 | } | ||
| 291 | $gtemplatatevar{meta}->{index} = get_firstphrase($s->{body}); # needs unstructured data - .. redesign? | ||
| 292 | |||
| 293 | # asume songline a verse when no context is applied. # check https://github.com/ChordPro/chordpro/pull/211 | ||
| 294 | # Songbook needs to have a verse otherwise the chords-makro is not in the right context | ||
| 295 | foreach my $item ( @{ $s->{body} } ) { | ||
| 296 | if ( $item->{type} eq "songline" && $item->{context} eq '' ){ | ||
| 297 | $item->{context} = 'verse'; | ||
| 298 | }} # end of pull -- | ||
| 299 | $s->structurize; # removes empty lines | ||
| 300 | |||
| 301 | |||
| 302 | for ( $s->{title} // "Untitled" ) { | ||
| 303 | $gtemplatatevar{title} = my_latex_encode($s->{title}); | ||
| 304 | } | ||
| 305 | if ( defined $s->{subtitle} ) { | ||
| 306 | $gtemplatatevar{subtitle} = my_latex_encode($s->{subtitle}); | ||
| 307 | } | ||
| 308 | |||
| 309 | |||
| 310 | if ( defined $s->{chords}->{chords} ) { | ||
| 311 | my @chords; | ||
| 312 | foreach my $mchord (@{$s->{chords}->{chords}}){ | ||
| 313 | # replace -1 with 'x' - alternative '-' | ||
| 314 | my $frets = join("", map { if($_ eq '-1'){ $_ = 'X'; } +"$_"} @{$s->{chordsinfo}->{$mchord}->{frets}}); | ||
| 315 | my %chorddef = ( | ||
| 316 | "chord" => $mchord, | ||
| 317 | "frets" => $frets, | ||
| 318 | "base" => $s->{chordsinfo}->{$mchord}->{base}, | ||
| 319 | "fingers" => $s->{chordsinfo}->{$mchord}->{fingers}); | ||
| 320 | push(@chords, \%chorddef); | ||
| 321 | } | ||
| 322 | $gtemplatatevar{chords} = \@chords; | ||
| 323 | } | ||
| 324 | |||
| 325 | $gtemplatatevar{songlines} = elt_handler($s->{body}); | ||
| 326 | |||
| 327 | # my $song = ''; | ||
| 328 | # $gtemplate->process($gcfg->{template_song}, \%gtemplatatevar, \$song) || die $gtemplate->error(); | ||
| 329 | |||
| 330 | return \%gtemplatatevar; | ||
| 331 | #$song; | ||
| 332 | } | ||
| 333 | |||
| 334 | 1; | ||
| 335 | |||
| 336 | #not implemented line-types | ||
| 337 | # sub line_rechorus { | ||
| 338 | # my ( $lineobject ) = @_; | ||
| 339 | # } | ||
| 340 | |||
| 341 | # sub line_control { | ||
| 342 | # my ( $lineobject ) = @_; | ||
| 343 | # } |