Up until recently I was taking all of my personal notes in dokuwiki format, because that’s what we use at my job for our internal wiki. It’s convenient to be able to copy notes straight into the wiki without having to convert.
Sometimes, however, I would want to print my notes. Since I’ve always been a fan of the beautiful output of LaTeX, and I wanted to work on my sed and awk chops, I wrote a utility to convert from DokuWiki to LaTeX based on regular expressions.
I’ve stopped using this code on my own computer in favor of a pandoc solution, which I’ll detail in a later post.
The script was originally one long BASH script which included calls to cat, sed, and awk. However, for the purposes of illustration I’m going to just show each important piece. If you want to download the complete script, you can get it here
The first part of the script is to wrap the document in a (xe)LaTeX header and footer. I chose xelatex because it can handle Unicode fonts. That’s the reason why I was using Lucida Grande as my font face, but any Unicode font would work.
..then comes the code for the conversion. Then the footer:
The conversion code has two parts: an awk script to parse multi-line elements, such as list items, and a sed script to parse single-line conversions. The awk script is as follows:
The results of the awk script get piped to a sed script which parses single-line items, such as bold, italic, etc:
So there you have it. It’s very informative to take on a project of this size: you learn pretty quickly how big of a mess regular expressions can be. But that’s the beauty of sed and awk - you can combine simple expressions until you have one big (ugly) program that does something significant. It’s amazing the power of what you can do with a few basic utilities that are included by default on just about every non-windows machine on the planet (and are available for windows machines as well).
I also learned that most of what you can do with sed can be done right from inside the amazing text editor, vim. An unintended consequence of learning sed is that you become a vim search/replace master. That alone is a great reason to learn how to use sed.