1 ;;; org-publish.el --- publish related org-mode files as a website
2 ;; Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 ;; Author: David O'Toole <dto@gnu.org>
5 ;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
6 ;; Keywords: hypermedia, outlines, wp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This program allow configurable publishing of related sets of
27 ;; Org-mode files as a complete website.
29 ;; org-publish.el can do the following:
31 ;; + Publish all one's org-files to HTML or PDF
32 ;; + Upload HTML, images, attachments and other files to a web server
33 ;; + Exclude selected private pages from publishing
34 ;; + Publish a clickable index of pages
35 ;; + Manage local timestamps for publishing only changed files
36 ;; + Accept plugin functions to extend range of publishable content
38 ;; Documentation for publishing is in the manual.
48 (unless (fboundp 'declare-function
)
49 (defmacro declare-function
(fn file
&optional arglist fileonly
))))
51 (defgroup org-publish nil
52 "Options for publishing a set of Org-mode and related files."
56 (defcustom org-publish-project-alist nil
57 "Association list to control publishing behavior.
58 Each element of the alist is a publishing 'project.' The CAR of
59 each element is a string, uniquely identifying the project. The
60 CDR of each element is in one of the following forms:
62 (:property value :property value ... )
66 (:components (\"project-1\" \"project-2\" ...))
68 When the CDR of an element of org-publish-project-alist is in
69 this second form, the elements of the list after :components are
70 taken to be components of the project, which group together files
71 requiring different publishing options. When you publish such a
72 project with \\[org-publish], the components all publish.
74 When a property is given a value in org-publish-project-alist, its
75 setting overrides the value of the corresponding user variable
76 \(if any) during publishing. However, options set within a file
79 Most properties are optional, but some should always be set:
81 :base-directory Directory containing publishing source files
82 :base-extension Extension (without the dot!) of source files.
83 This can be a regular expression.
84 :publishing-directory Directory (possibly remote) where output
85 files will be published
87 The :exclude property may be used to prevent certain files from
88 being published. Its value may be a string or regexp matching
89 file names you don't want to be published.
91 The :include property may be used to include extra files. Its
92 value may be a list of filenames to include. The filenames are
93 considered relative to the base directory.
95 When both :include and :exclude properties are given values, the
96 exclusion step happens first.
98 One special property controls which back-end function to use for
99 publishing files in the project. This can be used to extend the
100 set of file types publishable by org-publish, as well as the set
103 :publishing-function Function to publish file. The default is
104 `org-publish-org-to-html', but other
105 values are possible. May also be a
106 list of functions, in which case
107 each function in the list is invoked
110 Another property allows you to insert code that prepares a
111 project for publishing. For example, you could call GNU Make on a
112 certain makefile, to ensure published files are built up to date.
114 :preparation-function Function to be called before publishing
116 :completion-function Function to be called after publishing
119 Some properties control details of the Org publishing process,
120 and are equivalent to the corresponding user variables listed in
121 the right column. See the documentation for those variables to
122 learn more about their use and default values.
124 :language `org-export-default-language'
125 :headline-levels `org-export-headline-levels'
126 :section-numbers `org-export-with-section-numbers'
127 :table-of-contents `org-export-with-toc'
128 :emphasize `org-export-with-emphasize'
129 :sub-superscript `org-export-with-sub-superscripts'
130 :TeX-macros `org-export-with-TeX-macros'
131 :fixed-width `org-export-with-fixed-width'
132 :tables `org-export-with-tables'
133 :table-auto-headline `org-export-highlight-first-table-line'
134 :style `org-export-html-style'
135 :convert-org-links `org-export-html-link-org-files-as-html'
136 :inline-images `org-export-html-inline-images'
137 :expand-quoted-html `org-export-html-expand'
138 :timestamp `org-export-html-with-timestamp'
139 :publishing-directory `org-export-publishing-directory'
140 :preamble `org-export-html-preamble'
141 :postamble `org-export-html-postamble'
142 :auto-preamble `org-export-html-auto-preamble'
143 :auto-postamble `org-export-html-auto-postamble'
144 :author `user-full-name'
145 :email `user-mail-address'
147 The following properties may be used to control publishing of an
148 index of files or summary page for a given project.
150 :auto-index Whether to publish an index during
151 `org-publish-current-project' or `org-publish-all'.
152 :index-filename Filename for output of index. Defaults
153 to 'sitemap.org' (which becomes 'sitemap.html').
154 :index-title Title of index page. Defaults to name of file.
155 :index-function Plugin function to use for generation of index.
156 Defaults to `org-publish-org-index', which
157 generates a plain list of links to all files
159 :index-style Can be `list' (index is just an itemized list
160 of the titles of the files involved) or
161 `tree' (the directory structure of the source
162 files is reflected in the index). Defaults to
167 (defcustom org-publish-use-timestamps-flag t
168 "When non-nil, use timestamp checking to publish only changed files.
169 When nil, do no timestamp checking and always publish all files."
173 (defcustom org-publish-timestamp-directory
(convert-standard-filename
174 "~/.org-timestamps/")
175 "Name of directory in which to store publishing timestamps."
179 (defcustom org-publish-before-export-hook nil
180 "Hook run before export on the Org file.
181 The hook may modify the file in arbitrary ways before publishing happens.
182 The orgiginal version of the buffer will be restored after publishing."
186 (defcustom org-publish-after-export-hook nil
187 "Hook run after export on the exported buffer.
188 Any changes made by this hook will be saved."
192 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193 ;;; Timestamp-related functions
195 (defun org-publish-timestamp-filename (filename)
196 "Return path to timestamp file for filename FILENAME."
197 (concat (file-name-as-directory org-publish-timestamp-directory
)
198 "X" (if (fboundp 'sha1
) (sha1 filename
) (md5 filename
))))
200 (defun org-publish-needed-p (filename)
201 "Return `t' if FILENAME should be published."
203 (if org-publish-use-timestamps-flag
204 (if (file-exists-p org-publish-timestamp-directory
)
205 ;; first handle possible wrong timestamp directory
206 (if (not (file-directory-p org-publish-timestamp-directory
))
207 (error "Org publish timestamp: %s is not a directory"
208 org-publish-timestamp-directory
)
209 ;; there is a timestamp, check if FILENAME is newer
210 (file-newer-than-file-p
211 filename
(org-publish-timestamp-filename filename
)))
212 (make-directory org-publish-timestamp-directory
)
214 ;; don't use timestamps, always return t
217 (message "Publishing file %s" filename
)
218 (message "Skipping unmodified file %s" filename
))
221 (defun org-publish-update-timestamp (filename)
222 "Update publishing timestamp for file FILENAME.
223 If there is no timestamp, create one."
224 (let ((timestamp-file (org-publish-timestamp-filename filename
))
225 newly-created-timestamp
)
226 (if (not (file-exists-p timestamp-file
))
227 ;; create timestamp file if needed
229 (make-directory (file-name-directory timestamp-file
) t
)
230 (write-file timestamp-file
)
231 (setq newly-created-timestamp t
)))
232 ;; Emacs 21 doesn't have `set-file-times'
233 (if (and (fboundp 'set-file-times
)
234 (not newly-created-timestamp
))
235 (set-file-times timestamp-file
)
236 (call-process "touch" nil
0 nil
(expand-file-name timestamp-file
)))))
238 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
239 ;;; Mapping files to project names
241 (defvar org-publish-files-alist nil
242 "Alist of files and their parent projects.
243 Each element of this alist is of the form:
245 (file-name . project-name)")
247 (defvar org-publish-initial-buffer nil
248 "The buffer `org-publish' has been called from.")
249 (defvar org-publish-temp-files nil
250 "Temporary list of files to be published.")
252 (defun org-publish-initialize-files-alist (&optional refresh
)
253 "Set `org-publish-files-alist' if it is not set.
254 Also set it if the optional argument REFRESH is non-nil."
256 (when (or refresh
(not org-publish-files-alist
))
257 (setq org-publish-files-alist
258 (org-publish-get-files org-publish-project-alist
))))
260 (defun org-publish-validate-link (link &optional directory
)
261 "Check if LINK points to a file in the current project."
262 (assoc (expand-file-name link directory
) org-publish-files-alist
))
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265 ;;; Compatibility aliases
267 ;; Delete-dups is not in Emacs <22
268 (if (fboundp 'delete-dups
)
269 (defalias 'org-publish-delete-dups
'delete-dups
)
270 (defun org-publish-delete-dups (list)
271 "Destructively remove `equal' duplicates from LIST.
272 Store the result in LIST and return it. LIST must be a proper list.
273 Of several `equal' occurrences of an element in LIST, the first
276 This is a compatibility function for Emacsen without `delete-dups'."
277 ;; Code from `subr.el' in Emacs 22:
280 (setcdr tail
(delete (car tail
) (cdr tail
)))
281 (setq tail
(cdr tail
))))
284 (declare-function org-publish-delete-dups
"org-publish" (list))
286 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
287 ;;; Getting project information out of org-publish-project-alist
289 (defun org-publish-get-files (projects-alist &optional no-exclusion
)
290 "Return the list of all publishable files for PROJECTS-ALIST.
291 If NO-EXCLUSION is non-nil, don't exclude files."
296 (let* ((exclude (plist-get (cdr p
) :exclude
))
297 (files (and p
(org-publish-get-base-files p exclude
))))
298 ;; add all files from this project
300 (add-to-list 'all-files
301 (cons (expand-file-name f
) (car p
))))
303 (org-publish-expand-projects projects-alist
))
306 (defun org-publish-expand-projects (projects-alist)
307 "Expand projects in PROJECTS-ALIST.
308 This splices all the components into the list."
309 (let ((rest projects-alist
) rtn p components
)
310 (while (setq p
(pop rest
))
311 (if (setq components
(plist-get (cdr p
) :components
))
313 (mapcar (lambda (x) (assoc x org-publish-project-alist
))
317 (nreverse (org-publish-delete-dups (delq nil rtn
)))))
319 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir
)
320 "Set `org-publish-temp-files' with files from BASE-DIR directory.
321 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
322 non-nil, restrict this list to the files matching the regexp
323 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
324 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
325 matching the regexp SKIP-DIR when recursing through BASE-DIR."
327 (let ((fd-p (file-directory-p f
))
328 (fnd (file-name-nondirectory f
)))
329 (if (and fd-p recurse
330 (not (string-match "^\\.+$" fnd
))
331 (if skip-dir
(not (string-match skip-dir fnd
)) t
))
332 (org-publish-get-base-files-1 f recurse match skip-file skip-dir
)
333 (unless (or fd-p
;; this is a directory
334 (and skip-file
(string-match skip-file fnd
))
335 (not (file-exists-p (file-truename f
)))
336 (not (string-match match fnd
)))
337 (pushnew f org-publish-temp-files
)))))
338 (directory-files base-dir t
(unless recurse match
))))
340 (defun org-publish-get-base-files (project &optional exclude-regexp
)
341 "Return a list of all files in PROJECT.
342 If EXCLUDE-REGEXP is set, this will be used to filter out
344 (let* ((project-plist (cdr project
))
345 (base-dir (file-name-as-directory
346 (plist-get project-plist
:base-directory
)))
347 (include-list (plist-get project-plist
:include
))
348 (recurse (plist-get project-plist
:recursive
))
349 (extension (or (plist-get project-plist
:base-extension
) "org"))
350 (match (if (eq extension
'any
)
352 (concat "^[^\\.].*\\.\\(" extension
"\\)$"))))
353 (setq org-publish-temp-files nil
)
354 (org-publish-get-base-files-1 base-dir recurse match
355 ;; FIXME distinguish exclude regexp
356 ;; for skip-file and skip-dir?
357 exclude-regexp exclude-regexp
)
360 (expand-file-name (concat base-dir f
))
361 org-publish-temp-files
))
363 org-publish-temp-files
))
365 (defun org-publish-get-project-from-filename (filename &optional up
)
366 "Return the project FILENAME belongs."
367 (let* ((project-name (cdr (assoc (expand-file-name filename
)
368 org-publish-files-alist
))))
370 (dolist (prj org-publish-project-alist
)
371 (if (member project-name
(plist-get (cdr prj
) :components
))
372 (setq project-name
(car prj
)))))
373 (assoc project-name org-publish-project-alist
)))
375 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
376 ;;; Pluggable publishing back-end functions
378 (defun org-publish-org-to (format plist filename pub-dir
)
379 "Publish an org file to FORMAT.
380 PLIST is the property list for the given project.
381 FILENAME is the filename of the org file to be published.
382 PUB-DIR is the publishing directory."
384 (unless (file-exists-p pub-dir
)
385 (make-directory pub-dir t
))
386 (let ((visiting (find-buffer-visiting filename
)))
388 (switch-to-buffer (or visiting
(find-file filename
)))
389 (let* ((plist (cons :buffer-will-be-killed
(cons t plist
)))
390 (init-buf (current-buffer))
392 (init-buf-string (buffer-string))
394 ;; run hooks before exporting
395 (run-hooks 'org-publish-before-export-hook
)
396 ;; export the possibly modified buffer
397 (setq export-buf-or-file
398 (funcall (intern (concat "org-export-as-" format
))
399 (plist-get plist
:headline-levels
)
400 nil plist nil nil pub-dir
))
401 (when (and (bufferp export-buf-or-file
)
402 (buffer-live-p export-buf-or-file
))
403 (set-buffer export-buf-or-file
)
404 ;; run hooks after export and save export
405 (and (run-hooks 'org-publish-after-export-hook
)
406 (if (buffer-modified-p) (save-buffer)))
407 (kill-buffer export-buf-or-file
))
408 ;; maybe restore buffer's content
409 (set-buffer init-buf
)
410 (when (buffer-modified-p init-buf
)
412 (insert init-buf-string
)
414 (goto-char init-point
))
416 (kill-buffer init-buf
))))))
418 (defun org-publish-org-to-latex (plist filename pub-dir
)
419 "Publish an org file to LaTeX.
420 See `org-publish-org-to' to the list of arguments."
421 (org-publish-org-to "latex" plist filename pub-dir
))
423 (defun org-publish-org-to-pdf (plist filename pub-dir
)
424 "Publish an org file to PDF (via LaTeX).
425 See `org-publish-org-to' to the list of arguments."
426 (org-publish-org-to "pdf" plist filename pub-dir
))
428 (defun org-publish-org-to-html (plist filename pub-dir
)
429 "Publish an org file to HTML.
430 See `org-publish-org-to' to the list of arguments."
431 (org-publish-org-to "html" plist filename pub-dir
))
433 (defun org-publish-org-to-org (plist filename pub-dir
)
434 "Publish an org file to HTML.
435 See `org-publish-org-to' to the list of arguments."
436 (org-publish-org-to "org" plist filename pub-dir
))
438 (defun org-publish-attachment (plist filename pub-dir
)
439 "Publish a file with no transformation of any kind.
440 See `org-publish-org-to' to the list of arguments."
441 ;; make sure eshell/cp code is loaded
442 (unless (file-directory-p pub-dir
)
443 (make-directory pub-dir t
))
444 (or (equal (expand-file-name (file-name-directory filename
))
445 (file-name-as-directory (expand-file-name pub-dir
)))
446 (copy-file filename pub-dir t
)))
448 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
449 ;;; Publishing files, sets of files, and indices
451 (defun org-publish-file (filename &optional project
)
452 "Publish file FILENAME from PROJECT."
453 (when (org-publish-needed-p filename
)
456 (or (org-publish-get-project-from-filename filename
)
458 (format "%s is not in a project. Re-read the list of projects files? "
459 (abbreviate-file-name filename
)))
460 ;; If requested, re-initialize the list of projects files
461 (progn (org-publish-initialize-files-alist t
)
462 (or (org-publish-get-project-from-filename filename
)
463 (error "File %s not part of any known project"
464 (abbreviate-file-name filename
))))
465 (error "Can't publish file outside of a project")))))
466 (project-plist (cdr project
))
467 (ftname (file-truename filename
))
469 (or (plist-get project-plist
:publishing-function
)
470 'org-publish-org-to-html
))
471 (base-dir (file-name-as-directory
472 (file-truename (plist-get project-plist
:base-directory
))))
473 (pub-dir (file-name-as-directory
474 (file-truename (plist-get project-plist
:publishing-directory
))))
479 (and (string-match (regexp-quote base-dir
) ftname
)
480 (substring ftname
(match-end 0))))))
481 (if (listp publishing-function
)
482 ;; allow chain of publishing functions
484 (funcall f project-plist filename tmp-pub-dir
))
486 (funcall publishing-function project-plist filename tmp-pub-dir
)))
487 (org-publish-update-timestamp filename
)))
489 (defun org-publish-projects (projects)
490 "Publish all files belonging to the PROJECTS alist.
491 If :auto-index is set, publish the index too."
495 ((project-plist (cdr project
))
496 (exclude-regexp (plist-get project-plist
:exclude
))
497 (index-p (plist-get project-plist
:auto-index
))
498 (index-filename (or (plist-get project-plist
:index-filename
)
500 (index-function (or (plist-get project-plist
:index-function
)
501 'org-publish-org-index
))
502 (preparation-function (plist-get project-plist
:preparation-function
))
503 (completion-function (plist-get project-plist
:completion-function
))
504 (files (org-publish-get-base-files project exclude-regexp
)) file
)
505 (when preparation-function
(funcall preparation-function
))
506 (if index-p
(funcall index-function project index-filename
))
507 (while (setq file
(pop files
))
508 (org-publish-file file project
))
509 (when completion-function
(funcall completion-function
))))
510 (org-publish-expand-projects projects
)))
512 (defun org-publish-org-index (project &optional index-filename
)
513 "Create an index of pages in set defined by PROJECT.
514 Optionally set the filename of the index with INDEX-FILENAME.
515 Default for INDEX-FILENAME is 'sitemap.org'."
516 (let* ((project-plist (cdr project
))
517 (dir (file-name-as-directory
518 (plist-get project-plist
:base-directory
)))
519 (localdir (file-name-directory dir
))
520 (indent-str (make-string 2 ?\
))
521 (exclude-regexp (plist-get project-plist
:exclude
))
522 (files (nreverse (org-publish-get-base-files project exclude-regexp
)))
523 (index-filename (concat dir
(or index-filename
"sitemap.org")))
524 (index-title (or (plist-get project-plist
:index-title
)
525 (concat "Index for project " (car project
))))
526 (index-style (or (plist-get project-plist
:index-style
)
528 (visiting (find-buffer-visiting index-filename
))
529 (ifn (file-name-nondirectory index-filename
))
531 (with-current-buffer (setq index-buffer
532 (or visiting
(find-file index-filename
)))
534 (insert (concat "#+TITLE: " index-title
"\n\n"))
535 (while (setq file
(pop files
))
536 (let ((fn (file-name-nondirectory file
))
537 (link (file-relative-name file dir
))
539 ;; index shouldn't index itself
540 (unless (equal (file-truename index-filename
)
541 (file-truename file
))
542 (if (eq index-style
'list
)
543 (message "Generating list-style index for %s" index-title
)
544 (message "Generating tree-style index for %s" index-title
)
545 (setq localdir
(concat (file-name-as-directory dir
)
546 (file-name-directory link
)))
547 (unless (string= localdir oldlocal
)
548 (if (string= localdir dir
)
549 (setq indent-str
(make-string 2 ?\
))
554 (file-relative-name localdir dir
))) "/"))
556 (old-subdirs (split-string
557 (file-relative-name oldlocal dir
) "/")))
558 (setq indent-str
(make-string 2 ?\
))
559 (while (string= (car old-subdirs
) (car subdirs
))
560 (setq indent-str
(concat indent-str
(make-string 2 ?\
)))
564 (setq subdir
(concat subdir d
"/"))
565 (insert (concat indent-str
" + " d
"\n"))
566 (setq indent-str
(make-string
567 (+ (length indent-str
) 2) ?\
)))))))
568 ;; This is common to 'flat and 'tree
569 (insert (concat indent-str
" + [[file:" link
"]["
570 (org-publish-find-title file
)
573 (or visiting
(kill-buffer index-buffer
))))
575 (defun org-publish-find-title (file)
576 "Find the title of file in project."
577 (let* ((visiting (find-buffer-visiting file
))
578 (buffer (or visiting
(find-file-noselect file
)))
582 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
583 (org-infile-export-plist))))
585 (or (plist-get opt-plist
:title
)
587 (plist-get opt-plist
:skip-before-1st-heading
))
588 (org-export-grab-title-from-buffer))
589 (file-name-nondirectory (file-name-sans-extension file
))))))
591 (kill-buffer buffer
))
594 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
595 ;;; Interactive publishing functions
598 (defalias 'org-publish-project
'org-publish
)
601 (defun org-publish (project &optional force
)
605 (assoc (org-ido-completing-read
607 org-publish-project-alist nil t
)
608 org-publish-project-alist
)
610 (setq org-publish-initial-buffer
(current-buffer))
611 (save-window-excursion
612 (let* ((org-publish-use-timestamps-flag
613 (if force nil org-publish-use-timestamps-flag
)))
614 (org-publish-projects (list project
)))))
617 (defun org-publish-all (&optional force
)
618 "Publish all projects.
619 With prefix argument, force publish all files."
621 (org-publish-initialize-files-alist)
622 (save-window-excursion
623 (let ((org-publish-use-timestamps-flag
624 (if force nil org-publish-use-timestamps-flag
)))
625 (org-publish-projects org-publish-project-alist
))))
628 (defun org-publish-current-file (&optional force
)
629 "Publish the current file.
630 With prefix argument, force publish the file."
632 (org-publish-initialize-files-alist)
633 (save-window-excursion
634 (let ((org-publish-use-timestamps-flag
635 (if force nil org-publish-use-timestamps-flag
)))
636 (org-publish-file (buffer-file-name)))))
639 (defun org-publish-current-project (&optional force
)
640 "Publish the project associated with the current file.
641 With a prefix argument, force publishing of all files in
644 (org-publish-initialize-files-alist)
645 (save-window-excursion
646 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up
))
647 (org-publish-use-timestamps-flag
648 (if force nil org-publish-use-timestamps-flag
)))
650 (error "File %s is not part of any known project" (buffer-file-name)))
651 (org-publish project
))))
653 (provide 'org-publish
)
656 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
658 ;;; org-publish.el ends here