Skip to content
This repository

Github flavoured markdown #15

Open
clintongormley opened this Issue · 1 comment

2 participants

Clinton Gormley Mike Doherty
Clinton Gormley

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;
}
Mike Doherty doherty referenced this issue in bobtfish/text-multimarkdown
Open

Add option to insert <br>s at linebreaks #16

Mike Doherty

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

Mike Doherty doherty referenced this issue from a commit in doherty/text-multimarkdown
Mike Doherty doherty Add option break_at_newlines to be more like Gihub-flavoured Markdown
This implements newline handling somewhat like the description of
Github-flavoured Markdown, described at http://github.github.com/github-flavored-markdown/

Normal markdown (and, until now, Text::MultiMarkdown) allows you
to hard-wrap text. Markdown will combine those lines into a single
paragraph of text. With break_at_newlines, Text::MultiMarkdown will
treat newlines in your text blocks as real line breaks.

The markdown

    one
    two

was once rendered as

    <p>one
    two</p>

which will be one paragraph in your browser - but with break_at_newlines,
will now be:

    <p>one <br />
    two  </p>

which will be two paragraphs in your browser.

The default for break_at_newlines is off.

This resolves GH #15.

Thanks to Clinton Gormley for coding this up - see bobtfish/text-markdown#15
a574622
Mike Doherty doherty referenced this issue from a commit in doherty/text-multimarkdown
Mike Doherty doherty Add option break_at_newlines to be more like Gihub-flavoured Markdown
This implements newline handling somewhat like the description of
Github-flavoured Markdown, described at http://github.github.com/github-flavored-markdown/

Normal markdown (and, until now, Text::MultiMarkdown) allows you
to hard-wrap text. Markdown will combine those lines into a single
paragraph of text. With break_at_newlines, Text::MultiMarkdown will
treat newlines in your text blocks as real line breaks.

The markdown

    one
    two

was once rendered as

    <p>one
    two</p>

which will be one paragraph in your browser - but with break_at_newlines,
will now be:

    <p>one <br />
    two  </p>

which will be two paragraphs in your browser.

The default for break_at_newlines is off.

This resolves GH #16.

Thanks to Clinton Gormley for coding this up - see bobtfish/text-markdown#15
480bcdb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.