<?xml version="1.0" encoding="UTF-8"?>
<feed
  xmlns="http://www.w3.org/2005/Atom"
  xmlns:thr="http://purl.org/syndication/thread/1.0"
  xml:lang="en"
   >
  <title type="text">Death of a Gremmie</title>
  <subtitle type="text">Brian Neal's blog about programming.</subtitle>

  <updated>2012-01-21T05:35:56Z</updated>
  <generator uri="http://blogofile.com/">Blogofile</generator>

  <link rel="alternate" type="text/html" href="http://deathofagremmie.com" />
  <id>http://deathofagremmie.com/feed/atom/</id>
  <link rel="self" type="application/atom+xml" href="http://deathofagremmie.com/feed/atom/" />
  <entry>
    <author>
      <name>Brian Neal</name>
      <uri>http://deathofagremmie.com</uri>
    </author>
    <title type="html"><![CDATA[Blogofile, reStructuredText, and Pygments]]></title>
    <link rel="alternate" type="text/html" href="http://deathofagremmie.com/2011/04/17/blogofile-restructuredtext-and-pygments" />
    <id>http://deathofagremmie.com/2011/04/17/blogofile-restructuredtext-and-pygments</id>
    <updated>2011-04-17T19:15:00Z</updated>
    <published>2011-04-17T19:15:00Z</published>
    <category scheme="http://deathofagremmie.com" term="Pygments" />
    <category scheme="http://deathofagremmie.com" term="Blogofile" />
    <category scheme="http://deathofagremmie.com" term="reStructuredText" />
    <summary type="html"><![CDATA[Blogofile, reStructuredText, and Pygments]]></summary>
    <content type="html" xml:base="http://deathofagremmie.com/2011/04/17/blogofile-restructuredtext-and-pygments"><![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>
  </entry>
</feed>

