首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >红地毯语法高亮

红地毯语法高亮
EN

Stack Overflow用户
提问于 2014-05-21 14:20:16
回答 3查看 4.6K关注 0票数 3

我试着用红地毯来突出语法

我的appliaction_helper.rb

代码语言:javascript
复制
module ApplicationHelper

  def markdown(text)
    render_options = {
        # will remove from the output HTML tags inputted by user
        filter_html:     true,
        # will insert <br /> tags in paragraphs where are newlines
        hard_wrap:       true,
        # hash for extra link options, for example 'nofollow'
        link_attributes: { rel: 'nofollow' },
        # Prettify
        prettify:        true
    }
    renderer = Redcarpet::Render::HTML.new(render_options)

    extensions = {
        #will parse links without need of enclosing them
        autolink:           true,
        # blocks delimited with 3 ` or ~ will be considered as code block.
        # No need to indent.  You can provide language name too.
        # ```ruby
        # block of code
        # ```
        fenced_code_blocks: true,
        # will ignore standard require for empty lines surrounding HTML blocks
        lax_spacing:        true,
        # will not generate emphasis inside of words, for example no_emph_no
        no_intra_emphasis:  true,
        # will parse strikethrough from ~~, for example: ~~bad~~
        strikethrough:      true,
        # will parse superscript after ^, you can wrap superscript in ()
        superscript:        true
        # will require a space after # in defining headers
        # space_after_headers: true
    }
    Redcarpet::Markdown.new(renderer, extensions).render(text).html_safe
  end

end

虽然输出显示在代码块(红地毯)中:

我找不到Syntax Highlighting了。

我刚进入红地毯,有人知道解决办法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-05-21 15:07:02

好的,我找到了-> Rails中的标记和代码语法突出显示(使用RedCarpet和CodeRay),它工作得很好(还有一些针对Coderay的定制css )。

Gemfile:

代码语言:javascript
复制
gem 'redcarpet', github: 'vmg/redcarpet'
gem 'coderay'

Application_helper.rb

代码语言:javascript
复制
class CodeRayify < Redcarpet::Render::HTML
  def block_code(code, language)
    CodeRay.scan(code, language).div
  end
end

def markdown(text)
  coderayified = CodeRayify.new(:filter_html => true, 
                                :hard_wrap => true)
  options = {
    :fenced_code_blocks => true,
    :no_intra_emphasis => true,
    :autolink => true,
    :strikethrough => true,
    :lax_html_blocks => true,
    :superscript => true
  }
  markdown_to_html = Redcarpet::Markdown.new(coderayified, options)
  markdown_to_html.render(text).html_safe
end
票数 9
EN

Stack Overflow用户

发布于 2015-11-18 11:42:22

下面是一种使用胭脂快速完成此操作的方法

代码语言:javascript
复制
require 'redcarpet'
require 'rouge'
require 'rouge/plugins/redcarpet'

class HTML < Redcarpet::Render::HTML
  include Rouge::Plugins::Redcarpet # yep, that's it.
end

当然,这需要rouge在您的Gemfile中。

票数 4
EN

Stack Overflow用户

发布于 2014-05-21 14:31:35

我不认为Redcarpet能做到这一点。在我的项目中,我关注了关于Redcarpet的Railscasts一集,它也处理语法突出显示问题。它利用了俾格蒂白化

链接到ASCIIcast版本的是这里

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23785970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档