Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在RubyonRails上,如何使用带有CodeRay的Markdown(rdisont/BlueCloth)进行语法高亮显示?_Ruby On Rails_Markdown_Bluecloth_Coderay_Rdiscount - Fatal编程技术网

Ruby on rails 在RubyonRails上,如何使用带有CodeRay的Markdown(rdisont/BlueCloth)进行语法高亮显示?

Ruby on rails 在RubyonRails上,如何使用带有CodeRay的Markdown(rdisont/BlueCloth)进行语法高亮显示?,ruby-on-rails,markdown,bluecloth,coderay,rdiscount,Ruby On Rails,Markdown,Bluecloth,Coderay,Rdiscount,我知道在CodeRay中使用织物(红布)有一个and,它是由助手完成的: module ApplicationHelper def coderay(text) text.gsub(/\<code( lang="(.+?)")?\>(.+?)\<\/code\>/m) do CodeRay.scan($3, $2).div(:css => :class) end end end 因此,它首先获得Co

我知道在CodeRay中使用织物(红布)有一个and,它是由助手完成的:

module ApplicationHelper    
  def coderay(text)  
    text.gsub(/\<code( lang="(.+?)")?\>(.+?)\<\/code\>/m) do  
      CodeRay.scan($3, $2).div(:css => :class)  
    end  
  end  
end  
因此,它首先获得CodeRay使用的
格式,然后基本上用CodeRay结果替换
/
*?
/m


这样做合适吗?但是如果我们在“4空格缩进”代码上实际有
,那么它实际上会阻塞这个处理,因为现在有嵌套的
,所以第一个
将得到匹配,跳过第二个
作为CodeRay的内容,然后匹配第一个
,并将第二个
挂在那里,不进行处理。如何做到这一点——也许CodeRay有一些降价选项?

有一个叫做gem的小宝石可以帮助实现这一点。但是我不想使用gem,所以我提取了核心功能,归结起来如下:

options = {
    :code_formatter => lambda {|code, lang|
        CodeRay.scan(CGI::unescapeHTML(code), lang).html.div
    }
}

text += "\n" unless (text.rindex("\n") == text.size - 1)
text.gsub!(/\r\n/, "\n")
output = ""

while match = text.match(/---\s*?([\w\s\._+()-]*?)\s*?\n(.*?)---\n/m)
    captures = match.captures
    code = captures[1]
    lang = captures[0].blank? ? nil : captures[0].downcase.strip.intern

    output += 
    options[:text_formatter][match.pre_match] +
    options[:code_formatter][code, lang]

    text = match.post_match
end

output += options[:text_formatter][text.chomp]
您可以通过使用“---”来封装代码,并可以选择添加语言,如“-ruby”,从而在纺织/降价中表示代码,如下所示:

---ruby
def hi
  print "hi"
end
---
与降价完美配合

options = {
    :code_formatter => lambda {|code, lang|
        CodeRay.scan(CGI::unescapeHTML(code), lang).html.div
    }
}

text += "\n" unless (text.rindex("\n") == text.size - 1)
text.gsub!(/\r\n/, "\n")
output = ""

while match = text.match(/---\s*?([\w\s\._+()-]*?)\s*?\n(.*?)---\n/m)
    captures = match.captures
    code = captures[1]
    lang = captures[0].blank? ? nil : captures[0].downcase.strip.intern

    output += 
    options[:text_formatter][match.pre_match] +
    options[:code_formatter][code, lang]

    text = match.post_match
end

output += options[:text_formatter][text.chomp]
---ruby
def hi
  print "hi"
end
---