App-Music-ChordPro

 view release on metacpan or  search on metacpan

lib/ChordPro.pm  view on Meta::CPAN

    $config = ChordPro::Config::configurator({});

    # Now load the real backend. Note that the actual module name
    # may be changed by config.
    if ( exists($config->{lc($options->{generate})})
	 && exists($config->{lc($options->{generate})}->{module}) ) {
	$options->{generate} = $config->{lc($options->{generate})}->{module};
    }
    $pkg = "ChordPro::Output::".$options->{generate};
    eval "require $pkg;";
    die("No backend for ", $options->{generate}, "\n$@") if $@;
    $options->{backend} = $pkg;
    $pkg->version if $options->{verbose} && $pkg->can("version");

    # Parse the input(s).
    use ChordPro::Songbook;
    my $s = ChordPro::Songbook->new;
    my $res;

    # Shortcut a2crd conversion.
    if ( $options->{a2crd} ) {
	require ChordPro::A2Crd;
	$res = ChordPro::A2Crd::a2crd();
	push( @$res, '' );
	goto WRITE_OUTPUT;
    }

    # Check for metadata in filelist. Actually, this works on the
    # command line as well, but don't tell anybody.
    progress( phase => "Parsing", index => 0,
	      total => 0+grep { !/^--/ } @ARGV )
      if @ARGV > 1;

    my %gopts;
    foreach my $file ( @ARGV ) {

	my @w = ( $file );
	if ( $file =~ /(^|\s)--\w+/ || $file =~ /^["']/ ) {
	    @w = Text::ParseWords::shellwords($file);
	}
	my %meta;
	my %defs;
	my @cfg;
	my %opts;
	die("Error in filelist: $file\n")
	  unless Getopt::Long::GetOptionsFromArray
	  ( \@w, \%opts, 'config=s@' => \@cfg, 'meta=s%' => \%meta,
	    'define=s%' => \%defs,
	    'title=s', 'subtitle=s', 'dir:s', 'filelist:s',
	  )
	  && (    ( @w == 1 && ! keys(%opts) ) # filename
	       || ( @w == 0 &&   keys(%opts) ) # options
	     );

	for ( qw( title subtitle ) ) {
	    next unless defined $opts{$_};
	    $options->{$_} = $opts{$_};
	}
	for ( qw( filelist dir ) ) {
	    next unless defined $opts{$_};
	    $gopts{$_} = $opts{$_} eq "" ? undef : expand_tilde($opts{$_});
	}
	unless ( @w ) {
	    progress( msg => $file ) if @ARGV > 1 && $file !~ /^--/;
	    next;
	}

	$file = $w[0];
	if ( defined($gopts{dir})
	     && !fn_is_absolute($file) ) {
	    $file = fn_catfile( $gopts{dir}, $file );
	}
	my $opts = { meta => { map { $_, [ $meta{$_} ] } keys %meta },
		     defs => \%defs };
	if ( @cfg ) {
	    $opts->{meta}->{__config} = \@cfg;
	}
	$opts->{generate} = $options->{generate};
	# Wx runs on temp files, so pass real filename in.
	$opts->{filesource} = $options->{filesource};
	progress( msg => $file ) if @ARGV > 1;
	$s->parse_file( $file, $opts );
    }

    if ( $options->{'dump-chords'} ) {
	my $d = ChordPro::Song->new;
	$d->{title} = "ChordPro $VERSION Built-in Chords";
	$d->{subtitle} = [ "https://www.chordpro.org" ];
	my @body;
	my @chords;

	my $prev = "";
	foreach my $c ( @{ ChordPro::Chords::chordnames() } ) {
	    next if $c =~ /^n\.?c\.?$/i;
	    if ( $c =~ /^(.[b#]?)/ and $1 ne $prev )  {
		$prev = $1;
		push( @body, { type => "diagrams",
			       context => "",
			       origin => "__CLI__",
			       chords => [ @chords ]
			     } ) if @chords;
		@chords = ();
	    }
	    push( @chords, $c );
	    $d->{chordsinfo}->{$c} = ChordPro::Chords::known_chord($c);
	}

	push( @body, { type => "diagrams",
		       context => "",
		       origin => "__CLI__",
		       chords => [ @chords ]
		     } ) if @chords;

	$d->{body} = \@body;
	if ( @{ $s->{songs} } == 1
	     && !exists $s->{songs}->[0]->{body} ) {
	    $s->{songs} = [ $d ];
	}
	else {
	    push( @{ $s->{songs} }, $d );
	}



( run in 2.875 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )