gem 'redcarpet'
gem 'pygments.rb'
show.html.erb
<%= markdown @post.content %>
helpers/application_helper.rb
module ApplicationHelper
class HTMLwithPygments < Redcarpet::Render::HTML
def block_code(code, language)
Pygments.highlight(code, lexer: language)
end
end
def markdown(text)
renderer = HTMLwithPygments.new(filter_html: true, hard_wrap: true)
options = {
autolink: true,
no_intra_emphasis: true,
fenced_code_blocks: true,
lax_html_blocks: true,
strikethrough: true,
superscript: true
}
Redcarpet::Markdown.new(renderer, options).render(text).html_safe
end
end
assets/stylesheets/pygments.css.erb
<%= Pygments.css(:style => "default") %>
OK,这样就可以用markdown的语法来进入输入了,在显示的时候,会views会用helper中的markdown方法来调用Redcarpet::Markdown.new方法来渲染。
而pygments则是覆盖了Redcarpet::Markdown::HTML中的block_code方法,实现了语法高亮