简体   繁体   English

Redcarpet语法突出显示

[英]Redcarpet Syntax Highlighting

I'm trying to get Syntax Highlighting with Redcarpet working 我正试图通过Redcarpet工作来获得Syntax Highlighting

My appliaction_helper.rb : 我的appliaction_helper.rb

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

Although the output is displayed in a codeblock (redcarpet): 虽然输出显示在代码块(redcarpet)中:

在此输入图像描述

I can't find the Syntax Highlighting . 我找不到Syntax Highlighting

I just got into Redcarpet, someone know a solution for this? 我刚进入Redcarpet,有人知道这个解决方案吗?

Ok i found -> Markdown and code syntax highlighting in Ruby on Rails (using RedCarpet and CodeRay) which pretty much worked (together with some custom css for Coderay). 好吧,我发现 - > 在Ruby on Rails中使用Markdown和代码语法突出显示(使用RedCarpet和CodeRay) ,它们非常有效(与Coderay的一些自定义css一起)。

Gemfile: 的Gemfile:

gem 'redcarpet', github: 'vmg/redcarpet'
gem 'coderay'

Application_helper.rb Application_helper.rb

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

Here is a quick way to do it with Rouge : 以下是使用Rouge快速完成此操作的方法:

require 'redcarpet'
require 'rouge'
require 'rouge/plugins/redcarpet'

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

Of course this requires rouge to be in your Gemfile . 当然,这需要rouge要在你Gemfile

I don't think Redcarpet can do that. 我不认为Redcarpet可以做到这一点。 In my project I followed the Railscasts episode about Redcarpet which also tackles syntax highlighting. 在我的项目中,我关注了关于Redcarpet的Railscasts剧集,该剧集也解决了语法高亮问题。 It makes use of Pygments and Albino . 它使用了PygmentsAlbino

Link to the ASCIIcast version is here . 链接到ASCIIcast版本在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM