在Rails 3应用程序中使用CodeRay和Markdown(RDiscount)突出显示语法 [英] Syntax highlighting with CodeRay and Markdown (RDiscount) in a Rails 3 application

查看:102
本文介绍了在Rails 3应用程序中使用CodeRay和Markdown(RDiscount)突出显示语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向当前使用RDiscount的博客添加一些语法突出显示.我将Markdown转换为具有RDiscount的HTML,然后使用CodeRay解析HTML代码块以添加语法突出显示.这是我到目前为止的内容:

I'm trying to add some syntax highlighting to my blog that currently uses RDiscount. I am converting the Markdown to HTML with RDiscount then parsing the HTML code blocks with CodeRay to add syntax highlighting. This is what I have so far:

class Post < ActiveRecord::Base
  before_save :render_body

  def render_body
    self.rendered_body = coderay(markdown(self.body))
  end

  def markdown(text)
    RDiscount.new(text).to_html
  end

  def coderay(text)
    text.gsub(/\<code( lang="(.+?)")?\>(.+?)\<\/code\>/m) do
      CodeRay.scan($3, $2).div(:css => :class)
    end
  end
end

在我看来:

<%= raw @post.rendered_body %>

使用此标记:

<code lang="ruby">
def function(param1, param2)
  puts param1
    param2.each do |a|
      a.hello :world
    end
end
</code>

结果是代码块被包装了两次.

The result is that the code blocks are being wrapped twice.

<pre>
<div class="CodeRay">
<div class="code">
<pre>
def function(param1, param2)
  puts param1
  param2.each do |a|
    a.hello :world
  end
end
</pre>
</div>
</div>
</pre>

我该怎么办?

推荐答案

在您的render_body方法中,先调用coderay()方法,然后再调用markdown()方法.首先使用markdown方法会生成一些额外的html,而这种混淆的CodeRay会导致错误的输出.

In your render_body method call the coderay() method before calling the markdown() method. Using the markdown method first was generating some extra html and this confused CodeRay causing bad output.

我的测试假设您的原始数据在markdown源中看起来像这样

My tests assumed you had the raw data that looked something like this in the markdown source

<code lang="ruby">
      def function(param1, param2)
        puts param1
          param2.each do |a|
            a.hello :world
          end
      end
</code>

这是我用来测试它的完整类.请注意,我没有使用:css => :class选项,因为我没有CSS对其进行测试.

Here's the complete class I used to test it. Note I didn't use :css => :class option because I didn't have the css to test it.

class Post < ActiveRecord::Base
  before_save :render_body

  def render_body
    self.rendered_body = markdown(coderay(self.body))
  end

  def markdown(text)
    RDiscount.new(text).to_html
  end

  def coderay(text)
    text.gsub(/\<code( lang="(.+?)")?\>(.+?)\<\/code\>/m) do
      CodeRay.scan($3, $2).div
   end
  end
end

假设使用:css => :class选项的最终输出现在应该像这样

Your final output assuming the :css => :class option should now look something like this

<div class="CodeRay"> 
  <div class="code"><pre> 
      <span class="r">def</span> <span class="fu">function</span>(param1, param2)
        puts param1
          param2.each <span class="r">do</span> |a|
            a.hello <span class="sy">:world</span> 
          <span class="r">end</span> 
      <span class="r">end</span> 
</pre></div> 
</div> 

这篇关于在Rails 3应用程序中使用CodeRay和Markdown(RDiscount)突出显示语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆