Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your <code>render_body</code> method call the <code>coderay()</code> method before calling the <code>markdown()</code> method. Using the <code>markdown</code> method first was generating some extra html and this confused <code>CodeRay</code> causing bad output. </p> <p>My tests assumed you had the raw data that looked something like this in the markdown source</p> <pre><code>&lt;code lang="ruby"&gt; def function(param1, param2) puts param1 param2.each do |a| a.hello :world end end &lt;/code&gt; </code></pre> <p>Here's the complete class I used to test it. Note I didn't use <code>:css =&gt; :class</code> option because I didn't have the css to test it.</p> <pre><code>class Post &lt; 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(/\&lt;code( lang="(.+?)")?\&gt;(.+?)\&lt;\/code\&gt;/m) do CodeRay.scan($3, $2).div end end end </code></pre> <p>Your final output assuming the <code>:css =&gt; :class</code> option should now look something like this</p> <pre><code>&lt;div class="CodeRay"&gt; &lt;div class="code"&gt;&lt;pre&gt; &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;function&lt;/span&gt;(param1, param2) puts param1 param2.each &lt;span class="r"&gt;do&lt;/span&gt; |a| a.hello &lt;span class="sy"&gt;:world&lt;/span&gt; &lt;span class="r"&gt;end&lt;/span&gt; &lt;span class="r"&gt;end&lt;/span&gt; &lt;/pre&gt;&lt;/div&gt; &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload