Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Github flavoured markdown #15

Open
clintongormley opened this issue Jul 30, 2011 · 1 comment
Open

Github flavoured markdown #15

clintongormley opened this issue Jul 30, 2011 · 1 comment

Comments

@clintongormley
Copy link

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;
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants