Merge branch 'fix-todo-list-with-extended-today'
[org-mode.git] / lisp / org-publish.el
blobc3f33706842dd92b07688200a6a21695bd18803b
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.4
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 (defun org-publish-sanitize-plist (plist)
45 (mapcar (lambda (x)
46 (or (cdr (assq x '((:index-filename . :sitemap-filename)
47 (:index-title . :sitemap-title)
48 (:index-function . :sitemap-function)
49 (:index-style . :sitemap-style)
50 (:auto-index . :auto-sitemap))))
51 x))
52 plist))
54 (eval-when-compile
55 (require 'cl))
56 (require 'org)
57 (require 'org-exp)
59 (eval-and-compile
60 (unless (fboundp 'declare-function)
61 (defmacro declare-function (fn file &optional arglist fileonly))))
63 (defgroup org-publish nil
64 "Options for publishing a set of Org-mode and related files."
65 :tag "Org Publishing"
66 :group 'org)
68 (defcustom org-publish-project-alist nil
69 "Association list to control publishing behavior.
70 Each element of the alist is a publishing 'project.' The CAR of
71 each element is a string, uniquely identifying the project. The
72 CDR of each element is in one of the following forms:
74 1. A well-formed property list with an even number of elements, alternating
75 keys and values, specifying parameters for the publishing process.
77 (:property value :property value ... )
79 2. A meta-project definition, specifying of a list of sub-projects:
81 (:components (\"project-1\" \"project-2\" ...))
83 When the CDR of an element of org-publish-project-alist is in
84 this second form, the elements of the list after :components are
85 taken to be components of the project, which group together files
86 requiring different publishing options. When you publish such a
87 project with \\[org-publish], the components all publish.
89 When a property is given a value in org-publish-project-alist, its
90 setting overrides the value of the corresponding user variable
91 \(if any) during publishing. However, options set within a file
92 override everything.
94 Most properties are optional, but some should always be set:
96 :base-directory Directory containing publishing source files
97 :base-extension Extension (without the dot!) of source files.
98 This can be a regular expression. If not given,
99 \"org\" will be used as default extension.
100 :publishing-directory Directory (possibly remote) where output
101 files will be published
103 The :exclude property may be used to prevent certain files from
104 being published. Its value may be a string or regexp matching
105 file names you don't want to be published.
107 The :include property may be used to include extra files. Its
108 value may be a list of filenames to include. The filenames are
109 considered relative to the base directory.
111 When both :include and :exclude properties are given values, the
112 exclusion step happens first.
114 One special property controls which back-end function to use for
115 publishing files in the project. This can be used to extend the
116 set of file types publishable by org-publish, as well as the set
117 of output formats.
119 :publishing-function Function to publish file. The default is
120 `org-publish-org-to-html', but other
121 values are possible. May also be a
122 list of functions, in which case
123 each function in the list is invoked
124 in turn.
126 Another property allows you to insert code that prepares a
127 project for publishing. For example, you could call GNU Make on a
128 certain makefile, to ensure published files are built up to date.
130 :preparation-function Function to be called before publishing
131 this project. This may also be a list
132 of functions.
133 :completion-function Function to be called after publishing
134 this project. This may also be a list
135 of functions.
137 Some properties control details of the Org publishing process,
138 and are equivalent to the corresponding user variables listed in
139 the right column. See the documentation for those variables to
140 learn more about their use and default values.
142 :language `org-export-default-language'
143 :headline-levels `org-export-headline-levels'
144 :section-numbers `org-export-with-section-numbers'
145 :table-of-contents `org-export-with-toc'
146 :emphasize `org-export-with-emphasize'
147 :sub-superscript `org-export-with-sub-superscripts'
148 :TeX-macros `org-export-with-TeX-macros'
149 :fixed-width `org-export-with-fixed-width'
150 :tables `org-export-with-tables'
151 :table-auto-headline `org-export-highlight-first-table-line'
152 :style `org-export-html-style'
153 :convert-org-links `org-export-html-link-org-files-as-html'
154 :inline-images `org-export-html-inline-images'
155 :expand-quoted-html `org-export-html-expand'
156 :timestamp `org-export-html-with-timestamp'
157 :publishing-directory `org-export-publishing-directory'
158 :preamble `org-export-html-preamble'
159 :postamble `org-export-html-postamble'
160 :auto-preamble `org-export-html-auto-preamble'
161 :auto-postamble `org-export-html-auto-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'.
183 If you create a sitemap file, adjust the sorting like this:
185 :sitemap-sort-folders Where folders should appear in the sitemap.
186 Set this to `first' (default) or `last' to
187 display folders first or last, respectively.
188 Any other value will mix files and folders.
189 :sitemap-alphabetically The site map is normally sorted alphabetically.
190 Set this explicitly to nil to turn off sorting.
191 :sitemap-ignore-case Should sorting be case-sensitive? Default nil.
193 The following properties control the creation of a concept index.
195 :makeindex Create a concept index.
197 Other properties affecting publication.
199 :body-only Set this to 't' to publish only the body of the
200 documents, excluding everything outside and
201 including the <body> tags in HTML, or
202 \begin{document}..\end{document} in LaTeX."
203 :group 'org-publish
204 :type 'alist)
206 (defcustom org-publish-use-timestamps-flag t
207 "Non-nil means use timestamp checking to publish only changed files.
208 When nil, do no timestamp checking and always publish all files."
209 :group 'org-publish
210 :type 'boolean)
212 (defcustom org-publish-timestamp-directory (convert-standard-filename
213 "~/.org-timestamps/")
214 "Name of directory in which to store publishing timestamps."
215 :group 'org-publish
216 :type 'directory)
218 (defcustom org-publish-list-skipped-files t
219 "Non-nil means show message about files *not* published."
220 :group 'org-publish
221 :type 'boolean)
223 (defcustom org-publish-before-export-hook nil
224 "Hook run before export on the Org file.
225 The hook may modify the file in arbitrary ways before publishing happens.
226 The original version of the buffer will be restored after publishing."
227 :group 'org-publish
228 :type 'hook)
230 (defcustom org-publish-after-export-hook nil
231 "Hook run after export on the exported buffer.
232 Any changes made by this hook will be saved."
233 :group 'org-publish
234 :type 'hook)
236 (defcustom org-publish-sitemap-sort-alphabetically t
237 "Should sitemaps be sorted alphabetically by default?
239 You can overwrite this default per project in your
240 `org-publish-project-alist', using `:sitemap-alphabetically'."
241 :group 'org-publish
242 :type 'boolean)
244 (defcustom org-publish-sitemap-sort-folders 'first
245 "A symbol, denoting if folders are sorted first in sitemaps.
246 Possible values are `first', `last', and nil.
247 If `first', folders will be sorted before files.
248 If `last', folders are sorted to the end after the files.
249 Any other value will not mix files and folders.
251 You can overwrite this default per project in your
252 `org-publish-project-alist', using `:sitemap-sort-folders'."
253 :group 'org-publish
254 :type 'symbol)
256 (defcustom org-publish-sitemap-sort-ignore-case nil
257 "Sort sitemaps case insensitively by default?
259 You can overwrite this default per project in your
260 `org-publish-project-alist', using `:sitemap-ignore-case'."
261 :group 'org-publish
262 :type 'boolean)
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265 ;;; Timestamp-related functions
267 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
268 "Return path to timestamp file for filename FILENAME."
269 (setq filename (concat filename "::" (or pub-dir "") "::"
270 (format "%s" (or pub-func ""))))
271 (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
273 (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir)
274 "Return t if FILENAME should be published in PUB-DIR using PUB-FUNC.
275 TRUE-PUB-DIR is where the file will truly end up. Currently we are not using
276 this - maybe it can eventually be used to check if the file is present at
277 the target location, and how old it is. Right ow we cannot do this, because
278 we do not know under what file name the file will be stored - the publishing
279 function can still decide about that independently."
280 (let ((rtn
281 (if org-publish-use-timestamps-flag
282 (org-publish-cache-file-needs-publishing
283 filename pub-dir pub-func)
284 ;; don't use timestamps, always return t
285 t)))
286 (if rtn
287 (message "Publishing file %s using `%s'" filename pub-func)
288 (when org-publish-list-skipped-files
289 (message "Skipping unmodified file %s" filename)))
290 rtn))
292 (defun org-publish-update-timestamp (filename &optional pub-dir pub-func)
293 "Update publishing timestamp for file FILENAME.
294 If there is no timestamp, create one."
295 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func))
296 (stamp (org-publish-cache-ctime-of-src filename)))
297 (org-publish-cache-set key stamp)))
299 (defun org-publish-remove-all-timestamps ()
300 "Remove all files in the timestamp directory."
301 (let ((dir org-publish-timestamp-directory)
302 files)
303 (when (and (file-exists-p dir)
304 (file-directory-p dir))
305 (mapc 'delete-file (directory-files dir 'full "[^.]\\'"))
306 (org-publish-reset-cache))))
309 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
312 (defvar org-publish-initial-buffer nil
313 "The buffer `org-publish' has been called from.")
314 (defvar org-publish-temp-files nil
315 "Temporary list of files to be published.")
317 ;; Here, so you find the variable right before it's used the first time:
318 (defvar org-publish-cache nil
319 "This will cache timestamps and titles for files in publishing projects.
320 Blocks could hash sha1 values here.")
323 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
324 ;;; Compatibility aliases
326 ;; Delete-dups is not in Emacs <22
327 (if (fboundp 'delete-dups)
328 (defalias 'org-publish-delete-dups 'delete-dups)
329 (defun org-publish-delete-dups (list)
330 "Destructively remove `equal' duplicates from LIST.
331 Store the result in LIST and return it. LIST must be a proper list.
332 Of several `equal' occurrences of an element in LIST, the first
333 one is kept.
335 This is a compatibility function for Emacsen without `delete-dups'."
336 ;; Code from `subr.el' in Emacs 22:
337 (let ((tail list))
338 (while tail
339 (setcdr tail (delete (car tail) (cdr tail)))
340 (setq tail (cdr tail))))
341 list))
343 (declare-function org-publish-delete-dups "org-publish" (list))
344 (declare-function find-lisp-find-files "find-lisp" (directory regexp))
346 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
347 ;;; Getting project information out of org-publish-project-alist
349 (defun org-publish-expand-projects (projects-alist)
350 "Expand projects in PROJECTS-ALIST.
351 This splices all the components into the list."
352 (let ((rest projects-alist) rtn p components)
353 (while (setq p (pop rest))
354 (if (setq components (plist-get (cdr p) :components))
355 (setq rest (append
356 (mapcar (lambda (x) (assoc x org-publish-project-alist))
357 components)
358 rest))
359 (push p rtn)))
360 (nreverse (org-publish-delete-dups (delq nil rtn)))))
363 (defvar sitemap-alphabetically)
364 (defvar sitemap-sort-folders)
365 (defvar sitemap-ignore-case)
366 (defvar sitemap-requested)
367 (defun org-publish-compare-directory-files (a b)
368 "Predicate for `sort', that sorts folders-first/last and alphabetically."
369 (let ((retval t))
370 (when (or sitemap-alphabetically sitemap-sort-folders)
371 ;; First we sort alphabetically:
372 (when sitemap-alphabetically
373 (let* ((adir (file-directory-p a))
374 (aorg (and (string-match "\\.org$" a) (not adir)))
375 (bdir (file-directory-p b))
376 (borg (and (string-match "\\.org$" b) (not bdir)))
377 (A (if aorg
378 (concat (file-name-directory a)
379 (org-publish-find-title a)) a))
380 (B (if borg
381 (concat (file-name-directory b)
382 (org-publish-find-title b)) b)))
383 (setq retval (if sitemap-ignore-case
384 (not (string-lessp (upcase B) (upcase A)))
385 (not (string-lessp B A))))))
387 ;; Directory-wise wins:
388 (when sitemap-sort-folders
389 ;; a is directory, b not:
390 (cond
391 ((and (file-directory-p a) (not (file-directory-p b)))
392 (setq retval (equal sitemap-sort-folders 'first)))
393 ;; a is not a directory, but b is:
394 ((and (not (file-directory-p a)) (file-directory-p b))
395 (setq retval (equal sitemap-sort-folders 'last))))))
396 retval))
398 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
399 "Set `org-publish-temp-files' with files from BASE-DIR directory.
400 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
401 non-nil, restrict this list to the files matching the regexp
402 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
403 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
404 matching the regexp SKIP-DIR when recursing through BASE-DIR."
405 (mapc (lambda (f)
406 (let ((fd-p (file-directory-p f))
407 (fnd (file-name-nondirectory f)))
408 (if (and fd-p recurse
409 (not (string-match "^\\.+$" fnd))
410 (if skip-dir (not (string-match skip-dir fnd)) t))
411 (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
412 (unless (or fd-p ;; this is a directory
413 (and skip-file (string-match skip-file fnd))
414 (not (file-exists-p (file-truename f)))
415 (not (string-match match fnd)))
417 (pushnew f org-publish-temp-files)))))
418 (if sitemap-requested
419 (sort (directory-files base-dir t (unless recurse match))
420 'org-publish-compare-directory-files)
421 (directory-files base-dir t (unless recurse match)))))
423 (defun org-publish-get-base-files (project &optional exclude-regexp)
424 "Return a list of all files in PROJECT.
425 If EXCLUDE-REGEXP is set, this will be used to filter out
426 matching filenames."
427 (let* ((project-plist (cdr project))
428 (base-dir (file-name-as-directory
429 (plist-get project-plist :base-directory)))
430 (include-list (plist-get project-plist :include))
431 (recurse (plist-get project-plist :recursive))
432 (extension (or (plist-get project-plist :base-extension) "org"))
433 ;; sitemap-... variables are dynamically scoped for
434 ;; org-publish-compare-directory-files:
435 (sitemap-requested
436 (plist-get project-plist :auto-sitemap))
437 (sitemap-filename
438 (or (plist-get project-plist :sitemap-filename)
439 "sitemap.org"))
440 (sitemap-sort-folders
441 (if (plist-member project-plist :sitemap-sort-folders)
442 (plist-get project-plist :sitemap-sort-folders)
443 org-publish-sitemap-sort-folders))
444 (sitemap-alphabetically
445 (if (plist-member project-plist :sitemap-alphabetically)
446 (plist-get project-plist :sitemap-alphabetically)
447 org-publish-sitemap-sort-alphabetically))
448 (sitemap-ignore-case
449 (if (plist-member project-plist :sitemap-ignore-case)
450 (plist-get project-plist :sitemap-ignore-case)
451 org-publish-sitemap-sort-ignore-case))
452 (match (if (eq extension 'any)
453 "^[^\\.]"
454 (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
455 ;; Make sure sitemap-sort-folders' has an accepted value
456 (unless (memq sitemap-sort-folders '(first last))
457 (setq sitemap-sort-folders nil))
459 (setq org-publish-temp-files nil)
460 (if sitemap-requested
461 (pushnew (expand-file-name (concat base-dir sitemap-filename))
462 org-publish-temp-files))
463 (org-publish-get-base-files-1 base-dir recurse match
464 ;; FIXME distinguish exclude regexp
465 ;; for skip-file and skip-dir?
466 exclude-regexp exclude-regexp)
467 (mapc (lambda (f)
468 (pushnew
469 (expand-file-name (concat base-dir f))
470 org-publish-temp-files))
471 include-list)
472 org-publish-temp-files))
474 (defun org-publish-get-project-from-filename (filename &optional up)
475 "Return the project that FILENAME belongs to."
476 (let* ((filename (expand-file-name filename))
477 project-name)
479 (catch 'p-found
480 (dolist (prj org-publish-project-alist)
481 (unless (plist-get (cdr prj) :components)
482 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
483 (let* ((r (plist-get (cdr prj) :recursive))
484 (b (expand-file-name (file-name-as-directory
485 (plist-get (cdr prj) :base-directory))))
486 (x (or (plist-get (cdr prj) :base-extension) "org"))
487 (e (plist-get (cdr prj) :exclude))
488 (i (plist-get (cdr prj) :include))
489 (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
490 (when (or
491 (and
493 (member filename
494 (mapcar
495 (lambda (file) (expand-file-name file b))
496 i)))
497 (and
498 (not (and e (string-match e filename)))
499 (string-match xm filename)))
500 (setq project-name (car prj))
501 (throw 'p-found project-name))))))
502 (when up
503 (dolist (prj org-publish-project-alist)
504 (if (member project-name (plist-get (cdr prj) :components))
505 (setq project-name (car prj)))))
506 (assoc project-name org-publish-project-alist)))
508 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
509 ;;; Pluggable publishing back-end functions
511 (defun org-publish-org-to (format plist filename pub-dir)
512 "Publish an org file to FORMAT.
513 PLIST is the property list for the given project.
514 FILENAME is the filename of the org file to be published.
515 PUB-DIR is the publishing directory."
516 (require 'org)
517 (unless (file-exists-p pub-dir)
518 (make-directory pub-dir t))
519 (let ((visiting (find-buffer-visiting filename)))
520 (save-excursion
521 (switch-to-buffer (or visiting (find-file filename)))
522 (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
523 (init-buf (current-buffer))
524 (init-point (point))
525 (init-buf-string (buffer-string))
526 export-buf-or-file)
527 ;; run hooks before exporting
528 (run-hooks 'org-publish-before-export-hook)
529 ;; export the possibly modified buffer
530 (setq export-buf-or-file
531 (funcall (intern (concat "org-export-as-" format))
532 (plist-get plist :headline-levels)
533 nil plist nil
534 (plist-get plist :body-only)
535 pub-dir))
536 (when (and (bufferp export-buf-or-file)
537 (buffer-live-p export-buf-or-file))
538 (set-buffer export-buf-or-file)
539 ;; run hooks after export and save export
540 (progn (run-hooks 'org-publish-after-export-hook)
541 (if (buffer-modified-p) (save-buffer)))
542 (kill-buffer export-buf-or-file))
543 ;; maybe restore buffer's content
544 (set-buffer init-buf)
545 (when (buffer-modified-p init-buf)
546 (erase-buffer)
547 (insert init-buf-string)
548 (save-buffer)
549 (goto-char init-point))
550 (unless visiting
551 (kill-buffer init-buf))))))
553 (defmacro org-publish-with-aux-preprocess-maybe (&rest body)
554 "Execute BODY with a modified hook to preprocess for index."
555 `(let ((org-export-preprocess-after-headline-targets-hook
556 (if (plist-get project-plist :makeindex)
557 (cons 'org-publish-aux-preprocess
558 org-export-preprocess-after-headline-targets-hook)
559 org-export-preprocess-after-headline-targets-hook)))
560 ,@body))
562 (defvar project-plist)
563 (defun org-publish-org-to-latex (plist filename pub-dir)
564 "Publish an org file to LaTeX.
565 See `org-publish-org-to' to the list of arguments."
566 (org-publish-with-aux-preprocess-maybe
567 (org-publish-org-to "latex" plist filename pub-dir)))
569 (defun org-publish-org-to-pdf (plist filename pub-dir)
570 "Publish an org file to PDF (via LaTeX).
571 See `org-publish-org-to' to the list of arguments."
572 (org-publish-with-aux-preprocess-maybe
573 (org-publish-org-to "pdf" plist filename pub-dir)))
575 (defun org-publish-org-to-html (plist filename pub-dir)
576 "Publish an org file to HTML.
577 See `org-publish-org-to' to the list of arguments."
578 (org-publish-with-aux-preprocess-maybe
579 (org-publish-org-to "html" plist filename pub-dir)))
581 (defun org-publish-org-to-org (plist filename pub-dir)
582 "Publish an org file to HTML.
583 See `org-publish-org-to' to the list of arguments."
584 (org-publish-org-to "org" plist filename pub-dir))
586 (defun org-publish-org-to-ascii (plist filename pub-dir)
587 "Publish an org file to ASCII.
588 See `org-publish-org-to' to the list of arguments."
589 (org-publish-with-aux-preprocess-maybe
590 (org-publish-org-to "ascii" plist filename pub-dir)))
592 (defun org-publish-org-to-latin1 (plist filename pub-dir)
593 "Publish an org file to Latin-1.
594 See `org-publish-org-to' to the list of arguments."
595 (org-publish-with-aux-preprocess-maybe
596 (org-publish-org-to "latin1" plist filename pub-dir)))
598 (defun org-publish-org-to-utf8 (plist filename pub-dir)
599 "Publish an org file to UTF-8.
600 See `org-publish-org-to' to the list of arguments."
601 (org-publish-with-aux-preprocess-maybe
602 (org-publish-org-to "utf8" plist filename pub-dir)))
604 (defun org-publish-attachment (plist filename pub-dir)
605 "Publish a file with no transformation of any kind.
606 See `org-publish-org-to' to the list of arguments."
607 ;; make sure eshell/cp code is loaded
608 (unless (file-directory-p pub-dir)
609 (make-directory pub-dir t))
610 (or (equal (expand-file-name (file-name-directory filename))
611 (file-name-as-directory (expand-file-name pub-dir)))
612 (copy-file filename
613 (expand-file-name (file-name-nondirectory filename) pub-dir)
614 t)))
616 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
617 ;;; Publishing files, sets of files, and indices
619 (defun org-publish-file (filename &optional project no-cache)
620 "Publish file FILENAME from PROJECT.
621 If NO-CACHE is not nil, do not initialize org-publish-cache and
622 write it to disk. This is needed, since this function is used to
623 publish single files, when entire projects are published.
624 See `org-publish-projects'."
625 (let* ((project
626 (or project
627 (or (org-publish-get-project-from-filename filename)
628 (error "File %s not part of any known project"
629 (abbreviate-file-name filename)))))
630 (project-plist (cdr project))
631 (ftname (expand-file-name filename))
632 (publishing-function
633 (or (plist-get project-plist :publishing-function)
634 'org-publish-org-to-html))
635 (base-dir
636 (file-name-as-directory
637 (expand-file-name
638 (or (plist-get project-plist :base-directory)
639 (error "Project %s does not have :base-directory defined"
640 (car project))))))
641 (pub-dir
642 (file-name-as-directory
643 (file-truename
644 (or (plist-get project-plist :publishing-directory)
645 (error "Project %s does not have :publishing-directory defined"
646 (car project))))))
647 tmp-pub-dir)
649 (unless no-cache
650 (org-publish-initialize-cache (car project)))
652 (setq tmp-pub-dir
653 (file-name-directory
654 (concat pub-dir
655 (and (string-match (regexp-quote base-dir) ftname)
656 (substring ftname (match-end 0))))))
657 (if (listp publishing-function)
658 ;; allow chain of publishing functions
659 (mapc (lambda (f)
660 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir)
661 (funcall f project-plist filename tmp-pub-dir)
662 (org-publish-update-timestamp filename pub-dir f)))
663 publishing-function)
664 (when (org-publish-needed-p filename pub-dir publishing-function
665 tmp-pub-dir)
666 (funcall publishing-function project-plist filename tmp-pub-dir)
667 (org-publish-update-timestamp
668 filename pub-dir publishing-function)))
669 (unless no-cache (org-publish-write-cache-file))))
671 (defun org-publish-projects (projects)
672 "Publish all files belonging to the PROJECTS alist.
673 If :auto-sitemap is set, publish the sitemap too.
674 If :makeindex is set, also produce a file theindex.org."
675 (mapc
676 (lambda (project)
677 ;; Each project uses it's own cache file:
678 (org-publish-initialize-cache (car project))
679 (let*
680 ((project-plist (cdr project))
681 (exclude-regexp (plist-get project-plist :exclude))
682 (sitemap-p (plist-get project-plist :auto-sitemap))
683 (sitemap-filename (or (plist-get project-plist :sitemap-filename)
684 "sitemap.org"))
685 (sitemap-function (or (plist-get project-plist :sitemap-function)
686 'org-publish-org-sitemap))
687 (preparation-function (plist-get project-plist :preparation-function))
688 (completion-function (plist-get project-plist :completion-function))
689 (files (org-publish-get-base-files project exclude-regexp)) file)
690 (when preparation-function (run-hooks 'preparation-function))
691 (if sitemap-p (funcall sitemap-function project sitemap-filename))
692 (while (setq file (pop files))
693 (org-publish-file file project t))
694 (when (plist-get project-plist :makeindex)
695 (org-publish-index-generate-theindex.inc
696 (plist-get project-plist :base-directory))
697 (org-publish-file (expand-file-name
698 "theindex.org"
699 (plist-get project-plist :base-directory))
700 project t))
701 (when completion-function (run-hooks 'completion-function))
702 (org-publish-write-cache-file)))
703 (org-publish-expand-projects projects)))
705 (defun org-publish-org-sitemap (project &optional sitemap-filename)
706 "Create a sitemap of pages in set defined by PROJECT.
707 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
708 Default for SITEMAP-FILENAME is 'sitemap.org'."
709 (let* ((project-plist (cdr project))
710 (dir (file-name-as-directory
711 (plist-get project-plist :base-directory)))
712 (localdir (file-name-directory dir))
713 (indent-str (make-string 2 ?\ ))
714 (exclude-regexp (plist-get project-plist :exclude))
715 (files (nreverse (org-publish-get-base-files project exclude-regexp)))
716 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
717 (sitemap-title (or (plist-get project-plist :sitemap-title)
718 (concat "Sitemap for project " (car project))))
719 (sitemap-style (or (plist-get project-plist :sitemap-style)
720 'tree))
721 (visiting (find-buffer-visiting sitemap-filename))
722 (ifn (file-name-nondirectory sitemap-filename))
723 file sitemap-buffer)
724 (with-current-buffer (setq sitemap-buffer
725 (or visiting (find-file sitemap-filename)))
726 (erase-buffer)
727 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
728 (while (setq file (pop files))
729 (let ((fn (file-name-nondirectory file))
730 (link (file-relative-name file dir))
731 (oldlocal localdir))
732 ;; sitemap shouldn't list itself
733 (unless (equal (file-truename sitemap-filename)
734 (file-truename file))
735 (if (eq sitemap-style 'list)
736 (message "Generating list-style sitemap for %s" sitemap-title)
737 (message "Generating tree-style sitemap for %s" sitemap-title)
738 (setq localdir (concat (file-name-as-directory dir)
739 (file-name-directory link)))
740 (unless (string= localdir oldlocal)
741 (if (string= localdir dir)
742 (setq indent-str (make-string 2 ?\ ))
743 (let ((subdirs
744 (split-string
745 (directory-file-name
746 (file-name-directory
747 (file-relative-name localdir dir))) "/"))
748 (subdir "")
749 (old-subdirs (split-string
750 (file-relative-name oldlocal dir) "/")))
751 (setq indent-str (make-string 2 ?\ ))
752 (while (string= (car old-subdirs) (car subdirs))
753 (setq indent-str (concat indent-str (make-string 2 ?\ )))
754 (pop old-subdirs)
755 (pop subdirs))
756 (dolist (d subdirs)
757 (setq subdir (concat subdir d "/"))
758 (insert (concat indent-str " + " d "\n"))
759 (setq indent-str (make-string
760 (+ (length indent-str) 2) ?\ )))))))
761 ;; This is common to 'flat and 'tree
762 (insert (concat indent-str " + [[file:" link "]["
763 (org-publish-find-title file)
764 "]]\n")))))
765 (save-buffer))
766 (or visiting (kill-buffer sitemap-buffer))))
768 (defun org-publish-find-title (file)
769 "Find the title of FILE in project."
771 (org-publish-cache-get-file-property file :title nil t)
772 (let* ((visiting (find-buffer-visiting file))
773 (buffer (or visiting (find-file-noselect file)))
774 title)
775 (with-current-buffer buffer
776 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
777 (org-infile-export-plist))))
778 (setq title
779 (or (plist-get opt-plist :title)
780 (and (not
781 (plist-get opt-plist :skip-before-1st-heading))
782 (org-export-grab-title-from-buffer))
783 (file-name-nondirectory (file-name-sans-extension file))))))
784 (unless visiting
785 (kill-buffer buffer))
786 (org-publish-cache-set-file-property file :title title)
787 title)))
789 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
790 ;;; Interactive publishing functions
792 ;;;###autoload
793 (defalias 'org-publish-project 'org-publish)
795 ;;;###autoload
796 (defun org-publish (project &optional force)
797 "Publish PROJECT."
798 (interactive
799 (list
800 (assoc (org-icompleting-read
801 "Publish project: "
802 org-publish-project-alist nil t)
803 org-publish-project-alist)
804 current-prefix-arg))
805 (setq org-publish-initial-buffer (current-buffer))
806 (save-window-excursion
807 (let* ((org-publish-use-timestamps-flag
808 (if force nil org-publish-use-timestamps-flag)))
809 (org-publish-projects
810 (if (stringp project)
811 ;; If this function is called in batch mode,
812 ;; project is still a string here.
813 (list (assoc project org-publish-project-alist))
814 (list project))))))
816 ;;;###autoload
817 (defun org-publish-all (&optional force)
818 "Publish all projects.
819 With prefix argument, remove all files in the timestamp
820 directory and force publishing all files."
821 (interactive "P")
822 (when force
823 (org-publish-remove-all-timestamps))
824 (save-window-excursion
825 (let ((org-publish-use-timestamps-flag
826 (if force nil org-publish-use-timestamps-flag)))
827 (org-publish-projects org-publish-project-alist))))
830 ;;;###autoload
831 (defun org-publish-current-file (&optional force)
832 "Publish the current file.
833 With prefix argument, force publish the file."
834 (interactive "P")
835 (save-window-excursion
836 (let ((org-publish-use-timestamps-flag
837 (if force nil org-publish-use-timestamps-flag)))
838 (org-publish-file (buffer-file-name)))))
840 ;;;###autoload
841 (defun org-publish-current-project (&optional force)
842 "Publish the project associated with the current file.
843 With a prefix argument, force publishing of all files in
844 the project."
845 (interactive "P")
846 (save-window-excursion
847 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up))
848 (org-publish-use-timestamps-flag
849 (if force nil org-publish-use-timestamps-flag)))
850 (if (not project)
851 (error "File %s is not part of any known project" (buffer-file-name)))
852 ;; FIXME: force is not used here?
853 (org-publish project))))
856 ;;; Index generation
858 (defvar backend) ; dynamically scoped
859 (defun org-publish-aux-preprocess ()
860 "Find index entries and write them to an .orgx file."
861 (let ((case-fold-search t)
862 entry index target)
863 (goto-char (point-min))
864 (while
865 (and
866 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t)
867 (> (match-end 1) (match-beginning 1)))
868 (setq entry (match-string 1))
869 (when (eq backend 'latex)
870 (replace-match (format "\\index{%s}" entry) t t))
871 (save-excursion
872 (ignore-errors (org-back-to-heading t))
873 (setq target (get-text-property (point) 'target))
874 (setq target (or (cdr (assoc target org-export-preferred-target-alist))
875 (cdr (assoc target org-export-id-target-alist))
876 target ""))
877 (push (cons entry target) index)))
878 (with-temp-file
879 (concat (file-name-sans-extension org-current-export-file) ".orgx")
880 (dolist (entry (nreverse index))
881 (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
883 (defun org-publish-index-generate-theindex.inc (directory)
884 "Generate the index from all .orgx files in the current directory and below."
885 (require 'find-lisp)
886 (let* ((fulldir (file-name-as-directory
887 (expand-file-name directory)))
888 (full-files (find-lisp-find-files directory "\\.orgx\\'"))
889 (re (concat "\\`" fulldir))
890 (files (mapcar (lambda (f) (if (string-match re f)
891 (substring f (match-end 0))
893 full-files))
894 (default-directory directory)
895 index origfile buf target entry ibuffer
896 main last-main letter last-letter file sub link tgext)
897 ;; `files' contains the list of relative file names
898 (dolist (file files)
899 (setq origfile (substring file 0 -1))
900 (setq buf (find-file-noselect file))
901 (with-current-buffer buf
902 (goto-char (point-min))
903 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
904 (setq target (match-string 1)
905 entry (match-string 2))
906 (push (list entry origfile target) index)))
907 (kill-buffer buf))
908 (setq index (sort index (lambda (a b) (string< (downcase (car a))
909 (downcase (car b))))))
910 (setq ibuffer (find-file-noselect (expand-file-name "theindex.inc" directory)))
911 (with-current-buffer ibuffer
912 (erase-buffer)
913 (insert "* Index\n")
914 (setq last-letter nil)
915 (dolist (idx index)
916 (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
917 (if (and (stringp target) (string-match "\\S-" target))
918 (setq tgext (concat "::#" target))
919 (setq tgext ""))
920 (setq letter (upcase (substring entry 0 1)))
921 (when (not (equal letter last-letter))
922 (insert "** " letter "\n")
923 (setq last-letter letter))
924 (if (string-match "!" entry)
925 (setq main (substring entry 0 (match-beginning 0))
926 sub (substring entry (match-end 0)))
927 (setq main nil sub nil last-main nil))
928 (when (and main (not (equal main last-main)))
929 (insert " - " main "\n")
930 (setq last-main main))
931 (setq link (concat "[[file:" file tgext "]"
932 "[" (or sub entry) "]]"))
933 (if (and main sub)
934 (insert " - " link "\n")
935 (insert " - " link "\n")))
936 (save-buffer))
937 (kill-buffer ibuffer)
939 (let ((index-file (expand-file-name "theindex.org" directory)))
940 (unless (file-exists-p index-file)
941 (setq ibuffer (find-file-noselect index-file))
942 (with-current-buffer ibuffer
943 (erase-buffer)
944 (insert "\n\n#+include: \"theindex.inc\"\n\n")
945 (save-buffer))
946 (kill-buffer ibuffer)))))
949 ;; Caching functions:
951 (defun org-publish-write-cache-file (&optional free-cache)
952 "Write `org-publish-cache' to file.
953 If FREE-CACHE, empty the cache."
954 (unless org-publish-cache
955 (error "%s" "`org-publish-write-cache-file' called, but no cache present"))
957 (let ((cache-file (org-publish-cache-get ":cache-file:")))
958 (unless cache-file
959 (error
960 "%s" "Cannot find cache-file name in `org-publish-write-cache-file'"))
961 (with-temp-file cache-file
962 (let ((print-level nil)
963 (print-length nil))
964 (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
965 (maphash (lambda (k v)
966 (insert
967 (format (concat "(puthash %S "
968 (if (or (listp v) (symbolp v))
969 "'" "")
970 "%S org-publish-cache)\n") k v)))
971 org-publish-cache)))
972 (when free-cache (org-publish-reset-cache))))
974 (defun org-publish-initialize-cache (project-name)
975 "Initialize the projects cache if not initialized yet and return it."
977 (unless project-name
978 (error "%s%s" "Cannot initialize `org-publish-cache' without projects name"
979 " in `org-publish-initialize-cache'"))
981 (unless (file-exists-p org-publish-timestamp-directory)
982 (make-directory org-publish-timestamp-directory t))
983 (if (not (file-directory-p org-publish-timestamp-directory))
984 (error "Org publish timestamp: %s is not a directory"
985 org-publish-timestamp-directory))
987 (unless (and org-publish-cache
988 (string= (org-publish-cache-get ":project:") project-name))
989 (let* ((cache-file (concat
990 (expand-file-name org-publish-timestamp-directory)
991 project-name
992 ".cache"))
993 (cexists (file-exists-p cache-file)))
995 (when org-publish-cache
996 (org-publish-reset-cache))
998 (if cexists
999 (load-file cache-file)
1000 (setq org-publish-cache
1001 (make-hash-table :test 'equal :weakness nil :size 100))
1002 (org-publish-cache-set ":project:" project-name)
1003 (org-publish-cache-set ":cache-file:" cache-file))
1004 (unless cexists (org-publish-write-cache-file nil))))
1005 org-publish-cache)
1007 (defun org-publish-reset-cache ()
1008 "Empty org-publish-cache and reset it nil."
1009 (message "%s" "Resetting org-publish-cache")
1010 (if (hash-table-p org-publish-cache)
1011 (clrhash org-publish-cache))
1012 (setq org-publish-cache nil))
1014 (defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func)
1015 "Check the timestamp of the last publishing of FILENAME.
1016 Return `t', if the file needs publishing"
1017 (unless org-publish-cache
1018 (error "%s" "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1019 (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func))
1020 (pstamp (org-publish-cache-get key)))
1021 (if (null pstamp)
1023 (let ((ctime (org-publish-cache-ctime-of-src filename)))
1024 (< pstamp ctime)))))
1026 (defun org-publish-cache-set-file-property (filename property value &optional project-name)
1027 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1028 Use cache file of PROJECT-NAME. If the entry does not exist, it will be
1029 created. Return VALUE."
1030 ;; Evtl. load the requested cache file:
1031 (if project-name (org-publish-initialize-cache project-name))
1032 (let ((pl (org-publish-cache-get filename)))
1033 (if pl
1034 (progn
1035 (plist-put pl property value)
1036 value)
1037 (org-publish-cache-get-file-property
1038 filename property value nil project-name))))
1040 (defun org-publish-cache-get-file-property
1041 (filename property &optional default no-create project-name)
1042 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1043 Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
1044 DEFAULT, if the value does not yet exist.
1045 If the entry will be created, unless NO-CREATE is not nil."
1046 ;; Evtl. load the requested cache file:
1047 (if project-name (org-publish-initialize-cache project-name))
1048 (let ((pl (org-publish-cache-get filename))
1049 (retval nil))
1050 (if pl
1051 (if (plist-member pl property)
1052 (setq retval (plist-get pl property))
1053 (setq retval default))
1054 ;; no pl yet:
1055 (unless no-create
1056 (org-publish-cache-set filename (list property default)))
1057 (setq retval default))
1058 retval))
1060 (defun org-publish-cache-get (key)
1061 "Return the value stored in `org-publish-cache' for key KEY.
1062 Returns nil, if no value or nil is found, or the cache does not
1063 exist."
1064 (unless org-publish-cache
1065 (error "%s" "`org-publish-cache-get' called, but no cache present"))
1066 (gethash key org-publish-cache))
1068 (defun org-publish-cache-set (key value)
1069 "Store KEY VALUE pair in `org-publish-cache'.
1070 Returns value on success, else nil."
1071 (unless org-publish-cache
1072 (error "%s" "`org-publish-cache-set' called, but no cache present"))
1073 (puthash key value org-publish-cache))
1075 (defun org-publish-cache-ctime-of-src (filename)
1076 "Get the files ctime as integer."
1077 (let ((src-attr (file-attributes (if (stringp (file-symlink-p filename))
1078 (file-symlink-p filename)
1079 filename))))
1081 (lsh (car (nth 5 src-attr)) 16)
1082 (cadr (nth 5 src-attr)))))
1086 (provide 'org-publish)
1088 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
1090 ;;; org-publish.el ends here