Rails redcarpet pygments 代码解析 markdown 为何没有解析出语言种类

ryan · 2013年06月13日 · 最后由 tsaikoga 回复于 2013年08月29日 · 2890 次阅读

我在 markdown 里面写了这样的文本 "```ruby puts 'hello world' ```"

然后帮助方法是:

def render_markdown(content)
    require 'redcarpet'
    renderer = PygmentizeHTML
    extensions = {
        fenced_code_blocks:true,
        autolink:true,
        hard_wrap:true,
        filter_html:true,
        no_intra_emphasis:true,
        gh_blockcode:true
    }
    redcarpet = Redcarpet::Markdown.new(renderer, extensions)
    redcarpet.render content.html_safe
end

class PygmentizeHTML < Redcarpet::Render::HTML
    def block_code(code, language)
        require 'pygmentize'
        Pygmentize.highlight(code, :lexer => language,
        :options => {:linenos => true}
        )
    end
end

视图中代码是:

=render inline:render_markdown(@content) unless @content.nil?

为何最后生成的 html 是这样的

<code>ruby
puts 'hello world'
</code>

ruby 为何没有解析进去呢。。。是我代码写错了还是 markdown 写错了。。

update 晕,解决了。原来是 hard_wrap 要写在 HTML 这个类的实例里面,renderer 必须要 new,Pygmentize 没有 highlight 方法,得用 process 方法。。。网上别人的博客太坑爹了。都是错误。。。

原来是这样子,感谢楼主。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号