I am writing a book in orgmode with plenty of code snippets and their output. In order to make it available on Leanpub I needed to export to markdown. The default exporter, ox-md, produces basic markdown, which does not take advantage of the capabilities that Leanpub offers, and cannot distinguish between code and output.

So I used the excellent export architecture in orgmode to write the ox-leanpub exporter. It adds some functionality on top of the standard md one:

Distinction between code and its output

For example,

#+begin_src sh :exports results

: /Users/inesuka


A> {linenos=off}
A> ~~~~~~~~
A> /Users/inesuka
A> ~~~~~~~~

This will become two bits of code, the first typeset as sh, and the second in an aside (formatted in a box).


Exports properly links to external sources, to images added as


and to internal links made with IDs.

Add this to your .emacs to make sure that when you make an internal link (with C-c l to store, C-c C-l to paste) it will use IDs:

(add-to-list 'org-modules 'org-id)
(setq org-id-link-to-org-use-id t)

I add all my book files to the agenda with C-c [. Remember to run M-x org-id-update-id-locations when you rename a file.


It leaves tables as they are: turns out that, if you don't want to do sophisticated stuff, the table format in orgmode and the one that Leanpub understands are identical. You can add an #+attr_leanpub: line before the table to pass on Leanpub formatting directives, as

#+attr_leanpub: {title="Figure 32",width="60%"}
| a table | second col |
| second  | line       |
| Third   | line       |

Latex fragments and formulas

It converts them properly if you have written them inline between $, or if they are like this:

x = \frac{10}{y + z}

Make sure that you leave the \[ and the \] in lines by themselves.


Download from github, or clone with

git clone https://github.com/juanre/ox-leanpub.git

Put the ox-leanpub.el file somewhere in your load path, or add the directory where it is to the load path,

(add-to-list 'load-path "~/lib/emacs/wherever")

then add

(require 'ox-leanpub)

to your .emacs. When you export you'll see the new L option for Leanpub.

Stability and disclaimer

I am in no way associated with Leanpub. Leanpub is not responsible for this code. Leanpub is not responsible for any damage it may cause in your computer, and neither am I.

I have tested this code only with my book, and as far as I can tell it works. But I am sure there are cases and configurations out there that I have overlooked. Let me know if you find one and I'll do my best to help.

Juan Reyero Barcelona, 2014-01-27

blog comments powered by Disqus