27 September 2013

1 Problem

The org-expoort-dispatch command is very useful when I'm writing .org file and want to preview the exported .html file in web browser. The manual is copied from emacs on-line help:

It is bound to C-c C-e, <menu-bar> <Org> <Export/Publish...>.

(org-export-dispatch &optional ARG)

Export dispatcher for Org mode.

It provides an access to common export related tasks in a buffer.
Its interface comes in two flavours: standard and expert.

Whenever I want to preview the article, I just press C-c C-e h o, emacs will convert the .org file to .html file and open it in my default web browser.

But the problem is that the result .html file is stored at the same folder as .org file. I've been looking for the solution around the web, the emacs geeks just told us to look at org-publish-project-alist. But what I know is that org-publish-project-alist is used to set up a publish project and it is used to publish whole project to a destination folder.

This is not what I'm looking for.

2 Solution

After several try, I found that in the .html export backend, org-export-output-file-name is used to generated the output file as following code:

 1: (defun org-html-export-to-html
 2:   (&optional async subtreep visible-only body-only ext-plist)
 3:   (interactive)
 4:   (let* ((extension (concat "." org-html-extension))
 5:      (file (org-export-output-file-name extension subtreep))
 6:      (org-export-coding-system org-html-coding-system))
 7:     (if async
 8:     (org-export-async-start
 9:         (lambda (f) (org-export-add-to-stack f 'html))
10:       (let ((org-export-coding-system org-html-coding-system))
11:         `(expand-file-name
12:           (org-export-to-file
13:            'html ,file ,subtreep ,visible-only ,body-only ',ext-plist))))
14:       (let ((org-export-coding-system org-html-coding-system))
15:     (org-export-to-file
16:      'html file subtreep visible-only body-only ext-plist)))))

While org-export-output-file-name can accept three parameter:

(defun org-export-output-file-name (extension &optional subtreep pub-dir)

When optional argument PUB-DIR is set, use it as the publishing directory.

Thus change line 5 of org-html-export-to-html function, add the third paramter ~/exports/:

 1: (defun org-html-export-to-html
 2:   (&optional async subtreep visible-only body-only ext-plist)
 3:   (interactive)
 4:   (let* ((extension (concat "." org-html-extension))
 5:      (file (org-export-output-file-name extension subtreep "~/exports/"))
 6:      (org-export-coding-system org-html-coding-system))
 7:     (if async
 8:     (org-export-async-start
 9:         (lambda (f) (org-export-add-to-stack f 'html))
10:       (let ((org-export-coding-system org-html-coding-system))
11:         `(expand-file-name
12:           (org-export-to-file
13:            'html ,file ,subtreep ,visible-only ,body-only ',ext-plist))))
14:       (let ((org-export-coding-system org-html-coding-system))
15:     (org-export-to-file
16:      'html file subtreep visible-only body-only ext-plist)))))

Fantastique, now when you press C-c C-e h o, emacs will save the .html file in ~/exports/ and open it in web browser for your preview.

For other kind of export, I think this method could also be applied. Good luck.