How to Write a Book: Tools

Jun 13, 2013 at 5:50AM
Caleb Doxsey

This is the 2nd part of a series of posts on how I wrote my book: An Introduction to Programming in Go. You can find the previous post here. This post is about the tools I used.

Editors

Once you’ve decided to write a book the first thing you’ll have to do (outside of picking the topic) is decide what you are going to write the book in. You could write the whole thing on paper, use a word processor like Microsoft Word or Libre Office (or even Google Docs if you’re brave), write the whole thing in a simple text editor and use sophisticated typesetting software, or you could use Latex.

You probably think I used Latex, but I didn’t. I used Libre Office. Here’s why:

Images

I used Libre Office Draw and GIMP to make the diagrams in the book. As a web developer I was already very comfortable with GIMP, most people will probably find it overly cumbersome to use. GIMP has some advanced features - like using a very high DPI - which were vital when I sent the book to be published. My sister did the cover art and she used Adobe Illustrator.

Incidentally isn’t the cover art really good? The gopher riding a bike captures three things: (1) You know it’s Go because it’s the gopher, (2) he’s on a bike with training wheels (and streamers) which conveys the introductory nature of the book and (3) for a kid learning to ride a bike is super intimidating, but it’s also really fun (and something like a rite of passage). I wanted the reader to feel a bit like that.

Scripts

I needed a way to export my Libre Office document so I wrote some scripts which would convert it into the native Kindle format (which is itself just repackaged mobi) and HTML for the website. You can see an example here: github.com/calebdoxsey/go-book-converter.

That script is convoluted, but basically I open the document as a zip file (nice that they made it that way), grab the xml out of it, convert that into a DOM (I wrote a simple DOM library here), and then run a series of transformations on that DOM to get it the way I want.

I also extract all the images and resize them. (the source images are super high DPI and inappropriate for the web) Here’s the code that handles that:

for _, n := range css.Find(doc, "img") {
	el := n.(*dom.Element)

	fn, _ := el.Get("src")
	if fn == "" {
		continue
	}
	nfn := path.Base(strings.Replace(fn, ".png", ".jpg", -1))
	files = append(files, nfn)
	fn = "../golang-book-web/" + fn

	r, err := os.Open(fn)
	if err != nil {
		log.Fatalln(err)
	}
	defer r.Close()

	img, err := png.Decode(r)
	if err != nil {
		log.Fatalln(err)
	}

	w, err := os.Create("book/kindle/" + nfn)
	if err != nil {
		log.Fatalln(err)
	}
	defer w.Close()

	err = jpeg.Encode(w, img, &jpeg.Options{
		Quality: 90,
	})
	if err != nil {
		log.Fatalln(err)
	}

	el.Set("src", nfn)
}

For this project I also put together installers for OSX and Windows, which install both Go and a Text Editor. If you’re curious how they work I included scripts in the osx & windows folders in the github repository. (They’re just examples though...)

The Website

For the website (golang-book.com) I setup a simple Go web server. At first I used Heroku but I've since switched to Google's App Engine. Most of this site is just straight HTML, either hand-written as a template or pre-generated by my conversion script.

The Most Important Tool

Those are the main tools I used. One last thing though: Could you imagine working on a book for months and then losing the whole thing because your computer crashed? Or you screwed up and accidentally erased the whole thing?

It's vital that you use some sort of source control, commit frequently, and back all of this stuff up somewhere. I used git & Bitbucket (Bitbucket has free private repositories).

That’s it for this post. Next time I’ll talk about money & publishing.