<?xml version="1.0" encoding="UTF-8"?>
<posts type="array">
  <post>
    <body>&lt;p&gt;As with the LaTeX problem, I wanted something that could integrate with what I was alreadying using.  Parsing the entire text twice was just not an option.  RedCloth was already identifying the &lt;code&gt;pre&lt;/code&gt; and &lt;code&gt;code&lt;/code&gt; blocks because it doesn&amp;#8217;t textilse those.  I just had to figure out how to intercept and then stylize them.  I came across &lt;a href=&quot;http://rpheath.com/&quot;&gt;Ryan Heath&amp;#8217;s blog&lt;/a&gt; and his code snippits instantly caught my eye.  I shot him an email and asked him what he was using to preform the syntax highlighting.  Turned out he was using the Ruby Syntax &lt;a href=&quot;http://syntax.rubyforge.org/&quot;&gt;gem&lt;/a&gt;.  Apparently, he tried out &lt;a href=&quot;http://ultraviolet.rubyforge.org/&quot;&gt;UltraViolet&lt;/a&gt;, but couldn&amp;#8217;t get it to run on his hosts&amp;#8217; server.  I think the UltraViolet gem was overkill for what I wanted to accomplish and that the Syntax gem was just what I needed &amp;#8212; &lt;em&gt;simple&lt;/em&gt; and &lt;em&gt;fast&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Now that I found the tool sytlizing code, I needed to intercept the &lt;code&gt;code&lt;/code&gt; blocks from RedCloth.  After a little bit of fishing around in the RedCloth &lt;a href=&quot;http://redcloth.rubyforge.org/&quot;&gt;documentation&lt;/a&gt;, I found the function I needed to override: &lt;strong&gt;rip_offtags&lt;/strong&gt;.  Before we can begin though, we have to install the Sytax gem.  Preform the following on the command line:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;&amp;gt; sudo gem install syntax&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The syntax gem contains a function called &lt;code&gt;convert&lt;/code&gt; which takes code and tranforms into HTML with each token of the code put into spans.  It is very simple to use.  The following is taken directly from its docs:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;require &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;syntax/convertors/html&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;

convertor = &lt;span class=&quot;co&quot;&gt;Syntax&lt;/span&gt;::&lt;span class=&quot;co&quot;&gt;Convertors&lt;/span&gt;::&lt;span class=&quot;co&quot;&gt;HTML&lt;/span&gt;.for_syntax &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ruby&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
html = convertor.convert( &lt;span class=&quot;co&quot;&gt;File&lt;/span&gt;.read( &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;program.rb&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; ) )

puts html&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;From the above, you can see that once we are able to intercept the code blocks from RedCloth, we just pass it onto the &lt;code&gt;convert&lt;/code&gt; function and we are done.  We are now ready to edit the &lt;strong&gt;washcloth.rb&lt;/strong&gt; file we created when making the LaTeX extension.  Add the following to the very begining of the file (before the class tag):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;require &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;syntax/convertors/html&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# for the syntax gem&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Next we need to copy the existing &lt;strong&gt;rip_offtags&lt;/strong&gt; defintion from &lt;strong&gt;redcloth.rb&lt;/strong&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;&lt;span class=&quot;c&quot;&gt;# File lib/redcloth.rb, line 1009&lt;/span&gt;

&lt;span class=&quot;co&quot;&gt;OFFTAGS&lt;/span&gt; = &lt;span class=&quot;rx&quot;&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;(code|pre|kbd|notextile)&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;co&quot;&gt;OFFTAG_MATCH&lt;/span&gt;  = &lt;span class=&quot;rx&quot;&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;(?:(&amp;lt;&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\/&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;&lt;span class=&quot;idl&quot;&gt;#{&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;OFFTAGS&lt;/span&gt; &lt;span class=&quot;idl&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;&amp;gt;)|(&amp;lt;&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;&lt;span class=&quot;idl&quot;&gt;#{&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;OFFTAGS&lt;/span&gt; &lt;span class=&quot;idl&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;[^&amp;gt;]*&amp;gt;))(.*?)(?=&amp;lt;&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;&lt;span class=&quot;idl&quot;&gt;#{&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;OFFTAGS&lt;/span&gt; &lt;span class=&quot;idl&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\Z&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mod&quot;&gt;mi&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;rip_offtags&lt;/span&gt;( text )
  &lt;span class=&quot;r&quot;&gt;if&lt;/span&gt; text =~ &lt;span class=&quot;rx&quot;&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;&amp;lt;.*&amp;gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# strip and encode pre content&lt;/span&gt;
    codepre, used_offtags = &lt;span class=&quot;i&quot;&gt;0&lt;/span&gt;, {}
    text.gsub!( &lt;span class=&quot;co&quot;&gt;OFFTAG_MATCH&lt;/span&gt; ) &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt; |line|
      &lt;span class=&quot;r&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;gv&quot;&gt;$3&lt;/span&gt;
        offtag, aftertag = &lt;span class=&quot;gv&quot;&gt;$4&lt;/span&gt;, &lt;span class=&quot;gv&quot;&gt;$5&lt;/span&gt;
        codepre += &lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;
        used_offtags[offtag] = &lt;span class=&quot;pc&quot;&gt;true&lt;/span&gt;
        &lt;span class=&quot;r&quot;&gt;if&lt;/span&gt; codepre - used_offtags.length &amp;gt; &lt;span class=&quot;i&quot;&gt;0&lt;/span&gt;
          htmlesc( line, &lt;span class=&quot;sy&quot;&gt;:NoQuotes&lt;/span&gt; ) &lt;span class=&quot;r&quot;&gt;unless&lt;/span&gt; used_offtags[&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;notextile&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;]
          &lt;span class=&quot;iv&quot;&gt;@pre_list&lt;/span&gt;.last &amp;lt;&amp;lt; line
          line = &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;r&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# this is the part that we're interested in&lt;/span&gt;
          htmlesc( aftertag, &lt;span class=&quot;sy&quot;&gt;:NoQuotes&lt;/span&gt; ) &lt;span class=&quot;r&quot;&gt;if&lt;/span&gt; aftertag &lt;span class=&quot;r&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;r&quot;&gt;not&lt;/span&gt; used_offtags[&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;notextile&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;]
          line = &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;&amp;lt;redpre#&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;&lt;span class=&quot;idl&quot;&gt;#{&lt;/span&gt; &lt;span class=&quot;iv&quot;&gt;@pre_list&lt;/span&gt;.length &lt;span class=&quot;idl&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
          &lt;span class=&quot;iv&quot;&gt;@pre_list&lt;/span&gt; &amp;lt;&amp;lt; &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;&lt;span class=&quot;idl&quot;&gt;#{&lt;/span&gt; &lt;span class=&quot;gv&quot;&gt;$3&lt;/span&gt; &lt;span class=&quot;idl&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;&lt;span class=&quot;idl&quot;&gt;#{&lt;/span&gt; aftertag &lt;span class=&quot;idl&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
      &lt;span class=&quot;r&quot;&gt;elsif&lt;/span&gt; &lt;span class=&quot;gv&quot;&gt;$1&lt;/span&gt; &lt;span class=&quot;r&quot;&gt;and&lt;/span&gt; codepre &amp;gt; &lt;span class=&quot;i&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;r&quot;&gt;if&lt;/span&gt; codepre - used_offtags.length &amp;gt; &lt;span class=&quot;i&quot;&gt;0&lt;/span&gt;
          htmlesc( line, &lt;span class=&quot;sy&quot;&gt;:NoQuotes&lt;/span&gt; ) &lt;span class=&quot;r&quot;&gt;unless&lt;/span&gt; used_offtags[&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;notextile&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;]
          &lt;span class=&quot;iv&quot;&gt;@pre_list&lt;/span&gt;.last &amp;lt;&amp;lt; line
          line = &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
        codepre -= &lt;span class=&quot;i&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;r&quot;&gt;unless&lt;/span&gt; codepre.zero?
        used_offtags = {} &lt;span class=&quot;r&quot;&gt;if&lt;/span&gt; codepre.zero?
      &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
      line
    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
  text
&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The code that were need to change is the following:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;htmlesc( aftertag, &lt;span class=&quot;sy&quot;&gt;:NoQuotes&lt;/span&gt; ) &lt;span class=&quot;r&quot;&gt;if&lt;/span&gt; aftertag &lt;span class=&quot;r&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;r&quot;&gt;not&lt;/span&gt; used_offtags[&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;notextile&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;]
line = &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;&amp;lt;redpre#&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;&lt;span class=&quot;idl&quot;&gt;#{&lt;/span&gt; &lt;span class=&quot;iv&quot;&gt;@pre_list&lt;/span&gt;.length &lt;span class=&quot;idl&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;iv&quot;&gt;@pre_list&lt;/span&gt; &amp;lt;&amp;lt; &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;&lt;span class=&quot;idl&quot;&gt;#{&lt;/span&gt; &lt;span class=&quot;gv&quot;&gt;$3&lt;/span&gt; &lt;span class=&quot;idl&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;&lt;span class=&quot;idl&quot;&gt;#{&lt;/span&gt; aftertag &lt;span class=&quot;idl&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The local variable, &lt;code&gt;aftertag&lt;/code&gt;, contains the unformatted code that we are interested in.  What is happening in that block, is RedCloth is passing it off to another function which escapes special characters.  Since the syntax gem will do that for us we can use it&amp;#8217;s convert function as a drop in replacement.  Chage the above code to the following:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;&lt;span class=&quot;r&quot;&gt;if&lt;/span&gt; aftertag &lt;span class=&quot;r&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;r&quot;&gt;not&lt;/span&gt; used_offtags[&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;notextile&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;]
    convertor = &lt;span class=&quot;co&quot;&gt;Syntax&lt;/span&gt;::&lt;span class=&quot;co&quot;&gt;Convertors&lt;/span&gt;::&lt;span class=&quot;co&quot;&gt;HTML&lt;/span&gt;.for_syntax &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ruby&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
    aftertag = convertor.convert(aftertag, &lt;span class=&quot;pc&quot;&gt;false&lt;/span&gt;)
&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;

line = &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;&amp;lt;redpre#&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;&lt;span class=&quot;idl&quot;&gt;#{&lt;/span&gt; &lt;span class=&quot;iv&quot;&gt;@pre_list&lt;/span&gt;.length &lt;span class=&quot;idl&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;iv&quot;&gt;@pre_list&lt;/span&gt; &amp;lt;&amp;lt; &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;&lt;span class=&quot;idl&quot;&gt;#{&lt;/span&gt; &lt;span class=&quot;gv&quot;&gt;$3&lt;/span&gt; &lt;span class=&quot;idl&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;&lt;span class=&quot;idl&quot;&gt;#{&lt;/span&gt; aftertag &lt;span class=&quot;idl&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Notice that my call to convert has a second parameter set to &lt;code&gt;false&lt;/code&gt;.  This is because we already have &lt;code&gt;pre&lt;/code&gt; and &lt;code&gt;code&lt;/code&gt; tags that RedCloth is going to insert back in for us.  Setting the second paramter to &lt;code&gt;false&lt;/code&gt; tells &lt;strong&gt;convert&lt;/strong&gt; to not add in &lt;code&gt;pre&lt;/code&gt; tags.&lt;/p&gt;
&lt;p&gt;The only thing left to do is write some custom CSS to tell the browser how we want to style our tokens.  &lt;a href=&quot;/stylesheets/code.css&quot;&gt;Here&lt;/a&gt; is a good starting point.  Just edit the colors to fit the scheme you have for your own site!&lt;/p&gt;
&lt;h4&gt;&lt;em&gt;&lt;strong&gt;Update&lt;/strong&gt;&lt;/em&gt;:&lt;/h4&gt;
&lt;p&gt;There is a bug in the current implementation.  For instance if you have a &lt;code&gt;pre&lt;/code&gt; tag inside your &lt;code&gt;code&lt;/code&gt; block, RedCloth will break it out and deal with it separately which then breaks your highlighting.  Once solution would be to create &lt;code&gt;[code]&lt;/code&gt; blocks like we did for LaTeX rendering, but I am not sure if I like that way.  Have to think about this.  If you have any good ideas, let me know and post it in the comments!&lt;/p&gt;      <script type="text/javascript">
      var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
      document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
      </script>
      <script type="text/javascript">
      var pageTracker = _gat._getTracker("UA-4701682-1");
      pageTracker._initData();
      pageTracker._trackPageview();
      </script>
</body>
    <created-at type="datetime">2008-06-18T00:59:03Z</created-at>
    <id type="integer">8</id>
    <introduction>&lt;p&gt;After I implemented LaTeX rendering into my views, I was quite satisfied with how it came out.  LaTeX can now be effortless inserted into a field that is textile enabled with very good results.  But then I began to look at the code snippits and I realized how ugly they look when compared to the rest of the site.  I did a quick web search for ways to incorporate syntax highlighting in Rails applications, but I wasn&amp;#8217;t really satisfied with what I found.  Then the solution smacked me in the face.  Just extend RedCloth!&lt;/p&gt;</introduction>
    <permalink>syntax-highlighting-with-redcloth</permalink>
    <title>Syntax Highlighting with RedCloth</title>
    <topic-id type="integer">1</topic-id>
    <updated-at type="datetime">2008-12-01T01:14:52Z</updated-at>
    <user-id type="integer">1</user-id>
    <version type="integer">54</version>
  </post>
</posts>
