Agenda: Simplify old fix of face for deadline warning 0 day
[org-mode.git] / lisp / org-publish.el
blob354dcdec42975d2e4755771cec8d59d708372293
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))
605 (def-edebug-spec org-publish-with-aux-preprocess-maybe (body))
607 (defvar project-plist)
608 (defun org-publish-org-to-latex (plist filename pub-dir)
609 "Publish an org file to LaTeX.
610 See `org-publish-org-to' to the list of arguments."
611 (org-publish-with-aux-preprocess-maybe
612 (org-publish-org-to "latex" plist filename pub-dir)))
614 (defun org-publish-org-to-pdf (plist filename pub-dir)
615 "Publish an org file to PDF (via LaTeX).
616 See `org-publish-org-to' to the list of arguments."
617 (org-publish-with-aux-preprocess-maybe
618 (org-publish-org-to "pdf" plist filename pub-dir)))
620 (defun org-publish-org-to-html (plist filename pub-dir)
621 "Publish an org file to HTML.
622 See `org-publish-org-to' to the list of arguments."
623 (org-publish-with-aux-preprocess-maybe
624 (org-publish-org-to "html" plist filename pub-dir)))
626 (defun org-publish-org-to-org (plist filename pub-dir)
627 "Publish an org file to HTML.
628 See `org-publish-org-to' to the list of arguments."
629 (org-publish-org-to "org" plist filename pub-dir))
631 (defun org-publish-org-to-ascii (plist filename pub-dir)
632 "Publish an org file to ASCII.
633 See `org-publish-org-to' to the list of arguments."
634 (org-publish-with-aux-preprocess-maybe
635 (org-publish-org-to "ascii" plist filename pub-dir)))
637 (defun org-publish-org-to-latin1 (plist filename pub-dir)
638 "Publish an org file to Latin-1.
639 See `org-publish-org-to' to the list of arguments."
640 (org-publish-with-aux-preprocess-maybe
641 (org-publish-org-to "latin1" plist filename pub-dir)))
643 (defun org-publish-org-to-utf8 (plist filename pub-dir)
644 "Publish an org file to UTF-8.
645 See `org-publish-org-to' to the list of arguments."
646 (org-publish-with-aux-preprocess-maybe
647 (org-publish-org-to "utf8" plist filename pub-dir)))
649 (defun org-publish-attachment (plist filename pub-dir)
650 "Publish a file with no transformation of any kind.
651 See `org-publish-org-to' to the list of arguments."
652 ;; make sure eshell/cp code is loaded
653 (unless (file-directory-p pub-dir)
654 (make-directory pub-dir t))
655 (or (equal (expand-file-name (file-name-directory filename))
656 (file-name-as-directory (expand-file-name pub-dir)))
657 (copy-file filename
658 (expand-file-name (file-name-nondirectory filename) pub-dir)
659 t)))
661 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
662 ;;; Publishing files, sets of files, and indices
664 (defun org-publish-file (filename &optional project no-cache)
665 "Publish file FILENAME from PROJECT.
666 If NO-CACHE is not nil, do not initialize org-publish-cache and
667 write it to disk. This is needed, since this function is used to
668 publish single files, when entire projects are published.
669 See `org-publish-projects'."
670 (let* ((project
671 (or project
672 (or (org-publish-get-project-from-filename filename)
673 (error "File %s not part of any known project"
674 (abbreviate-file-name filename)))))
675 (project-plist (cdr project))
676 (ftname (expand-file-name filename))
677 (publishing-function
678 (or (plist-get project-plist :publishing-function)
679 'org-publish-org-to-html))
680 (base-dir
681 (file-name-as-directory
682 (expand-file-name
683 (or (plist-get project-plist :base-directory)
684 (error "Project %s does not have :base-directory defined"
685 (car project))))))
686 (pub-dir
687 (file-name-as-directory
688 (file-truename
689 (or (plist-get project-plist :publishing-directory)
690 (error "Project %s does not have :publishing-directory defined"
691 (car project))))))
692 tmp-pub-dir)
694 (unless no-cache
695 (org-publish-initialize-cache (car project)))
697 (setq tmp-pub-dir
698 (file-name-directory
699 (concat pub-dir
700 (and (string-match (regexp-quote base-dir) ftname)
701 (substring ftname (match-end 0))))))
702 (if (listp publishing-function)
703 ;; allow chain of publishing functions
704 (mapc (lambda (f)
705 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir)
706 (funcall f project-plist filename tmp-pub-dir)
707 (org-publish-update-timestamp filename pub-dir f)))
708 publishing-function)
709 (when (org-publish-needed-p filename pub-dir publishing-function
710 tmp-pub-dir)
711 (funcall publishing-function project-plist filename tmp-pub-dir)
712 (org-publish-update-timestamp
713 filename pub-dir publishing-function)))
714 (unless no-cache (org-publish-write-cache-file))))
716 (defun org-publish-projects (projects)
717 "Publish all files belonging to the PROJECTS alist.
718 If :auto-sitemap is set, publish the sitemap too.
719 If :makeindex is set, also produce a file theindex.org."
720 (mapc
721 (lambda (project)
722 ;; Each project uses it's own cache file:
723 (org-publish-initialize-cache (car project))
724 (let*
725 ((project-plist (cdr project))
726 (exclude-regexp (plist-get project-plist :exclude))
727 (sitemap-p (plist-get project-plist :auto-sitemap))
728 (sitemap-filename (or (plist-get project-plist :sitemap-filename)
729 "sitemap.org"))
730 (sitemap-function (or (plist-get project-plist :sitemap-function)
731 'org-publish-org-sitemap))
732 (org-sitemap-date-format (or (plist-get project-plist :sitemap-date-format)
733 org-publish-sitemap-date-format))
734 (org-sitemap-file-entry-format (or (plist-get project-plist :sitemap-file-entry-format)
735 org-publish-sitemap-file-entry-format))
736 (preparation-function (plist-get project-plist :preparation-function))
737 (completion-function (plist-get project-plist :completion-function))
738 (files (org-publish-get-base-files project exclude-regexp)) file)
739 (when preparation-function (run-hooks 'preparation-function))
740 (if sitemap-p (funcall sitemap-function project sitemap-filename))
741 (while (setq file (pop files))
742 (org-publish-file file project t))
743 (when (plist-get project-plist :makeindex)
744 (org-publish-index-generate-theindex
745 (plist-get project-plist :base-directory))
746 (org-publish-file (expand-file-name
747 "theindex.org"
748 (plist-get project-plist :base-directory))
749 project t))
750 (when completion-function (run-hooks 'completion-function))
751 (org-publish-write-cache-file)))
752 (org-publish-expand-projects projects)))
754 (defun org-publish-org-sitemap (project &optional sitemap-filename)
755 "Create a sitemap of pages in set defined by PROJECT.
756 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
757 Default for SITEMAP-FILENAME is 'sitemap.org'."
758 (let* ((project-plist (cdr project))
759 (dir (file-name-as-directory
760 (plist-get project-plist :base-directory)))
761 (localdir (file-name-directory dir))
762 (indent-str (make-string 2 ?\ ))
763 (exclude-regexp (plist-get project-plist :exclude))
764 (files (nreverse (org-publish-get-base-files project exclude-regexp)))
765 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
766 (sitemap-title (or (plist-get project-plist :sitemap-title)
767 (concat "Sitemap for project " (car project))))
768 (sitemap-style (or (plist-get project-plist :sitemap-style)
769 'tree))
770 (sitemap-sans-extension (plist-get project-plist :sitemap-sans-extension))
771 (visiting (find-buffer-visiting sitemap-filename))
772 (ifn (file-name-nondirectory sitemap-filename))
773 file sitemap-buffer)
774 (with-current-buffer (setq sitemap-buffer
775 (or visiting (find-file sitemap-filename)))
776 (erase-buffer)
777 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
778 (while (setq file (pop files))
779 (let ((fn (file-name-nondirectory file))
780 (link (file-relative-name file dir))
781 (oldlocal localdir))
782 (when sitemap-sans-extension
783 (setq link (file-name-sans-extension link)))
784 ;; sitemap shouldn't list itself
785 (unless (equal (file-truename sitemap-filename)
786 (file-truename file))
787 (if (eq sitemap-style 'list)
788 (message "Generating list-style sitemap for %s" sitemap-title)
789 (message "Generating tree-style sitemap for %s" sitemap-title)
790 (setq localdir (concat (file-name-as-directory dir)
791 (file-name-directory link)))
792 (unless (string= localdir oldlocal)
793 (if (string= localdir dir)
794 (setq indent-str (make-string 2 ?\ ))
795 (let ((subdirs
796 (split-string
797 (directory-file-name
798 (file-name-directory
799 (file-relative-name localdir dir))) "/"))
800 (subdir "")
801 (old-subdirs (split-string
802 (file-relative-name oldlocal dir) "/")))
803 (setq indent-str (make-string 2 ?\ ))
804 (while (string= (car old-subdirs) (car subdirs))
805 (setq indent-str (concat indent-str (make-string 2 ?\ )))
806 (pop old-subdirs)
807 (pop subdirs))
808 (dolist (d subdirs)
809 (setq subdir (concat subdir d "/"))
810 (insert (concat indent-str " + " d "\n"))
811 (setq indent-str (make-string
812 (+ (length indent-str) 2) ?\ )))))))
813 ;; This is common to 'flat and 'tree
814 (let ((entry
815 (org-publish-format-file-entry org-sitemap-file-entry-format
816 file project-plist))
817 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
818 (cond ((string-match-p regexp entry)
819 (string-match regexp entry)
820 (insert (concat indent-str " + " (match-string 1 entry)
821 "[[file:" link "]["
822 (match-string 2 entry)
823 "]]" (match-string 3 entry) "\n")))
825 (insert (concat indent-str " + [[file:" link "]["
826 entry
827 "]]\n"))))))))
828 (save-buffer))
829 (or visiting (kill-buffer sitemap-buffer))))
831 (defun org-publish-format-file-entry (fmt file project-plist)
832 (format-spec fmt
833 `((?t . ,(org-publish-find-title file t))
834 (?d . ,(format-time-string org-sitemap-date-format
835 (org-publish-find-date file)))
836 (?a . ,(or (plist-get project-plist :author) user-full-name)))))
838 (defun org-publish-find-title (file &optional reset)
839 "Find the title of FILE in project."
841 (and (not reset) (org-publish-cache-get-file-property file :title nil t))
842 (let* ((visiting (find-buffer-visiting file))
843 (buffer (or visiting (find-file-noselect file)))
844 title)
845 (with-current-buffer buffer
846 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
847 (org-infile-export-plist))))
848 (setq title
849 (or (plist-get opt-plist :title)
850 (and (not
851 (plist-get opt-plist :skip-before-1st-heading))
852 (org-export-grab-title-from-buffer))
853 (file-name-nondirectory (file-name-sans-extension file))))))
854 (unless visiting
855 (kill-buffer buffer))
856 (org-publish-cache-set-file-property file :title title)
857 title)))
859 (defun org-publish-find-date (file)
860 "Find the date of FILE in project.
861 If FILE provides a #+date keyword use it else use the file
862 system's modification time.
864 It returns time in `current-time' format."
865 (let ((visiting (find-buffer-visiting file)))
866 (save-excursion
867 (org-pop-to-buffer-same-window (or visiting (find-file-noselect file nil t)))
868 (let* ((plist (org-infile-export-plist))
869 (date (plist-get plist :date)))
870 (unless visiting
871 (kill-buffer (current-buffer)))
872 (if date
873 (org-time-string-to-time date)
874 (when (file-exists-p file)
875 (nth 5 (file-attributes file))))))))
877 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
878 ;;; Interactive publishing functions
880 ;;;###autoload
881 (defalias 'org-publish-project 'org-publish)
883 ;;;###autoload
884 (defun org-publish (project &optional force)
885 "Publish PROJECT."
886 (interactive
887 (list
888 (assoc (org-icompleting-read
889 "Publish project: "
890 org-publish-project-alist nil t)
891 org-publish-project-alist)
892 current-prefix-arg))
893 (setq org-publish-initial-buffer (current-buffer))
894 (save-window-excursion
895 (let* ((org-publish-use-timestamps-flag
896 (if force nil org-publish-use-timestamps-flag)))
897 (org-publish-projects
898 (if (stringp project)
899 ;; If this function is called in batch mode,
900 ;; project is still a string here.
901 (list (assoc project org-publish-project-alist))
902 (list project))))))
904 ;;;###autoload
905 (defun org-publish-all (&optional force)
906 "Publish all projects.
907 With prefix argument, remove all files in the timestamp
908 directory and force publishing all files."
909 (interactive "P")
910 (when force
911 (org-publish-remove-all-timestamps))
912 (save-window-excursion
913 (let ((org-publish-use-timestamps-flag
914 (if force nil org-publish-use-timestamps-flag)))
915 (org-publish-projects org-publish-project-alist))))
918 ;;;###autoload
919 (defun org-publish-current-file (&optional force)
920 "Publish the current file.
921 With prefix argument, force publish the file."
922 (interactive "P")
923 (save-window-excursion
924 (let ((org-publish-use-timestamps-flag
925 (if force nil org-publish-use-timestamps-flag)))
926 (org-publish-file (buffer-file-name)))))
928 ;;;###autoload
929 (defun org-publish-current-project (&optional force)
930 "Publish the project associated with the current file.
931 With a prefix argument, force publishing of all files in
932 the project."
933 (interactive "P")
934 (save-window-excursion
935 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up))
936 (org-publish-use-timestamps-flag
937 (if force nil org-publish-use-timestamps-flag)))
938 (if (not project)
939 (error "File %s is not part of any known project" (buffer-file-name)))
940 ;; FIXME: force is not used here?
941 (org-publish project))))
944 ;;; Index generation
946 (defun org-publish-aux-preprocess ()
947 "Find index entries and write them to an .orgx file."
948 (let ((case-fold-search t)
949 entry index target)
950 (goto-char (point-min))
951 (while
952 (and
953 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t)
954 (> (match-end 1) (match-beginning 1)))
955 (setq entry (match-string 1))
956 (when (eq org-export-current-backend 'latex)
957 (replace-match (format "\\index{%s}" entry) t t))
958 (save-excursion
959 (ignore-errors (org-back-to-heading t))
960 (setq target (get-text-property (point) 'target))
961 (setq target (or (cdr (assoc target org-export-preferred-target-alist))
962 (cdr (assoc target org-export-id-target-alist))
963 target ""))
964 (push (cons entry target) index)))
965 (with-temp-file
966 (concat
967 (file-name-directory org-current-export-file) "."
968 (file-name-sans-extension
969 (file-name-nondirectory org-current-export-file)) ".orgx")
970 (dolist (entry (nreverse index))
971 (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
973 (defun org-publish-index-generate-theindex (directory)
974 "Generate the index from all .orgx files in DIRECTORY."
975 (require 'find-lisp)
976 (let* ((fulldir (file-name-as-directory
977 (expand-file-name directory)))
978 (full-files (find-lisp-find-files directory "\\.orgx\\'"))
979 (re (concat "\\`" fulldir))
980 (files (mapcar (lambda (f) (if (string-match re f)
981 (substring f (match-end 0))
983 full-files))
984 (default-directory directory)
985 index origfile buf target entry ibuffer
986 main last-main letter last-letter file sub link tgext)
987 ;; `files' contains the list of relative file names
988 (dolist (file files)
989 (setq origfile (substring file 1 -1))
990 (setq buf (find-file-noselect file))
991 (with-current-buffer buf
992 (goto-char (point-min))
993 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
994 (setq target (match-string 1)
995 entry (match-string 2))
996 (push (list entry origfile target) index)))
997 (kill-buffer buf))
998 (setq index (sort index (lambda (a b) (string< (downcase (car a))
999 (downcase (car b))))))
1000 (setq ibuffer (find-file-noselect (expand-file-name "theindex.org" directory)))
1001 (with-current-buffer ibuffer
1002 (erase-buffer)
1003 (insert "* Index\n")
1004 (setq last-letter nil)
1005 (dolist (idx index)
1006 (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
1007 (if (and (stringp target) (string-match "\\S-" target))
1008 (setq tgext (concat "::#" target))
1009 (setq tgext ""))
1010 (setq letter (upcase (substring entry 0 1)))
1011 (when (not (equal letter last-letter))
1012 (insert "** " letter "\n")
1013 (setq last-letter letter))
1014 (if (string-match "!" entry)
1015 (setq main (substring entry 0 (match-beginning 0))
1016 sub (substring entry (match-end 0)))
1017 (setq main nil sub nil last-main nil))
1018 (when (and main (not (equal main last-main)))
1019 (insert " - " main "\n")
1020 (setq last-main main))
1021 (setq link (concat "[[file:" file tgext "]"
1022 "[" (or sub entry) "]]"))
1023 (if (and main sub)
1024 (insert " - " link "\n")
1025 (insert " - " link "\n")))
1026 (save-buffer))
1027 (kill-buffer ibuffer)))
1029 ;; Caching functions:
1031 (defun org-publish-write-cache-file (&optional free-cache)
1032 "Write `org-publish-cache' to file.
1033 If FREE-CACHE, empty the cache."
1034 (unless org-publish-cache
1035 (error "%s" "`org-publish-write-cache-file' called, but no cache present"))
1037 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1038 (unless cache-file
1039 (error
1040 "%s" "Cannot find cache-file name in `org-publish-write-cache-file'"))
1041 (with-temp-file cache-file
1042 (let ((print-level nil)
1043 (print-length nil))
1044 (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
1045 (maphash (lambda (k v)
1046 (insert
1047 (format (concat "(puthash %S "
1048 (if (or (listp v) (symbolp v))
1049 "'" "")
1050 "%S org-publish-cache)\n") k v)))
1051 org-publish-cache)))
1052 (when free-cache (org-publish-reset-cache))))
1054 (defun org-publish-initialize-cache (project-name)
1055 "Initialize the projects cache if not initialized yet and return it."
1057 (unless project-name
1058 (error "%s%s" "Cannot initialize `org-publish-cache' without projects name"
1059 " in `org-publish-initialize-cache'"))
1061 (unless (file-exists-p org-publish-timestamp-directory)
1062 (make-directory org-publish-timestamp-directory t))
1063 (if (not (file-directory-p org-publish-timestamp-directory))
1064 (error "Org publish timestamp: %s is not a directory"
1065 org-publish-timestamp-directory))
1067 (unless (and org-publish-cache
1068 (string= (org-publish-cache-get ":project:") project-name))
1069 (let* ((cache-file (concat
1070 (expand-file-name org-publish-timestamp-directory)
1071 project-name
1072 ".cache"))
1073 (cexists (file-exists-p cache-file)))
1075 (when org-publish-cache
1076 (org-publish-reset-cache))
1078 (if cexists
1079 (load-file cache-file)
1080 (setq org-publish-cache
1081 (make-hash-table :test 'equal :weakness nil :size 100))
1082 (org-publish-cache-set ":project:" project-name)
1083 (org-publish-cache-set ":cache-file:" cache-file))
1084 (unless cexists (org-publish-write-cache-file nil))))
1085 org-publish-cache)
1087 (defun org-publish-reset-cache ()
1088 "Empty org-publish-cache and reset it nil."
1089 (message "%s" "Resetting org-publish-cache")
1090 (if (hash-table-p org-publish-cache)
1091 (clrhash org-publish-cache))
1092 (setq org-publish-cache nil))
1094 (defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func)
1095 "Check the timestamp of the last publishing of FILENAME.
1096 Return `t', if the file needs publishing. The function also
1097 checks if any included files have been more recently published,
1098 so that the file including them will be republished as well."
1099 (unless org-publish-cache
1100 (error "%s" "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1101 (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func))
1102 (pstamp (org-publish-cache-get key))
1103 (visiting (find-buffer-visiting filename))
1104 included-files-ctime buf)
1106 (when (equal (file-name-extension filename) "org")
1107 (setq buf (find-file (expand-file-name filename)))
1108 (with-current-buffer buf
1109 (goto-char (point-min))
1110 (while (re-search-forward "^#\\+INCLUDE:[ \t]+\"?\\([^ \t\"]*\\)\"?[ \t]*.*$" nil t)
1111 (let* ((included-file (expand-file-name (match-string 1))))
1112 (add-to-list 'included-files-ctime
1113 (org-publish-cache-ctime-of-src included-file) t))))
1114 ;; FIXME don't kill current buffer
1115 (unless visiting (kill-buffer buf)))
1116 (if (null pstamp)
1118 (let ((ctime (org-publish-cache-ctime-of-src filename)))
1119 (or (< pstamp ctime)
1120 (when included-files-ctime
1121 (not (null (delq nil (mapcar (lambda(ct) (< ctime ct))
1122 included-files-ctime))))))))))
1124 (defun org-publish-cache-set-file-property (filename property value &optional project-name)
1125 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1126 Use cache file of PROJECT-NAME. If the entry does not exist, it will be
1127 created. Return VALUE."
1128 ;; Evtl. load the requested cache file:
1129 (if project-name (org-publish-initialize-cache project-name))
1130 (let ((pl (org-publish-cache-get filename)))
1131 (if pl
1132 (progn
1133 (plist-put pl property value)
1134 value)
1135 (org-publish-cache-get-file-property
1136 filename property value nil project-name))))
1138 (defun org-publish-cache-get-file-property
1139 (filename property &optional default no-create project-name)
1140 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1141 Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
1142 DEFAULT, if the value does not yet exist.
1143 If the entry will be created, unless NO-CREATE is not nil."
1144 ;; Evtl. load the requested cache file:
1145 (if project-name (org-publish-initialize-cache project-name))
1146 (let ((pl (org-publish-cache-get filename))
1147 (retval nil))
1148 (if pl
1149 (if (plist-member pl property)
1150 (setq retval (plist-get pl property))
1151 (setq retval default))
1152 ;; no pl yet:
1153 (unless no-create
1154 (org-publish-cache-set filename (list property default)))
1155 (setq retval default))
1156 retval))
1158 (defun org-publish-cache-get (key)
1159 "Return the value stored in `org-publish-cache' for key KEY.
1160 Returns nil, if no value or nil is found, or the cache does not
1161 exist."
1162 (unless org-publish-cache
1163 (error "%s" "`org-publish-cache-get' called, but no cache present"))
1164 (gethash key org-publish-cache))
1166 (defun org-publish-cache-set (key value)
1167 "Store KEY VALUE pair in `org-publish-cache'.
1168 Returns value on success, else nil."
1169 (unless org-publish-cache
1170 (error "%s" "`org-publish-cache-set' called, but no cache present"))
1171 (puthash key value org-publish-cache))
1173 (defun org-publish-cache-ctime-of-src (filename)
1174 "Get the FILENAME ctime as an integer."
1175 (let* ((symlink-maybe (or (file-symlink-p filename) filename))
1176 (src-attr (file-attributes (if (file-name-absolute-p symlink-maybe)
1177 symlink-maybe
1178 (expand-file-name
1179 symlink-maybe
1180 (file-name-directory filename))))))
1182 (lsh (car (nth 5 src-attr)) 16)
1183 (cadr (nth 5 src-attr)))))
1185 (provide 'org-publish)
1187 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
1189 ;;; org-publish.el ends here