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的語法來進(jìn)入輸入了袁滥,在顯示的時候诱篷,會views會用helper中的markdown方法來調(diào)用Redcarpet::Markdown.new方法來渲染幻馁。
而pygments則是覆蓋了Redcarpet::Markdown::HTML中的block_code方法提鸟,實現(xiàn)了語法高亮