App-Music-ChordPro

 view release on metacpan or  search on metacpan

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

		    $text .= "[" . fq(chord( $s, $elt->{chords}->[$_])) . "]"
		      if $elt->{chords}->[$_] ne "";
		    $text .= $elt->{phrases}->[$_];
		}
	    }
	    $text = fmt_subst( $s, $text ) if $msp;
	    push(@s, "") if $tidy;
	    push(@s, "{$type: ".fq($text)."}");
	    push(@s, "") if $tidy;
	    next;
	}

	if ( $elt->{type} eq "image" && !$msp ) {
	    my $uri = $s->{assets}->{$elt->{id}}->{uri};
	    if ( $msp && $uri !~ /^id=/ ) {
		$imgs{$uri} //= keys(%imgs);
		$uri = sprintf("id=img%02d", $imgs{$uri});
	    }
	    my @args = ( "image:", qquote($uri) );
	    while ( my($k,$v) = each( %{ $elt->{opts} } ) ) {
		$v = join( ",",@$v ) if is_arrayref($v);
		push( @args, "$k=$v" );
	    }
	    foreach ( @args ) {
		next unless /\s/;
		$_ = '"' . $_ . '"';
	    }
	    push( @s, "{@args}" );
	    next;
	}

	if ( $elt->{type} eq "diagrams" ) {
	    for ( @{$elt->{chords}} ) {
		push( @s, define( $s->{chordsinfo}->{$_}, 1 ) );
	    }
	}

	if ( $elt->{type} eq "set" ) {
	    if ( $elt->{name} eq "lyrics-only" ) {
		$lyrics_only = $elt->{value}
		  unless $lyrics_only > 1;
	    }
	    elsif ( $elt->{name} eq "transpose" ) {
	    }
	    # Arbitrary config values.
	    elsif ( $elt->{name} =~ /^(chordpro\..+)/ ) {
		my @k = split( /[.]/, $1 );
		my $cc = {};
		my $c = \$cc;
		foreach ( @k ) {
		    $c = \($$c->{$_});
		}
		$$c = $elt->{value};
		$config->augment($cc);
		upd_config();
	    }
	    next;
	}

	if ( $elt->{type} eq "control" ) {
	    if ( $elt->{name} =~ /^(\w+)-(size|color|font)/ ) {
		my $t = "{$1$2: " . $elt->{value} . "}";
		push( @s, $t ) unless $t =~ s/^\{\Kchorus/text/r eq $s[-1];
	    }
	    next;
	}

	if ( $elt->{type} eq "meta" ) {
	    if ( $elt->{key} eq "key" ) {
		next if $movable;
		push( @s, "{key: " . $elt->{value}->[-1] . "}" );
	    }
	    next;
	}

	if ( $elt->{type} eq "ignore" ) {
	    push( @s, $elt->{text} );
	    next;
	}

    }

    push(@s, "{end_of_$ctx}") if $ctx;

    my $did = 0;
    if ( $s->{chords} && @{ $s->{chords}->{chords} } && $variant ne 'msp' ) {
	for ( @{ $s->{chords}->{chords} } ) {
	    last unless $s->{chordsinfo}->{$_}->parser->has_diagrams;
	    push( @s, "" ) unless $did++;
	    push( @s, define( $s->{chordsinfo}->{$_}, 1 ) );
	}
    }

    # Process image assets.
    foreach ( sort { $imgs{$a} <=> $imgs{$b} } keys %imgs ) {
	my $url = $_;
	my $id = $imgs{$url};
	my $type = "jpg";
	$type = lc($1) if $url =~ /\.(\w+)$/;
	require MIME::Base64;
	require Image::Info;

	# Slurp the image.
	my $data = fs_blob($url);
	unless ( defined $data ) {
	    warn("$url: $!\n");
	    next;
	}

	# Get info.
	my $info = Image::Info::image_info(\$data);
	if ( $info->{error} ) {
	    do_warn($info->{error});
	    next;
	}

	# Write in-line data.
	push( @s,
	      sprintf( "##image: id=img%02d" .
		       " src=%s type=%s width=%d height=%d enc=base64",
		       $id, $url, $info->{file_ext},



( run in 0.636 second using v1.01-cache-2.11-cpan-5735350b133 )