<?xml version="1.0" encoding="UTF-8"?>
<posts type="array">
  <post>
    <body>&lt;p&gt;[latex]\LaTeX{}[/latex] is the permier typesetting tool of the engineering and math world.  It allows you to create beautifully typeset mathematical documents with a minimal learning curve.  So why would you care about this on the web?  Well for one, there really is no way to typset math in a browser.  Sure there is &lt;a href=&quot;http://www.w3.org/Math/&quot;&gt;MathML&lt;/a&gt; but that is supported by only a few browsers nativly, most will require a plugin (if even available).&lt;/p&gt;
&lt;p&gt;So how should one go about doing this, ensuring that the math formulas will be seen by the widest possible auidence?  Use images!  Every browser can render an image, so if we are able to take the LaTeX and render it as an image, we will be set.  This is exactly the approach taked by &lt;a href=&quot;http://www.mayer.dial.pipex.com/tex.htm&quot;&gt;LatexRender&lt;/a&gt;, a set of scripts for rendering LaTeX in &lt;a href=&quot;http://www.php.net/&quot;&gt;php&lt;/a&gt; programs.  Unfortunatly, as far as I know, there is not drop in solution for Ruby.&lt;/p&gt;
&lt;p&gt;However, there were a few projects that pointed me in the right direction.  Most noteable was redsymbols &lt;a href=&quot;http://redsymbol.net/software/l2p/&quot;&gt;l2p&lt;/a&gt; Perl script and the afformentioned LatexRender.  I have already done the hard stuff for you and put it up on my GitHub &lt;a href=&quot;http://github.com/mpetnuch&quot;&gt;repository&lt;/a&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;&lt;span class=&quot;gv&quot;&gt;$&amp;gt;&lt;/span&gt; git clone git&lt;span class=&quot;sy&quot;&gt;:/&lt;/span&gt;/github.com/mpetnuch/latex-renderer.git vendor/plugins/latex_render&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You also need to make sure that you have the RedCloth Ruby Gem and a LaTeX package for the &lt;a href=&quot;http://github.com/mpetnuch/latex-renderer/tree/master&quot;&gt;Latex Render plugin&lt;/a&gt; installed.  Next we need to write our own custom RedCloth.  I am going to call it &lt;strong&gt;Washcloth&lt;/strong&gt; since it is cleaning up the site.  Create a new file called &lt;strong&gt;washcloth.rb&lt;/strong&gt; in your projects &lt;strong&gt;lib&lt;/strong&gt; directory with the following contents:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;&lt;span class=&quot;r&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;cl&quot;&gt;Washcloth&lt;/span&gt; &amp;lt; &lt;span class=&quot;co&quot;&gt;RedCloth&lt;/span&gt;

  &lt;span class=&quot;co&quot;&gt;LATEX_START_CODE&lt;/span&gt;  = &lt;span class=&quot;rx&quot;&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;latex&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mod&quot;&gt;i&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;co&quot;&gt;LATEX_END_CODE&lt;/span&gt;    = &lt;span class=&quot;rx&quot;&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\[&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;latex&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mod&quot;&gt;i&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;co&quot;&gt;LATEX_RE&lt;/span&gt;          = &lt;span class=&quot;rx&quot;&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;latex&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;ch&quot;&gt;\[&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;latex&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mod&quot;&gt;i&lt;/span&gt;&lt;/span&gt;

  &lt;span class=&quot;co&quot;&gt;RULES&lt;/span&gt; = [&lt;span class=&quot;sy&quot;&gt;:block_textile_table&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:block_textile_lists&lt;/span&gt;,
           &lt;span class=&quot;sy&quot;&gt;:block_textile_prefix&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:inline_textile_image&lt;/span&gt;, 
           &lt;span class=&quot;sy&quot;&gt;:inline_latex&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:inline_textile_link&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:inline_textile_code&lt;/span&gt;, 
           &lt;span class=&quot;sy&quot;&gt;:inline_textile_span&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:glyphs_textile&lt;/span&gt;]

  &lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;inline_latex&lt;/span&gt;( text )

    &lt;span class=&quot;c&quot;&gt;# Don't do anything unless we have same number of start/end tags&lt;/span&gt;
    &lt;span class=&quot;r&quot;&gt;if&lt;/span&gt; text.scan(&lt;span class=&quot;co&quot;&gt;LATEX_START_CODE&lt;/span&gt;).size == text.scan(&lt;span class=&quot;co&quot;&gt;LATEX_END_CODE&lt;/span&gt;).size
      text.gsub!( &lt;span class=&quot;co&quot;&gt;LATEX_RE&lt;/span&gt; ) &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt; |m|
        latex = &lt;span class=&quot;co&quot;&gt;LatexRenderer&lt;/span&gt;.new
        latex.formula = &lt;span class=&quot;gv&quot;&gt;$1&lt;/span&gt;
        &lt;span class=&quot;r&quot;&gt;begin&lt;/span&gt;
          filename = latex.process
          md5 = latex.md5hash
          &lt;span class=&quot;c&quot;&gt;# if the processing was successful we just display the rendered image&lt;/span&gt;
          sprintf &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;&amp;lt;img src=&amp;quot;%s&amp;quot; alt=&amp;quot;latex image&amp;quot; /&amp;gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, 
                         filename.gsub(&lt;span class=&quot;rx&quot;&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;.*?&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;images&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;/images&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;)
        &lt;span class=&quot;r&quot;&gt;rescue&lt;/span&gt; =&amp;gt; e
          sprintf(&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, latex.formula)
        &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
      &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;to_html&lt;/span&gt;
    &lt;span class=&quot;r&quot;&gt;super&lt;/span&gt;(*&lt;span class=&quot;co&quot;&gt;RULES&lt;/span&gt;)
  &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Let&amp;#8217;s test it out.  Fire up the console:&lt;br /&gt;
 &lt;br /&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;&amp;gt;&amp;gt; latex = &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;[latex]x^2 + y^2 = z^2[/latex]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
=&amp;gt; &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;[latex]x^2 + y^2 = z^2[/latex]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&amp;gt;&amp;gt; r = &lt;span class=&quot;co&quot;&gt;WashCloth&lt;/span&gt;.new latex
=&amp;gt; &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;[latex]x^2 + y^2 = z^2[/latex]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&amp;gt;&amp;gt; r.to_html
=&amp;gt; &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;p&amp;gt;&amp;lt;img src=&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;/images/latex/image.png&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;gt;&amp;lt;/p&amp;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;
&lt;p&gt;So we see that our [latex]\LaTeX{}[/latex] is replaced with a link to a rendered image of the code.  All that is left is to replace any instance of RedCloth in your program with WashCloth and it will automatically render the LaTeX in your views.  What if you use something like Err the Blog&amp;#8217;s &lt;a href=&quot;http://errtheblog.com/posts/12-actsastextiled&quot;&gt;acts_as_textiled&lt;/a&gt;?  You going to have to edit the plugins source and change RedCloth to WashCloth, but that&amp;#8217;s it! So what have we accomplished?  We can now render latex commands inline with our Textiled HTML.  For instance, the following command:&lt;/p&gt;
&lt;pre&gt;
[latex]
$$\Gamma(s) \zeta(s) =
  \int_0^{\infty} \frac{x^{s-1}e^{-x}}{1-e^{-x}}\,dx$$
[/latex]
&lt;/pre&gt;&lt;p&gt;Will render as: [latex]$$\Gamma(s) \zeta(s) = \int_0^{\infty} \frac{x^{s-1}e^{-x}}{1-e^{-x}}\,dx$$[/latex]&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-16T03:37:49Z</created-at>
    <id type="integer">7</id>
    <introduction>&lt;p&gt;&lt;a href=&quot;http://whytheluckystiff.net/ruby/redcloth/&quot;&gt;RedCloth&lt;/a&gt; is Ruby&amp;#8217;s Textile module.  Textile is a lightweight markup language originally developed by Dean Allen that allows a user to create well-formed styled XHTML.  This site employes the RedCloth Textile module through the &lt;a href=&quot;http://errtheblog.com/posts/12-actsastextiled&quot;&gt;acts_as_textiled&lt;/a&gt; plugin.  The nice thing about RedCloth is it is faily easy to extend the markup which it recognies.  In this tutorial, I am going to show you how to render [latex]\LaTeX{}[/latex] in your site by extending RedCloth.&lt;/p&gt;</introduction>
    <permalink>extending-redcloth</permalink>
    <title>Extending RedCloth</title>
    <topic-id type="integer">1</topic-id>
    <updated-at type="datetime">2008-12-01T00:15:21Z</updated-at>
    <user-id type="integer">1</user-id>
    <version type="integer">48</version>
  </post>
  <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>
