plain text transforming with pandoc and markdown

Moin Moin,

actually we are writing a CouchDB book. Today I was starting to read the whole book and make some notes which todo’s we have. I was thinking how I write them down - means in which format. No question that it will be a plain text format anyway.

I decided to use markdown because it’s really simple and easy to transform. Yes transform because of three reasons:

  • actually I don’t know which format I need later
  • does transform it to HTML
  • it could be also transformed to LaTex

So how to transform the markdown. There is a really powerfull programm called pandoc written by John MacFarlane. pandoc is able to transform from various plain text formats to various other formats. E.g it’s easy to transform markdown to HTML.

As a Mac OS X user I was first looking in homebrew if there is a package - unfortunately there isn’t yet. So I had to use MacPorts. Be sure to update the ports tree because pandoc is changing rapidly. So use sudo port selfupdate, sudo port upgrade outdated and then sudo port install pandoc.

So after having installed pandoc the usecase is quite simple illustrated. I have a simple textfile called with some content. The first target is to create a html file. The creation process is as easy as this:

$ pandoc -s -t html -5 -f markdown -o TODO.html

The options are:

  • -s -> we want a complete html file with enclosing <html></html> tags and not only a html snippet.
  • -t html -> the output format is html
  • -5 -> use html5
  • -f markdown -> the input format is markdown
  • -o TODO.html -> the file to be written
  • as the last option is the input file

Well the result is really good. Try it yourself.

Well the next target is to create a PDF file. So therefor, a detour over LaTex is required. But don’t worry, it’s dead simple because pandoc is shipped with a program called markdown2pdf. Assuming you have LaTex installed, take these steps:

$ markdown2pdf -o TODO.pdf

Wow - the result is a PDF TODO.pdf. Cool isn’t it?



Published: March 07 2011