最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在Ruby on Rails中使用Markdown的方法

 更新時(shí)間:2015年08月01日 12:12:05   作者:libuchao  
這篇文章主要介紹了在Ruby on Rails中使用Markdown的方法,不過(guò)依賴于pygments.rb這個(gè)工具,事先得安裝Python,需要的朋友可以參考下

實(shí)現(xiàn) Markdown 語(yǔ)法和代碼語(yǔ)法高亮分別是用的 Redcarpet 和 pygments.rb 兩個(gè) Gem:

    https://github.com/vmg/redcarpet
    https://github.com/tmm1/pygments.rb
    https://github.com/richleland/pygments-css
    http://pygments.org/docs/lexers/

在/Gemfile中添加如下兩行:

gem 'redcarpet'
gem 'pygments.rb'

需要說(shuō)明的是,pygments.rb 依賴于 Python,所以確保機(jī)器已經(jīng)安裝了 Python 2.x。

然后在/app/controllers/comments_controller.rb中添加相應(yīng) redcarpet 和 pygments.rb 的代碼:

class ApplicationController < ActionController::Base
 # Prevent CSRF attacks by raising an exception.
 # For APIs, you may want to use :null_session instead.
 protect_from_forgery with: :exception

 helper_method [:markdown]

 # Highlight code with Pygments
 class HTMLwithPygments < Redcarpet::Render::HTML
  def block_code(code, language)
   language = "text" if language.blank?
   sha = Digest::SHA1.hexdigest(code)
   Rails.cache.fetch ["code", language, sha].join("-") do
    Pygments.highlight(code, :lexer => language)
   end
  end
 end

 protected

 # Markdown with Redcarpet
 def markdown(text)
  renderer = HTMLwithPygments.new({
   :filter_html => true,
   :hard_wrap => true,
   :link_attributes => {:rel => 'external nofollow'}
  })

  options = {
   :autolink => true,
   :no_intra_emphasis => true,
   :fenced_code_blocks => true,
   :lax_html_blocks => true,
   :strikethrough => true,
   :superscript => true,
   :tables => true
  }

  Redcarpet::Markdown.new(renderer, options).render(text).html_safe
 end
end

最后在 View 中就可以直接調(diào)用 markdown 方法來(lái)處理博客正文了:

<%= markdown @post.content %>

語(yǔ)法規(guī)則類似 Github 上的 Markdown,碼字效率大大提高。

相關(guān)文章

最新評(píng)論

榆树市| 司法| 志丹县| 砚山县| 宁海县| 泸水县| 鹤岗市| 沐川县| 甘洛县| 塘沽区| 响水县| 合山市| 蒙山县| 临夏县| 昌黎县| 琼中| 阿拉善盟| 庆云县| 秭归县| 镇安县| 大姚县| 定襄县| 三都| 勐海县| 田林县| 石台县| 无棣县| 赣榆县| 萍乡市| 嫩江县| 米易县| 广东省| 灵台县| 监利县| 襄樊市| 苏尼特左旗| 长沙市| 西林县| 天台县| 慈溪市| 盐山县|