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