<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <title>Death of a Gremmie</title>
    <link>http://deathofagremmie.com</link>
    <description>Brian Neal's blog about programming.</description>
    <pubDate>Sat, 21 Jan 2012 05:35:56 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Blogofile, reStructuredText, and Pygments</title>
      <link>http://deathofagremmie.com/2011/04/17/blogofile-restructuredtext-and-pygments</link>
      <pubDate>Sun, 17 Apr 2011 19:15:00 CDT</pubDate>
      <category><![CDATA[Pygments]]></category>
      <category><![CDATA[Blogofile]]></category>
      <category><![CDATA[reStructuredText]]></category>
      <guid isPermaLink="true">http://deathofagremmie.com/2011/04/17/blogofile-restructuredtext-and-pygments</guid>
      <description>Blogofile, reStructuredText, and Pygments</description>
      <content:encoded><![CDATA[<div class="document">
<p><a class="reference external" href="http://blogofile.com">Blogofile</a> has support out-of-the-box for <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> and <a class="reference external" href="http://pygments.org/">Pygments</a>. Blogofile's
<tt class="docutils literal">syntax_highlight.py</tt> filter wants you to mark your code blocks with a token such as
<tt class="docutils literal">$$code(lang=python)</tt>. I wanted to use the method I am more familiar with, by configuring
reStructuredText with a <a class="reference external" href="http://pygments.org/docs/rstdirective/">custom directive</a>. Luckily this is very easy. Here is how I did it.</p>
<p>First of all, I checked what version of Pygments I had since I used Ubuntu's package
manager to install it. I then visited <a class="reference external" href="https://bitbucket.org/birkenfeld/pygments-main">Pygments on BitBucket</a>, and switched to the tag that matched
my version. I then drilled into the <tt class="docutils literal">external</tt> directory. I then saved the <tt class="docutils literal"><span class="pre">rst-directive.py</span></tt>
file to my blog's local repository under the name <tt class="docutils literal">_rst_directive.py</tt>. I named it with a leading
underscore so that Blogofile would ignore it. If this bothers you, you could also add it to
Blogofile's <tt class="docutils literal">site.file_ignore_patterns</tt> setting.</p>
<p>Next, I tweaked the settings in <tt class="docutils literal">_rst_directive.py</tt> by un-commenting the <tt class="docutils literal">linenos</tt> variant.</p>
<p>All we have to do now is to get Blogofile to import this module. This can be accomplished by making
use of the <a class="reference external" href="http://blogofile.com/documentation/config_file.html#pre-build">pre_build() hook</a> in your <tt class="docutils literal">_config.py</tt> file. This is a convenient place to hang
custom code that will run before your blog is built. I added the following code to my
<tt class="docutils literal">_config.py</tt> module</p>
<div class="highlight"><pre><span class="k">def</span> <span class="nf">pre_build</span><span class="p">():</span>
    <span class="c"># Register the Pygments Docutils directive</span>
    <span class="kn">import</span> <span class="nn">_rst_directive</span>
</pre></div>
<p>This allows me to embed code in my <tt class="docutils literal">.rst</tt> files with the <tt class="docutils literal">sourcecode</tt> directive. For example,
here is what I typed to create the source code snippet above:</p>
<pre class="literal-block">
.. sourcecode:: python

   def pre_build():
       # Register the Pygments Docutils directive
       import _rst_directive
</pre>
<p>Of course to get it to look nice, we'll need some CSS. I used this Pygments command to generate
a <tt class="docutils literal">.css</tt> file for the blog.</p>
<div class="highlight"><pre><span class="nv">$ </span>pygmentize -f html -S monokai -a .highlight &gt; pygments.css
</pre></div>
<p>I saved <tt class="docutils literal">pygments.css</tt> in my <tt class="docutils literal">css</tt> directory and updated my site template to link it in.
Blogofile will copy this file into my <tt class="docutils literal">_site</tt> directory when I build the blog.</p>
<p>Here is what I added to my blog's main <tt class="docutils literal">.css</tt> file to style the code snippets. The important thing
for me was to add an <tt class="docutils literal">overflow: auto;</tt> setting. This will ensure that a scrollbar will
appear on long lines instead of the code being truncated.</p>
<div class="highlight"><pre><span class="nc">.highlight</span> <span class="p">{</span>
   <span class="k">width</span><span class="o">:</span> <span class="m">96%</span><span class="p">;</span>
   <span class="k">padding</span><span class="o">:</span> <span class="m">0.5em</span> <span class="m">0.5em</span><span class="p">;</span>
   <span class="k">border</span><span class="o">:</span> <span class="m">1px</span> <span class="k">solid</span> <span class="m">#00ff00</span><span class="p">;</span>
   <span class="k">margin</span><span class="o">:</span> <span class="m">1.0em</span> <span class="k">auto</span><span class="p">;</span>
   <span class="k">overflow</span><span class="o">:</span> <span class="k">auto</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>That's it!</p>
</div>
]]></content:encoded>
    </item>
  </channel>
</rss>

