App-Music-ChordPro

 view release on metacpan or  search on metacpan

lib/ChordPro/Output/HTML.pm  view on Meta::CPAN

#!/usr/bin/perl

package ChordPro::Output::HTML;

# Produce nice viewable HMTL output.
#
# You should be able to print it using a decent browser (notexisting)
# or a formatting tool like weasyprint.

use strict;
use warnings;
use ChordPro::Files;
use ChordPro::Paths;
use ChordPro::Output::Common;
use ChordPro::Utils qw();
use ChordPro::Assets;
use Storable 'dclone';

sub generate_songbook {
    my ( $self, $sb ) = @_;

    my @book;
    my $cfg = $::config->{html} // {};
    $cfg->{styles}->{default} //= "chordpro.css";
    $cfg->{styles}->{screen}  //= $cfg->{styles}->{display} // "";
    $cfg->{styles}->{print}   //= "chordpro_print.css";

    my %styles = %{ $cfg->{styles} };
    my @styles = ( "default" );
    for ( sort keys %styles ) {
	next if /^(?:default|embed)$/ ;
	next unless $styles{$_};
	push( @styles, $_ );
    }

    push( @book,
	  '<html>',
	  '<head>',
	  '<meta charset="utf-8">' );

    if ( delete $styles{embed} ) {
	use File::LoadLines;
 	foreach my $class ( @styles ) {
	    my $style = CP->findres( $styles{$class}, class => "styles" );
	    next unless fs_test( 'rf', $style );
	    my @lines = loadlines($style, { fail => 'hard' } );
	    push( @book, "<style type=\"text/css\" ".
		  ($class eq "default" ? "" : "media=\"$class\"").">",
		  @lines, "</style>" );
	    delete $styles{$class};
	}
    }
    foreach ( @styles ) {
	next unless $styles{$_};
	push( @book,
	      '<link rel="stylesheet" href="'.$styles{$_}.'"'.
	      ( $_ =~ /^(default)$/ ? "" : qq{ media="$_"} ).
	      '>' );
    }
    push( @book, '</head>',
	  '<body>',
	);

    foreach my $song ( @{$sb->{songs}} ) {
	push( @book, @{ generate_song($song) } );
    }

    push( @book, "</body>", "</html>" );
    \@book;
}

my $config;
my $single_space = 0;		# suppress chords line when empty
my $lyrics_only = 0;		# suppress all chords lines
my $layout;

sub generate_song {
    my ( $s ) = @_;

    my $tidy      = $::options->{tidy};
    $single_space = $::options->{'single-space'};
    $config = dclone( $s->{config} // $::config );
    $lyrics_only  = $config->{settings}->{'lyrics-only'};
    $s->structurize;
    prepare_assets($s);
    $s->dump(0) if $config->{debug}->{song};
    $layout = Text::Layout::HTML->new;
    while ( my($k,$v) = each( %{$config->{markup}->{shortcodes}}) ) {
	unless ( $layout->can("register_shortcode") ) {
	    warn("Cannot register shortcodes, upgrade Text::Layout module\n");
	    last;
	}
	$layout->register_shortcode( $k, $v );
    }

    my @s;

    for ( $s->{title} // "Untitled" ) {
	push( @s,



( run in 0.653 second using v1.01-cache-2.11-cpan-39bf76dae61 )