r00tyfruit::Blog http://r00tyfruit.com/linkorama/categories Sat, 30 Aug 2008 17:49:54 GMT Teh Fruitful Bl0g! Syntax highlighting - Ultraviolet vs CodeRay http://r00tyfruit.com/blog/posts/2-syntax-highlighting-ultraviolet-vs-coderay <p>If you are like me you are easily allured by any type of programming eye candy and enjoy syntax highlighting a lot. My natural choices for stitching highlighting support to this wannabe blog were limited to these:</p> <ul> <li><a href="http://syntax.rubyforge.org/">Syntax</a></li> <li><a href="http://ultraviolet.rubyforge.org/">Ultraviolet</a></li> <li><a href="http://coderay.rubychan.de/">CodeRay</a></li> <li><a href="http://code.google.com/p/syntaxhighlighter/">SyntaxHighligher</a> and other on-the-fly JavaScript highlighters alike</li> </ul> <p>The Syntax gem was crossed out of this list as it provides very limited support for language schemes &#8211; Ruby only. It ain&#8217;t no good for me as I am all about using any effective tool at hand to get the job done &#8211; C++, bash, Perl, Python, C#, <a href="http://nmap.org/book/nse.html"><span class="caps">NSE</span></a> and probably some others.</p> <p>Although SyntaxHighligher seemed very yummy any sexily pretty it&#8217;s a bit like a duct-tape having other solutions at hand. It may easily be another interesting thing to try out here.</p> <p>Ultraviolet is incredible. The overall list of supported syntax schemes tops <a href="http://macromates.com/svn/Bundles/trunk/Bundles/">50</a>. It is capable of using themes from TextMate (which by itself makes me drool pretty easily). And then there is a very handy tm_syntax_highlighting plugin to simplify working with the gem even further.</p> <p>Alas&#8230; it pulls in a number of dependencies among which the ominous Oniguruma library. Compiling it is relatively easy and is limited to the usuals configure/make/make-install and you can find instructions <a href="http://snippets.aktagon.com/snippets/61-Installing-Ultraviolet-and-Oniguruma">here</a>.</p> <p>All would be good or great but the impenetrable obstacle I am currently struggling to overcome with <a href="http://www.dreamhost.com">Dreamhost</a> and <a href="http://www.modrails.com/" title="aka mod_rails">Phusion Passenger</a>. It seems that Passenger is for some reason unable to pick up any additional library paths through setting them in environment &#8211; so far I only tried setting LD_LIBRARY_PATH via <span class="caps">ENV</span> in environment.rb which fails in solving this. This leaves me in a very awkward state:</p> <p><code>libonig.so.2: cannot open shared object file: No such file or directory - /home/user/.gems/gems/oniguruma-1.1.0/lib/oregexp.so</code></p> <p>This probably has something to do with Passenger&#8217;s configuration at DreamHost and maybe there is a way to fix this but so far it does not seem to work out well. <a href="http://rpheath.com/posts/284-upgraded-syntax-highlighting">Ryan Heath</a> tried something similar here with similar results and switched back to using Syntax.</p> <p>Oh well. So this leaves me biting my elbows. But wait &#8211; there is CodeRay and it is actually not that bad. The set of supported language schemes is definitely quite limited. The fact that Ryan Bates of <a href="http://railscasts.com/">Railscasts</a> uses it got me enthusiastic and excited in no time. I gave CodeRay a try.</p> <p>The solution I have for now is quite basic &#8211; I use RedCloth to enable textile in all the content in my blog so I simply want to plug the highlighting into my textilize processing. The code below is pretty self-explanatory.</p> <div class='CodeRay'><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt>17<tt> </tt>18<tt> </tt>19<tt> </tt><strong>20</strong><tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="c"># in redclothext.rb</span><tt> </tt>require <span class="s"><span class="dl">'</span><span class="k">coderay</span><span class="dl">'</span></span><tt> </tt><tt> </tt><span class="r">module</span> <span class="cl">RedClothExt</span><tt> </tt> include <span class="co">WhiteListHelper</span><tt> </tt> <span class="r">def</span> <span class="fu">syntax</span>(text)<tt> </tt> text.gsub!(<span class="rx"><span class="dl">/</span><span class="k">&lt;pre(</span><span class="ch">\s</span><span class="k">*lang</span><span class="ch">\=</span><span class="k">[&quot;']?(</span><span class="ch">\w</span><span class="k">+)[&quot;']?[^&gt;]*)?&gt;(.*?)&lt;</span><span class="ch">\/</span><span class="k">pre&gt;</span><span class="dl">/</span><span class="mod">m</span></span>) <span class="r">do</span><tt> </tt> code = <span class="gv">$3</span>.nil? ? <span class="gv">$2</span> : <span class="gv">$3</span><tt> </tt> lang = <span class="gv">$2</span> <span class="r">unless</span> <span class="gv">$3</span>.nil?<tt> </tt> html = <span class="s"><span class="dl">&quot;</span><span class="k">&lt;notextile&gt;&lt;div class='CodeRay'&gt;</span><span class="dl">&quot;</span></span> + <tt> </tt> <span class="co">CodeRay</span>.scan(code, lang).html.numerize.div + <tt> </tt> <span class="s"><span class="dl">&quot;</span><span class="k">&lt;/div&gt;&lt;/notextile&gt;</span><span class="dl">&quot;</span></span><tt> </tt> <span class="r">end</span><tt> </tt> <span class="r">end</span><tt> </tt> <span class="r">def</span> <span class="fu">whiten</span>(text)<tt> </tt> white_list(text)<tt> </tt> <span class="r">end</span><tt> </tt><span class="r">end</span><tt> </tt><tt> </tt><span class="co">RedCloth</span>.send(<span class="sy">:include</span>, <span class="co">RedClothExt</span>)<tt> </tt></pre></td> </tr></table> </div><p>Few notes here. The regular expression I have concocted here allows having code snippets embraced in <code>pre</code> as well as having them start with <code>pre class="language-of-choice"</code>. This is all to allow having some minimal colorization for unsupported languages.</p> <p>I am using <a href="http://code.whytheluckystiff.net/redcloth/wiki/SuperRedCloth">RedCloth 4.0.3</a> which I believe has some changes in terms of how extensions should be implemented &#8211; I looked in the tests folder of the gem to grasp some basic understanding.</p> <p>Now in the application helper I have all the RedCloth processing goodness chained in the following sequence &#8211; texile, whiten, then syntax:</p> <div class='CodeRay'><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="c"># in appplication_helper.rb</span><tt> </tt><span class="r">module</span> <span class="cl">ApplicationHelper</span><tt> </tt> <span class="r">def</span> <span class="fu">secure_textilize</span>(text)<tt> </tt> <span class="co">RedCloth</span>.new(text).to_html(<span class="sy">:textile</span>, <span class="sy">:whiten</span>, <span class="sy">:syntax</span>)<tt> </tt> <span class="r">end</span><tt> </tt><span class="r">end</span><tt> </tt></pre></td> </tr></table> </div><p>You may have noticed I am using the <a href="http://weblog.techno-weenie.net/2006/9/3/white-listing-plugin-for-rails">white_list plugin</a> here. The reason the syntax method is after sanitize in this sequence &#8211; white_list would have obliterated the minimal JavaScript created by CodeRay. The code generated there is for the purpose of toggling the line numbers.</p> <p>Regarding the <span class="caps">CSS</span> &#8211; it&#8217;s a shameless mix of the one from Railscasts with some colors from UltraViolet&#8217;s <code>sunburst.css</code> for brighter glowing colors.</p> <p>That&#8217;s all for now&#8230; Hope this is useful for someone. There may be other better solutions &#8211; let me know!</p> Sat, 30 Aug 2008 17:49:54 GMT http://r00tyfruit.com/blog/posts/2-syntax-highlighting-ultraviolet-vs-coderay r00tyfruit (r00tyfruit) Why does this world need yet another blog? http://r00tyfruit.com/blog/posts/1-why-does-this-world-need-yet-another-blog <p>First, let me confess &#8211; I don&#8217;t know the answer. Although every attempt will be made to make this blog <em>useful</em> to some, less <em>annoying</em> to many, and simply fulfilling for myself I cannot guarantee that it will always be achieving these objectives at every single moment. One thing I can promise right now &#8212; it will contain the stuff I actually care about. This world is full of virtual nobodies so I am going to try really hard not to become one as much as possible&#8230; Now putting aside the ideas that every decent software professional has to be blogging about his work and practical endeavors I sincerely think that this place is more of a testing area (<em>do you feel tested?</em>) to generate and try out some of my ideas, have some dialog with nice and fine people and have fun in general.</p> <p>If we were looking for some absurd justifications here &#8211; I also hope that this blog will contribute to the overall information production nightmare, unleash the powers of the cosmic energy treadmill which will cause the super-intelligence to saturate the universe and have the Singularity come earlier (who wants to wait another 20-30 years?)</p> <p>Technically, I was following this guy&#8217;s advice <a href="http://nubyonrails.com/articles/about-this-blog-memcached">here</a> &#8211; it does provide a great learning experience. Indeed.</p> Sat, 23 Aug 2008 08:58:06 GMT http://r00tyfruit.com/blog/posts/1-why-does-this-world-need-yet-another-blog r00tyfruit (r00tyfruit)