Merge branch 'master' of git+ssh://repo.or.cz/srv/git/org-mode
[org-mode/org-jambu.git] / lisp / org-publish.el
bloba50cf567249dde552b810586329f885817fa5ae6
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: 6.36trans
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 "When non-nil, 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 truely 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 timstamp 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 all the timestamps and titles for
309 files in publishing projects. 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
358 eventually alphabetically."
359 (let ((retval t))
360 (when (or sitemap-alphabetically sitemap-sort-folders)
361 ;; First we sort alphabetically:
362 (when sitemap-alphabetically
363 (let* ((adir (file-directory-p a))
364 (aorg (and (string-match "\\.org$" a) (not adir)))
365 (bdir (file-directory-p b))
366 (borg (and (string-match "\\.org$" b) (not bdir)))
367 (A (if aorg
368 (concat (file-name-directory a)
369 (org-publish-find-title a)) a))
370 (B (if borg
371 (concat (file-name-directory b)
372 (org-publish-find-title b)) b)))
373 (setq retval (if sitemap-ignore-case
374 (not (string-lessp (upcase B) (upcase A)))
375 (not (string-lessp B A))))))
377 ;; Directory-wise wins:
378 (when sitemap-sort-folders
379 ;; a is directory, b not:
380 (cond
381 ((and (file-directory-p a) (not (file-directory-p b)))
382 (setq retval (equal sitemap-sort-folders 'first)))
383 ;; a is not a directory, but b is:
384 ((and (not (file-directory-p a)) (file-directory-p b))
385 (setq retval (equal sitemap-sort-folders 'last))))))
386 retval))
388 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
389 "Set `org-publish-temp-files' with files from BASE-DIR directory.
390 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
391 non-nil, restrict this list to the files matching the regexp
392 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
393 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
394 matching the regexp SKIP-DIR when recursing through BASE-DIR."
395 (mapc (lambda (f)
396 (let ((fd-p (file-directory-p f))
397 (fnd (file-name-nondirectory f)))
398 (if (and fd-p recurse
399 (not (string-match "^\\.+$" fnd))
400 (if skip-dir (not (string-match skip-dir fnd)) t))
401 (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
402 (unless (or fd-p ;; this is a directory
403 (and skip-file (string-match skip-file fnd))
404 (not (file-exists-p (file-truename f)))
405 (not (string-match match fnd)))
407 (pushnew f org-publish-temp-files)))))
408 (if sitemap-requested
409 (sort (directory-files base-dir t (unless recurse match))
410 'org-publish-compare-directory-files)
411 (directory-files base-dir t (unless recurse match)))))
413 (defun org-publish-get-base-files (project &optional exclude-regexp)
414 "Return a list of all files in PROJECT.
415 If EXCLUDE-REGEXP is set, this will be used to filter out
416 matching filenames."
417 (let* ((project-plist (cdr project))
418 (base-dir (file-name-as-directory
419 (plist-get project-plist :base-directory)))
420 (include-list (plist-get project-plist :include))
421 (recurse (plist-get project-plist :recursive))
422 (extension (or (plist-get project-plist :base-extension) "org"))
423 ;; sitemap-... variables are dynamically scoped for
424 ;; org-publish-compare-directory-files:
425 (sitemap-requested
426 (plist-get project-plist :auto-sitemap))
427 (sitemap-sort-folders
428 (if (plist-member project-plist :sitemap-sort-folders)
429 (plist-get project-plist :sitemap-sort-folders)
430 org-publish-sitemap-sort-folders))
431 (sitemap-alphabetically
432 (if (plist-member project-plist :sitemap-alphabetically)
433 (plist-get project-plist :sitemap-alphabetically)
434 org-publish-sitemap-sort-alphabetically))
435 (sitemap-ignore-case
436 (if (plist-member project-plist :sitemap-ignore-case)
437 (plist-get project-plist :sitemap-ignore-case)
438 org-publish-sitemap-sort-ignore-case))
439 (match (if (eq extension 'any)
440 "^[^\\.]"
441 (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
442 ;; Make sure sitemap-sort-folders' has an accepted value
443 (unless (memq sitemap-sort-folders '(first last))
444 (setq sitemap-sort-folders nil))
446 (setq org-publish-temp-files nil)
447 (org-publish-get-base-files-1 base-dir recurse match
448 ;; FIXME distinguish exclude regexp
449 ;; for skip-file and skip-dir?
450 exclude-regexp exclude-regexp)
451 (mapc (lambda (f)
452 (pushnew
453 (expand-file-name (concat base-dir f))
454 org-publish-temp-files))
455 include-list)
456 org-publish-temp-files))
458 (defun org-publish-get-project-from-filename (filename &optional up)
459 "Return the project FILENAME belongs."
460 (let* ((filename (expand-file-name filename))
461 project-name)
463 (catch 'p-found
464 (dolist (prj org-publish-project-alist)
465 (unless (plist-get (cdr prj) :components)
466 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
467 (let* ((r (plist-get (cdr prj) :recursive))
468 (b (expand-file-name (plist-get (cdr prj) :base-directory)))
469 (x (or (plist-get (cdr prj) :base-extension) "org"))
470 (e (plist-get (cdr prj) :exclude))
471 (i (plist-get (cdr prj) :include))
472 (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
473 (when (or
474 (and i (string-match i filename))
475 (and
476 (not (and e (string-match e filename)))
477 (string-match xm filename)))
478 (setq project-name (car prj))
479 (throw 'p-found project-name))))))
480 (when up
481 (dolist (prj org-publish-project-alist)
482 (if (member project-name (plist-get (cdr prj) :components))
483 (setq project-name (car prj)))))
484 (assoc project-name org-publish-project-alist)))
486 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
487 ;;; Pluggable publishing back-end functions
489 (defun org-publish-org-to (format plist filename pub-dir)
490 "Publish an org file to FORMAT.
491 PLIST is the property list for the given project.
492 FILENAME is the filename of the org file to be published.
493 PUB-DIR is the publishing directory."
494 (require 'org)
495 (unless (file-exists-p pub-dir)
496 (make-directory pub-dir t))
497 (let ((visiting (find-buffer-visiting filename)))
498 (save-excursion
499 (switch-to-buffer (or visiting (find-file filename)))
500 (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
501 (init-buf (current-buffer))
502 (init-point (point))
503 (init-buf-string (buffer-string))
504 export-buf-or-file)
505 ;; run hooks before exporting
506 (run-hooks 'org-publish-before-export-hook)
507 ;; export the possibly modified buffer
508 (setq export-buf-or-file
509 (funcall (intern (concat "org-export-as-" format))
510 (plist-get plist :headline-levels)
511 nil plist nil nil pub-dir))
512 (when (and (bufferp export-buf-or-file)
513 (buffer-live-p export-buf-or-file))
514 (set-buffer export-buf-or-file)
515 ;; run hooks after export and save export
516 (progn (run-hooks 'org-publish-after-export-hook)
517 (if (buffer-modified-p) (save-buffer)))
518 (kill-buffer export-buf-or-file))
519 ;; maybe restore buffer's content
520 (set-buffer init-buf)
521 (when (buffer-modified-p init-buf)
522 (erase-buffer)
523 (insert init-buf-string)
524 (save-buffer)
525 (goto-char init-point))
526 (unless visiting
527 (kill-buffer init-buf))))))
529 (defmacro org-publish-with-aux-preprocess-maybe (&rest body)
530 "Execute BODY with a modified hook to preprocess for index."
531 `(let ((org-export-preprocess-after-headline-targets-hook
532 (if (plist-get project-plist :makeindex)
533 (cons 'org-publish-aux-preprocess
534 org-export-preprocess-after-headline-targets-hook)
535 org-export-preprocess-after-headline-targets-hook)))
536 ,@body))
538 (defvar project-plist)
539 (defun org-publish-org-to-latex (plist filename pub-dir)
540 "Publish an org file to LaTeX.
541 See `org-publish-org-to' to the list of arguments."
542 (org-publish-with-aux-preprocess-maybe
543 (org-publish-org-to "latex" plist filename pub-dir)))
545 (defun org-publish-org-to-pdf (plist filename pub-dir)
546 "Publish an org file to PDF (via LaTeX).
547 See `org-publish-org-to' to the list of arguments."
548 (org-publish-with-aux-preprocess-maybe
549 (org-publish-org-to "pdf" plist filename pub-dir)))
551 (defun org-publish-org-to-html (plist filename pub-dir)
552 "Publish an org file to HTML.
553 See `org-publish-org-to' to the list of arguments."
554 (org-publish-with-aux-preprocess-maybe
555 (org-publish-org-to "html" plist filename pub-dir)))
557 (defun org-publish-org-to-org (plist filename pub-dir)
558 "Publish an org file to HTML.
559 See `org-publish-org-to' to the list of arguments."
560 (org-publish-org-to "org" plist filename pub-dir))
562 (defun org-publish-attachment (plist filename pub-dir)
563 "Publish a file with no transformation of any kind.
564 See `org-publish-org-to' to the list of arguments."
565 ;; make sure eshell/cp code is loaded
566 (unless (file-directory-p pub-dir)
567 (make-directory pub-dir t))
568 (or (equal (expand-file-name (file-name-directory filename))
569 (file-name-as-directory (expand-file-name pub-dir)))
570 (copy-file filename
571 (expand-file-name (file-name-nondirectory filename) pub-dir)
572 t)))
574 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
575 ;;; Publishing files, sets of files, and indices
577 (defun org-publish-file (filename &optional project no-cache)
578 "Publish file FILENAME from PROJECT.
579 If NO-CACHE is not nil, do not initialize org-publish-cache and
580 write it to disk. This is needed, since this function is used to
581 publish single files, when entire projects are published.
582 See `org-publish-projects'."
583 (let* ((project
584 (or project
585 (or (org-publish-get-project-from-filename filename)
586 (error "File %s not part of any known project"
587 (abbreviate-file-name filename)))))
588 (project-plist (cdr project))
589 (ftname (file-truename filename))
590 (publishing-function
591 (or (plist-get project-plist :publishing-function)
592 'org-publish-org-to-html))
593 (base-dir (file-name-as-directory
594 (file-truename (plist-get project-plist :base-directory))))
595 (pub-dir (file-name-as-directory
596 (file-truename (plist-get project-plist :publishing-directory))))
597 tmp-pub-dir)
599 (unless no-cache
600 (org-publish-initialize-cache (car project)))
602 (setq tmp-pub-dir
603 (file-name-directory
604 (concat pub-dir
605 (and (string-match (regexp-quote base-dir) ftname)
606 (substring ftname (match-end 0))))))
607 (if (listp publishing-function)
608 ;; allow chain of publishing functions
609 (mapc (lambda (f)
610 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir)
611 (funcall f project-plist filename tmp-pub-dir)
612 (org-publish-update-timestamp filename pub-dir f)))
613 publishing-function)
614 (when (org-publish-needed-p filename pub-dir publishing-function
615 tmp-pub-dir)
616 (funcall publishing-function project-plist filename tmp-pub-dir)
617 (org-publish-update-timestamp
618 filename pub-dir publishing-function)))
619 (unless no-cache (org-publish-write-cache-file))))
621 (defun org-publish-projects (projects)
622 "Publish all files belonging to the PROJECTS alist.
623 If :auto-sitemap is set, publish the sitemap too.
624 If :makeindex is set, also produce a file theindex.org."
625 (mapc
626 (lambda (project)
627 ;; Each project uses it's own cache file:
628 (org-publish-initialize-cache (car project))
629 (let*
630 ((project-plist (cdr project))
631 (exclude-regexp (plist-get project-plist :exclude))
632 (sitemap-p (plist-get project-plist :auto-sitemap))
633 (sitemap-filename (or (plist-get project-plist :sitemap-filename)
634 "sitemap.org"))
635 (sitemap-function (or (plist-get project-plist :sitemap-function)
636 'org-publish-org-sitemap))
637 (preparation-function (plist-get project-plist :preparation-function))
638 (completion-function (plist-get project-plist :completion-function))
639 (files (org-publish-get-base-files project exclude-regexp)) file)
640 (when preparation-function (run-hooks 'preparation-function))
641 (if sitemap-p (funcall sitemap-function project sitemap-filename))
642 (while (setq file (pop files))
643 (org-publish-file file project t))
644 (when (plist-get project-plist :makeindex)
645 (org-publish-index-generate-theindex.inc
646 (plist-get project-plist :base-directory))
647 (org-publish-file (expand-file-name
648 "theindex.org"
649 (plist-get project-plist :base-directory))
650 project t))
651 (when completion-function (run-hooks 'completion-function))
652 (org-publish-write-cache-file)))
653 (org-publish-expand-projects projects)))
655 (defun org-publish-org-sitemap (project &optional sitemap-filename)
656 "Create a sitemap of pages in set defined by PROJECT.
657 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
658 Default for SITEMAP-FILENAME is 'sitemap.org'."
659 (let* ((project-plist (cdr project))
660 (dir (file-name-as-directory
661 (plist-get project-plist :base-directory)))
662 (localdir (file-name-directory dir))
663 (indent-str (make-string 2 ?\ ))
664 (exclude-regexp (plist-get project-plist :exclude))
665 (files (nreverse (org-publish-get-base-files project exclude-regexp)))
666 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
667 (sitemap-title (or (plist-get project-plist :sitemap-title)
668 (concat "Sitemap for project " (car project))))
669 (sitemap-style (or (plist-get project-plist :sitemap-style)
670 'tree))
671 (visiting (find-buffer-visiting sitemap-filename))
672 (ifn (file-name-nondirectory sitemap-filename))
673 file sitemap-buffer)
674 (with-current-buffer (setq sitemap-buffer
675 (or visiting (find-file sitemap-filename)))
676 (erase-buffer)
677 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
678 (while (setq file (pop files))
679 (let ((fn (file-name-nondirectory file))
680 (link (file-relative-name file dir))
681 (oldlocal localdir))
682 ;; sitemap shouldn't list itself
683 (unless (equal (file-truename sitemap-filename)
684 (file-truename file))
685 (if (eq sitemap-style 'list)
686 (message "Generating list-style sitemap for %s" sitemap-title)
687 (message "Generating tree-style sitemap for %s" sitemap-title)
688 (setq localdir (concat (file-name-as-directory dir)
689 (file-name-directory link)))
690 (unless (string= localdir oldlocal)
691 (if (string= localdir dir)
692 (setq indent-str (make-string 2 ?\ ))
693 (let ((subdirs
694 (split-string
695 (directory-file-name
696 (file-name-directory
697 (file-relative-name localdir dir))) "/"))
698 (subdir "")
699 (old-subdirs (split-string
700 (file-relative-name oldlocal dir) "/")))
701 (setq indent-str (make-string 2 ?\ ))
702 (while (string= (car old-subdirs) (car subdirs))
703 (setq indent-str (concat indent-str (make-string 2 ?\ )))
704 (pop old-subdirs)
705 (pop subdirs))
706 (dolist (d subdirs)
707 (setq subdir (concat subdir d "/"))
708 (insert (concat indent-str " + " d "\n"))
709 (setq indent-str (make-string
710 (+ (length indent-str) 2) ?\ )))))))
711 ;; This is common to 'flat and 'tree
712 (insert (concat indent-str " + [[file:" link "]["
713 (org-publish-find-title file)
714 "]]\n")))))
715 (save-buffer))
716 (or visiting (kill-buffer sitemap-buffer))))
718 (defun org-publish-find-title (file)
719 "Find the title of file in project."
721 (org-publish-cache-get-file-property file :title nil t)
722 (let* ((visiting (find-buffer-visiting file))
723 (buffer (or visiting (find-file-noselect file)))
724 title)
725 (with-current-buffer buffer
726 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
727 (org-infile-export-plist))))
728 (setq title
729 (or (plist-get opt-plist :title)
730 (and (not
731 (plist-get opt-plist :skip-before-1st-heading))
732 (org-export-grab-title-from-buffer))
733 (file-name-nondirectory (file-name-sans-extension file))))))
734 (unless visiting
735 (kill-buffer buffer))
736 (org-publish-cache-set-file-property file :title title)
737 title)))
739 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
740 ;;; Interactive publishing functions
742 ;;;###autoload
743 (defalias 'org-publish-project 'org-publish)
745 ;;;###autoload
746 (defun org-publish (project &optional force)
747 "Publish PROJECT."
748 (interactive
749 (list
750 (assoc (org-icompleting-read
751 "Publish project: "
752 org-publish-project-alist nil t)
753 org-publish-project-alist)
754 current-prefix-arg))
755 (setq org-publish-initial-buffer (current-buffer))
756 (save-window-excursion
757 (let* ((org-publish-use-timestamps-flag
758 (if force nil org-publish-use-timestamps-flag)))
759 (org-publish-projects
760 (if (stringp project)
761 ;; If this function is called in batch mode,
762 ;; project is still a string here.
763 (list (assoc project org-publish-project-alist))
764 (list project))))))
766 ;;;###autoload
767 (defun org-publish-all (&optional force)
768 "Publish all projects.
769 With prefix argument, remove all files in the timestamp
770 directory and force publishing all files."
771 (interactive "P")
772 (when force
773 (org-publish-remove-all-timestamps))
774 ;; (org-publish-initialize-files-alist force)
775 (save-window-excursion
776 (let ((org-publish-use-timestamps-flag
777 (if force nil org-publish-use-timestamps-flag)))
778 (org-publish-projects org-publish-project-alist))))
781 ;;;###autoload
782 (defun org-publish-current-file (&optional force)
783 "Publish the current file.
784 With prefix argument, force publish the file."
785 (interactive "P")
786 (save-window-excursion
787 (let ((org-publish-use-timestamps-flag
788 (if force nil org-publish-use-timestamps-flag)))
789 (org-publish-file (buffer-file-name)))))
791 ;;;###autoload
792 (defun org-publish-current-project (&optional force)
793 "Publish the project associated with the current file.
794 With a prefix argument, force publishing of all files in
795 the project."
796 (interactive "P")
797 (save-window-excursion
798 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up))
799 (org-publish-use-timestamps-flag
800 (if force nil org-publish-use-timestamps-flag)))
801 (if (not project)
802 (error "File %s is not part of any known project" (buffer-file-name)))
803 ;; FIXME: force is not used here?
804 (org-publish project))))
807 ;;; Index generation
809 (defvar backend) ; dynamically scoped
810 (defun org-publish-aux-preprocess ()
811 "Find index entries and write them to an .orgx file."
812 (let ((case-fold-search t)
813 entry index target)
814 (goto-char (point-min))
815 (while
816 (and
817 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t)
818 (> (match-end 1) (match-beginning 1)))
819 (setq entry (match-string 1))
820 (when (eq backend 'latex)
821 (replace-match (format "\\index{%s}" entry) t t))
822 (save-excursion
823 (ignore-errors (org-back-to-heading t))
824 (setq target (get-text-property (point) 'target))
825 (setq target (or (cdr (assoc target org-export-preferred-target-alist))
826 (cdr (assoc target org-export-id-target-alist))
827 target ""))
828 (push (cons entry target) index)))
829 (with-temp-file
830 (concat (file-name-sans-extension org-current-export-file) ".orgx")
831 (dolist (entry (nreverse index))
832 (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
834 (defun org-publish-index-generate-theindex.inc (directory)
835 "Generate the index from all .orgx files in the current directory and below."
836 (require 'find-lisp)
837 (let* ((fulldir (file-name-as-directory
838 (expand-file-name directory)))
839 (full-files (find-lisp-find-files directory "\\.orgx\\'"))
840 (re (concat "\\`" fulldir))
841 (files (mapcar (lambda (f) (if (string-match re f)
842 (substring f (match-end 0))
844 full-files))
845 (default-directory directory)
846 index origfile buf target entry ibuffer
847 main last-main letter last-letter file sub link tgext)
848 ;; `files' contains the list of relative file names
849 (dolist (file files)
850 (setq origfile (substring file 0 -1))
851 (setq buf (find-file-noselect file))
852 (with-current-buffer buf
853 (goto-char (point-min))
854 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
855 (setq target (match-string 1)
856 entry (match-string 2))
857 (push (list entry origfile target) index)))
858 (kill-buffer buf))
859 (setq index (sort index (lambda (a b) (string< (downcase (car a))
860 (downcase (car b))))))
861 (setq ibuffer (find-file-noselect (expand-file-name "theindex.inc" directory)))
862 (with-current-buffer ibuffer
863 (erase-buffer)
864 (insert "* Index\n")
865 (setq last-letter nil)
866 (dolist (idx index)
867 (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
868 (if (and (stringp target) (string-match "\\S-" target))
869 (setq tgext (concat "::#" target))
870 (setq tgext ""))
871 (setq letter (upcase (substring entry 0 1)))
872 (when (not (equal letter last-letter))
873 (insert "** " letter "\n")
874 (setq last-letter letter))
875 (if (string-match "!" entry)
876 (setq main (substring entry 0 (match-beginning 0))
877 sub (substring entry (match-end 0)))
878 (setq main nil sub nil last-main nil))
879 (when (and main (not (equal main last-main)))
880 (insert " - " main "\n")
881 (setq last-main main))
882 (setq link (concat "[[file:" file tgext "]"
883 "[" (or sub entry) "]]"))
884 (if (and main sub)
885 (insert " - " link "\n")
886 (insert " - " link "\n")))
887 (save-buffer))
888 (kill-buffer ibuffer)
890 (let ((index-file (expand-file-name "theindex.org" directory)))
891 (unless (file-exists-p index-file)
892 (setq ibuffer (find-file-noselect index-file))
893 (with-current-buffer ibuffer
894 (erase-buffer)
895 (insert "\n\n#+include: \"theindex.inc\"\n\n")
896 (save-buffer))
897 (kill-buffer ibuffer)))))
900 ;; Caching functions:
902 (defun org-publish-write-cache-file (&optional free-cache)
903 "Write `org-publish-cache' to file.
904 If FREE-CACHE, empty the cache."
905 (unless org-publish-cache
906 (error "%s" "`org-publish-write-cache-file' called, but no cache present"))
908 (let ((cache-file (org-publish-cache-get ":cache-file:")))
909 (unless cache-file
910 (error
911 "%s" "Cannot find cache-file name in `org-publish-write-cache-file'"))
912 (with-temp-file cache-file
913 (let ((print-level nil)
914 (print-length nil))
915 (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
916 (maphash (lambda (k v)
917 (insert
918 (format (concat "(puthash %S "
919 (if (or (listp v) (symbolp v))
920 "'" "")
921 "%S org-publish-cache)\n") k v)))
922 org-publish-cache)))
923 (when free-cache (org-publish-reset-cache))))
925 (defun org-publish-initialize-cache (project-name)
926 "Initialize the projects cache if not intialized yet
927 and return it."
929 (unless project-name
930 (error "%s%s" "Cannot initialize `org-publish-cache' without projects name"
931 " in `org-publish-initialize-cache'"))
933 (unless (file-exists-p org-publish-timestamp-directory)
934 (make-directory org-publish-timestamp-directory t))
935 (if (not (file-directory-p org-publish-timestamp-directory))
936 (error "Org publish timestamp: %s is not a directory"
937 org-publish-timestamp-directory))
939 (unless (and org-publish-cache
940 (string= (org-publish-cache-get ":project:") project-name))
941 (let* ((cache-file (concat
942 (expand-file-name org-publish-timestamp-directory)
943 project-name
944 ".cache"))
945 (cexists (file-exists-p cache-file)))
947 (when org-publish-cache
948 (org-publish-reset-cache))
950 (if cexists
951 (load-file cache-file)
952 (setq org-publish-cache
953 (make-hash-table :test 'equal :weakness nil :size 100))
954 (org-publish-cache-set ":project:" project-name)
955 (org-publish-cache-set ":cache-file:" cache-file))
956 (unless cexists (org-publish-write-cache-file nil))))
957 org-publish-cache)
959 (defun org-publish-reset-cache ()
960 "Empty org-publish-cache and reset it nil."
961 (message "%s" "Resetting org-publish-cache")
962 (if (hash-table-p org-publish-cache)
963 (clrhash org-publish-cache))
964 (setq org-publish-cache nil))
966 (defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func)
967 "Check the timestamp of the last publishing of FILENAME.
968 Return `t', if the file needs publishing"
969 (unless org-publish-cache
970 (error "%s" "`org-publish-cache-file-needs-publishing' called, but no cache present"))
971 (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func))
972 (pstamp (org-publish-cache-get key)))
973 (if (null pstamp)
975 (let ((ctime (org-publish-cache-ctime-of-src filename)))
976 (< pstamp ctime)))))
978 (defun org-publish-cache-set-file-property (filename property value &optional project-name)
979 "Set the VALUE for a PORPERTY of file FILENAME in publishing cache
980 to VALUE. Use cache file of PROJECT-NAME.
981 If the entry does not exist, it will be created.
982 Return VALUE."
983 ;; Evtl. load the requested cache file:
984 (if project-name (org-publish-initialize-cache project-name))
985 (let ((pl (org-publish-cache-get filename)))
986 (if pl
987 (progn
988 (plist-put pl property value)
989 value)
990 (org-publish-cache-get-file-property
991 filename property value nil project-name))))
993 (defun org-publish-cache-get-file-property
994 (filename property &optional default no-create project-name)
995 "Return the value for a PORPERTY of file FILENAME in publishing cache.
996 Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
997 DEFAULT, if the value does not yet exist.
998 If the entry will be created, unless NO-CREATE is not nil."
999 ;; Evtl. load the requested cache file:
1000 (if project-name (org-publish-initialize-cache project-name))
1001 (let ((pl (org-publish-cache-get filename))
1002 (retval nil))
1003 (if pl
1004 (if (plist-member pl property)
1005 (setq retval (plist-get pl property))
1006 (setq retval default))
1007 ;; no pl yet:
1008 (unless no-create
1009 (org-publish-cache-set filename (list property default)))
1010 (setq retval default))
1011 retval))
1013 (defun org-publish-cache-get (key)
1014 "Return the value stored in `org-publish-cache' for key KEY.
1015 Returns nil, if no value or nil is found, or the cache does not
1016 exist."
1017 (unless org-publish-cache
1018 (error "%s" "`org-publish-cache-get' called, but no cache present"))
1019 (gethash key org-publish-cache))
1021 (defun org-publish-cache-set (key value)
1022 "Store KEY VLAUE pair in `org-publish-cache'.
1023 Returns value on success, else nil."
1024 (unless org-publish-cache
1025 (error "%s" "`org-publish-cache-set' called, but no cache present"))
1026 (puthash key value org-publish-cache))
1028 (defun org-publish-cache-ctime-of-src (filename)
1029 "Get the files ctime as integer."
1030 (let ((src-attr (file-attributes filename)))
1032 (lsh (car (nth 5 src-attr)) 16)
1033 (cadr (nth 5 src-attr)))))
1037 (provide 'org-publish)
1039 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
1041 ;;; org-publish.el ends here