<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged markdown</title>
    <link href="https://passingcuriosity.com/tags/markdown/markdown.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/markdown/markdown.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2008-12-08T00:00:00Z</updated>
    <entry>
    <title>Literate Haskell with Markdown and Syntax Highlighting</title>
    <link href="https://passingcuriosity.com/2008/literate-haskell-with-markdown-syntax-hightlighting/" />
    <id>https://passingcuriosity.com/2008/literate-haskell-with-markdown-syntax-hightlighting/</id>
    <published>2008-12-08T00:00:00Z</published>
    <updated>2008-12-08T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>There are two schools of thought when it comes to documenting programs and
libraries: some embed fragments of code within the documentation (so called
“<em>literate</em> programming”) and others embed fragments of documentation in their
code (i.e. comments). The “comments” approach makes it easy to generate API
documentation and the like (a feature <a href="http://haskell.org/haddock/" title="Haddock: A Haskell Documentation Tool">built-in to Haskell’s Hackage
system</a>) but help me write blog posts and other documents containing
code, which is where literate programming shines. Happily, Haskell supports
both of these approaches and has a few rather useful tools available to make
both easier. In this post, I’ll describe how to take literate Haskell with
Markdown formatted text and produce syntax highlighted documents in HTML and
PDF.</p>
<p>The basic idea of literate programming is that my document is a sequence of
code “chunks” and documentation “chunks”. Haskell tools can ignore the
documentation chunks when compiling the code and documentation tools can
ignore or, hopefully, prettyify the code when preparing the document for
publishing. The <a href="http://www.haskell.org/onlinereport/literate.html" title="Haskell 98 Report -- 9.6 Literate comments">Haskell 98 report</a> describes two ways to denote
code chunks in a literate Haskell file: the TeX way and with “Bird tracks”. In
the TeX way, each block of code is wrapped with TeX macros:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode latex"><code class="sourceCode latex"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">\begin</span>{<span class="ex">code</span>}</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>-- Some Haskell</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="kw">\end</span>{<span class="ex">code</span>}</span></code></pre></div>
<p>whereas with “Bird tracks” each code line begins with a <code>&gt;</code>:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode literatehaskell"><code class="sourceCode literatehaskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ot">&gt;</span> <span class="co">-- Some Haskell</span></span></code></pre></div>
<p>TeX style code chunks are excellent when my document is a TeX (common for
academic papers), and “Bird tracks” are more convenient when I’m writing
pretty much anything else (such as blog posts).</p>
<p><a href="http://daringfireball.net/projects/markdown/syntax">Markdown</a> is a simple plain text syntax created by <a href="http://daringfireball.com/">John
Gruber</a> designed to resemble the conventions of
e-mail so the “Bird tracks” style of literate Haskell is a good fit. It’s
reasonably simply to learn so I won’t bother to say any more about Markdown
itself.</p>
<p>Once I’ve got some literate Haskell all documented in Markdown I’m ready to
produce a document. The first step is to add syntax highlighting to the code
chunks using <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/hscolour" title="The hscolour package on Hackage">hscolour</a>. <code>hscolour</code> can generate its output in several
formats, but we’ll use HTML with CSS styles. I’m a UNIX person, so I usually
use <code>hscolour</code> as a filter:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>    <span class="fu">cat</span> blah.lhs <span class="kw">|</span> <span class="ex">hscolour</span> <span class="at">-lit</span> <span class="at">-css</span> <span class="op">&gt;</span> blah.mkd</span></code></pre></div>
<p>This pipeline takes <code>blah.lhs</code>, performs syntax highlighting on it
(interpreting it as literate Haskell), generates HTML for use with CSS and
stores the result in <code>blah.mkd</code>. I’ve now got a file with with literate chunks
untouched (they’re still Markdown) and the code chunks converted into HTML.
The next step is to convert this Markdown+HTML file into “pure” HTML for
publishing.</p>
<p>There are a few ways to convert Markdown into HTML: use the <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/pandoc" title="The Pandoc package on Hackage">pandoc</a> Haskell
program, use the original
<a href="http://daringfireball.net/projects/markdown/"><code>markdown</code></a> perl program, rely
on your blogging software (many packages support <a href="http://michelf.com/projects/php-markdown/">PHP
Markdown</a>), etc. <code>pandoc</code> is a
Haskell program, very fast, and can generate more than just HTML output, so I
use it quite a bit. Again, I tend to use <code>pandoc</code> as a filter:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>    <span class="fu">cat</span> blah.mkd <span class="kw">|</span> <span class="ex">pandoc</span> <span class="at">-sS</span> <span class="at">--no-wrap</span> <span class="at">-c</span> hscolour.css <span class="op">&gt;</span> blah.html</span></code></pre></div>
<p>This pipeline takes <code>blah.mkd</code>, translates it from Markdown to HTML (both
options are the implicit defaults) and puts the result into <code>blah.html</code>. It
also uses fancy typography (<code>-S</code>), creates a complete document (‘-s’),
includes a link to the <code>hscolour.css</code> stylesheet, and does <em>not</em> wrap the
code. This last point is essential if the document contains <code>hscolour</code> output
– if it <em>does</em> wrap the code, then the highlighted Haskell code will be
displayed incorrectly.</p>
<p>I often combine both steps into a single pipeline:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span> blah.lhs <span class="kw">|</span> <span class="ex">hscolour</span> <span class="at">-lit</span> <span class="at">-css</span> <span class="kw">|</span> <span class="ex">pandoc</span> <span class="at">--no-wrap</span> <span class="at">-sS</span> <span class="at">-c</span> hscolour.css <span class="dt">\</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="op">&gt;</span> blah.html</span></code></pre></div>
<p>Generating a PDF by way of LaTeX is simple – just change the output format
for both <code>hscolour</code> and <code>pandoc</code>:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span> blah.lhs <span class="kw">|</span> <span class="ex">hscolour</span> <span class="at">-lit</span> <span class="at">-latex</span> <span class="kw">|</span> <span class="ex">pandoc</span> <span class="at">--no-wrap</span> <span class="at">-sS</span> <span class="at">-t</span> latex <span class="dt">\</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="op">&gt;</span> blah.tex</span></code></pre></div>
<p>and then do whatever you normally do to get a PDF:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="ex">pdflatex</span> blah.tex <span class="kw">&amp;&amp;</span> <span class="ex">pdflatex</span> blah.tex <span class="kw">&amp;&amp;</span> <span class="ex">pdflatex</span> blah.tex </span></code></pre></div>
<p>But I usually cram all of this into a <code>Makefile</code>:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode makefile"><code class="sourceCode makefile"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="ot">.SUFFIXES:</span><span class="dt"> .lhs .mkd .html .tex .pdf</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="dt">PANDOC</span> <span class="ch">:=</span><span class="st"> pandoc --no-wrap -sS</span></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a><span class="dt">HSCOLOUR</span> <span class="ch">:=</span><span class="st"> hscolour -lit</span></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a><span class="ot">.lhs.mkd:</span></span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a><span class="er">    </span>cat <span class="ch">$&lt;</span> | <span class="ch">$(</span><span class="dt">HSCOLOUR</span><span class="ch">)</span> -css &gt; <span class="ch">$@</span></span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a><span class="ot">.lhs.html:</span></span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a><span class="er">    </span>cat <span class="ch">$&lt;</span> | <span class="ch">$(</span><span class="dt">HSCOLOUR</span><span class="ch">)</span> -css | <span class="ch">$(</span><span class="dt">PANDOC</span><span class="ch">)</span> -t html -c hscolour.css &gt; <span class="ch">$@</span></span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a><span class="ot">.lhs.tex:</span></span>
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a><span class="er">    </span>cat <span class="ch">$&lt;</span> | <span class="ch">$(</span><span class="dt">HSCOLOUR</span><span class="ch">)</span> -latex | <span class="ch">$(</span><span class="dt">PANDOC</span><span class="ch">)</span> -t latex&gt; <span class="ch">$@</span></span>
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a><span class="ot">.tex.pdf:</span></span>
<span id="cb8-16"><a href="#cb8-16" aria-hidden="true" tabindex="-1"></a><span class="er">    </span>pdflatex <span class="ch">$&lt;</span> &amp;&amp; pdflatex <span class="ch">$&lt;</span> &amp;&amp; pdflatex <span class="ch">$&lt;</span></span></code></pre></div>
<p>And call <code>make</code> to sort it all out for me:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="fu">make</span> blah.html</span></code></pre></div>]]></summary>
</entry>

</feed>
