<?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>
  <post>
    <body>&lt;p&gt;According to A List Apart&amp;#8217;s Derek Powazek&amp;#8217;s &lt;a href=&quot;http://alistapart.com/articles/wheream&quot;&gt;article&lt;/a&gt;, the hallmarks of a good navigation system lets the user know the three basic things: where they are, where they have been, and where they can go.  While there are many methods to achieve this, I decided to go the &lt;a href=&quot;http://en.wikipedia.org/wiki/Breadcrumb_%28navigation%29&quot;&gt;breadcrumb&lt;/a&gt; route.&lt;/p&gt;
&lt;p&gt;While breadcrumbs are great, there is no easy was to make them with Rails.  The problem is that the breadcrumb trail may span across more than one controller.  So how do you model the relationships?  Use  ActsAsNestedSet.  The ActsAsNestedSet plugin allows you to have tree like relationships and retrieve the entire tree with just one SQL query.&lt;/p&gt;
&lt;p&gt;First we need to get the plugin:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;git clone git&lt;span class=&quot;sy&quot;&gt;:/&lt;/span&gt;/github.com/chris/better_nested_set.git&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;and create our menu model:&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;Menu&lt;/span&gt; &amp;lt; &lt;span class=&quot;co&quot;&gt;ActiveRecord&lt;/span&gt;::&lt;span class=&quot;co&quot;&gt;Base&lt;/span&gt;
  acts_as_nested_set

  after_save &lt;span class=&quot;sy&quot;&gt;:move_to_proposed_parent&lt;/span&gt;

  &lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;proposed_parent_id&lt;/span&gt;
    &lt;span class=&quot;iv&quot;&gt;@proposed_parent_id&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;proposed_parent_id=&lt;/span&gt;(parent)
    &lt;span class=&quot;iv&quot;&gt;@proposed_parent_id&lt;/span&gt; = parent
  &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;

  private

  &lt;span class=&quot;c&quot;&gt;# After the menu is saved, we move it to the correct location&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# If the proposed parent is nil, or doesn't exist then move it to the end of the list.&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# If the proposed parent has children - move it to the right of the last child&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# If the proposed parent has no children, move it directly under the parent&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;move_to_proposed_parent&lt;/span&gt;
    &lt;span class=&quot;r&quot;&gt;unless&lt;/span&gt; proposed_parent_id.to_s == parent_id.to_s
      new_parent = &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;.class.find_by_id(proposed_parent_id)

      &lt;span class=&quot;r&quot;&gt;if&lt;/span&gt; new_parent.nil?
        &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;.move_to_right_of(&lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;.class.roots.last)
      &lt;span class=&quot;r&quot;&gt;else&lt;/span&gt;
        &lt;span class=&quot;r&quot;&gt;unless&lt;/span&gt; new_parent.children.last.nil?
          &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;.move_to_right_of(new_parent.children.last)
        &lt;span class=&quot;r&quot;&gt;else&lt;/span&gt;
          &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;.move_to_child_of(new_parent)
        &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;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;and finally our migration for the table that will contain our data.  Note, in our migration we need to make sure that we create the base menus for whatever model we plan on listing.  In this example I create a base menu for the models &lt;code&gt;home&lt;/code&gt;, &lt;code&gt;post&lt;/code&gt;, and &lt;code&gt;topics&lt;/code&gt;.  My migration file looks like this.&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;CreateMenus&lt;/span&gt; &amp;lt; &lt;span class=&quot;co&quot;&gt;ActiveRecord&lt;/span&gt;::&lt;span class=&quot;co&quot;&gt;Migration&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;.up
    create_table &lt;span class=&quot;sy&quot;&gt;:menus&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:force&lt;/span&gt; =&amp;gt; &lt;span class=&quot;pc&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt; |t|
      t.string  &lt;span class=&quot;sy&quot;&gt;:name&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:controller&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:action&lt;/span&gt;
      t.references &lt;span class=&quot;sy&quot;&gt;:menuable&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:polymorphic&lt;/span&gt; =&amp;gt; &lt;span class=&quot;pc&quot;&gt;true&lt;/span&gt;
      t.integer &lt;span class=&quot;sy&quot;&gt;:parent_id&lt;/span&gt;
      t.integer &lt;span class=&quot;sy&quot;&gt;:lft&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:rgt&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:null&lt;/span&gt; =&amp;gt; &lt;span class=&quot;pc&quot;&gt;false&lt;/span&gt;
      t.integer &lt;span class=&quot;sy&quot;&gt;:lock_version&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:default&lt;/span&gt; =&amp;gt; &lt;span class=&quot;i&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;

    add_index &lt;span class=&quot;sy&quot;&gt;:menus&lt;/span&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;name&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&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;parent_id&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;], 
              &lt;span class=&quot;sy&quot;&gt;:name&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;index_menu_on_name_and_parent_id&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, 
              &lt;span class=&quot;sy&quot;&gt;:unique&lt;/span&gt; =&amp;gt; &lt;span class=&quot;pc&quot;&gt;false&lt;/span&gt;

    &lt;span class=&quot;c&quot;&gt;# add base menus &lt;/span&gt;
    home = &lt;span class=&quot;co&quot;&gt;Menu&lt;/span&gt;.create &lt;span class=&quot;sy&quot;&gt;:name&lt;/span&gt; =&amp;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;Home&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;,
                                          &lt;span class=&quot;sy&quot;&gt;:controller&lt;/span&gt; =&amp;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;home&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, 
                                          &lt;span class=&quot;sy&quot;&gt;:action&lt;/span&gt; =&amp;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;index&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;

    posts = &lt;span class=&quot;co&quot;&gt;Menu&lt;/span&gt;.create &lt;span class=&quot;sy&quot;&gt;:name&lt;/span&gt; =&amp;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;Posts&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;,
                                          &lt;span class=&quot;sy&quot;&gt;:controller&lt;/span&gt; =&amp;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;posts&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, 
                                          &lt;span class=&quot;sy&quot;&gt;:action&lt;/span&gt; =&amp;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;index&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;,
                                          &lt;span class=&quot;sy&quot;&gt;:proposed_parent_id&lt;/span&gt; =&amp;gt; home

    topics = &lt;span class=&quot;co&quot;&gt;Menu&lt;/span&gt;.create &lt;span class=&quot;sy&quot;&gt;:name&lt;/span&gt; =&amp;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;Topics&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;,
                                          &lt;span class=&quot;sy&quot;&gt;:controller&lt;/span&gt; =&amp;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;topics&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, 
                                          &lt;span class=&quot;sy&quot;&gt;:action&lt;/span&gt; =&amp;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;index&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;,
                                          &lt;span class=&quot;sy&quot;&gt;:proposed_parent_id&lt;/span&gt; =&amp;gt; posts

  &lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;.down
    drop_table &lt;span class=&quot;sy&quot;&gt;:menus&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;The &lt;code&gt;menu&lt;/code&gt; model has  pseudo column, &lt;code&gt;proposed_parent_id&lt;/code&gt;.   This allows us to use an &lt;code&gt;after_save&lt;/code&gt; callback, which  automatically creates the new menu item as a child of the correct parent.  If &lt;code&gt;proposed_parent_id&lt;/code&gt; is blank, then the new menu item will become a root menu.&lt;/p&gt;
&lt;p&gt;Now that we have the menu table set up, we need to update our models so that it populates the menu table with the newly created information.  In each model that you wish to associate a menu item with add the following (I am assuming the model named Topic):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;has_one &lt;span class=&quot;sy&quot;&gt;:menu&lt;/span&gt;,  &lt;span class=&quot;sy&quot;&gt;:as&lt;/span&gt; =&amp;gt; &lt;span class=&quot;sy&quot;&gt;:menuable&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:dependent&lt;/span&gt; =&amp;gt; &lt;span class=&quot;sy&quot;&gt;:destroy&lt;/span&gt;
after_create &lt;span class=&quot;sy&quot;&gt;:create_menu&lt;/span&gt;

private

&lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;create_menu&lt;/span&gt;  parent_menu = &lt;span class=&quot;co&quot;&gt;Menu&lt;/span&gt;.find(
    &lt;span class=&quot;sy&quot;&gt;:first&lt;/span&gt;,
    &lt;span class=&quot;sy&quot;&gt;:conditions&lt;/span&gt; =&amp;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;controller = ? and action = ?&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;topics&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;index&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;]
  )

  new_menu = &lt;span class=&quot;co&quot;&gt;Menu&lt;/span&gt;.new(
    &lt;span class=&quot;sy&quot;&gt;:name&lt;/span&gt; =&amp;gt; &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;.name.titleize,
    &lt;span class=&quot;sy&quot;&gt;:controller&lt;/span&gt; =&amp;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;topics&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;,
    &lt;span class=&quot;sy&quot;&gt;:action&lt;/span&gt; =&amp;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;show&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;,
    &lt;span class=&quot;sy&quot;&gt;:proposed_parent_id&lt;/span&gt; =&amp;gt; parent_menu
  )

  &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;.menu = new_menu

&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now depending upon how static this model is, you might want to create an &lt;code&gt;after_code&lt;/code&gt; callback that would update the menu name if, for instance, the topic name is a mutable attribute.  After you have everything to you liking, it&amp;#8217;s time to migrate your database:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;rake db&lt;span class=&quot;sy&quot;&gt;:migrate&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Next we create a helper method so that we can access the current menu. We are exploting the fact that Rails sets for us the name of the current controller and action via the global &lt;code&gt;controller&lt;/code&gt; variable. We are then able to set the correct submenu page (if appropriate) if we are passed an &lt;code&gt;id&lt;/code&gt; as well.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;&lt;span class=&quot;r&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;cl&quot;&gt;MenuHelper&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;current_menu&lt;/span&gt;

    id = params[&lt;span class=&quot;sy&quot;&gt;:id&lt;/span&gt;]

    &lt;span class=&quot;r&quot;&gt;unless&lt;/span&gt; id.nil?
      &lt;span class=&quot;co&quot;&gt;Menu&lt;/span&gt;.find(&lt;span class=&quot;sy&quot;&gt;:first&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:conditions&lt;/span&gt; =&amp;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;controller = ? and action = ? and menuable_id = ?&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, controller.controller_name, controller.action_name, id ] )
    &lt;span class=&quot;r&quot;&gt;else&lt;/span&gt;
      &lt;span class=&quot;co&quot;&gt;Menu&lt;/span&gt;.find(&lt;span class=&quot;sy&quot;&gt;:first&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:conditions&lt;/span&gt; =&amp;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;controller = ? and action = ?&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, controller.controller_name, controller.action_name ] )
    &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;/code&gt;&lt;/pre&gt;&lt;p&gt;With that setup, all that is left is for us to create the view to see our navigation.  This is were we get to see the power of &lt;code&gt;ActsAsNestedSet&lt;/code&gt;.  In order to retrieve all of the root menus (in our case the children of Home) you create the following loop:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;&lt;span class=&quot;co&quot;&gt;Menu&lt;/span&gt;.root.children.each &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt; |menu|
  &lt;span class=&quot;c&quot;&gt;#  Do something special with the current selected menu&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;unless&lt;/span&gt; menu == current_menu.self_and_ancestors[&lt;span class=&quot;i&quot;&gt;1&lt;/span&gt;]
    ....
   &lt;span class=&quot;c&quot;&gt;# Standard formatting for the rest&lt;/span&gt;
  else 
    ....
  end
&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Notice that I use NestedSet&amp;#8217;s &lt;code&gt;self_and_ancestors&lt;/code&gt; method on the &lt;code&gt;current_menu&lt;/code&gt;, but instead of checking the first item in the array I am actually checking the second.  The reason for this is that is &lt;code&gt;home&lt;/code&gt; is the parent of every item (according to how I setup my menus) and is therefore the first item in the array.  If, on the other hand, you decide that you want your Posts, About, etc&amp;#8230; to be the topic level menus along with home you would use &lt;code&gt;self_and_ancestors.first&lt;/code&gt; instead.&lt;/p&gt;
&lt;p&gt;Now to retrieve the breadcrumbs we do something quite similar.   Again we use NestedSet&amp;#8217;s &lt;code&gt;anestor&lt;/code&gt; method to get all the breadcrumbs.  Notice however, that we do not use &lt;code&gt;self_and_ancestors&lt;/code&gt; because we want to treat the current menu differently, for example, we want to provide links to all the ancestors.  But since we are viewing the current page there is no reason to link to it.  Also, notice that we omit breadcrumbs if were are at the home page.  This we done as a suggestion from &lt;a href=&quot;http://www.alistapart.com&quot;&gt;A List Apart&lt;/a&gt;, but the choice is up to you.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Don't display breadcrumbs if we are in the home menu&lt;/span&gt;
&lt;span class=&quot;r&quot;&gt;unless&lt;/span&gt; current_menu.root?
  current_menu.ancestors.each &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt; |menu|
    ...
  &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;# Do something with current_menu&lt;/span&gt;
  ...

&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;All that is left is for you to decide on how you want to style your menus.  &lt;a href=&quot;http://www.webdesignpractices.com&quot;&gt;Web Design Practices&lt;/a&gt; presents a good overview of some &lt;a href=&quot;http://www.webdesignpractices.com/navigation/breadcrumb.html&quot;&gt;more popular setups&lt;/a&gt;.&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-26T02:54:44Z</created-at>
    <id type="integer">9</id>
    <introduction>&lt;p&gt;Deciding upon a navigation system is probably one of the most important choices in website design.  The navigation system can make or break a sites usability.  Because of the importance a good navigation system plays, I choose to ignore the many available Rails menu plugins and roll out my own so that it would work exactly as I needed it.  In this tutorial, I will detail how to use the Rails plugin &lt;a href=&quot;http://github.com/rails/acts_as_nested_set/tree/master&quot;&gt;acts_as_nested_set&lt;/a&gt; to create your own dynamic navigation system.&lt;/p&gt;</introduction>
    <permalink>creating-a-navigation-system-with-rails</permalink>
    <title>Creating a Navigation System with Rails</title>
    <topic-id type="integer">1</topic-id>
    <updated-at type="datetime">2009-03-06T17:11:41Z</updated-at>
    <user-id type="integer">1</user-id>
    <version type="integer">29</version>
  </post>
</posts>
