bobtfish/text-markdown

Github flavoured markdown

Opened this issue · 1 comments

Hiya

I've written some code to support github flavoured markdown, if you want to add it to Text::Markdown:

http://github.github.com/github-flavored-markdown/

sub gfm{
    my $content = shift // '';
    my %extractions;

    # Extract pre blocks
    my $extract = sub {
        my $content = shift // '';
        my $md5 = md5_hex($content);
        $extractions{$md5} = $content;
        return "{gfm-extraction-$md5}";
    };

    $content =~ s{(<pre>.*?</pre>)}{$extract->($1)}ges;

    # prevent foo_bar_baz from ending up with an italic word in the middle
    my $underscores = sub {
        my $content = shift // '';
        $content =~ s/_/\\_/g
            if ( $content =~ tr/_// ) > 1;
        return $content;
    };

    $content =~ s/(^(?! {4}|\t)\w+_\w+_\w[\w_]*)/$underscores->($1)/gem;

    # in very clear cases, let newlines become <br /> tags
    my $brs = sub {
        my $content = shift // '';
        return $content if $content =~ /\n\n/;
        $content =~ s/^\s+//;
        $content =~ s/\s+$//;
        return "$content  \n";
    };

    $content =~ s/^([\w<][^\n]*\n+)/$brs->($1)/gexm;

    # Insert pre block extractions
    $content =~ s/\{gfm-extraction-([0-9a-f]{32})\}/$extractions{$1}/ge;

    return $content;
}

I think this probably belongs in Text::MultiMarkdown instead - see bobtfish/text-multimarkdown#16 for example.