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