Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch export of org-mode files from the command line

Tags:

emacs

org-mode

Assume that I have in a certain directory several org-mode files: foo1.org, foo2.org, etc. I would like to have a script (maybe a makefile) that I could invoke something like

$ generate-pdfs

and foo1.pdf, foo2.pdf, etc. will be generated.

I thought that something like emacs --batch --eval <MAGIC> is a good start, but I don't know the magic.

A solution that is solely inside emacs could be of interest as well.

like image 719
Dror Avatar asked Feb 27 '14 15:02

Dror


3 Answers

As you said, Emacs has the --batch option to perform operations with Emacs from the shell. In addition to that, you can use the -l flag to load Emacs Lisp code from a file and execute it, and the -f flag to execute a single Lisp function.

Here is a basic example, which exports a single org-mode file to HTML:

emacs myorgfile.org --batch -f org-html-export-to-html --kill

Perhaps you want something more advanced like exporting/publishing a full org-mode project. I do not have sample code for that, but it should not be too complicated.

I also have a sample Makefile I wrote some time ago to export all org-mode files in the directory to HTML (and also copy the HTML files to another directory):

OUT_DIR=/some/output/dir/html
# Using GNU Make-specific functions here
FILES=$(patsubst %.org,$(OUT_DIR)/%.html,$(wildcard *.org))

.PHONY: all clean install-doc

all: install-doc

install-doc: $(OUT_DIR) $(FILES)

$(OUT_DIR):
        mkdir -v -p $(OUT_DIR)

%.html: %.org
        emacs $< --batch -f org-html-export-to-html--kill

$(OUT_DIR)/%.html: %.html
        install -v -m 644 -t $(OUT_DIR) $<
        rm $<

clean:
        rm *.html

EDIT:

With Org-mode 8 and the new export engine the function for HTML export has changed.

To make the previous examples work with Org 7 or older, replace org-html-export-to-html with org-export-as-html.

like image 163
florianlh Avatar answered Sep 24 '22 21:09

florianlh


I expect to publish (by the end of this week-end) OrgMk, a suite of Makefile and standalone Bash scripts (usable as well under Cygwin) just to do that! Even more: generation of HTML, Ascii, Beamer, etc.

You'll find it on my GitHub account: https://github.com/fniessen/ (where I already have Emacs configuration files, color themes and other stuff such as an Org Babel refcard -- in progress).

like image 44
fniessen Avatar answered Sep 24 '22 21:09

fniessen


Mark a few org files in dired and call this:

(defun dired-org-to-pdf ()
  (interactive)
  (mapc
   (lambda (f)
     (with-current-buffer
         (find-file-noselect f)
       (org-latex-export-to-pdf)))
   (dired-get-marked-files)))

If you know what async is, wrap the call as it can take a while.

update:

Here's a version that combines the awesome dired approach with the lame other one:)

(defun dired-org-to-pdf ()
  (interactive)
  (let ((files
         (if (eq major-mode 'dired-mode)
             (dired-get-marked-files)
           (let ((default-directory (read-directory-name "dir: ")))
             (mapcar #'expand-file-name 
                     (file-expand-wildcards "*.org"))))))
    (mapc
     (lambda (f)
       (with-current-buffer
           (find-file-noselect f)
         (org-latex-export-to-pdf)))
     files)))
like image 40
abo-abo Avatar answered Sep 22 '22 21:09

abo-abo