Skip to content

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
robtillotson committed Jul 29, 2013
1 parent 8ebf4ec commit 362f8a7
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 3 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
org-pandoc
==========
# org-pandoc

Export from org-mode using pandoc
This is an exporter for Org 8.0 that outputs via Pandoc, using Markdown as the intermediate format. It is mainly intended to allow the creation of e-books via Org export; while Org's own LaTeX and HTML exporters are fine for PDF/print and web, it has no native way to make EPUBs and the like. So to deal with those, we can use Pandoc to process a format that Org does know how to export. Although Pandoc supports LaTeX, Markdown is used as the intermediate format here because it seems as though Pandoc has a harder time (stack overflows, etc.) with big LaTeX documents than it does with big Markdown documents.

At present it is rather simple: it is derived from the Markdown exporter, simply adding the title and author block to the heading of the exported file in the format that Pandoc expects. If you export to a file instead of to a temporary buffer, it will run pandoc on the output file using the configured output format and options.

## (Possible) Future Additions

- in-buffer Org options to control Pandoc export
- extra support for EPUB style sheets and metadata
117 changes: 117 additions & 0 deletions ox-pandoc.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
;;; ox-pandoc.el --- Pandoc backend for org-mode exporter

;; Copyright 2013 Rob Tillotson

;; Author: Rob Tillotson <[email protected]>

;;; Commentary:

;; This is a pandoc exporter for Org, built upon Markdown as an
;; intermediate format.

;;; Code:

(eval-when-compile (require 'cl))
(require 'ox-md)

;;; User Modifiable Variables:

(defgroup org-export-pandoc nil
"Options specific to Pandoc export back-end."
:tag "Org Pandoc"
:group 'org-export
:version "24.4"
:package-version '(Org . "8.0"))

(defcustom org-pandoc-process-after-export t
"Run pandoc to process the file after exporting it?"
:group 'org-export-pandoc
:type '(choice
(const :tag "Yes" t)
(const :tag "No" nil)))

(defcustom org-pandoc-command "pandoc"
"Command to run pandoc."
:group 'org-export-pandoc
:type 'string)

(defcustom org-pandoc-extra-options ""
"Extra pandoc options to use every time.
For example, if you encounter stack overflows, put the options
to expand the stack here."
:group 'org-export-pandoc
:type 'string)

(defcustom org-pandoc-output-format 'html
"Default output format for pandoc conversion."
:group 'org-export-pandoc
:type 'symbol)

(defcustom org-pandoc-output-standalone t
"Should output be a single standalone file or not?"
:group 'org-export-pandoc
:type 'boolean)

(org-export-define-derived-backend 'pandoc 'md
:menu-entry
'(?d "Export to Pandoc"
((?P "Intermediate to temp buffer"
(lambda (a s v b) (org-pandoc-export-as-pandoc a s v)))
(?p "To file"
(lambda (a s v b) (org-pandoc-export-to-pandoc a s v)))))
:translate-alist '((template . org-pandoc-template)))

(defun org-pandoc-template (contents info)
(let ((title (org-export-data (plist-get info :title) info))
(author (org-export-data (plist-get info :author) info)))
(concat "% " title "\n"
"% " author "\n"
"\n"
contents)))

(defun org-pandoc-export-as-pandoc (&optional async subtreep visible-only)
(interactive)
(if async
(org-export-async-start
(lambda (output)
(with-current-buffer (get-buffer-create "*Org Pandoc Export*")
(erase-buffer)
(insert output)
(goto-char (point-min))
(markdown-mode)
(org-export-add-to-stack (current-buffer) 'pandoc)))
`(org-export-as 'pandoc ,subtreep ,visible-only))
(let ((outbuf (org-export-to-buffer 'pandoc "*Org Pandoc Export*" subtreep visible-only)))
(with-current-buffer outbuf (markdown-mode))
(when org-export-show-temporary-export-buffer
(switch-to-buffer-other-window outbuf)))))

(defun org-pandoc-run-pandoc (filename outfilename format &optional options)
(let* ((args (list "-t" (symbol-name format)
"-o" outfilename
org-pandoc-extra-options
options
filename))
(command (concat org-pandoc-command " " (mapconcat 'identity args " "))))
(message "Running pandoc: %s" (shell-command-to-string command))))

(defun org-pandoc-export-to-file (&optional outfile subtreep visible-only)
(let* ((format org-pandoc-output-format)
(standalone org-pandoc-output-standalone)
(options (concat (if standalone " -s")))
(pandoc-output (concat (file-name-base outfile) "." (symbol-name format))))
(org-export-to-file 'pandoc outfile subtreep visible-only)
(org-pandoc-run-pandoc outfile pandoc-output format options)
))

(defun org-pandoc-export-to-pandoc (&optional async subtreep visible-only)
(interactive)
(let ((outfile (org-export-output-file-name ".md" subtreep)))
(if async
(org-export-async-start
(lambda (f) (org-export-add-to-stack f 'pandoc))
`(expand-file-name
(org-pandoc-export-to-file ,outfile ,subtreep ,visible-only)))
(org-pandoc-export-to-file outfile subtreep visible-only))))

(provide 'ox-pandoc)

0 comments on commit 362f8a7

Please sign in to comment.