A leanpub-optimized markdown exporter for org-mode
Note: This is the original ox-leanpub exporter that I created in 2014. The project has since been taken over by Diego Zamboni.
- Original code (this page): github.com/juanre/ox-leanpub
- Current maintained version: github.com/zzamboni/ox-leanpub
For new projects, please use Diego’s version which has many improvements and is actively maintained.
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
pwd
#+end_src
#+results:
: /Users/inesuka
yields
~~~~~~~~
pwd
~~~~~~~~
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).
Footnotes
It adds the footnote definitions at the end as
[^fn:disc]: Whatever your footnote is about.
and the reference in place.
Links
Exports properly links to external sources, to images added as
[[file:img/source.png]]
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
:
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.
Tables
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.
Installation
Download from github, or clone with
Put the ox-leanpub.el
file somewhere in your load path, or add the directory where it is to the load path,
then add
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.