校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > 在Ruby on Rails中使用Markdown的方法

在Ruby on Rails中使用Markdown的方法

熱門標簽:徐州網絡外呼系統哪個好 湖州u友防封電銷卡 地圖標注賺錢項目注冊 百度地圖標注自定義圖片 高德地圖標注客服 常德電銷平臺外呼系統軟件價格 白銀外呼paas系統 滴滴外呼系統 電銷機器人廠商代理

實現 Markdown 語法和代碼語法高亮分別是用的 Redcarpet 和 pygments.rb 兩個 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'

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

然后在/app/controllers/comments_controller.rb中添加相應 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 中就可以直接調用 markdown 方法來處理博客正文了:

%= markdown @post.content %>

語法規則類似 Github 上的 Markdown,碼字效率大大提高。

您可能感興趣的文章:
  • python 自動化將markdown文件轉成html文件的方法
  • 獻給寫作者的 Markdown 新手指南
  • 可能是最通俗的一篇介紹markdown的文章
  • 玩轉markdown 分享幾個需要用到的工具
  • Markdown+Bootstrap圖片自適應屬性詳解
  • Markdown與Bootstrap相結合實現圖片自適應屬性
  • 使用Python來開發Markdown腳本擴展的實例分享
  • 如何使用Gitblog和Markdown建自己的博客
  • Markdown語法備忘
  • markdown簡介和語法介紹
  • js驗證手機號碼

標簽:公主嶺 普洱 梧州 張家界 永州 遼寧 荊門 三沙

巨人網絡通訊聲明:本文標題《在Ruby on Rails中使用Markdown的方法》,本文關鍵詞  在,Ruby,Rails,中,使用,Markdown,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《在Ruby on Rails中使用Markdown的方法》相關的同類信息!
  • 本頁收集關于在Ruby on Rails中使用Markdown的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 莱西市| 凌云县| 洞口县| 娱乐| 休宁县| 湖州市| 甘泉县| 永川市| 赣榆县| 定结县| 青龙| 阿勒泰市| 丰镇市| 辉县市| 株洲县| 广丰县| 鄄城县| 崇州市| 于都县| 静宁县| 荔波县| 霍州市| 麟游县| 丹阳市| 灵台县| 芜湖县| 襄汾县| 谷城县| 青海省| 象山县| 五台县| 龙里县| 许昌县| 璧山县| 永昌县| 三门县| 辽宁省| 漳平市| 五莲县| 和硕县| 吉隆县|