Writing a Book in Markdown

28th February 2013

I'm writing my first book at the moment. Lately I've been doing all my writing in Markdown, but ultimately i'll need an alternative format. For word processing, such as it is, I tend to use Open Office - so really I wanted to be able to write in Markdown, but convert to Open Office (or indeed, any of a variety of formats). For one thing, it's nice to get an idea of the number of pages, which my Markown editor just can't do. Also, I prefer to work with separate files for each chapter - but I'm not entirely sure what order the chapters will end up in. So, what I really wanted was a quick way to do two things - concatenate all my chapters, and then convert the lot to Open Office format.

My solution to this is in two parts.

First, a shell script to concatenate the files. It's important it's fairly easy to rearrange chapters, so the format of this was crucial.

Second, the conversion. For this I turned to Pandoc, a small command-line utility which supports a huge raft of formats including Open Office, Markdown, Microsoft Word, HTML, ePub, LaTeX and so on.

So without further ado, here's my shell script. Easy to reorganise, and does everything I need in minutes.

cat a_chapter_about_rabbits.md \
a_chapter_about_giraffes.md \
all_about_ocelots.md \
lets_talk_about_elephants.md \
wrapping_up.md \
> concatenated.md

pandoc concatenated.md -o book.odt
rm concatenated.md

Now, when I add a new chapter I simply need to pop the filename in the appropriate place, I can see the chapter listing clearly, and rearranging is a basic cut & paste job. Simple!

(Note: the book's not really about ocelots.)