1 | Publishing eBooks with AsciiDoc |
---|
2 | =============================== |
---|
3 | // Web page meta data. |
---|
4 | :keywords: AsciiDoc, EPUB tools, EPUB, PDF, ebooks |
---|
5 | :description: How to create EPUB and PDF ebooks using AsciiDoc. |
---|
6 | |
---|
7 | ifdef::blogpost[] |
---|
8 | // Use the same article for both a blog post and on the AsciiDoc |
---|
9 | // website. |
---|
10 | :blogpost-title: Publishing eBooks with AsciiDoc |
---|
11 | :blogpost-status: published |
---|
12 | :blogpost-doctype: article |
---|
13 | :blogpost-posttype: post |
---|
14 | :blogpost-categories: AsciiDoc,eBook,EPUB,DocBook |
---|
15 | |
---|
16 | *Published*: 2010-08-23 + |
---|
17 | *Updated*: 2010-05-08 |
---|
18 | endif::blogpost[] |
---|
19 | |
---|
20 | It's easy to write and publish books in |
---|
21 | http://en.wikipedia.org/wiki/EPUB[EPUB] and PDF formats using |
---|
22 | http://www.methods.co.nz/asciidoc/[AsciiDoc]. |
---|
23 | ifdef::blogpost[] |
---|
24 | This article was originally published on the |
---|
25 | http://www.methods.co.nz/asciidoc/publishing-ebooks-with-asciidoc.html[AsciiDoc |
---|
26 | website]. |
---|
27 | endif::blogpost[] |
---|
28 | |
---|
29 | Here are three examples: The first is a <<X1,minimal example>> |
---|
30 | introducing the AsciiDoc format, the second <<X2,The Brothers |
---|
31 | Karamazov>> is a rather long multi-part book and the third <<X3,The |
---|
32 | Adventures of Sherlock Holmes>> includes a front cover image and |
---|
33 | customized page styling. |
---|
34 | |
---|
35 | ifdef::blogpost[] |
---|
36 | // Wordpress processing instruction. |
---|
37 | pass::[<!--more-->] |
---|
38 | endif::blogpost[] |
---|
39 | |
---|
40 | The examples presented below were created on a PC running Xubuntu |
---|
41 | Linux 10.04 but should be applicable to any UNIX-based operating |
---|
42 | system. |
---|
43 | |
---|
44 | NOTE: A number of 'asciidoc' and 'a2x' features used in this article |
---|
45 | require newer versions of AsciiDoc -- version 8.6.5 or better is |
---|
46 | recommended. The version of |
---|
47 | http://wiki.docbook.org/topic/DocBookXslStylesheets[DocBook XSL |
---|
48 | Stylesheets] used was 1.76.1. |
---|
49 | |
---|
50 | TIP: If you are experiencing 'xsltproc' errors when you run 'a2x' take |
---|
51 | a look at Francis Shanahan's |
---|
52 | http://francisshanahan.com/index.php/2011/fixing-epub-problem-docbook-xsl-asciidoc-a2x/[Fixing |
---|
53 | the ePub problem with Docbook-XSL/A2X/Asciidoc] blog post. |
---|
54 | |
---|
55 | |
---|
56 | [[X1]] |
---|
57 | Minimal Book |
---|
58 | ------------ |
---|
59 | This didactic example contains a title and two chapters. The AsciiDoc |
---|
60 | source is a plain text file named `minimal-book.txt`: |
---|
61 | |
---|
62 | --------------------------------------------------------------------- |
---|
63 | = The Book Title |
---|
64 | |
---|
65 | == The first chapter |
---|
66 | Nec vitae mus fringilla eu vel pede sed pellentesque. Nascetur fugiat |
---|
67 | nobis. Eu felis id mauris sollicitudin ut. Sem volutpat feugiat. |
---|
68 | Ornare convallis urna vitae. |
---|
69 | |
---|
70 | Nec mauris sed aliquam nam mauris dolor lorem imperdiet. |
---|
71 | |
---|
72 | == The second chapter |
---|
73 | Ut suspendisse nulla. Auctor felis facilisis. Rutrum vivamus nec |
---|
74 | lectus porttitor dui dapibus eu ridiculus tempor sodales et. Sit a |
---|
75 | cras. Id tellus cubilia erat. |
---|
76 | |
---|
77 | Quisque nullam et. Blandit dui tempor. Posuere in elit diam egestas |
---|
78 | sem vivamus vel ac. |
---|
79 | --------------------------------------------------------------------- |
---|
80 | |
---|
81 | To generate the EPUB formatted book file `minimal-book.epub` run |
---|
82 | AsciiDoc's 'a2x' toolchain wrapper command from the command-line: |
---|
83 | |
---|
84 | a2x -fepub -dbook minimal-book.txt |
---|
85 | |
---|
86 | The `-f` option specifies the output format, the `-d` option specifies |
---|
87 | the document type (book, article or manpage). The optional |
---|
88 | `--epubcheck` option tells 'a2x' to validate the EPUB file using |
---|
89 | http://code.google.com/p/epubcheck/[epubcheck]. To generate a PDF |
---|
90 | version (`minimal-book.pdf`) with 'dblatex' run: |
---|
91 | |
---|
92 | a2x -fpdf -dbook minimal-book.txt |
---|
93 | |
---|
94 | The distributed example PDFs were built using FOP -- if you prefer |
---|
95 | http://xmlgraphics.apache.org/fop/[FOP] over |
---|
96 | http://dblatex.sourceforge.net/[dblatex] use: |
---|
97 | |
---|
98 | a2x -fpdf -dbook --fop minimal-book.txt |
---|
99 | |
---|
100 | You can also generate an HTML formatted book, either using DocBook XSL |
---|
101 | Stylesheets: |
---|
102 | |
---|
103 | a2x -fxhtml -dbook minimal-book.txt |
---|
104 | |
---|
105 | or directly using the 'asciidoc' command: |
---|
106 | |
---|
107 | asciidoc -dbook minimal-book.txt |
---|
108 | |
---|
109 | [NOTE] |
---|
110 | ====== |
---|
111 | The http://www.methods.co.nz/asciidoc/a2x.1.html[a2x toolchain |
---|
112 | wrapper] uses the following programs (most will be available |
---|
113 | prepackaged for your Linux distribution): |
---|
114 | |
---|
115 | - http://xmlsoft.org/XSLT/[xsltproc] |
---|
116 | - http://docbook.sourceforge.net/projects/xsl/[DocBook XSL |
---|
117 | Stylesheets] |
---|
118 | - http://dblatex.sourceforge.net/[dblatex] (PDF file generator) |
---|
119 | - http://xmlgraphics.apache.org/fop/[FOP] (alternative PDF file |
---|
120 | generator) |
---|
121 | - http://code.google.com/p/epubcheck/[epubcheck] (optional EPUB file |
---|
122 | validator) |
---|
123 | ====== |
---|
124 | |
---|
125 | |
---|
126 | [[X2]] |
---|
127 | The Brothers Karamazov |
---|
128 | ---------------------- |
---|
129 | 'The Brothers Karamazov' is an example of a multi-part book. To |
---|
130 | generate the AsciiDoc source I downloaded the |
---|
131 | http://www.gutenberg.org[Project Gutenberg] plain text source and |
---|
132 | edited it manually (this took around 15 minutes). To generate the |
---|
133 | `brothers-karamazov.epub` EPUB file run this command: |
---|
134 | |
---|
135 | a2x brothers-karamazov.txt |
---|
136 | |
---|
137 | :examples-url: http://www.methods.co.nz/misc/examples/books/ |
---|
138 | |
---|
139 | [frame="topbot",options="header",caption=""] |
---|
140 | .Brothers Karamazov source files and eBooks |
---|
141 | |==================================================================== |
---|
142 | |EPUB |PDF |AsciiDoc source |Project Gutenburg text |
---|
143 | |{examples-url}brothers-karamazov.epub[brothers-karamazov.epub] |
---|
144 | |
---|
145 | |{examples-url}brothers-karamazov.pdf[brothers-karamazov.pdf] |
---|
146 | |
---|
147 | |{examples-url}brothers-karamazov.zip[brothers-karamazov.zip] |
---|
148 | |
---|
149 | |http://www.gutenberg.org/etext/28054 |
---|
150 | |
---|
151 | |==================================================================== |
---|
152 | |
---|
153 | Here's the start of the AsciiDoc source file showing the AsciiDoc |
---|
154 | specific meta-data: |
---|
155 | |
---|
156 | --------------------------------------------------------------------- |
---|
157 | // |
---|
158 | // Text from Project Gutenburg http://www.gutenberg.org/etext/28054 |
---|
159 | // |
---|
160 | // Formatted for AsciiDoc by Stuart Rackham. |
---|
161 | // |
---|
162 | // a2x default options. |
---|
163 | // a2x: -f epub -d book --epubcheck |
---|
164 | // Suppress revision history in dblatex outputs. |
---|
165 | // a2x: --dblatex-opts "-P latex.output.revhistory=0" |
---|
166 | // Suppress book part, chapter and section numbering in DocBook XSL outputs. |
---|
167 | // a2x: --xsltproc-opts |
---|
168 | // a2x: "--stringparam part.autolabel 0 |
---|
169 | // a2x: --stringparam chapter.autolabel 0 |
---|
170 | // a2x: --stringparam section.autolabel.max.depth 0" |
---|
171 | // |
---|
172 | |
---|
173 | = The Brothers Karamazov |
---|
174 | :author: Fyodor Dostoyevsky |
---|
175 | :encoding: iso-8859-1 |
---|
176 | :plaintext: |
---|
177 | |
---|
178 | .......................................................................... |
---|
179 | Translated from the Russian of Fyodor Dostoyevsky by Constance Garnett |
---|
180 | The Lowell Press New York |
---|
181 | |
---|
182 | : |
---|
183 | : |
---|
184 | |
---|
185 | ***START OF THE PROJECT GUTENBERG EBOOK THE BROTHERS KARAMAZOV*** |
---|
186 | .......................................................................... |
---|
187 | |
---|
188 | |
---|
189 | = PART I |
---|
190 | |
---|
191 | == The History Of A Family |
---|
192 | |
---|
193 | === Fyodor Pavlovitch Karamazov |
---|
194 | |
---|
195 | Alexey Fyodorovitch Karamazov was the third son of Fyodor Pavlovitch |
---|
196 | ... |
---|
197 | --------------------------------------------------------------------- |
---|
198 | |
---|
199 | - The book, book part and chapter titles have been edited to conform |
---|
200 | to http://www.methods.co.nz/asciidoc/userguide.html#X17[AsciiDoc |
---|
201 | title conventions]. |
---|
202 | |
---|
203 | - Book part titles must be level zero titles: |
---|
204 | |
---|
205 | = Book title (level 0) |
---|
206 | = Part one title (level 0) |
---|
207 | == Chapter 1 title (level 1) |
---|
208 | : |
---|
209 | |
---|
210 | - Sub-chapter levels can be accommodated with `===`, `====` and |
---|
211 | `=====` title prefixes. |
---|
212 | |
---|
213 | - In this example the 'Project Gutenberg' front matter was put in the |
---|
214 | untitled preface inside an AsciiDoc literal block. |
---|
215 | |
---|
216 | - The comment lines starting with `// a2x:` contain 'a2x' command |
---|
217 | options for this document (it's easier to put them in the document |
---|
218 | once than type them every time on the command-line). |
---|
219 | |
---|
220 | - A number of 'xsltproc' options are passed to 'a2x' to suppress book |
---|
221 | part, chapter and section numbering. |
---|
222 | |
---|
223 | - Setting the AsciiDoc 'plaintext' attribute suppresses most of |
---|
224 | http://www.methods.co.nz/asciidoc/userguide.html#_text_formatting[AsciiDoc's |
---|
225 | text formatting] and substitution conventions, this allows large |
---|
226 | amounts of text to be imported with little or no manual editing. |
---|
227 | |
---|
228 | * Once you have defined the AsciiDoc 'plaintext' attribute the only |
---|
229 | requisite manual editing will be to format the titles and rectify |
---|
230 | the odd paragraph being interpreted as some other AsciiDoc block |
---|
231 | element. |
---|
232 | * In the combined 49 thousand lines of imported 'Sherlock Holmes' |
---|
233 | and 'Brothers Karamazov' text I only encountered two |
---|
234 | misinterpreted list items, neither affected the rendered output or |
---|
235 | necessitated editing. |
---|
236 | * You can selectively enable and disable the 'plaintext' attribute |
---|
237 | throughout your document using AsciiDoc |
---|
238 | http://www.methods.co.nz/asciidoc/userguide.html#X18[attribute |
---|
239 | entries]. |
---|
240 | |
---|
241 | |
---|
242 | [[X3]] |
---|
243 | The Adventures of Sherlock Holmes |
---|
244 | --------------------------------- |
---|
245 | This book includes a front cover image and a customized page design. |
---|
246 | To generate the `adventures-of-sherlock-holmes.epub` EPUB file run |
---|
247 | this command: |
---|
248 | |
---|
249 | a2x adventures-of-sherlock-holmes.txt |
---|
250 | |
---|
251 | [frame="topbot",options="header",caption=""] |
---|
252 | .Sherlock Holmes source files and eBooks |
---|
253 | |==================================================================== |
---|
254 | |EPUB |PDF |AsciiDoc source |Project Gutenburg text |
---|
255 | |{examples-url}adventures-of-sherlock-holmes.epub[adventures-of-sherlock-holmes.epub] |
---|
256 | |
---|
257 | |{examples-url}adventures-of-sherlock-holmes.pdf[adventures-of-sherlock-holmes.pdf] |
---|
258 | |
---|
259 | |{examples-url}adventures-of-sherlock-holmes.zip[adventures-of-sherlock-holmes.zip] |
---|
260 | |
---|
261 | |http://www.gutenberg.org/etext/1661 |
---|
262 | |
---|
263 | |==================================================================== |
---|
264 | |
---|
265 | Here's a screenshot of the first page of the first chapter (rendered |
---|
266 | by the http://www.epubread.com/en/[Firefox EPUBReader addon]): |
---|
267 | |
---|
268 | image::images/epub.png[] |
---|
269 | |
---|
270 | The {examples-url}adventures-of-sherlock-holmes.zip[AsciiDoc source |
---|
271 | Zip file] contains the following files: |
---|
272 | |
---|
273 | `adventures-of-sherlock-holmes.txt`:: |
---|
274 | The AsciiDoc source (derived from the |
---|
275 | http://www.gutenberg.org/etext/1661[Project Gutenberg plain text |
---|
276 | source]). |
---|
277 | |
---|
278 | `adventures-of-sherlock-holmes.jpg`:: |
---|
279 | The front cover image. |
---|
280 | |
---|
281 | `adventures-of-sherlock-holmes-docinfo.xml`:: |
---|
282 | A http://www.methods.co.nz/asciidoc/userguide.html#X87[docinfo] file |
---|
283 | containing DocBook markup for the front cover image and the Project |
---|
284 | Gutenberg frontmatter. DocBook XSL Stylesheets identifies the book |
---|
285 | cover image by the `role="cover"` attribute in the DocBook |
---|
286 | `mediaobject` element. |
---|
287 | |
---|
288 | `adventures-of-sherlock-holmes.css`:: |
---|
289 | CSS rules for styling the page layout. The design is based on the |
---|
290 | http://epubzengarden.com[ePub Zen Garden] 'Gbs' style. Because this |
---|
291 | file is not named `docbook-xsl.css` the name must be specified |
---|
292 | explicitly using the 'a2x' `--stylesheet` option. |
---|
293 | |
---|
294 | `underline.png`:: |
---|
295 | A title underline image that is used by the CSS stylesheet. This |
---|
296 | resource has to be explicitly passed to 'a2x' because 'a2x' only |
---|
297 | searches HTML files for resources. |
---|
298 | |
---|
299 | Here's the start of the AsciiDoc source file showing the AsciiDoc |
---|
300 | specific meta-data: |
---|
301 | |
---|
302 | --------------------------------------------------------------------- |
---|
303 | // |
---|
304 | // Text from Project Gutenburg http://www.gutenberg.org/etext/1661 |
---|
305 | // |
---|
306 | // Formatted for AsciiDoc by Stuart Rackham. |
---|
307 | // |
---|
308 | // a2x default options. |
---|
309 | // a2x: -f epub -d book -a docinfo --epubcheck |
---|
310 | // a2x: --stylesheet adventures-of-sherlock-holmes.css |
---|
311 | // Suppress revision history in dblatex outputs. |
---|
312 | // a2x: --dblatex-opts "-P latex.output.revhistory=0" |
---|
313 | // Suppress book part, chapter and section numbering in DocBook XSL outputs. |
---|
314 | // a2x: --xsltproc-opts |
---|
315 | // a2x: "--stringparam part.autolabel 0 |
---|
316 | // a2x: --stringparam chapter.autolabel 0 |
---|
317 | // a2x: --stringparam section.autolabel.max.depth 0" |
---|
318 | // Additional resources. |
---|
319 | // a2x: --resource underline.png |
---|
320 | // |
---|
321 | |
---|
322 | = The Adventures of Sherlock Holmes |
---|
323 | :author: Sir Arthur Conan Doyle |
---|
324 | :encoding: iso-8859-1 |
---|
325 | :plaintext: |
---|
326 | |
---|
327 | == A Scandal in Bohemia |
---|
328 | |
---|
329 | To Sherlock Holmes she is always THE woman. I have seldom heard |
---|
330 | ... |
---|
331 | --------------------------------------------------------------------- |
---|
332 | |
---|
333 | The manual editing of imported plain text involves formatting the |
---|
334 | title and chapter names as |
---|
335 | http://www.methods.co.nz/asciidoc/userguide.html#X17[AsciiDoc titles] |
---|
336 | (in this example we've used the 'single line' title format). Sherlock |
---|
337 | Holmes only has two title levels: |
---|
338 | |
---|
339 | --------------------------------------------------------------------- |
---|
340 | = The Adventures of Sherlock Holmes |
---|
341 | == A Scandal in Bohemia |
---|
342 | == The Red-Headed League |
---|
343 | == A Case of Identity |
---|
344 | == The Boscombe Valley Mystery |
---|
345 | == The Five Orange Pips |
---|
346 | == The Man with the Twisted Lip |
---|
347 | == The Adventure of the Blue Carbuncle |
---|
348 | == The Adventure of the Speckled Band |
---|
349 | == The Adventure of the Engineer's Thumb |
---|
350 | == The Adventure of the Noble Bachelor |
---|
351 | == The Adventure of the Beryl Coronet |
---|
352 | == The Adventure of the Copper Beeches |
---|
353 | == Colophon |
---|
354 | --------------------------------------------------------------------- |
---|
355 | |
---|
356 | |
---|
357 | Styling your books |
---|
358 | ------------------ |
---|
359 | You can customize the appearance of a document with CSS. CSS file. |
---|
360 | Either create your own or copy and edit the existing default |
---|
361 | `docbook-xsl.css` file (located in the AsciiDoc `stylesheets` |
---|
362 | configuration directory) then place it in the same directory as your |
---|
363 | AsciiDoc source file. Use the 'a2x' `--stylesheet` option if you want |
---|
364 | to use a different stylesheet file. Take a look at the |
---|
365 | `adventures-of-sherlock-holmes.css` CSS file. |
---|
366 | |
---|
367 | There are some great examples of EPUB book styles at |
---|
368 | http://epubzengarden.com/[ePub Zen Garden]. |
---|
369 | |
---|
370 | |
---|
371 | Including embedded fonts |
---|
372 | ------------------------ |
---|
373 | See http://www.methods.co.nz/asciidoc/faq.html#X5[this FAQ]. |
---|
374 | |
---|
375 | |
---|
376 | Reading EPUB documents |
---|
377 | ---------------------- |
---|
378 | My current favorite software epub reader is the |
---|
379 | http://www.epubread.com/en/[Firefox EPUBReader addon]. EPUBReader |
---|
380 | honors the book's CSS styling rules and renders the page as the author |
---|
381 | intended (many EPUB readers only process a sub-set of CSS styling |
---|
382 | rules). |
---|
383 | |
---|
384 | Browsers are a natural fit for EPUB (EPUB is just a bunch of zipped |
---|
385 | XHTML files) -- I'd love to see browsers support EPUB natively. |
---|
386 | |
---|
387 | As of writing this article most eBook readers (with the notable |
---|
388 | exception of Amazon's Kindle) support the EPUB format. |
---|
389 | |
---|
390 | |
---|
391 | Non-fiction Books and Manuals |
---|
392 | ----------------------------- |
---|
393 | AsciiDoc supports a rich set of markup conventions and can generate |
---|
394 | reference works and technical manuals complete with tables, |
---|
395 | illustrations, indexes, bibliographies and appendices. All the |
---|
396 | examples on the http:///www.methods.co.nz/asciidoc/[AsciiDoc web site] |
---|
397 | can be formatted as EPUB eBooks. |
---|
398 | |
---|