Saturday, 16 April 2011

Bloggin' with pandoc and blogspot

17.4.2011, update 1: Changed the -fhighlight -> -fhighlighting. Thanks to Joni Freeman for spotting this.

Pandoc is a tool which allows conversion of multiple input markup formats (markdown, literal haskell) to various output formats (pdf, html). It is implemented using Haskell, and has (optional) support for syntax highlighting.

In this blog entry I'll show how

  • pandoc is set up,
  • how you can type up texts using Markdown syntax,
  • syntax highlighting works, and
  • what modifications you need to blogspot theme to support the syntax highlighting.

Let's start with pandoc.

Installing pandoc

Since pandoc is implemented in Haskell, the usual Haskell installation procedure applies. You need to have Haskell platform installed (hint: on Mac OS X, use brew). Cabal is the easiest way to install Haskell software once the base platform is installed, so install it from here.

Pandoc has optional support for syntax highlighting using highlighting-kate. It is not enabled by default, you have to add -fhighlighting flag to installation command:

    $ cabal install -fhighlighting pandoc

Test the pandoc command:

    $ pandoc -v

The command should produce following output (if the list of supported highlight languages is missing, the you are missing the highlighting support).

    pandoc 1.8.1.1
Compiled with citeproc support.
Compiled with syntax highlighting support for:
Actionscript, Ada, Alert, Alert_indent, Ansys, Apache, Asn1, Asp, Awk, Bash,
<...>

Writing blog texts

I use the Markdown syntax for writing up the texts. Markdown syntax is quite simple and elegant plain text syntax for writing lightly formatted text (such as blog texts).

Here's quick taster of Markdown syntax:

    # Title

This is the body text, with *highlight* and [an url](http://www.google.com).

* First item in list
* Second item
* Third item

Example of command call

~~~ {.bash}
echo "Foobar" > /dev/null
~~~

Note that I use some pandoc extensions here for command syntax highlighting. You can remove the tilde-lines to have everything in standard Markdown. (Hint: I use syntax .mediawiki to highlight these markup snippets).

Syntax highlighting

Pandoc has extended Markdown syntax with couple of useful additions. The most important for technical blogging purposes is the delimited code block extension. It allows marking up code blocks with tilde-characters, and adding language hint for syntax highlighting. Following example is from above Pandoc page:

    ~~~ {.haskell}
qsort [] = []
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++
qsort (filter (>= x) xs)
~~~

The markup produces following output:

    qsort []     = []
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++
qsort (filter (>= x) xs)

As a second example, here is some (contrived) Python code highlighted:

    import os

def current_path():
return os.getcwd()

if __name__ == "__main__":
print "Hello, %s" % os.path.abspath(current_path())

Putting the examples together, the typical text I write looks something like this in markup form:

    # Title

This is the body text, with *highlight* and [an url](http://www.google.com).

* First item in list
* Second item
* Third item

Example of command call

~~~ {.bash}
$ echo "Foobar" > /dev/null
~~~

## Code samples

Quicksort in Haskell:

~~~ {.haskell}
qsort [] = []
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++
qsort (filter (>= x) xs)
~~~

Contrived example of Python code:

~~~ {.python}
import os

def current_path():
return os.getcwd()

if __name__ == "__main__":
print "Hello, %s" % os.path.abspath(current_path())
~~~

I use Vim as an editor with syntax highlighting, so the markup is nicely colored.

Building output

Following command can be used to build stand-alone document, which contains header and footer elements:

    $ pandoc -s blogentry.md -o blogentry.html

The produced document can be opened in browser to see how the output is laid out.

If you are using pandoc as a helper for blogging to, for example, blogspot, then you can easily produce output that can be copy-pasted to blog editor there by omitting the -s flag:

    $ pandoc -s blogentry.md -o blogentry.html

I typically at this point open the html-file to Chrome using command

    $ open blogentry.html

and then copy and paste the HTML code to blogspot for preview. You may need to snip the first heading out, since the blog software usually provides it for you.

Modding blogspot style

The generated output generally works fine without any modifications, but syntax highlighting requires some extra CSS to be added to your blog template.

Fortunately, there are existing stylesheets for highlighting-kate module which pandoc uses, and they can be found from highlighting-kate Git repository.

The extra CSS must be copy-pasted to Blogger's template editor. First, open the template editor, find the Extra settings selection on topright (last item), then from the opened list select the "Add CSS". Paste the desired stylesheet here and save.

Final comments

I first planned to add quick instructions for using Google command line tools for automatic upload of generated comments, but this entry got fairly long so I'll write a new entry about that.

Meanwhile, hopefully you found this entry useful. You may also want to check out Pandoc documentation, Markdown syntax documentation and Hakyll (a site generator written in Haskell which uses Pandoc for markup handling).

6 comments:

  1. -fhighlight did not enable highlighting on my machine, but -fhighlighting did the trick.

    ReplyDelete
  2. Nice catch, thanks! I updated the text.

    ReplyDelete
  3. There seems to be an issue when compiling the required highlighting-kate library on Mac OSX. For me, the flag --disable-library-for-ghci helped. So, if you get

    ld: scattered reloc r_address too large

    Just try doing

    cabal install --disable-library-for-ghci -fhighlighting pandoc

    ReplyDelete
  4. Also, after intalling Pandoc, it wasn't in my PATH by default. So, you might want to add ~/.cabal/bin to your PATH.

    ReplyDelete
  5. Juha: Ah, thanks for pointing these out. I have had trouble with Mac OS X Lion and Haskell 7.0.x, so I haven't really tested pandoc installation of late.

    I noticed the tricks have also been added to Pandoc documentation (http://johnmacfarlane.net/pandoc/installing.html).

    ReplyDelete
  6. GHC 7.0.x, I mean (of course). GHC 7.2 (which has the fixes) is held back because no matching Haskell Platform release currently exists.

    ReplyDelete