0

I would like to use Emacs in batch mode to export a number of org files to HTML from the command-line. And I would like to get the same result than interactively using C-cC-eh, in particular:

  • honor file-local variables (such as org-export-publishing-directory)
  • honor all options specified through #+KEYWORD: headlines

Starting from the example given in org-export-as-html-batch, I got to this point:

emacs --batch \
    --visit=/tmp/foo.org \
    --eval "(defun safe-local-variable-p (sym val) t)" \
    --funcall hack-local-variables \
    --eval "(setq org-export-headline-levels 4)" \
    --funcall org-export-as-html-batch

However, some problems remain:

  • I need to explicitly specify the headline level and I fail to see why all other #+OPTIONS are honored (like toc:nil) but not this one

  • I had to manually trigger file-local variables parsing using hack-local-variables (I guess it is not automatically done in batch mode) but more importantly I had to resort to a hack to mark all local variables as safe (I'm sure there is much space for improvement here).


NB:

In case it matters, I'm using emacs 23.2.1 (Debian Squeeze flavour)

Here is a sample org file on which I tested this:

#+TITLE: Foo
#+OPTIONS: H:4 toc:nil author:nil

* 1
** 2
*** 3
**** 4

# Local Variables:
#  org-export-publishing-directory: "/some/where";
# End:
François Févotte
  • 19,520
  • 4
  • 51
  • 74
  • Have you had a look at org-publish? – BtD Jan 03 '13 at 16:01
  • Yes, I know of `org-publish`, but it does not seem to do exactly what I want since the files I want to export do not belong to a global project and are potentially unrelated. For example, some of them should be exported to a custom NFS directory so that I can share them with colleagues, while others are for my own use and should be exported right where they are. – François Févotte Jan 03 '13 at 16:20

1 Answers1

1

I eventually got the following script, which seems to fulfill all my requirements:

#!/bin/sh
":"; exec emacs --script "$0" -- "$@" # -*-emacs-lisp-*-
;; 
;; Usage:
;;    org2html FILE1 [FILE2 ...]


;; Mark org-related variables as safe local variables,
;; regardless of their value.
(defun my/always-safe-local-variable (val) t)
(dolist (sym '(org-export-publishing-directory
               org-export-html-preamble
               org-export-html-postamble))
  (put sym 'safe-local-variable 'my/always-safe-local-variable))


(defun my/org-export-as-html (filename)
  "Export FILENAME as html, as if `org-export-to-html' had been called
interactively.

This ensures that `org-export-headline-levels' is correctly read from
the #+OPTIONS: headline."
  (save-excursion
    (find-file filename)
    (message "Exporting file `%s' to HTML" filename)
    (call-interactively 'org-export-as-html)))

(mapcar 'my/org-export-as-html
        (cdr argv)) ;; "--" is the first element of argv

A few notes on this script:

  • The executable emacs-lisp script trick comes from this question.

  • The only way I found to use the org-export-headline-levels value from the #+OPTIONS: headline is to call org-export-as-html interactively, instead of org-export-as-html-batch.

  • hack-local-variables does not need to be explicitly called, provided that local variables are marked as safe before the file is opened.

  • I think it is better to only mark org-related variables as safe, using the safe-local-variable symbol property.

Community
  • 1
  • 1
François Févotte
  • 19,520
  • 4
  • 51
  • 74