summaryrefslogtreecommitdiff
path: root/src/published/2015-01-24_how-to-write-ebook.txt
blob: 7163dadf7cd2769ff8eff0edb432a93262a07d02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
---
title: How to Write an Ebook
pub_date: 2015-01-24 12:52:53
slug: /blog/2015/01/how-to-write-ebook
metadesc: The tools I use to write and publish ebooks. All free and open source.

---

When I set out to write a book I had little more than an outline in Markdown. Just a few headers and bullet points on each of what became the major chapters of my [book on responsive web design](https://longhandpixels.net/books/responsive-web-design). 

It never really occurred to me to research which tools I would need to create a book because I knew I was going to use Markdown, which could then be translated into pretty much any format using [Pandoc](http://johnmacfarlane.net/pandoc/).

Since quite a few people have [asked](https://twitter.com/situjapan/status/549935669129142272) for more details on exactly which tools I used, here's a quick rundown:

1. I write books as single text files lightly marked up with Pandoc-flavored Markdown.
2. Then I run Pandoc, passing in custom templates, CSS files, fonts I bought and so on. Pretty much as [detailed here in the Pandoc documentation](http://johnmacfarlane.net/pandoc/epub.html). I run these commands often enough that I write a shell script for each project so I don't have to type in all the flags and file paths each time.
3. Pandoc outputs an ePub file and an HTML file. The latter is then used with [Weasyprint](http://weasyprint.org/) to generate the PDF version of the ebook. Then I used the ePub file and the [Kindle command line tool](http://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000765211) to create a .mobi file.
4. All of the formatting and design is just CSS, which I am already comfortable working with (though ePub is only a subset of CSS and reader support is somewhat akin to building website in 1998 -- who knows if it's gonna work? The PDF is what I consider the reference copy.)

In the end I get the book in TXT, HTML, PDF, ePub and .mobi formats, which covers pretty much every digital reader I'm aware of. Out of those I actually include the PDF, ePub and Mobi files when you [buy the book](https://longhandpixels.net/books/responsive-web-design).

### FAQs and Notes.

<strong>Why Not Use iBook Author?</strong>

I don't want my book tied to a company's software which may or may not continue to exist. Plus I wanted to use open source software. And I wanted more control over the process than I could get with monolithic tools like visual layout editors. 

The above tools are, for me anyway, the simplest possible workflow which outputs the highest quality product. 

<strong>What about Prince?</strong>

What does The Purple One have to do with writing books? Oh, that [Prince](http://www.princexml.com/). Actually I really like Prince and it can do a few things that WeasyPrint cannot (like execute JavaScript which is handy for code highlighting or allow for `@font-face` font embedding), but it's not free and in the end, I decided, not worth the money.

<strong>Can you share your shell script?</strong>

Here's the basic idea, adjust file paths to suit your working habits.

~~~.language-bash
#!/bin/sh
#Update PDF:
pandoc --toc --toc-depth=2 --smart --template=lib/template.html5 --include-before-body=lib/header.html -t html5 -o rwd.html draft.txt && weasyprint rwd.html rwd.pdf

#Update epub:
pandoc -S -s --smart -t epub3 --include-before-body=lib/header.html --template=lib/template_epub.html --epub-metadata=lib/epub-metadata.xml --epub-stylesheet=lib/print-epub.css --epub-cover-image=lib/covers/cover-portrait.png --toc --toc-depth=2 -o rwd.epub draft.txt

#update Mobi:
pandoc -S -s --smart -t epub3 --include-before-body=lib/header.html --template=lib/template_epub.html --epub-metadata=lib/epub-metadata.xml --epub-stylesheet=lib/print-kindle.css --epub-cover-image=lib/covers/cover-portrait.png --toc --toc-depth=2 -o kindle.epub Draft.txt && kindlegen kindle.epub -o rwd.mobi
~~~

I just run this script and bang, all my files are updated. 

<strong>What Advice can you Offer for People Wanting to Write an Ebook?</strong>

At the risk of sounding trite, just do it. 

Writing a book is not easy, or rather the writing is never easy, but I don't think it's ever been this easy to *produce* a book. It took me two afternoons to come up with a workflow that involves all free, open source software and allows me to publish literally any text file on my hard drive as a book that can then be read by millions. I type two key strokes and I have a book. Even if millions don't ever read your book (and, for the record, millions have most definitely not read my books), that is still f'ing amazing. 

Now go make something cool (and be sure to tell me about it).