[MT Plugin]全新改写的 Markdown 解析插件

一直在用 Markdown 写Blog。
总觉得貌似解析效率有点不高。何况引入 代码高亮 后,处理代码显示是个问题。

检查了 MT 自带的 Markdown 解析插件 ( plugins/Markdown/Markdown.pl ) , 原来是 John Gruber 2004 写的第一版。

My God , so oooold。。。

John Gruber 这个原代码写的很经典, 但是究竟很老了。。

而且现在 Cpan 上有现成的模块 Text::Markdown 可以使用。 干脆改写吧。。。

另外,改写的插件还是丢到 plugins/Easun 目录吧,方便升级。 直接删除 plugins/Markdown/ 目录, 在plugins/Easun下新建立 Markdown_md.pl 文件,全文如下:

Perl    #
    # Markdown -- A text-to-HTML conversion tool for web writers
    #
    # Copyright (c) 2004 John Gruber
    # <http://daringfireball.net/projects/markdown/>
    # Modify 2017 By EasunLee<https://easun.org>
    # <https://github.com/easunlee/MT-copy/blob/master/plugins/Easun/Markdown_md.pl>
    #

    package MT::Plugin::Easun::Markdown;
    use strict;
    use warnings;

    use vars qw($MYNAME $VERSION $DEBUG $Useproxy );
    $VERSION = "1.03e";


    #### Movable Type plug-in interface #####################################
    use MT::Plugin;

    my $plugin = new MT::Plugin(
    {   name=> "Markdown_Modify",
    author_name => "John Gruber",
    author_link => "http://daringfireball.net/",
    plugin_link => "http://daringfireball.net/projects/markdown/",
    version => $VERSION,
    description =>
    '<MT_TRANS phrase="A plain-text-to-HTML formatting plugin.">',
    doc_link => 'http://daringfireball.net/projects/markdown/',
    registry => {
    tags => {
    block => {
    MarkdownOptions => sub {
    my $ctx = shift;
    my $args= shift;
    my $builder = $ctx->stash('builder');
    my $tokens  = $ctx->stash('tokens');

    if ( defined( $args->{'output'} ) ) {
    $ctx->stash( 'markdown_output',
    lc $args->{'output'} );
    }

    defined( my $str = $builder->build( $ctx, $tokens ) )
    or return $ctx->error( $builder->errstr );
    $str;# return value
    },
    },
    },
    text_filters => {
    'markdown' => {
    label => 'Markdown_Modify',
    docs  => 'http://daringfireball.net/projects/markdown/',
    code  => sub {
    my $text = shift;
    my $ctx  = shift;
    my $raw  = 0;
    if ( defined $ctx ) {
    my $output = $ctx->stash('markdown_output');
    if ( defined $output && $output =~ m/^html/i ) {
    #$g_empty_element_suffix = ">";
    $ctx->stash( 'markdown_output', '' );
    }
    elsif ( defined $output && $output eq 'raw' ) {
    $raw = 1;
    $ctx->stash( 'markdown_output', '' );
    }
    else {
    $raw= 0;
      #  $g_empty_element_suffix = " />";
    }
    }
    $text = $raw ? $text : &_easunCode( $text );
    $text;
    },
    },


    },
    },
    }
    );
    MT->add_plugin($plugin);

    sub _easunCode {
    my $text = shift;
      &_DoCodeBlocks_github(\$text);

    require Text::Markdown;
    my $m = Text::Markdown->new;
    $text = $m->markdown($text);


    $text =~ s{<pre><code>```(.*?)\n}{<pre><b class=\"name\">$1<\/b><code class=\"language-$1\">}g; 
    $text =~ s/<pre><code>/<pre><b class=\"name\">code<\/b><code class=\"code\">/g;
    return $text;
    }

    #
    #  GitHub style Code
    #
    sub _DoCodeBlocks_github {

    my $text = shift; 
    $$text =~ s{\r\n}{\n}g;# DOS to Unix
    $$text =~ s{\r}{\n}g;  # Mac to Unix

     #$$text =~ s{(\n\n|\A)(```\w+)\n}{$1\t$2\n}g;

    $$text =~ s{
       (?:\n\n|\A)
       ```(\w+)\n
    (   # $2 = the code block -- one or more lines, starting with a space/tab
    .*?
      )
       \n```
       (?:\n|\Z)
       }{
    my $codeType = $1;
    my $codeblock = $2;
    my $result; # return value

    $codeblock =~ s/\A\n+//; # trim leading newlines
    $codeblock =~ s/\s+\z//; # trim trailing whitespace  
    $codeblock =~ s/\n/\n\t/g;   
    $result = "\n\n\t```" .$codeType ."\n\t" . $codeblock . "\n\n";
    $result;
    }segmx;

    }

    1;

更详细的插件信息可以见 GitHub 地址: https://github.com/easunlee/MT-copy/blob/master/plugins/Easun/Markdown_md.pl

当然,不要忘记安装 Text::Markdown 模块。

Bashcpanm Text::Markdown

保存,重启 mt-starman, 到此新的 Markdown 解析器替换完成。