App-Music-ChordPro

 view release on metacpan or  search on metacpan

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

$line_routines{line_default} = \&line_default;

sub chord {
    my ( $c ) = @_;
    return "" unless length($c);
    return $c->key if $c->info->is_annotation;
    $text_layout->set_markup($c->chord_display);
    return $text_layout->render;
}

sub md_textline{
	my ( $songline ) = @_;
	my $empty = $songline;
    my $textline = $songline;
    my $nbsp = "\x{00A0}"; #unicode for nbsp sign
    if($empty =~ /^\s+/){ # starts with spaces
	    $empty =~ s/^(\s+).*$/$1/; # not the elegant solution - but working - replace all spaces in the beginning of a line
        my $replaces = $empty;  #with a nbsp symbol as the intend tend to be intentional
        $replaces =~ s/\s/$nbsp/g;
        $textline =~ s/$empty/$replaces/;
    }
	$textline = $textline."  "; # append two spaces to force linebreak in Markdown
	return $textline;
}

sub line_songline {
    my ( $elt ) = @_;
    my $t_line = "";
    my @phrases = map { $text_layout->set_markup($_); $text_layout->render }
      @{ $elt->{phrases} };

    if ( $lyrics_only or
	   $single_space && ! ( $elt->{chords} && join( "", map { $_->raw } @{ $elt->{chords} } ) =~ /\S/ )
       ) {
	$t_line = join( "", @phrases );
	return md_textline($cp.$t_line);
    }

    unless ( $elt->{chords} ) { # i guess we have a line with no chords now... 
	   return ($cp.  md_textline( join( " ", @phrases )) );
    }
 	
	if ( my $f = $::config->{settings}->{'inline-chords'} ) {
	$f = '[%s]' unless $f =~ /^[^%]*\%s[^%]*$/;
	$f .= '%s';
	foreach ( 0..$#{$elt->{chords}} ) {
	    $t_line .= sprintf( $f,
				chord( $elt->{chords}->[$_]->raw ),
				$phrases[$_] );
	}
	return ( md_textline($cp.$t_line) );
    }

    my $c_line = "";
    foreach ( 0..$#{$elt->{chords}} ) {
		$c_line .= chord( $elt->{chords}->[$_] ) . " ";
		$t_line .= $phrases[$_];
		my $d = length($c_line) - length($t_line);
		$t_line .= "-" x $d if $d > 0;
		$c_line .= " " x -$d if $d < 0;
    } # this looks like setting the chords above the words.

    s/\s+$// for ( $t_line, $c_line );

	# main problem in markdown - a fixed position is only available in "Code escapes" so weather to set
	# a tab or a double backticks (``)  - i tend to the tab - so all lines with tabs are "together"
	if ($c_line ne ""){ # Block-lines are not replacing initial spaces - as the are "code"
		$t_line = $cp.$t_line."  ";
		$c_line = $cp.$c_line."  ";
		}
	else{
		$t_line = md_textline($cp.$t_line);
	}
	return $chords_under
		? ( $t_line, $c_line )
		: ( $c_line, $t_line );
}
$line_routines{line_songline} = \&line_songline;

sub line_newpage {
    return "---------------  \n";
}
$line_routines{line_newpage} = \&line_newpage;

sub line_empty {
    return "$cp";
}
$line_routines{line_empty} = \&line_empty;

sub line_comment {
    my ( $elt ) = @_; # Template for comment?
	my @s;
	my $text = $elt->{text};
	if ( $elt->{chords} ) {
		$text = "";
		for ( 0..$#{ $elt->{chords} } ) {
		    $text .= "[" . $elt->{chords}->[$_]->raw . "]"
		      if $elt->{chords}->[$_] ne "";
		    $text .= $elt->{phrases}->[$_];
	}}
	if ($elt->{type} =~ /italic$/) {
			$text = "*" . $text . "*  ";
		}
	push(@s, "> $text  ");	
    return @s;
}
$line_routines{line_comment} = \&line_comment;

sub line_comment_italic {
    my ( $lineobject ) = @_; # Template for comment?
    return "> *". $lineobject->{text} ."*";;
}
$line_routines{line_comment_italic} = \&line_comment_italic;


sub line_image {
    my ( $elt ) = @_;
	return "![](".$elt->{uri}.")";
}
$line_routines{line_image} = \&line_image;



( run in 2.134 seconds using v1.01-cache-2.11-cpan-8f98c5d2c55 )