Fix bug #9221 with memory leak in bidi display.
[emacs.git] / lisp / org / org-publish.el
blob67bccae15a27242a336b8baf9b9ea957ac0a1f12
1 ;;; org-publish.el --- publish related org-mode files as a website
2 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010
3 ;; Free Software Foundation, Inc.
5 ;; Author: David O'Toole <dto@gnu.org>
6 ;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
7 ;; Keywords: hypermedia, outlines, wp
8 ;; Version: 7.7
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This program allow configurable publishing of related sets of
28 ;; Org-mode files as a complete website.
30 ;; org-publish.el can do the following:
32 ;; + Publish all one's org-files to HTML or PDF
33 ;; + Upload HTML, images, attachments and other files to a web server
34 ;; + Exclude selected private pages from publishing
35 ;; + Publish a clickable sitemap of pages
36 ;; + Manage local timestamps for publishing only changed files
37 ;; + Accept plugin functions to extend range of publishable content
39 ;; Documentation for publishing is in the manual.
41 ;;; Code:
44 (eval-when-compile
45 (require 'cl))
46 (require 'org)
47 (require 'org-exp)
48 (require 'format-spec)
50 (eval-and-compile
51 (unless (fboundp 'declare-function)
52 (defmacro declare-function (fn file &optional arglist fileonly))))
54 (defvar org-publish-initial-buffer nil
55 "The buffer `org-publish' has been called from.")
57 (defvar org-publish-temp-files nil
58 "Temporary list of files to be published.")
60 ;; Here, so you find the variable right before it's used the first time:
61 (defvar org-publish-cache nil
62 "This will cache timestamps and titles for files in publishing projects.
63 Blocks could hash sha1 values here.")
65 (defgroup org-publish nil
66 "Options for publishing a set of Org-mode and related files."
67 :tag "Org Publishing"
68 :group 'org)
70 (defcustom org-publish-project-alist nil
71 "Association list to control publishing behavior.
72 Each element of the alist is a publishing 'project.' The CAR of
73 each element is a string, uniquely identifying the project. The
74 CDR of each element is in one of the following forms:
76 1. A well-formed property list with an even number of elements, alternating
77 keys and values, specifying parameters for the publishing process.
79 (:property value :property value ... )
81 2. A meta-project definition, specifying of a list of sub-projects:
83 (:components (\"project-1\" \"project-2\" ...))
85 When the CDR of an element of org-publish-project-alist is in
86 this second form, the elements of the list after :components are
87 taken to be components of the project, which group together files
88 requiring different publishing options. When you publish such a
89 project with \\[org-publish], the components all publish.
91 When a property is given a value in org-publish-project-alist, its
92 setting overrides the value of the corresponding user variable
93 \(if any) during publishing. However, options set within a file
94 override everything.
96 Most properties are optional, but some should always be set:
98 :base-directory Directory containing publishing source files
99 :base-extension Extension (without the dot!) of source files.
100 This can be a regular expression. If not given,
101 \"org\" will be used as default extension.
102 :publishing-directory Directory (possibly remote) where output
103 files will be published
105 The :exclude property may be used to prevent certain files from
106 being published. Its value may be a string or regexp matching
107 file names you don't want to be published.
109 The :include property may be used to include extra files. Its
110 value may be a list of filenames to include. The filenames are
111 considered relative to the base directory.
113 When both :include and :exclude properties are given values, the
114 exclusion step happens first.
116 One special property controls which back-end function to use for
117 publishing files in the project. This can be used to extend the
118 set of file types publishable by org-publish, as well as the set
119 of output formats.
121 :publishing-function Function to publish file. The default is
122 `org-publish-org-to-html', but other
123 values are possible. May also be a
124 list of functions, in which case
125 each function in the list is invoked
126 in turn.
128 Another property allows you to insert code that prepares a
129 project for publishing. For example, you could call GNU Make on a
130 certain makefile, to ensure published files are built up to date.
132 :preparation-function Function to be called before publishing
133 this project. This may also be a list
134 of functions.
135 :completion-function Function to be called after publishing
136 this project. This may also be a list
137 of functions.
139 Some properties control details of the Org publishing process,
140 and are equivalent to the corresponding user variables listed in
141 the right column. See the documentation for those variables to
142 learn more about their use and default values.
144 :language `org-export-default-language'
145 :headline-levels `org-export-headline-levels'
146 :section-numbers `org-export-with-section-numbers'
147 :table-of-contents `org-export-with-toc'
148 :emphasize `org-export-with-emphasize'
149 :sub-superscript `org-export-with-sub-superscripts'
150 :TeX-macros `org-export-with-TeX-macros'
151 :fixed-width `org-export-with-fixed-width'
152 :tables `org-export-with-tables'
153 :table-auto-headline `org-export-highlight-first-table-line'
154 :style `org-export-html-style'
155 :convert-org-links `org-export-html-link-org-files-as-html'
156 :inline-images `org-export-html-inline-images'
157 :expand-quoted-html `org-export-html-expand'
158 :timestamp `org-export-html-with-timestamp'
159 :publishing-directory `org-export-publishing-directory'
160 :html-preamble `org-export-html-preamble'
161 :html-postamble `org-export-html-postamble'
162 :author `user-full-name'
163 :email `user-mail-address'
165 The following properties may be used to control publishing of a
166 sitemap of files or summary page for a given project.
168 :auto-sitemap Whether to publish a sitemap during
169 `org-publish-current-project' or `org-publish-all'.
170 :sitemap-filename Filename for output of sitemap. Defaults
171 to 'sitemap.org' (which becomes 'sitemap.html').
172 :sitemap-title Title of sitemap page. Defaults to name of file.
173 :sitemap-function Plugin function to use for generation of sitemap.
174 Defaults to `org-publish-org-sitemap', which
175 generates a plain list of links to all files
176 in the project.
177 :sitemap-style Can be `list' (sitemap is just an itemized list
178 of the titles of the files involved) or
179 `tree' (the directory structure of the source
180 files is reflected in the sitemap). Defaults to
181 `tree'.
182 :sitemap-sans-extension Remove extension from sitemap's
183 filenames. Useful to have cool
184 URIs (see
185 http://www.w3.org/Provider/Style/URI).
186 Defaults to nil.
188 If you create a sitemap file, adjust the sorting like this:
190 :sitemap-sort-folders Where folders should appear in the sitemap.
191 Set this to `first' (default) or `last' to
192 display folders first or last, respectively.
193 Any other value will mix files and folders.
194 :sitemap-sort-files The site map is normally sorted alphabetically.
195 You can change this behaviour setting this to
196 `chronologically', `anti-chronologically' or nil.
197 :sitemap-ignore-case Should sorting be case-sensitive? Default nil.
199 The following properties control the creation of a concept index.
201 :makeindex Create a concept index.
203 Other properties affecting publication.
205 :body-only Set this to 't' to publish only the body of the
206 documents, excluding everything outside and
207 including the <body> tags in HTML, or
208 \begin{document}..\end{document} in LaTeX."
209 :group 'org-publish
210 :type 'alist)
212 (defcustom org-publish-use-timestamps-flag t
213 "Non-nil means use timestamp checking to publish only changed files.
214 When nil, do no timestamp checking and always publish all files."
215 :group 'org-publish
216 :type 'boolean)
218 (defcustom org-publish-timestamp-directory (convert-standard-filename
219 "~/.org-timestamps/")
220 "Name of directory in which to store publishing timestamps."
221 :group 'org-publish
222 :type 'directory)
224 (defcustom org-publish-list-skipped-files t
225 "Non-nil means show message about files *not* published."
226 :group 'org-publish
227 :type 'boolean)
229 (defcustom org-publish-before-export-hook nil
230 "Hook run before export on the Org file.
231 The hook may modify the file in arbitrary ways before publishing happens.
232 The original version of the buffer will be restored after publishing."
233 :group 'org-publish
234 :type 'hook)
236 (defcustom org-publish-after-export-hook nil
237 "Hook run after export on the exported buffer.
238 Any changes made by this hook will be saved."
239 :group 'org-publish
240 :type 'hook)
242 (defcustom org-publish-sitemap-sort-files 'alphabetically
243 "How sitemaps files should be sorted by default?
244 Possible values are `alphabetically', `chronologically', `anti-chronologically' and nil.
245 If `alphabetically', files will be sorted alphabetically.
246 If `chronologically', files will be sorted with older modification time first.
247 If `anti-chronologically', files will be sorted with newer modification time first.
248 nil won't sort files.
250 You can overwrite this default per project in your
251 `org-publish-project-alist', using `:sitemap-sort-files'."
252 :group 'org-publish
253 :type 'symbol)
255 (defcustom org-publish-sitemap-sort-folders 'first
256 "A symbol, denoting if folders are sorted first in sitemaps.
257 Possible values are `first', `last', and nil.
258 If `first', folders will be sorted before files.
259 If `last', folders are sorted to the end after the files.
260 Any other value will not mix files and folders.
262 You can overwrite this default per project in your
263 `org-publish-project-alist', using `:sitemap-sort-folders'."
264 :group 'org-publish
265 :type 'symbol)
267 (defcustom org-publish-sitemap-sort-ignore-case nil
268 "Sort sitemaps case insensitively by default?
270 You can overwrite this default per project in your
271 `org-publish-project-alist', using `:sitemap-ignore-case'."
272 :group 'org-publish
273 :type 'boolean)
275 (defcustom org-publish-sitemap-date-format "%Y-%m-%d"
276 "Format for `format-time-string' which is used to print a date
277 in the sitemap."
278 :group 'org-publish
279 :type 'string)
281 (defcustom org-publish-sitemap-file-entry-format "%t"
282 "How a sitemap file entry is formated.
283 You could use brackets to delimit on what part the link will be.
285 %t is the title.
286 %a is the author.
287 %d is the date formated using `org-publish-sitemap-date-format'."
288 :group 'org-publish
289 :type 'string)
292 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
293 ;;; Sanitize-plist (FIXME why?)
295 (defun org-publish-sanitize-plist (plist)
296 ;; FIXME document
297 (mapcar (lambda (x)
298 (or (cdr (assq x '((:index-filename . :sitemap-filename)
299 (:index-title . :sitemap-title)
300 (:index-function . :sitemap-function)
301 (:index-style . :sitemap-style)
302 (:auto-index . :auto-sitemap))))
304 plist))
306 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
307 ;;; Timestamp-related functions
309 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
310 "Return path to timestamp file for filename FILENAME."
311 (setq filename (concat filename "::" (or pub-dir "") "::"
312 (format "%s" (or pub-func ""))))
313 (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
315 (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir)
316 "Return t if FILENAME should be published in PUB-DIR using PUB-FUNC.
317 TRUE-PUB-DIR is where the file will truly end up. Currently we are not using
318 this - maybe it can eventually be used to check if the file is present at
319 the target location, and how old it is. Right now we cannot do this, because
320 we do not know under what file name the file will be stored - the publishing
321 function can still decide about that independently."
322 (let ((rtn
323 (if org-publish-use-timestamps-flag
324 (org-publish-cache-file-needs-publishing
325 filename pub-dir pub-func)
326 ;; don't use timestamps, always return t
327 t)))
328 (if rtn
329 (message "Publishing file %s using `%s'" filename pub-func)
330 (when org-publish-list-skipped-files
331 (message "Skipping unmodified file %s" filename)))
332 rtn))
334 (defun org-publish-update-timestamp (filename &optional pub-dir pub-func)
335 "Update publishing timestamp for file FILENAME.
336 If there is no timestamp, create one."
337 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func))
338 (stamp (org-publish-cache-ctime-of-src filename)))
339 (org-publish-cache-set key stamp)))
341 (defun org-publish-remove-all-timestamps ()
342 "Remove all files in the timestamp directory."
343 (let ((dir org-publish-timestamp-directory)
344 files)
345 (when (and (file-exists-p dir)
346 (file-directory-p dir))
347 (mapc 'delete-file (directory-files dir 'full "[^.]\\'"))
348 (org-publish-reset-cache))))
351 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
352 ;;; Compatibility aliases
354 ;; Delete-dups is not in Emacs <22
355 (if (fboundp 'delete-dups)
356 (defalias 'org-publish-delete-dups 'delete-dups)
357 (defun org-publish-delete-dups (list)
358 "Destructively remove `equal' duplicates from LIST.
359 Store the result in LIST and return it. LIST must be a proper list.
360 Of several `equal' occurrences of an element in LIST, the first
361 one is kept.
363 This is a compatibility function for Emacsen without `delete-dups'."
364 ;; Code from `subr.el' in Emacs 22:
365 (let ((tail list))
366 (while tail
367 (setcdr tail (delete (car tail) (cdr tail)))
368 (setq tail (cdr tail))))
369 list))
371 (declare-function org-publish-delete-dups "org-publish" (list))
372 (declare-function find-lisp-find-files "find-lisp" (directory regexp))
373 (declare-function org-pop-to-buffer-same-window
374 "org-compat" (&optional buffer-or-name norecord label))
376 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
377 ;;; Getting project information out of org-publish-project-alist
379 (defun org-publish-expand-projects (projects-alist)
380 "Expand projects in PROJECTS-ALIST.
381 This splices all the components into the list."
382 (let ((rest projects-alist) rtn p components)
383 (while (setq p (pop rest))
384 (if (setq components (plist-get (cdr p) :components))
385 (setq rest (append
386 (mapcar (lambda (x) (assoc x org-publish-project-alist))
387 components)
388 rest))
389 (push p rtn)))
390 (nreverse (org-publish-delete-dups (delq nil rtn)))))
392 (defvar org-sitemap-sort-files)
393 (defvar org-sitemap-sort-folders)
394 (defvar org-sitemap-ignore-case)
395 (defvar org-sitemap-requested)
396 (defvar org-sitemap-date-format)
397 (defvar org-sitemap-file-entry-format)
398 (defun org-publish-compare-directory-files (a b)
399 "Predicate for `sort', that sorts folders and files for sitemap."
400 (let ((retval t))
401 (when (or org-sitemap-sort-files org-sitemap-sort-folders)
402 ;; First we sort files:
403 (when org-sitemap-sort-files
404 (cond ((equal org-sitemap-sort-files 'alphabetically)
405 (let* ((adir (file-directory-p a))
406 (aorg (and (string-match "\\.org$" a) (not adir)))
407 (bdir (file-directory-p b))
408 (borg (and (string-match "\\.org$" b) (not bdir)))
409 (A (if aorg
410 (concat (file-name-directory a)
411 (org-publish-find-title a)) a))
412 (B (if borg
413 (concat (file-name-directory b)
414 (org-publish-find-title b)) b)))
415 (setq retval (if org-sitemap-ignore-case
416 (not (string-lessp (upcase B) (upcase A)))
417 (not (string-lessp B A))))))
418 ((or (equal org-sitemap-sort-files 'chronologically)
419 (equal org-sitemap-sort-files 'anti-chronologically))
420 (let* ((adate (org-publish-find-date a))
421 (bdate (org-publish-find-date b))
422 (A (+ (lsh (car adate) 16) (cadr adate)))
423 (B (+ (lsh (car bdate) 16) (cadr bdate))))
424 (setq retval (if (equal org-sitemap-sort-files 'chronologically)
425 (<= A B)
426 (>= A B)))))))
427 ;; Directory-wise wins:
428 (when org-sitemap-sort-folders
429 ;; a is directory, b not:
430 (cond
431 ((and (file-directory-p a) (not (file-directory-p b)))
432 (setq retval (equal org-sitemap-sort-folders 'first)))
433 ;; a is not a directory, but b is:
434 ((and (not (file-directory-p a)) (file-directory-p b))
435 (setq retval (equal org-sitemap-sort-folders 'last))))))
436 retval))
438 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
439 "Set `org-publish-temp-files' with files from BASE-DIR directory.
440 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
441 non-nil, restrict this list to the files matching the regexp
442 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
443 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
444 matching the regexp SKIP-DIR when recursing through BASE-DIR."
445 (mapc (lambda (f)
446 (let ((fd-p (file-directory-p f))
447 (fnd (file-name-nondirectory f)))
448 (if (and fd-p recurse
449 (not (string-match "^\\.+$" fnd))
450 (if skip-dir (not (string-match skip-dir fnd)) t))
451 (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
452 (unless (or fd-p ;; this is a directory
453 (and skip-file (string-match skip-file fnd))
454 (not (file-exists-p (file-truename f)))
455 (not (string-match match fnd)))
457 (pushnew f org-publish-temp-files)))))
458 (if org-sitemap-requested
459 (sort (directory-files base-dir t (unless recurse match))
460 'org-publish-compare-directory-files)
461 (directory-files base-dir t (unless recurse match)))))
463 (defun org-publish-get-base-files (project &optional exclude-regexp)
464 "Return a list of all files in PROJECT.
465 If EXCLUDE-REGEXP is set, this will be used to filter out
466 matching filenames."
467 (let* ((project-plist (cdr project))
468 (base-dir (file-name-as-directory
469 (plist-get project-plist :base-directory)))
470 (include-list (plist-get project-plist :include))
471 (recurse (plist-get project-plist :recursive))
472 (extension (or (plist-get project-plist :base-extension) "org"))
473 ;; sitemap-... variables are dynamically scoped for
474 ;; org-publish-compare-directory-files:
475 (org-sitemap-requested
476 (plist-get project-plist :auto-sitemap))
477 (sitemap-filename
478 (or (plist-get project-plist :sitemap-filename)
479 "sitemap.org"))
480 (org-sitemap-sort-folders
481 (if (plist-member project-plist :sitemap-sort-folders)
482 (plist-get project-plist :sitemap-sort-folders)
483 org-publish-sitemap-sort-folders))
484 (org-sitemap-sort-files
485 (cond ((plist-member project-plist :sitemap-sort-files)
486 (plist-get project-plist :sitemap-sort-files))
487 ;; For backward compatibility:
488 ((plist-member project-plist :sitemap-alphabetically)
489 (if (plist-get project-plist :sitemap-alphabetically)
490 'alphabetically nil))
491 (t org-publish-sitemap-sort-files)))
492 (org-sitemap-ignore-case
493 (if (plist-member project-plist :sitemap-ignore-case)
494 (plist-get project-plist :sitemap-ignore-case)
495 org-publish-sitemap-sort-ignore-case))
496 (match (if (eq extension 'any)
497 "^[^\\.]"
498 (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
499 ;; Make sure `org-sitemap-sort-folders' has an accepted value
500 (unless (memq org-sitemap-sort-folders '(first last))
501 (setq org-sitemap-sort-folders nil))
503 (setq org-publish-temp-files nil)
504 (if org-sitemap-requested
505 (pushnew (expand-file-name (concat base-dir sitemap-filename))
506 org-publish-temp-files))
507 (org-publish-get-base-files-1 base-dir recurse match
508 ;; FIXME distinguish exclude regexp
509 ;; for skip-file and skip-dir?
510 exclude-regexp exclude-regexp)
511 (mapc (lambda (f)
512 (pushnew
513 (expand-file-name (concat base-dir f))
514 org-publish-temp-files))
515 include-list)
516 org-publish-temp-files))
518 (defun org-publish-get-project-from-filename (filename &optional up)
519 "Return the project that FILENAME belongs to."
520 (let* ((filename (expand-file-name filename))
521 project-name)
523 (catch 'p-found
524 (dolist (prj org-publish-project-alist)
525 (unless (plist-get (cdr prj) :components)
526 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
527 (let* ((r (plist-get (cdr prj) :recursive))
528 (b (expand-file-name (file-name-as-directory
529 (plist-get (cdr prj) :base-directory))))
530 (x (or (plist-get (cdr prj) :base-extension) "org"))
531 (e (plist-get (cdr prj) :exclude))
532 (i (plist-get (cdr prj) :include))
533 (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
534 (when
536 (and
537 i (member filename
538 (mapcar
539 (lambda (file) (expand-file-name file b))
540 i)))
541 (and
542 (not (and e (string-match e filename)))
543 (string-match xm filename)))
544 (setq project-name (car prj))
545 (throw 'p-found project-name))))))
546 (when up
547 (dolist (prj org-publish-project-alist)
548 (if (member project-name (plist-get (cdr prj) :components))
549 (setq project-name (car prj)))))
550 (assoc project-name org-publish-project-alist)))
552 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
553 ;;; Pluggable publishing back-end functions
555 (defun org-publish-org-to (format plist filename pub-dir)
556 "Publish an org file to FORMAT.
557 PLIST is the property list for the given project.
558 FILENAME is the filename of the org file to be published.
559 PUB-DIR is the publishing directory."
560 (require 'org)
561 (unless (file-exists-p pub-dir)
562 (make-directory pub-dir t))
563 (let ((visiting (find-buffer-visiting filename)))
564 (save-excursion
565 (org-pop-to-buffer-same-window (or visiting (find-file filename)))
566 (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
567 (init-buf (current-buffer))
568 (init-point (point))
569 (init-buf-string (buffer-string))
570 export-buf-or-file)
571 ;; run hooks before exporting
572 (run-hooks 'org-publish-before-export-hook)
573 ;; export the possibly modified buffer
574 (setq export-buf-or-file
575 (funcall (intern (concat "org-export-as-" format))
576 (plist-get plist :headline-levels)
577 nil plist nil
578 (plist-get plist :body-only)
579 pub-dir))
580 (when (and (bufferp export-buf-or-file)
581 (buffer-live-p export-buf-or-file))
582 (set-buffer export-buf-or-file)
583 ;; run hooks after export and save export
584 (progn (run-hooks 'org-publish-after-export-hook)
585 (if (buffer-modified-p) (save-buffer)))
586 (kill-buffer export-buf-or-file))
587 ;; maybe restore buffer's content
588 (set-buffer init-buf)
589 (when (buffer-modified-p init-buf)
590 (erase-buffer)
591 (insert init-buf-string)
592 (save-buffer)
593 (goto-char init-point))
594 (unless visiting
595 (kill-buffer init-buf))))))
597 (defmacro org-publish-with-aux-preprocess-maybe (&rest body)
598 "Execute BODY with a modified hook to preprocess for index."
599 `(let ((org-export-preprocess-after-headline-targets-hook
600 (if (plist-get project-plist :makeindex)
601 (cons 'org-publish-aux-preprocess
602 org-export-preprocess-after-headline-targets-hook)
603 org-export-preprocess-after-headline-targets-hook)))
604 ,@body))
606 (defvar project-plist)
607 (defun org-publish-org-to-latex (plist filename pub-dir)
608 "Publish an org file to LaTeX.
609 See `org-publish-org-to' to the list of arguments."
610 (org-publish-with-aux-preprocess-maybe
611 (org-publish-org-to "latex" plist filename pub-dir)))
613 (defun org-publish-org-to-pdf (plist filename pub-dir)
614 "Publish an org file to PDF (via LaTeX).
615 See `org-publish-org-to' to the list of arguments."
616 (org-publish-with-aux-preprocess-maybe
617 (org-publish-org-to "pdf" plist filename pub-dir)))
619 (defun org-publish-org-to-html (plist filename pub-dir)
620 "Publish an org file to HTML.
621 See `org-publish-org-to' to the list of arguments."
622 (org-publish-with-aux-preprocess-maybe
623 (org-publish-org-to "html" plist filename pub-dir)))
625 (defun org-publish-org-to-org (plist filename pub-dir)
626 "Publish an org file to HTML.
627 See `org-publish-org-to' to the list of arguments."
628 (org-publish-org-to "org" plist filename pub-dir))
630 (defun org-publish-org-to-ascii (plist filename pub-dir)
631 "Publish an org file to ASCII.
632 See `org-publish-org-to' to the list of arguments."
633 (org-publish-with-aux-preprocess-maybe
634 (org-publish-org-to "ascii" plist filename pub-dir)))
636 (defun org-publish-org-to-latin1 (plist filename pub-dir)
637 "Publish an org file to Latin-1.
638 See `org-publish-org-to' to the list of arguments."
639 (org-publish-with-aux-preprocess-maybe
640 (org-publish-org-to "latin1" plist filename pub-dir)))
642 (defun org-publish-org-to-utf8 (plist filename pub-dir)
643 "Publish an org file to UTF-8.
644 See `org-publish-org-to' to the list of arguments."
645 (org-publish-with-aux-preprocess-maybe
646 (org-publish-org-to "utf8" plist filename pub-dir)))
648 (defun org-publish-attachment (plist filename pub-dir)
649 "Publish a file with no transformation of any kind.
650 See `org-publish-org-to' to the list of arguments."
651 ;; make sure eshell/cp code is loaded
652 (unless (file-directory-p pub-dir)
653 (make-directory pub-dir t))
654 (or (equal (expand-file-name (file-name-directory filename))
655 (file-name-as-directory (expand-file-name pub-dir)))
656 (copy-file filename
657 (expand-file-name (file-name-nondirectory filename) pub-dir)
658 t)))
660 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
661 ;;; Publishing files, sets of files, and indices
663 (defun org-publish-file (filename &optional project no-cache)
664 "Publish file FILENAME from PROJECT.
665 If NO-CACHE is not nil, do not initialize org-publish-cache and
666 write it to disk. This is needed, since this function is used to
667 publish single files, when entire projects are published.
668 See `org-publish-projects'."
669 (let* ((project
670 (or project
671 (or (org-publish-get-project-from-filename filename)
672 (error "File %s not part of any known project"
673 (abbreviate-file-name filename)))))
674 (project-plist (cdr project))
675 (ftname (expand-file-name filename))
676 (publishing-function
677 (or (plist-get project-plist :publishing-function)
678 'org-publish-org-to-html))
679 (base-dir
680 (file-name-as-directory
681 (expand-file-name
682 (or (plist-get project-plist :base-directory)
683 (error "Project %s does not have :base-directory defined"
684 (car project))))))
685 (pub-dir
686 (file-name-as-directory
687 (file-truename
688 (or (plist-get project-plist :publishing-directory)
689 (error "Project %s does not have :publishing-directory defined"
690 (car project))))))
691 tmp-pub-dir)
693 (unless no-cache
694 (org-publish-initialize-cache (car project)))
696 (setq tmp-pub-dir
697 (file-name-directory
698 (concat pub-dir
699 (and (string-match (regexp-quote base-dir) ftname)
700 (substring ftname (match-end 0))))))
701 (if (listp publishing-function)
702 ;; allow chain of publishing functions
703 (mapc (lambda (f)
704 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir)
705 (funcall f project-plist filename tmp-pub-dir)
706 (org-publish-update-timestamp filename pub-dir f)))
707 publishing-function)
708 (when (org-publish-needed-p filename pub-dir publishing-function
709 tmp-pub-dir)
710 (funcall publishing-function project-plist filename tmp-pub-dir)
711 (org-publish-update-timestamp
712 filename pub-dir publishing-function)))
713 (unless no-cache (org-publish-write-cache-file))))
715 (defun org-publish-projects (projects)
716 "Publish all files belonging to the PROJECTS alist.
717 If :auto-sitemap is set, publish the sitemap too.
718 If :makeindex is set, also produce a file theindex.org."
719 (mapc
720 (lambda (project)
721 ;; Each project uses it's own cache file:
722 (org-publish-initialize-cache (car project))
723 (let*
724 ((project-plist (cdr project))
725 (exclude-regexp (plist-get project-plist :exclude))
726 (sitemap-p (plist-get project-plist :auto-sitemap))
727 (sitemap-filename (or (plist-get project-plist :sitemap-filename)
728 "sitemap.org"))
729 (sitemap-function (or (plist-get project-plist :sitemap-function)
730 'org-publish-org-sitemap))
731 (org-sitemap-date-format (or (plist-get project-plist :sitemap-date-format)
732 org-publish-sitemap-date-format))
733 (org-sitemap-file-entry-format (or (plist-get project-plist :sitemap-file-entry-format)
734 org-publish-sitemap-file-entry-format))
735 (preparation-function (plist-get project-plist :preparation-function))
736 (completion-function (plist-get project-plist :completion-function))
737 (files (org-publish-get-base-files project exclude-regexp)) file)
738 (when preparation-function (run-hooks 'preparation-function))
739 (if sitemap-p (funcall sitemap-function project sitemap-filename))
740 (while (setq file (pop files))
741 (org-publish-file file project t))
742 (when (plist-get project-plist :makeindex)
743 (org-publish-index-generate-theindex
744 (plist-get project-plist :base-directory))
745 (org-publish-file (expand-file-name
746 "theindex.org"
747 (plist-get project-plist :base-directory))
748 project t))
749 (when completion-function (run-hooks 'completion-function))
750 (org-publish-write-cache-file)))
751 (org-publish-expand-projects projects)))
753 (defun org-publish-org-sitemap (project &optional sitemap-filename)
754 "Create a sitemap of pages in set defined by PROJECT.
755 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
756 Default for SITEMAP-FILENAME is 'sitemap.org'."
757 (let* ((project-plist (cdr project))
758 (dir (file-name-as-directory
759 (plist-get project-plist :base-directory)))
760 (localdir (file-name-directory dir))
761 (indent-str (make-string 2 ?\ ))
762 (exclude-regexp (plist-get project-plist :exclude))
763 (files (nreverse (org-publish-get-base-files project exclude-regexp)))
764 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
765 (sitemap-title (or (plist-get project-plist :sitemap-title)
766 (concat "Sitemap for project " (car project))))
767 (sitemap-style (or (plist-get project-plist :sitemap-style)
768 'tree))
769 (sitemap-sans-extension (plist-get project-plist :sitemap-sans-extension))
770 (visiting (find-buffer-visiting sitemap-filename))
771 (ifn (file-name-nondirectory sitemap-filename))
772 file sitemap-buffer)
773 (with-current-buffer (setq sitemap-buffer
774 (or visiting (find-file sitemap-filename)))
775 (erase-buffer)
776 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
777 (while (setq file (pop files))
778 (let ((fn (file-name-nondirectory file))
779 (link (file-relative-name file dir))
780 (oldlocal localdir))
781 (when sitemap-sans-extension
782 (setq link (file-name-sans-extension link)))
783 ;; sitemap shouldn't list itself
784 (unless (equal (file-truename sitemap-filename)
785 (file-truename file))
786 (if (eq sitemap-style 'list)
787 (message "Generating list-style sitemap for %s" sitemap-title)
788 (message "Generating tree-style sitemap for %s" sitemap-title)
789 (setq localdir (concat (file-name-as-directory dir)
790 (file-name-directory link)))
791 (unless (string= localdir oldlocal)
792 (if (string= localdir dir)
793 (setq indent-str (make-string 2 ?\ ))
794 (let ((subdirs
795 (split-string
796 (directory-file-name
797 (file-name-directory
798 (file-relative-name localdir dir))) "/"))
799 (subdir "")
800 (old-subdirs (split-string
801 (file-relative-name oldlocal dir) "/")))
802 (setq indent-str (make-string 2 ?\ ))
803 (while (string= (car old-subdirs) (car subdirs))
804 (setq indent-str (concat indent-str (make-string 2 ?\ )))
805 (pop old-subdirs)
806 (pop subdirs))
807 (dolist (d subdirs)
808 (setq subdir (concat subdir d "/"))
809 (insert (concat indent-str " + " d "\n"))
810 (setq indent-str (make-string
811 (+ (length indent-str) 2) ?\ )))))))
812 ;; This is common to 'flat and 'tree
813 (let ((entry
814 (org-publish-format-file-entry org-sitemap-file-entry-format
815 file project-plist))
816 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
817 (cond ((string-match-p regexp entry)
818 (string-match regexp entry)
819 (insert (concat indent-str " + " (match-string 1 entry)
820 "[[file:" link "]["
821 (match-string 2 entry)
822 "]]" (match-string 3 entry) "\n")))
824 (insert (concat indent-str " + [[file:" link "]["
825 entry
826 "]]\n"))))))))
827 (save-buffer))
828 (or visiting (kill-buffer sitemap-buffer))))
830 (defun org-publish-format-file-entry (fmt file project-plist)
831 (format-spec fmt
832 `((?t . ,(org-publish-find-title file t))
833 (?d . ,(format-time-string org-sitemap-date-format
834 (org-publish-find-date file)))
835 (?a . ,(or (plist-get project-plist :author) user-full-name)))))
837 (defun org-publish-find-title (file &optional reset)
838 "Find the title of FILE in project."
840 (and (not reset) (org-publish-cache-get-file-property file :title nil t))
841 (let* ((visiting (find-buffer-visiting file))
842 (buffer (or visiting (find-file-noselect file)))
843 title)
844 (with-current-buffer buffer
845 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
846 (org-infile-export-plist))))
847 (setq title
848 (or (plist-get opt-plist :title)
849 (and (not
850 (plist-get opt-plist :skip-before-1st-heading))
851 (org-export-grab-title-from-buffer))
852 (file-name-nondirectory (file-name-sans-extension file))))))
853 (unless visiting
854 (kill-buffer buffer))
855 (org-publish-cache-set-file-property file :title title)
856 title)))
858 (defun org-publish-find-date (file)
859 "Find the date of FILE in project.
860 If FILE provides a #+date keyword use it else use the file
861 system's modification time.
863 It returns time in `current-time' format."
864 (let ((visiting (find-buffer-visiting file)))
865 (save-excursion
866 (org-pop-to-buffer-same-window (or visiting (find-file-noselect file nil t)))
867 (let* ((plist (org-infile-export-plist))
868 (date (plist-get plist :date)))
869 (unless visiting
870 (kill-buffer (current-buffer)))
871 (if date
872 (org-time-string-to-time date)
873 (when (file-exists-p file)
874 (nth 5 (file-attributes file))))))))
876 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
877 ;;; Interactive publishing functions
879 ;;;###autoload
880 (defalias 'org-publish-project 'org-publish)
882 ;;;###autoload
883 (defun org-publish (project &optional force)
884 "Publish PROJECT."
885 (interactive
886 (list
887 (assoc (org-icompleting-read
888 "Publish project: "
889 org-publish-project-alist nil t)
890 org-publish-project-alist)
891 current-prefix-arg))
892 (setq org-publish-initial-buffer (current-buffer))
893 (save-window-excursion
894 (let* ((org-publish-use-timestamps-flag
895 (if force nil org-publish-use-timestamps-flag)))
896 (org-publish-projects
897 (if (stringp project)
898 ;; If this function is called in batch mode,
899 ;; project is still a string here.
900 (list (assoc project org-publish-project-alist))
901 (list project))))))
903 ;;;###autoload
904 (defun org-publish-all (&optional force)
905 "Publish all projects.
906 With prefix argument, remove all files in the timestamp
907 directory and force publishing all files."
908 (interactive "P")
909 (when force
910 (org-publish-remove-all-timestamps))
911 (save-window-excursion
912 (let ((org-publish-use-timestamps-flag
913 (if force nil org-publish-use-timestamps-flag)))
914 (org-publish-projects org-publish-project-alist))))
917 ;;;###autoload
918 (defun org-publish-current-file (&optional force)
919 "Publish the current file.
920 With prefix argument, force publish the file."
921 (interactive "P")
922 (save-window-excursion
923 (let ((org-publish-use-timestamps-flag
924 (if force nil org-publish-use-timestamps-flag)))
925 (org-publish-file (buffer-file-name)))))
927 ;;;###autoload
928 (defun org-publish-current-project (&optional force)
929 "Publish the project associated with the current file.
930 With a prefix argument, force publishing of all files in
931 the project."
932 (interactive "P")
933 (save-window-excursion
934 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up))
935 (org-publish-use-timestamps-flag
936 (if force nil org-publish-use-timestamps-flag)))
937 (if (not project)
938 (error "File %s is not part of any known project" (buffer-file-name)))
939 ;; FIXME: force is not used here?
940 (org-publish project))))
943 ;;; Index generation
945 (defun org-publish-aux-preprocess ()
946 "Find index entries and write them to an .orgx file."
947 (let ((case-fold-search t)
948 entry index target)
949 (goto-char (point-min))
950 (while
951 (and
952 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t)
953 (> (match-end 1) (match-beginning 1)))
954 (setq entry (match-string 1))
955 (when (eq org-export-current-backend 'latex)
956 (replace-match (format "\\index{%s}" entry) t t))
957 (save-excursion
958 (ignore-errors (org-back-to-heading t))
959 (setq target (get-text-property (point) 'target))
960 (setq target (or (cdr (assoc target org-export-preferred-target-alist))
961 (cdr (assoc target org-export-id-target-alist))
962 target ""))
963 (push (cons entry target) index)))
964 (with-temp-file
965 (concat
966 (file-name-directory org-current-export-file) "."
967 (file-name-sans-extension
968 (file-name-nondirectory org-current-export-file)) ".orgx")
969 (dolist (entry (nreverse index))
970 (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
972 (defun org-publish-index-generate-theindex (directory)
973 "Generate the index from all .orgx files in DIRECTORY."
974 (require 'find-lisp)
975 (let* ((fulldir (file-name-as-directory
976 (expand-file-name directory)))
977 (full-files (find-lisp-find-files directory "\\.orgx\\'"))
978 (re (concat "\\`" fulldir))
979 (files (mapcar (lambda (f) (if (string-match re f)
980 (substring f (match-end 0))
982 full-files))
983 (default-directory directory)
984 index origfile buf target entry ibuffer
985 main last-main letter last-letter file sub link tgext)
986 ;; `files' contains the list of relative file names
987 (dolist (file files)
988 (setq origfile (substring file 1 -1))
989 (setq buf (find-file-noselect file))
990 (with-current-buffer buf
991 (goto-char (point-min))
992 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
993 (setq target (match-string 1)
994 entry (match-string 2))
995 (push (list entry origfile target) index)))
996 (kill-buffer buf))
997 (setq index (sort index (lambda (a b) (string< (downcase (car a))
998 (downcase (car b))))))
999 (setq ibuffer (find-file-noselect (expand-file-name "theindex.org" directory)))
1000 (with-current-buffer ibuffer
1001 (erase-buffer)
1002 (insert "* Index\n")
1003 (setq last-letter nil)
1004 (dolist (idx index)
1005 (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
1006 (if (and (stringp target) (string-match "\\S-" target))
1007 (setq tgext (concat "::#" target))
1008 (setq tgext ""))
1009 (setq letter (upcase (substring entry 0 1)))
1010 (when (not (equal letter last-letter))
1011 (insert "** " letter "\n")
1012 (setq last-letter letter))
1013 (if (string-match "!" entry)
1014 (setq main (substring entry 0 (match-beginning 0))
1015 sub (substring entry (match-end 0)))
1016 (setq main nil sub nil last-main nil))
1017 (when (and main (not (equal main last-main)))
1018 (insert " - " main "\n")
1019 (setq last-main main))
1020 (setq link (concat "[[file:" file tgext "]"
1021 "[" (or sub entry) "]]"))
1022 (if (and main sub)
1023 (insert " - " link "\n")
1024 (insert " - " link "\n")))
1025 (save-buffer))
1026 (kill-buffer ibuffer)))
1028 ;; Caching functions:
1030 (defun org-publish-write-cache-file (&optional free-cache)
1031 "Write `org-publish-cache' to file.
1032 If FREE-CACHE, empty the cache."
1033 (unless org-publish-cache
1034 (error "%s" "`org-publish-write-cache-file' called, but no cache present"))
1036 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1037 (unless cache-file
1038 (error
1039 "%s" "Cannot find cache-file name in `org-publish-write-cache-file'"))
1040 (with-temp-file cache-file
1041 (let ((print-level nil)
1042 (print-length nil))
1043 (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
1044 (maphash (lambda (k v)
1045 (insert
1046 (format (concat "(puthash %S "
1047 (if (or (listp v) (symbolp v))
1048 "'" "")
1049 "%S org-publish-cache)\n") k v)))
1050 org-publish-cache)))
1051 (when free-cache (org-publish-reset-cache))))
1053 (defun org-publish-initialize-cache (project-name)
1054 "Initialize the projects cache if not initialized yet and return it."
1056 (unless project-name
1057 (error "%s%s" "Cannot initialize `org-publish-cache' without projects name"
1058 " in `org-publish-initialize-cache'"))
1060 (unless (file-exists-p org-publish-timestamp-directory)
1061 (make-directory org-publish-timestamp-directory t))
1062 (if (not (file-directory-p org-publish-timestamp-directory))
1063 (error "Org publish timestamp: %s is not a directory"
1064 org-publish-timestamp-directory))
1066 (unless (and org-publish-cache
1067 (string= (org-publish-cache-get ":project:") project-name))
1068 (let* ((cache-file (concat
1069 (expand-file-name org-publish-timestamp-directory)
1070 project-name
1071 ".cache"))
1072 (cexists (file-exists-p cache-file)))
1074 (when org-publish-cache
1075 (org-publish-reset-cache))
1077 (if cexists
1078 (load-file cache-file)
1079 (setq org-publish-cache
1080 (make-hash-table :test 'equal :weakness nil :size 100))
1081 (org-publish-cache-set ":project:" project-name)
1082 (org-publish-cache-set ":cache-file:" cache-file))
1083 (unless cexists (org-publish-write-cache-file nil))))
1084 org-publish-cache)
1086 (defun org-publish-reset-cache ()
1087 "Empty org-publish-cache and reset it nil."
1088 (message "%s" "Resetting org-publish-cache")
1089 (if (hash-table-p org-publish-cache)
1090 (clrhash org-publish-cache))
1091 (setq org-publish-cache nil))
1093 (defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func)
1094 "Check the timestamp of the last publishing of FILENAME.
1095 Return `t', if the file needs publishing. The function also
1096 checks if any included files have been more recently published,
1097 so that the file including them will be republished as well."
1098 (unless org-publish-cache
1099 (error "%s" "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1100 (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func))
1101 (pstamp (org-publish-cache-get key))
1102 (visiting (find-buffer-visiting filename))
1103 included-files-ctime buf)
1105 (when (equal (file-name-extension filename) "org")
1106 (setq buf (find-file (expand-file-name filename)))
1107 (with-current-buffer buf
1108 (goto-char (point-min))
1109 (while (re-search-forward "^#\\+INCLUDE:[ \t]+\"?\\([^ \t\"]*\\)\"?[ \t]*.*$" nil t)
1110 (let* ((included-file (expand-file-name (match-string 1))))
1111 (add-to-list 'included-files-ctime
1112 (org-publish-cache-ctime-of-src included-file) t))))
1113 ;; FIXME don't kill current buffer
1114 (unless visiting (kill-buffer buf)))
1115 (if (null pstamp)
1117 (let ((ctime (org-publish-cache-ctime-of-src filename)))
1118 (or (< pstamp ctime)
1119 (when included-files-ctime
1120 (not (null (delq nil (mapcar (lambda(ct) (< ctime ct))
1121 included-files-ctime))))))))))
1123 (defun org-publish-cache-set-file-property (filename property value &optional project-name)
1124 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1125 Use cache file of PROJECT-NAME. If the entry does not exist, it will be
1126 created. Return VALUE."
1127 ;; Evtl. load the requested cache file:
1128 (if project-name (org-publish-initialize-cache project-name))
1129 (let ((pl (org-publish-cache-get filename)))
1130 (if pl
1131 (progn
1132 (plist-put pl property value)
1133 value)
1134 (org-publish-cache-get-file-property
1135 filename property value nil project-name))))
1137 (defun org-publish-cache-get-file-property
1138 (filename property &optional default no-create project-name)
1139 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1140 Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
1141 DEFAULT, if the value does not yet exist.
1142 If the entry will be created, unless NO-CREATE is not nil."
1143 ;; Evtl. load the requested cache file:
1144 (if project-name (org-publish-initialize-cache project-name))
1145 (let ((pl (org-publish-cache-get filename))
1146 (retval nil))
1147 (if pl
1148 (if (plist-member pl property)
1149 (setq retval (plist-get pl property))
1150 (setq retval default))
1151 ;; no pl yet:
1152 (unless no-create
1153 (org-publish-cache-set filename (list property default)))
1154 (setq retval default))
1155 retval))
1157 (defun org-publish-cache-get (key)
1158 "Return the value stored in `org-publish-cache' for key KEY.
1159 Returns nil, if no value or nil is found, or the cache does not
1160 exist."
1161 (unless org-publish-cache
1162 (error "%s" "`org-publish-cache-get' called, but no cache present"))
1163 (gethash key org-publish-cache))
1165 (defun org-publish-cache-set (key value)
1166 "Store KEY VALUE pair in `org-publish-cache'.
1167 Returns value on success, else nil."
1168 (unless org-publish-cache
1169 (error "%s" "`org-publish-cache-set' called, but no cache present"))
1170 (puthash key value org-publish-cache))
1172 (defun org-publish-cache-ctime-of-src (filename)
1173 "Get the FILENAME ctime as an integer."
1174 (let* ((symlink-maybe (or (file-symlink-p filename) filename))
1175 (src-attr (file-attributes (if (file-name-absolute-p symlink-maybe)
1176 symlink-maybe
1177 (expand-file-name
1178 symlink-maybe
1179 (file-name-directory filename))))))
1181 (lsh (car (nth 5 src-attr)) 16)
1182 (cadr (nth 5 src-attr)))))
1184 (provide 'org-publish)
1186 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
1188 ;;; org-publish.el ends here