<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Wolfmans Howlings: Howto format ruby code for blogs</title>
    <link>http://blog.wolfman.com/articles/2006/05/26/howto-format-ruby-code-for-blogs</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>A programmers Blog about Ruby, Rails and a few other issues</description>
    <item>
      <title>Howto format ruby code for blogs</title>
      <description>&lt;p&gt;How do I get that nice formatted ruby code inline?&lt;/p&gt;

&lt;p&gt;Well if you are on typo trunk use this&amp;#8230;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;div class="typocode"&amp;gt;&amp;lt;pre&amp;gt;&amp;lt;code class="typocode_ruby "&amp;gt;       &amp;lt;span class="punct"&amp;gt;...&amp;lt;/span&amp;gt;&amp;lt;span class="ident"&amp;gt;ruby&amp;lt;/span&amp;gt; &amp;lt;span class="ident"&amp;gt;code&amp;lt;/span&amp;gt;&amp;lt;span class="punct"&amp;gt;...&amp;lt;/span&amp;gt;
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you are not on typo trunk (which I am not yet) you can do the following&amp;#8230;&lt;/p&gt;

&lt;p&gt;I had to search around for this so I thought I&amp;#8217;d put the recipe here.&lt;/p&gt;

&lt;p&gt;I got most of my information from this
&lt;a href="http://www.carldr.com/blog/article/3/ruby-syntax-highlighting"&gt;site&lt;/a&gt;,
you can also use &lt;a href="http://syntax.carldr.com"&gt;this site&lt;/a&gt; to convert the
code for you, but I found that more cumbersome than what I do below.&lt;/p&gt;

&lt;p&gt;Basically all the hard work is done by the syntax gem install it as&amp;#8230;&lt;/p&gt;

&lt;div class="terminal"&gt;
&gt; gem install syntax
&lt;/div&gt;

&lt;p&gt;Then I use this little script called code2html.rb which converts the code in the clipboard and puts it back in the clipbboard&amp;#8230;&lt;/p&gt;

&lt;pre&gt;&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;rio&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;rubygems&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;syntax/convertors/html&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;

&lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="constant"&gt;ARGV&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;size&lt;/span&gt; &lt;span class="punct"&gt;&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;0&lt;/span&gt;
    &lt;span class="ident"&gt;code&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;File&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;read&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;ARGV&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;])&lt;/span&gt;
&lt;span class="keyword"&gt;else&lt;/span&gt;
    &lt;span class="ident"&gt;code&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt; `&lt;span class="ident"&gt;dcop&lt;/span&gt; &lt;span class="ident"&gt;klipper&lt;/span&gt; &lt;span class="ident"&gt;klipper&lt;/span&gt; &lt;span class="ident"&gt;getClipboardContents`&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;span class="ident"&gt;convertor&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;Syntax&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Convertors&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;HTML&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;for_syntax&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;ruby&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="attribute"&gt;@code_html&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;convertor&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;convert&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt; &lt;span class="ident"&gt;code&lt;/span&gt; &lt;span class="punct"&gt;)&lt;/span&gt;

&lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="attribute"&gt;@code_html&lt;/span&gt;

&lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="constant"&gt;ARGV&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;size&lt;/span&gt; &lt;span class="punct"&gt;&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;0&lt;/span&gt;
    &lt;span class="ident"&gt;fn&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;&lt;span class="expr"&gt;#{File.basename(ARGV[0], File.extname(ARGV[0]))}&lt;/span&gt;.html&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
    &lt;span class="ident"&gt;rio&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;fn&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="attribute"&gt;@code_html&lt;/span&gt;
&lt;span class="keyword"&gt;else&lt;/span&gt;
    &lt;span class="comment"&gt;# put the results back on the clipboard, NB this may fail if there are shell specific characters&lt;/span&gt;
    &lt;span class="ident"&gt;system&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;dcop klipper klipper setClipboardContents &lt;span class="escape"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="expr"&gt;#{@code_html}&lt;/span&gt;&lt;span class="escape"&gt;\&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;)&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;/pre&gt;

&lt;p&gt;The clipboard stuff is kind of kde specific.&lt;/p&gt;

&lt;p&gt;Alternatively you can specify a filename on the command line, and it
will convert that file and put the results in a file with .html as the extension.
(Note this requires the rio gem).&lt;/p&gt;

&lt;p&gt;You will need this CSS available to your web page to render it nicely.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pre {
    background-color: #f1f1f3;
    color: #112;
    padding: 10px;
    font-size: 1.1em;
    overflow: auto;
    margin: 4px 0px;
          width: 95%;
}



/* Syntax highlighting */
pre .normal {}
pre .comment { color: #005; font-style: italic; }
pre .keyword { color: #A00; font-weight: bold; }
pre .method { color: #077; }
pre .class { color: #074; }
pre .module { color: #050; }
pre .punct { color: #447; font-weight: bold; }
pre .symbol { color: #099; }
pre .string { color: #944; background: #FFE; }
pre .char { color: #F07; }
pre .ident { color: #004; }
pre .constant { color: #07F; }
pre .regex { color: #B66; background: #FEF; }
pre .number { color: #F99; }
pre .attribute { color: #5bb; }
pre .global { color: #7FB; }
pre .expr { color: #227; }
pre .escape { color: #277; }
&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Fri, 26 May 2006 00:14:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:0d03c376940926bb644bb94acf0fc3eb</guid>
      <author>Jim Morris</author>
      <link>http://blog.wolfman.com/articles/2006/05/26/howto-format-ruby-code-for-blogs</link>
      <category>Ruby</category>
      <category>ruby</category>
      <category>syntax</category>
      <category>highlighting</category>
      <trackback:ping>http://blog.wolfman.com/articles/trackback/17</trackback:ping>
    </item>
    <item>
      <title>"Howto format ruby code for blogs" by aidy_lewis</title>
      <description>&lt;p&gt;meant to say I could just use the code tag&lt;/p&gt;</description>
      <pubDate>Tue, 15 Apr 2008 04:19:33 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:fa7e900f-47c7-4dec-a7ce-d7907b0fe1c7</guid>
      <link>http://blog.wolfman.com/articles/2006/05/26/howto-format-ruby-code-for-blogs#comment-213</link>
    </item>
    <item>
      <title>"Howto format ruby code for blogs" by aidy_lewis</title>
      <description>&lt;p&gt;It doesn't seem that the ruby publishing code, syntax highlights. I could just use
&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;</description>
      <pubDate>Mon, 14 Apr 2008 05:54:53 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:1bc1287f-b48d-4dff-b73c-0ec53c5043cc</guid>
      <link>http://blog.wolfman.com/articles/2006/05/26/howto-format-ruby-code-for-blogs#comment-212</link>
    </item>
    <item>
      <title>"Howto format ruby code for blogs" by ritu sodhi</title>
      <description>&lt;p&gt;syntax/convertors/html  The error comes that its unable to find convertor&lt;/p&gt;</description>
      <pubDate>Sun, 09 Mar 2008 03:26:15 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:dfe9c4aa-3d9d-4359-8ac6-aaf849e4406f</guid>
      <link>http://blog.wolfman.com/articles/2006/05/26/howto-format-ruby-code-for-blogs#comment-206</link>
    </item>
    <item>
      <title>"Howto format ruby code for blogs" by Brent</title>
      <description>&lt;p&gt;Thanks, this is really helpful.  I had been wondering how people get their Ruby code to display so nicely on blogs.  I've posted code on my blog at &lt;a href="http://brentrubyrails.blogspot.com/2007/12/formatting-ruby-and-html-code-for-blog.html" rel="nofollow"&gt;http://brentrubyrails.blogspot.com/2007/12/formatting-ruby-and-html-code-for-blog.html&lt;/a&gt; that uses Syntax for Ruby and HTML code, and copies it to the Windows clipboard.&lt;/p&gt;</description>
      <pubDate>Sun, 02 Dec 2007 20:05:56 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:1ae0b89c-581a-4deb-a6ad-0eaf3fdc57e0</guid>
      <link>http://blog.wolfman.com/articles/2006/05/26/howto-format-ruby-code-for-blogs#comment-174</link>
    </item>
    <item>
      <title>"Howto format ruby code for blogs" by Romulo A. Ceccon</title>
      <description>&lt;p&gt;Something might have happened to &lt;a href="http://www.carldr.com" rel="nofollow"&gt;www.carldr.com&lt;/a&gt;. Neither the blog entry nor the convert tool is there anymore.&lt;/p&gt;

&lt;p&gt;Thanks for the tips, by the way.&lt;/p&gt;</description>
      <pubDate>Thu, 03 May 2007 05:35:14 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:5f89758a-abc8-4ed9-b2a7-0a250eb330a9</guid>
      <link>http://blog.wolfman.com/articles/2006/05/26/howto-format-ruby-code-for-blogs#comment-66</link>
    </item>
    <item>
      <title>"Howto format ruby code for blogs" by madcoderspeak.blogspot.com</title>
      <description>&lt;p&gt;Thanks a lot for this post..
My blog ate a lot of my ruby code... had to use your script to make sure my blog doesn't do it again :D&lt;/p&gt;

&lt;p&gt;Cool post this one!&lt;/p&gt;</description>
      <pubDate>Mon, 19 Feb 2007 10:38:28 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:50b1c6f1-2f10-4381-9a06-abd4939f2030</guid>
      <link>http://blog.wolfman.com/articles/2006/05/26/howto-format-ruby-code-for-blogs#comment-58</link>
    </item>
    <item>
      <title>"Howto format ruby code for blogs" by rio4ruby@rubyforge.org</title>
      <description>&lt;p&gt;I like your use of Rio here. It illustrates well the reason for Rio's existence -- to make common I/O operations simple, so that one could spend their time writing code that does something worthwhile.  With your approval, I would like to use this example in Rio's documentation -- with a couple of changes that illustrate some of Rio's functionality besides its copy operator:&lt;/p&gt;

&lt;p&gt;code= File.read(ARGV[0])
could be replaced with
  code = rio(ARGV[0]).read&lt;/p&gt;

&lt;p&gt;and&lt;/p&gt;

&lt;p&gt;fn= "#{File.basename(ARGV[0], File.extname(ARGV[0]))}.html"
   rio(fn) &amp;lt;&amp;lt; @code_html&lt;/p&gt;

&lt;p&gt;could be replaced with&lt;/p&gt;

&lt;p&gt;rio(ARGV[0]).basename + '.html' &amp;lt; @code_html&lt;/p&gt;

&lt;p&gt;Cheers, 
-Christopher&lt;/p&gt;</description>
      <pubDate>Thu, 28 Sep 2006 23:31:46 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:31827b08-e59d-48a2-8ecb-b005ceeb9d5b</guid>
      <link>http://blog.wolfman.com/articles/2006/05/26/howto-format-ruby-code-for-blogs#comment-38</link>
    </item>
  </channel>
</rss>
