Spelling fixes.
[bpt/emacs.git] / lisp / org / org-publish.el
CommitLineData
9542795a 1;;; org-publish.el --- publish related org-mode files as a website
cbd20947 2;; Copyright (C) 2006-2011 Free Software Foundation, Inc.
9542795a
CD
3
4;; Author: David O'Toole <dto@gnu.org>
c8d0cf5c 5;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
699b9291 6;; Keywords: hypermedia, outlines, wp
3ab2c837 7;; Version: 7.7
9542795a 8
514a6ce6
CD
9;; This file is part of GNU Emacs.
10;;
b1fc2b50 11;; GNU Emacs is free software: you can redistribute it and/or modify
9542795a 12;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
9542795a 15
514a6ce6 16;; GNU Emacs is distributed in the hope that it will be useful,
9542795a
CD
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
b1fc2b50 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
9542795a 23
9542795a
CD
24;;; Commentary:
25
699b9291
CD
26;; This program allow configurable publishing of related sets of
27;; Org-mode files as a complete website.
9542795a
CD
28;;
29;; org-publish.el can do the following:
30;;
c8d0cf5c 31;; + Publish all one's org-files to HTML or PDF
699b9291 32;; + Upload HTML, images, attachments and other files to a web server
9542795a 33;; + Exclude selected private pages from publishing
ed21c5c8 34;; + Publish a clickable sitemap of pages
699b9291 35;; + Manage local timestamps for publishing only changed files
9542795a
CD
36;; + Accept plugin functions to extend range of publishable content
37;;
c8d0cf5c 38;; Documentation for publishing is in the manual.
9542795a
CD
39
40;;; Code:
41
ed21c5c8 42
9542795a
CD
43(eval-when-compile
44 (require 'cl))
b349f79f
CD
45(require 'org)
46(require 'org-exp)
3ab2c837 47(require 'format-spec)
9542795a 48
1e63a7fb
GM
49(eval-and-compile
50 (unless (fboundp 'declare-function)
51 (defmacro declare-function (fn file &optional arglist fileonly))))
52
3ab2c837
BG
53(defvar org-publish-initial-buffer nil
54 "The buffer `org-publish' has been called from.")
55
56(defvar org-publish-temp-files nil
57 "Temporary list of files to be published.")
58
59;; Here, so you find the variable right before it's used the first time:
60(defvar org-publish-cache nil
61 "This will cache timestamps and titles for files in publishing projects.
62Blocks could hash sha1 values here.")
63
9542795a 64(defgroup org-publish nil
b349f79f
CD
65 "Options for publishing a set of Org-mode and related files."
66 :tag "Org Publishing"
67 :group 'org)
9542795a 68
9542795a
CD
69(defcustom org-publish-project-alist nil
70 "Association list to control publishing behavior.
71Each element of the alist is a publishing 'project.' The CAR of
50243adf 72each element is a string, uniquely identifying the project. The
9542795a
CD
73CDR of each element is in one of the following forms:
74
afe98dfa
CD
751. A well-formed property list with an even number of elements, alternating
76 keys and values, specifying parameters for the publishing process.
9542795a 77
afe98dfa 78 (:property value :property value ... )
9542795a 79
afe98dfa
CD
802. A meta-project definition, specifying of a list of sub-projects:
81
82 (:components (\"project-1\" \"project-2\" ...))
9542795a
CD
83
84When the CDR of an element of org-publish-project-alist is in
85this second form, the elements of the list after :components are
86taken to be components of the project, which group together files
50243adf
JB
87requiring different publishing options. When you publish such a
88project with \\[org-publish], the components all publish.
9542795a
CD
89
90When a property is given a value in org-publish-project-alist, its
91setting overrides the value of the corresponding user variable
50243adf 92\(if any) during publishing. However, options set within a file
9542795a
CD
93override everything.
94
95Most properties are optional, but some should always be set:
96
699b9291
CD
97 :base-directory Directory containing publishing source files
98 :base-extension Extension (without the dot!) of source files.
afe98dfa
CD
99 This can be a regular expression. If not given,
100 \"org\" will be used as default extension.
699b9291
CD
101 :publishing-directory Directory (possibly remote) where output
102 files will be published
9542795a
CD
103
104The :exclude property may be used to prevent certain files from
50243adf 105being published. Its value may be a string or regexp matching
9542795a
CD
106file names you don't want to be published.
107
50243adf 108The :include property may be used to include extra files. Its
b349f79f
CD
109value may be a list of filenames to include. The filenames are
110considered relative to the base directory.
9542795a
CD
111
112When both :include and :exclude properties are given values, the
113exclusion step happens first.
114
115One special property controls which back-end function to use for
50243adf 116publishing files in the project. This can be used to extend the
9542795a
CD
117set of file types publishable by org-publish, as well as the set
118of output formats.
119
50243adf
JB
120 :publishing-function Function to publish file. The default is
121 `org-publish-org-to-html', but other
122 values are possible. May also be a
699b9291
CD
123 list of functions, in which case
124 each function in the list is invoked
125 in turn.
9542795a
CD
126
127Another property allows you to insert code that prepares a
50243adf 128project for publishing. For example, you could call GNU Make on a
699b9291 129certain makefile, to ensure published files are built up to date.
9542795a 130
699b9291 131 :preparation-function Function to be called before publishing
ed21c5c8
CD
132 this project. This may also be a list
133 of functions.
b349f79f 134 :completion-function Function to be called after publishing
ed21c5c8
CD
135 this project. This may also be a list
136 of functions.
9542795a
CD
137
138Some properties control details of the Org publishing process,
139and are equivalent to the corresponding user variables listed in
50243adf 140the right column. See the documentation for those variables to
9542795a
CD
141learn more about their use and default values.
142
50243adf
JB
143 :language `org-export-default-language'
144 :headline-levels `org-export-headline-levels'
145 :section-numbers `org-export-with-section-numbers'
146 :table-of-contents `org-export-with-toc'
147 :emphasize `org-export-with-emphasize'
148 :sub-superscript `org-export-with-sub-superscripts'
149 :TeX-macros `org-export-with-TeX-macros'
150 :fixed-width `org-export-with-fixed-width'
151 :tables `org-export-with-tables'
152 :table-auto-headline `org-export-highlight-first-table-line'
153 :style `org-export-html-style'
154 :convert-org-links `org-export-html-link-org-files-as-html'
155 :inline-images `org-export-html-inline-images'
156 :expand-quoted-html `org-export-html-expand'
157 :timestamp `org-export-html-with-timestamp'
158 :publishing-directory `org-export-publishing-directory'
3ab2c837
BG
159 :html-preamble `org-export-html-preamble'
160 :html-postamble `org-export-html-postamble'
50243adf
JB
161 :author `user-full-name'
162 :email `user-mail-address'
9542795a 163
ed21c5c8
CD
164The following properties may be used to control publishing of a
165sitemap of files or summary page for a given project.
9542795a 166
ed21c5c8 167 :auto-sitemap Whether to publish a sitemap during
50243adf 168 `org-publish-current-project' or `org-publish-all'.
ed21c5c8 169 :sitemap-filename Filename for output of sitemap. Defaults
c8d0cf5c 170 to 'sitemap.org' (which becomes 'sitemap.html').
ed21c5c8
CD
171 :sitemap-title Title of sitemap page. Defaults to name of file.
172 :sitemap-function Plugin function to use for generation of sitemap.
173 Defaults to `org-publish-org-sitemap', which
699b9291 174 generates a plain list of links to all files
2c3ad40d 175 in the project.
ed21c5c8 176 :sitemap-style Can be `list' (sitemap is just an itemized list
ff4be292 177 of the titles of the files involved) or
2c3ad40d 178 `tree' (the directory structure of the source
ed21c5c8 179 files is reflected in the sitemap). Defaults to
86fbb8ca 180 `tree'.
3ab2c837
BG
181 :sitemap-sans-extension Remove extension from sitemap's
182 filenames. Useful to have cool
183 URIs (see
184 http://www.w3.org/Provider/Style/URI).
185 Defaults to nil.
86fbb8ca
CD
186
187 If you create a sitemap file, adjust the sorting like this:
188
189 :sitemap-sort-folders Where folders should appear in the sitemap.
190 Set this to `first' (default) or `last' to
191 display folders first or last, respectively.
192 Any other value will mix files and folders.
3ab2c837 193 :sitemap-sort-files The site map is normally sorted alphabetically.
91af3942 194 You can change this behavior setting this to
3ab2c837 195 `chronologically', `anti-chronologically' or nil.
86fbb8ca
CD
196 :sitemap-ignore-case Should sorting be case-sensitive? Default nil.
197
198The following properties control the creation of a concept index.
199
afe98dfa
CD
200 :makeindex Create a concept index.
201
202Other properties affecting publication.
203
204 :body-only Set this to 't' to publish only the body of the
3ab2c837
BG
205 documents, excluding everything outside and
206 including the <body> tags in HTML, or
afe98dfa 207 \begin{document}..\end{document} in LaTeX."
9542795a
CD
208 :group 'org-publish
209 :type 'alist)
210
9542795a 211(defcustom org-publish-use-timestamps-flag t
86fbb8ca 212 "Non-nil means use timestamp checking to publish only changed files.
50243adf 213When nil, do no timestamp checking and always publish all files."
9542795a
CD
214 :group 'org-publish
215 :type 'boolean)
216
ff4be292 217(defcustom org-publish-timestamp-directory (convert-standard-filename
71d35b24 218 "~/.org-timestamps/")
9542795a
CD
219 "Name of directory in which to store publishing timestamps."
220 :group 'org-publish
699b9291 221 :type 'directory)
9542795a 222
c8d0cf5c 223(defcustom org-publish-list-skipped-files t
ed21c5c8 224 "Non-nil means show message about files *not* published."
c8d0cf5c
CD
225 :group 'org-publish
226 :type 'boolean)
227
699b9291
CD
228(defcustom org-publish-before-export-hook nil
229 "Hook run before export on the Org file.
c8d0cf5c 230The hook may modify the file in arbitrary ways before publishing happens.
8bfe682a 231The original version of the buffer will be restored after publishing."
699b9291
CD
232 :group 'org-publish
233 :type 'hook)
9542795a 234
699b9291
CD
235(defcustom org-publish-after-export-hook nil
236 "Hook run after export on the exported buffer.
c8d0cf5c 237Any changes made by this hook will be saved."
699b9291
CD
238 :group 'org-publish
239 :type 'hook)
9542795a 240
3ab2c837
BG
241(defcustom org-publish-sitemap-sort-files 'alphabetically
242 "How sitemaps files should be sorted by default?
243Possible values are `alphabetically', `chronologically', `anti-chronologically' and nil.
244If `alphabetically', files will be sorted alphabetically.
245If `chronologically', files will be sorted with older modification time first.
246If `anti-chronologically', files will be sorted with newer modification time first.
247nil won't sort files.
86fbb8ca
CD
248
249You can overwrite this default per project in your
3ab2c837 250`org-publish-project-alist', using `:sitemap-sort-files'."
86fbb8ca 251 :group 'org-publish
3ab2c837 252 :type 'symbol)
86fbb8ca
CD
253
254(defcustom org-publish-sitemap-sort-folders 'first
255 "A symbol, denoting if folders are sorted first in sitemaps.
256Possible values are `first', `last', and nil.
257If `first', folders will be sorted before files.
258If `last', folders are sorted to the end after the files.
259Any other value will not mix files and folders.
260
261You can overwrite this default per project in your
262`org-publish-project-alist', using `:sitemap-sort-folders'."
263 :group 'org-publish
264 :type 'symbol)
265
266(defcustom org-publish-sitemap-sort-ignore-case nil
267 "Sort sitemaps case insensitively by default?
268
269You can overwrite this default per project in your
270`org-publish-project-alist', using `:sitemap-ignore-case'."
271 :group 'org-publish
272 :type 'boolean)
273
3ab2c837
BG
274(defcustom org-publish-sitemap-date-format "%Y-%m-%d"
275 "Format for `format-time-string' which is used to print a date
276in the sitemap."
277 :group 'org-publish
278 :type 'string)
279
280(defcustom org-publish-sitemap-file-entry-format "%t"
281 "How a sitemap file entry is formated.
282You could use brackets to delimit on what part the link will be.
283
284%t is the title.
285%a is the author.
286%d is the date formated using `org-publish-sitemap-date-format'."
287 :group 'org-publish
288 :type 'string)
289
290
291;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
292;;; Sanitize-plist (FIXME why?)
293
294(defun org-publish-sanitize-plist (plist)
295 ;; FIXME document
296 (mapcar (lambda (x)
297 (or (cdr (assq x '((:index-filename . :sitemap-filename)
298 (:index-title . :sitemap-title)
299 (:index-function . :sitemap-function)
300 (:index-style . :sitemap-style)
301 (:auto-index . :auto-sitemap))))
302 x))
303 plist))
304
699b9291
CD
305;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
306;;; Timestamp-related functions
9542795a 307
c8d0cf5c 308(defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
9542795a 309 "Return path to timestamp file for filename FILENAME."
c8d0cf5c
CD
310 (setq filename (concat filename "::" (or pub-dir "") "::"
311 (format "%s" (or pub-func ""))))
86fbb8ca 312 (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
9542795a 313
c8d0cf5c 314(defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir)
86fbb8ca
CD
315 "Return t if FILENAME should be published in PUB-DIR using PUB-FUNC.
316TRUE-PUB-DIR is where the file will truly end up. Currently we are not using
c8d0cf5c 317this - maybe it can eventually be used to check if the file is present at
3ab2c837 318the target location, and how old it is. Right now we cannot do this, because
c8d0cf5c
CD
319we do not know under what file name the file will be stored - the publishing
320function can still decide about that independently."
2c3ad40d
CD
321 (let ((rtn
322 (if org-publish-use-timestamps-flag
86fbb8ca
CD
323 (org-publish-cache-file-needs-publishing
324 filename pub-dir pub-func)
2c3ad40d
CD
325 ;; don't use timestamps, always return t
326 t)))
327 (if rtn
c8d0cf5c
CD
328 (message "Publishing file %s using `%s'" filename pub-func)
329 (when org-publish-list-skipped-files
330 (message "Skipping unmodified file %s" filename)))
2c3ad40d 331 rtn))
9542795a 332
c8d0cf5c 333(defun org-publish-update-timestamp (filename &optional pub-dir pub-func)
699b9291
CD
334 "Update publishing timestamp for file FILENAME.
335If there is no timestamp, create one."
86fbb8ca
CD
336 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func))
337 (stamp (org-publish-cache-ctime-of-src filename)))
338 (org-publish-cache-set key stamp)))
c8d0cf5c
CD
339
340(defun org-publish-remove-all-timestamps ()
86fbb8ca 341 "Remove all files in the timestamp directory."
c8d0cf5c
CD
342 (let ((dir org-publish-timestamp-directory)
343 files)
344 (when (and (file-exists-p dir)
345 (file-directory-p dir))
86fbb8ca
CD
346 (mapc 'delete-file (directory-files dir 'full "[^.]\\'"))
347 (org-publish-reset-cache))))
c8d0cf5c 348
699b9291 349
699b9291
CD
350;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
351;;; Compatibility aliases
352
353;; Delete-dups is not in Emacs <22
354(if (fboundp 'delete-dups)
355 (defalias 'org-publish-delete-dups 'delete-dups)
356 (defun org-publish-delete-dups (list)
357 "Destructively remove `equal' duplicates from LIST.
358Store the result in LIST and return it. LIST must be a proper list.
359Of several `equal' occurrences of an element in LIST, the first
360one is kept.
361
362This is a compatibility function for Emacsen without `delete-dups'."
363 ;; Code from `subr.el' in Emacs 22:
364 (let ((tail list))
365 (while tail
366 (setcdr tail (delete (car tail) (cdr tail)))
367 (setq tail (cdr tail))))
368 list))
369
1e63a7fb 370(declare-function org-publish-delete-dups "org-publish" (list))
ed21c5c8 371(declare-function find-lisp-find-files "find-lisp" (directory regexp))
1e63a7fb 372
699b9291
CD
373;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
374;;; Getting project information out of org-publish-project-alist
375
699b9291 376(defun org-publish-expand-projects (projects-alist)
621f83e4
CD
377 "Expand projects in PROJECTS-ALIST.
378This splices all the components into the list."
379 (let ((rest projects-alist) rtn p components)
380 (while (setq p (pop rest))
381 (if (setq components (plist-get (cdr p) :components))
382 (setq rest (append
383 (mapcar (lambda (x) (assoc x org-publish-project-alist))
384 components)
385 rest))
386 (push p rtn)))
387 (nreverse (org-publish-delete-dups (delq nil rtn)))))
ff4be292 388
3ab2c837
BG
389(defvar org-sitemap-sort-files)
390(defvar org-sitemap-sort-folders)
391(defvar org-sitemap-ignore-case)
392(defvar org-sitemap-requested)
393(defvar org-sitemap-date-format)
394(defvar org-sitemap-file-entry-format)
86fbb8ca 395(defun org-publish-compare-directory-files (a b)
3ab2c837 396 "Predicate for `sort', that sorts folders and files for sitemap."
86fbb8ca 397 (let ((retval t))
3ab2c837
BG
398 (when (or org-sitemap-sort-files org-sitemap-sort-folders)
399 ;; First we sort files:
400 (when org-sitemap-sort-files
401 (cond ((equal org-sitemap-sort-files 'alphabetically)
402 (let* ((adir (file-directory-p a))
403 (aorg (and (string-match "\\.org$" a) (not adir)))
404 (bdir (file-directory-p b))
405 (borg (and (string-match "\\.org$" b) (not bdir)))
406 (A (if aorg
407 (concat (file-name-directory a)
408 (org-publish-find-title a)) a))
409 (B (if borg
410 (concat (file-name-directory b)
411 (org-publish-find-title b)) b)))
412 (setq retval (if org-sitemap-ignore-case
413 (not (string-lessp (upcase B) (upcase A)))
414 (not (string-lessp B A))))))
415 ((or (equal org-sitemap-sort-files 'chronologically)
416 (equal org-sitemap-sort-files 'anti-chronologically))
417 (let* ((adate (org-publish-find-date a))
418 (bdate (org-publish-find-date b))
419 (A (+ (lsh (car adate) 16) (cadr adate)))
420 (B (+ (lsh (car bdate) 16) (cadr bdate))))
421 (setq retval (if (equal org-sitemap-sort-files 'chronologically)
422 (<= A B)
423 (>= A B)))))))
86fbb8ca 424 ;; Directory-wise wins:
3ab2c837 425 (when org-sitemap-sort-folders
86fbb8ca
CD
426 ;; a is directory, b not:
427 (cond
428 ((and (file-directory-p a) (not (file-directory-p b)))
3ab2c837 429 (setq retval (equal org-sitemap-sort-folders 'first)))
86fbb8ca
CD
430 ;; a is not a directory, but b is:
431 ((and (not (file-directory-p a)) (file-directory-p b))
3ab2c837 432 (setq retval (equal org-sitemap-sort-folders 'last))))))
86fbb8ca
CD
433 retval))
434
20908596
CD
435(defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
436 "Set `org-publish-temp-files' with files from BASE-DIR directory.
437If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
438non-nil, restrict this list to the files matching the regexp
439MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
440SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
33306645 441matching the regexp SKIP-DIR when recursing through BASE-DIR."
20908596 442 (mapc (lambda (f)
93b62de8 443 (let ((fd-p (file-directory-p f))
20908596
CD
444 (fnd (file-name-nondirectory f)))
445 (if (and fd-p recurse
446 (not (string-match "^\\.+$" fnd))
447 (if skip-dir (not (string-match skip-dir fnd)) t))
448 (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
449 (unless (or fd-p ;; this is a directory
450 (and skip-file (string-match skip-file fnd))
93b62de8 451 (not (file-exists-p (file-truename f)))
20908596 452 (not (string-match match fnd)))
86fbb8ca 453
20908596 454 (pushnew f org-publish-temp-files)))))
3ab2c837 455 (if org-sitemap-requested
86fbb8ca
CD
456 (sort (directory-files base-dir t (unless recurse match))
457 'org-publish-compare-directory-files)
458 (directory-files base-dir t (unless recurse match)))))
20908596 459
699b9291
CD
460(defun org-publish-get-base-files (project &optional exclude-regexp)
461 "Return a list of all files in PROJECT.
9542795a
CD
462If EXCLUDE-REGEXP is set, this will be used to filter out
463matching filenames."
699b9291
CD
464 (let* ((project-plist (cdr project))
465 (base-dir (file-name-as-directory
466 (plist-get project-plist :base-directory)))
33306645
CD
467 (include-list (plist-get project-plist :include))
468 (recurse (plist-get project-plist :recursive))
469 (extension (or (plist-get project-plist :base-extension) "org"))
86fbb8ca
CD
470 ;; sitemap-... variables are dynamically scoped for
471 ;; org-publish-compare-directory-files:
3ab2c837 472 (org-sitemap-requested
86fbb8ca 473 (plist-get project-plist :auto-sitemap))
3ab2c837
BG
474 (sitemap-filename
475 (or (plist-get project-plist :sitemap-filename)
476 "sitemap.org"))
477 (org-sitemap-sort-folders
86fbb8ca
CD
478 (if (plist-member project-plist :sitemap-sort-folders)
479 (plist-get project-plist :sitemap-sort-folders)
480 org-publish-sitemap-sort-folders))
3ab2c837
BG
481 (org-sitemap-sort-files
482 (cond ((plist-member project-plist :sitemap-sort-files)
483 (plist-get project-plist :sitemap-sort-files))
484 ;; For backward compatibility:
485 ((plist-member project-plist :sitemap-alphabetically)
486 (if (plist-get project-plist :sitemap-alphabetically)
487 'alphabetically nil))
488 (t org-publish-sitemap-sort-files)))
489 (org-sitemap-ignore-case
86fbb8ca
CD
490 (if (plist-member project-plist :sitemap-ignore-case)
491 (plist-get project-plist :sitemap-ignore-case)
492 org-publish-sitemap-sort-ignore-case))
c8d0cf5c
CD
493 (match (if (eq extension 'any)
494 "^[^\\.]"
495 (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
3ab2c837
BG
496 ;; Make sure `org-sitemap-sort-folders' has an accepted value
497 (unless (memq org-sitemap-sort-folders '(first last))
498 (setq org-sitemap-sort-folders nil))
86fbb8ca 499
20908596 500 (setq org-publish-temp-files nil)
3ab2c837
BG
501 (if org-sitemap-requested
502 (pushnew (expand-file-name (concat base-dir sitemap-filename))
503 org-publish-temp-files))
20908596
CD
504 (org-publish-get-base-files-1 base-dir recurse match
505 ;; FIXME distinguish exclude regexp
506 ;; for skip-file and skip-dir?
507 exclude-regexp exclude-regexp)
b349f79f 508 (mapc (lambda (f)
ff4be292 509 (pushnew
b349f79f
CD
510 (expand-file-name (concat base-dir f))
511 org-publish-temp-files))
512 include-list)
20908596 513 org-publish-temp-files))
9542795a 514
c8d0cf5c 515(defun org-publish-get-project-from-filename (filename &optional up)
86fbb8ca
CD
516 "Return the project that FILENAME belongs to."
517 (let* ((filename (expand-file-name filename))
518 project-name)
519
520 (catch 'p-found
521 (dolist (prj org-publish-project-alist)
522 (unless (plist-get (cdr prj) :components)
523 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
524 (let* ((r (plist-get (cdr prj) :recursive))
afe98dfa
CD
525 (b (expand-file-name (file-name-as-directory
526 (plist-get (cdr prj) :base-directory))))
86fbb8ca
CD
527 (x (or (plist-get (cdr prj) :base-extension) "org"))
528 (e (plist-get (cdr prj) :exclude))
529 (i (plist-get (cdr prj) :include))
530 (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
3ab2c837
BG
531 (when
532 (or
533 (and
534 i (member filename
535 (mapcar
afe98dfa
CD
536 (lambda (file) (expand-file-name file b))
537 i)))
86fbb8ca
CD
538 (and
539 (not (and e (string-match e filename)))
540 (string-match xm filename)))
541 (setq project-name (car prj))
542 (throw 'p-found project-name))))))
c8d0cf5c
CD
543 (when up
544 (dolist (prj org-publish-project-alist)
545 (if (member project-name (plist-get (cdr prj) :components))
546 (setq project-name (car prj)))))
699b9291 547 (assoc project-name org-publish-project-alist)))
daa89d0f 548
699b9291
CD
549;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
550;;; Pluggable publishing back-end functions
daa89d0f 551
699b9291 552(defun org-publish-org-to (format plist filename pub-dir)
daa89d0f
CD
553 "Publish an org file to FORMAT.
554PLIST is the property list for the given project.
699b9291
CD
555FILENAME is the filename of the org file to be published.
556PUB-DIR is the publishing directory."
03f3cf35 557 (require 'org)
699b9291
CD
558 (unless (file-exists-p pub-dir)
559 (make-directory pub-dir t))
71d35b24
CD
560 (let ((visiting (find-buffer-visiting filename)))
561 (save-excursion
c3313451 562 (switch-to-buffer (or visiting (find-file filename)))
71d35b24
CD
563 (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
564 (init-buf (current-buffer))
565 (init-point (point))
566 (init-buf-string (buffer-string))
567 export-buf-or-file)
568 ;; run hooks before exporting
569 (run-hooks 'org-publish-before-export-hook)
570 ;; export the possibly modified buffer
571 (setq export-buf-or-file
572 (funcall (intern (concat "org-export-as-" format))
573 (plist-get plist :headline-levels)
3ab2c837
BG
574 nil plist nil
575 (plist-get plist :body-only)
afe98dfa 576 pub-dir))
71d35b24
CD
577 (when (and (bufferp export-buf-or-file)
578 (buffer-live-p export-buf-or-file))
579 (set-buffer export-buf-or-file)
580 ;; run hooks after export and save export
ed21c5c8
CD
581 (progn (run-hooks 'org-publish-after-export-hook)
582 (if (buffer-modified-p) (save-buffer)))
71d35b24
CD
583 (kill-buffer export-buf-or-file))
584 ;; maybe restore buffer's content
585 (set-buffer init-buf)
586 (when (buffer-modified-p init-buf)
587 (erase-buffer)
588 (insert init-buf-string)
589 (save-buffer)
590 (goto-char init-point))
591 (unless visiting
592 (kill-buffer init-buf))))))
699b9291 593
ed21c5c8
CD
594(defmacro org-publish-with-aux-preprocess-maybe (&rest body)
595 "Execute BODY with a modified hook to preprocess for index."
596 `(let ((org-export-preprocess-after-headline-targets-hook
597 (if (plist-get project-plist :makeindex)
598 (cons 'org-publish-aux-preprocess
599 org-export-preprocess-after-headline-targets-hook)
600 org-export-preprocess-after-headline-targets-hook)))
601 ,@body))
602
603(defvar project-plist)
699b9291
CD
604(defun org-publish-org-to-latex (plist filename pub-dir)
605 "Publish an org file to LaTeX.
606See `org-publish-org-to' to the list of arguments."
ed21c5c8
CD
607 (org-publish-with-aux-preprocess-maybe
608 (org-publish-org-to "latex" plist filename pub-dir)))
699b9291 609
71d35b24
CD
610(defun org-publish-org-to-pdf (plist filename pub-dir)
611 "Publish an org file to PDF (via LaTeX).
612See `org-publish-org-to' to the list of arguments."
ed21c5c8
CD
613 (org-publish-with-aux-preprocess-maybe
614 (org-publish-org-to "pdf" plist filename pub-dir)))
71d35b24 615
699b9291
CD
616(defun org-publish-org-to-html (plist filename pub-dir)
617 "Publish an org file to HTML.
618See `org-publish-org-to' to the list of arguments."
ed21c5c8
CD
619 (org-publish-with-aux-preprocess-maybe
620 (org-publish-org-to "html" plist filename pub-dir)))
699b9291 621
c8d0cf5c
CD
622(defun org-publish-org-to-org (plist filename pub-dir)
623 "Publish an org file to HTML.
624See `org-publish-org-to' to the list of arguments."
625 (org-publish-org-to "org" plist filename pub-dir))
626
afe98dfa
CD
627(defun org-publish-org-to-ascii (plist filename pub-dir)
628 "Publish an org file to ASCII.
629See `org-publish-org-to' to the list of arguments."
630 (org-publish-with-aux-preprocess-maybe
631 (org-publish-org-to "ascii" plist filename pub-dir)))
632
633(defun org-publish-org-to-latin1 (plist filename pub-dir)
634 "Publish an org file to Latin-1.
635See `org-publish-org-to' to the list of arguments."
636 (org-publish-with-aux-preprocess-maybe
637 (org-publish-org-to "latin1" plist filename pub-dir)))
638
639(defun org-publish-org-to-utf8 (plist filename pub-dir)
640 "Publish an org file to UTF-8.
641See `org-publish-org-to' to the list of arguments."
642 (org-publish-with-aux-preprocess-maybe
643 (org-publish-org-to "utf8" plist filename pub-dir)))
644
699b9291 645(defun org-publish-attachment (plist filename pub-dir)
9542795a 646 "Publish a file with no transformation of any kind.
699b9291 647See `org-publish-org-to' to the list of arguments."
93b62de8 648 ;; make sure eshell/cp code is loaded
3ab2c837
BG
649 (unless (file-directory-p pub-dir)
650 (make-directory pub-dir t))
651 (or (equal (expand-file-name (file-name-directory filename))
652 (file-name-as-directory (expand-file-name pub-dir)))
c8d0cf5c
CD
653 (copy-file filename
654 (expand-file-name (file-name-nondirectory filename) pub-dir)
655 t)))
699b9291
CD
656
657;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
658;;; Publishing files, sets of files, and indices
659
86fbb8ca
CD
660(defun org-publish-file (filename &optional project no-cache)
661 "Publish file FILENAME from PROJECT.
662If NO-CACHE is not nil, do not initialize org-publish-cache and
663write it to disk. This is needed, since this function is used to
664publish single files, when entire projects are published.
665See `org-publish-projects'."
c8d0cf5c
CD
666 (let* ((project
667 (or project
668 (or (org-publish-get-project-from-filename filename)
86fbb8ca
CD
669 (error "File %s not part of any known project"
670 (abbreviate-file-name filename)))))
c8d0cf5c 671 (project-plist (cdr project))
afe98dfa 672 (ftname (expand-file-name filename))
c8d0cf5c
CD
673 (publishing-function
674 (or (plist-get project-plist :publishing-function)
675 'org-publish-org-to-html))
afe98dfa
CD
676 (base-dir
677 (file-name-as-directory
678 (expand-file-name
679 (or (plist-get project-plist :base-directory)
680 (error "Project %s does not have :base-directory defined"
681 (car project))))))
682 (pub-dir
683 (file-name-as-directory
684 (file-truename
685 (or (plist-get project-plist :publishing-directory)
686 (error "Project %s does not have :publishing-directory defined"
687 (car project))))))
c8d0cf5c 688 tmp-pub-dir)
86fbb8ca
CD
689
690 (unless no-cache
691 (org-publish-initialize-cache (car project)))
692
c8d0cf5c
CD
693 (setq tmp-pub-dir
694 (file-name-directory
695 (concat pub-dir
696 (and (string-match (regexp-quote base-dir) ftname)
697 (substring ftname (match-end 0))))))
698 (if (listp publishing-function)
699 ;; allow chain of publishing functions
700 (mapc (lambda (f)
701 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir)
702 (funcall f project-plist filename tmp-pub-dir)
703 (org-publish-update-timestamp filename pub-dir f)))
704 publishing-function)
705 (when (org-publish-needed-p filename pub-dir publishing-function
706 tmp-pub-dir)
707 (funcall publishing-function project-plist filename tmp-pub-dir)
708 (org-publish-update-timestamp
86fbb8ca
CD
709 filename pub-dir publishing-function)))
710 (unless no-cache (org-publish-write-cache-file))))
699b9291
CD
711
712(defun org-publish-projects (projects)
713 "Publish all files belonging to the PROJECTS alist.
ed21c5c8
CD
714If :auto-sitemap is set, publish the sitemap too.
715If :makeindex is set, also produce a file theindex.org."
699b9291
CD
716 (mapc
717 (lambda (project)
86fbb8ca
CD
718 ;; Each project uses it's own cache file:
719 (org-publish-initialize-cache (car project))
b349f79f
CD
720 (let*
721 ((project-plist (cdr project))
722 (exclude-regexp (plist-get project-plist :exclude))
ed21c5c8
CD
723 (sitemap-p (plist-get project-plist :auto-sitemap))
724 (sitemap-filename (or (plist-get project-plist :sitemap-filename)
725 "sitemap.org"))
726 (sitemap-function (or (plist-get project-plist :sitemap-function)
727 'org-publish-org-sitemap))
3ab2c837
BG
728 (org-sitemap-date-format (or (plist-get project-plist :sitemap-date-format)
729 org-publish-sitemap-date-format))
730 (org-sitemap-file-entry-format (or (plist-get project-plist :sitemap-file-entry-format)
731 org-publish-sitemap-file-entry-format))
b349f79f
CD
732 (preparation-function (plist-get project-plist :preparation-function))
733 (completion-function (plist-get project-plist :completion-function))
734 (files (org-publish-get-base-files project exclude-regexp)) file)
ed21c5c8
CD
735 (when preparation-function (run-hooks 'preparation-function))
736 (if sitemap-p (funcall sitemap-function project sitemap-filename))
699b9291 737 (while (setq file (pop files))
86fbb8ca 738 (org-publish-file file project t))
ed21c5c8 739 (when (plist-get project-plist :makeindex)
3ab2c837 740 (org-publish-index-generate-theindex
ed21c5c8
CD
741 (plist-get project-plist :base-directory))
742 (org-publish-file (expand-file-name
743 "theindex.org"
744 (plist-get project-plist :base-directory))
86fbb8ca
CD
745 project t))
746 (when completion-function (run-hooks 'completion-function))
747 (org-publish-write-cache-file)))
699b9291
CD
748 (org-publish-expand-projects projects)))
749
ed21c5c8 750(defun org-publish-org-sitemap (project &optional sitemap-filename)
86fbb8ca 751 "Create a sitemap of pages in set defined by PROJECT.
ed21c5c8
CD
752Optionally set the filename of the sitemap with SITEMAP-FILENAME.
753Default for SITEMAP-FILENAME is 'sitemap.org'."
699b9291
CD
754 (let* ((project-plist (cdr project))
755 (dir (file-name-as-directory
756 (plist-get project-plist :base-directory)))
b349f79f
CD
757 (localdir (file-name-directory dir))
758 (indent-str (make-string 2 ?\ ))
699b9291 759 (exclude-regexp (plist-get project-plist :exclude))
b349f79f 760 (files (nreverse (org-publish-get-base-files project exclude-regexp)))
ed21c5c8
CD
761 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
762 (sitemap-title (or (plist-get project-plist :sitemap-title)
763 (concat "Sitemap for project " (car project))))
764 (sitemap-style (or (plist-get project-plist :sitemap-style)
2c3ad40d 765 'tree))
3ab2c837 766 (sitemap-sans-extension (plist-get project-plist :sitemap-sans-extension))
ed21c5c8
CD
767 (visiting (find-buffer-visiting sitemap-filename))
768 (ifn (file-name-nondirectory sitemap-filename))
769 file sitemap-buffer)
770 (with-current-buffer (setq sitemap-buffer
771 (or visiting (find-file sitemap-filename)))
ff4be292 772 (erase-buffer)
ed21c5c8 773 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
699b9291 774 (while (setq file (pop files))
b349f79f
CD
775 (let ((fn (file-name-nondirectory file))
776 (link (file-relative-name file dir))
777 (oldlocal localdir))
3ab2c837
BG
778 (when sitemap-sans-extension
779 (setq link (file-name-sans-extension link)))
ed21c5c8
CD
780 ;; sitemap shouldn't list itself
781 (unless (equal (file-truename sitemap-filename)
ff4be292 782 (file-truename file))
ed21c5c8
CD
783 (if (eq sitemap-style 'list)
784 (message "Generating list-style sitemap for %s" sitemap-title)
785 (message "Generating tree-style sitemap for %s" sitemap-title)
2c3ad40d
CD
786 (setq localdir (concat (file-name-as-directory dir)
787 (file-name-directory link)))
788 (unless (string= localdir oldlocal)
789 (if (string= localdir dir)
790 (setq indent-str (make-string 2 ?\ ))
791 (let ((subdirs
792 (split-string
793 (directory-file-name
794 (file-name-directory
795 (file-relative-name localdir dir))) "/"))
93b62de8
CD
796 (subdir "")
797 (old-subdirs (split-string
798 (file-relative-name oldlocal dir) "/")))
2c3ad40d 799 (setq indent-str (make-string 2 ?\ ))
93b62de8
CD
800 (while (string= (car old-subdirs) (car subdirs))
801 (setq indent-str (concat indent-str (make-string 2 ?\ )))
802 (pop old-subdirs)
803 (pop subdirs))
2c3ad40d
CD
804 (dolist (d subdirs)
805 (setq subdir (concat subdir d "/"))
93b62de8
CD
806 (insert (concat indent-str " + " d "\n"))
807 (setq indent-str (make-string
2c3ad40d
CD
808 (+ (length indent-str) 2) ?\ )))))))
809 ;; This is common to 'flat and 'tree
3ab2c837
BG
810 (let ((entry
811 (org-publish-format-file-entry org-sitemap-file-entry-format
812 file project-plist))
813 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
814 (cond ((string-match-p regexp entry)
815 (string-match regexp entry)
816 (insert (concat indent-str " + " (match-string 1 entry)
817 "[[file:" link "]["
818 (match-string 2 entry)
819 "]]" (match-string 3 entry) "\n")))
820 (t
821 (insert (concat indent-str " + [[file:" link "]["
822 entry
823 "]]\n"))))))))
ff4be292 824 (save-buffer))
ed21c5c8 825 (or visiting (kill-buffer sitemap-buffer))))
9542795a 826
3ab2c837
BG
827(defun org-publish-format-file-entry (fmt file project-plist)
828 (format-spec fmt
829 `((?t . ,(org-publish-find-title file t))
830 (?d . ,(format-time-string org-sitemap-date-format
831 (org-publish-find-date file)))
832 (?a . ,(or (plist-get project-plist :author) user-full-name)))))
833
834(defun org-publish-find-title (file &optional reset)
86fbb8ca
CD
835 "Find the title of FILE in project."
836 (or
3ab2c837 837 (and (not reset) (org-publish-cache-get-file-property file :title nil t))
86fbb8ca 838 (let* ((visiting (find-buffer-visiting file))
3ab2c837
BG
839 (buffer (or visiting (find-file-noselect file)))
840 title)
841 (with-current-buffer buffer
842 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
843 (org-infile-export-plist))))
844 (setq title
845 (or (plist-get opt-plist :title)
846 (and (not
847 (plist-get opt-plist :skip-before-1st-heading))
848 (org-export-grab-title-from-buffer))
849 (file-name-nondirectory (file-name-sans-extension file))))))
850 (unless visiting
851 (kill-buffer buffer))
852 (org-publish-cache-set-file-property file :title title)
853 title)))
854
855(defun org-publish-find-date (file)
856 "Find the date of FILE in project.
857If FILE provides a #+date keyword use it else use the file
858system's modification time.
859
860It returns time in `current-time' format."
861 (let ((visiting (find-buffer-visiting file)))
862 (save-excursion
c3313451 863 (switch-to-buffer (or visiting (find-file-noselect file nil t)))
3ab2c837
BG
864 (let* ((plist (org-infile-export-plist))
865 (date (plist-get plist :date)))
866 (unless visiting
867 (kill-buffer (current-buffer)))
868 (if date
869 (org-time-string-to-time date)
870 (when (file-exists-p file)
871 (nth 5 (file-attributes file))))))))
b349f79f 872
699b9291
CD
873;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
874;;; Interactive publishing functions
9542795a 875
71d35b24 876;;;###autoload
20908596 877(defalias 'org-publish-project 'org-publish)
9542795a
CD
878
879;;;###autoload
699b9291
CD
880(defun org-publish (project &optional force)
881 "Publish PROJECT."
c8d0cf5c
CD
882 (interactive
883 (list
54a0dee5 884 (assoc (org-icompleting-read
c8d0cf5c
CD
885 "Publish project: "
886 org-publish-project-alist nil t)
887 org-publish-project-alist)
888 current-prefix-arg))
20908596 889 (setq org-publish-initial-buffer (current-buffer))
9542795a 890 (save-window-excursion
c8d0cf5c 891 (let* ((org-publish-use-timestamps-flag
699b9291 892 (if force nil org-publish-use-timestamps-flag)))
86fbb8ca
CD
893 (org-publish-projects
894 (if (stringp project)
895 ;; If this function is called in batch mode,
896 ;; project is still a string here.
897 (list (assoc project org-publish-project-alist))
898 (list project))))))
9542795a
CD
899
900;;;###autoload
699b9291
CD
901(defun org-publish-all (&optional force)
902 "Publish all projects.
c8d0cf5c
CD
903With prefix argument, remove all files in the timestamp
904directory and force publishing all files."
9542795a 905 (interactive "P")
c8d0cf5c
CD
906 (when force
907 (org-publish-remove-all-timestamps))
9542795a 908 (save-window-excursion
699b9291
CD
909 (let ((org-publish-use-timestamps-flag
910 (if force nil org-publish-use-timestamps-flag)))
911 (org-publish-projects org-publish-project-alist))))
9542795a 912
ed21c5c8 913
9542795a
CD
914;;;###autoload
915(defun org-publish-current-file (&optional force)
916 "Publish the current file.
917With prefix argument, force publish the file."
918 (interactive "P")
919 (save-window-excursion
920 (let ((org-publish-use-timestamps-flag
699b9291 921 (if force nil org-publish-use-timestamps-flag)))
9542795a
CD
922 (org-publish-file (buffer-file-name)))))
923
9542795a 924;;;###autoload
699b9291
CD
925(defun org-publish-current-project (&optional force)
926 "Publish the project associated with the current file.
927With a prefix argument, force publishing of all files in
928the project."
9542795a
CD
929 (interactive "P")
930 (save-window-excursion
c8d0cf5c 931 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up))
699b9291
CD
932 (org-publish-use-timestamps-flag
933 (if force nil org-publish-use-timestamps-flag)))
934 (if (not project)
935 (error "File %s is not part of any known project" (buffer-file-name)))
86fbb8ca 936 ;; FIXME: force is not used here?
699b9291 937 (org-publish project))))
d5098885 938
ed21c5c8
CD
939
940;;; Index generation
941
ed21c5c8
CD
942(defun org-publish-aux-preprocess ()
943 "Find index entries and write them to an .orgx file."
944 (let ((case-fold-search t)
945 entry index target)
946 (goto-char (point-min))
947 (while
948 (and
949 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t)
950 (> (match-end 1) (match-beginning 1)))
951 (setq entry (match-string 1))
3ab2c837 952 (when (eq org-export-current-backend 'latex)
ed21c5c8
CD
953 (replace-match (format "\\index{%s}" entry) t t))
954 (save-excursion
86fbb8ca 955 (ignore-errors (org-back-to-heading t))
ed21c5c8
CD
956 (setq target (get-text-property (point) 'target))
957 (setq target (or (cdr (assoc target org-export-preferred-target-alist))
958 (cdr (assoc target org-export-id-target-alist))
86fbb8ca 959 target ""))
ed21c5c8
CD
960 (push (cons entry target) index)))
961 (with-temp-file
3ab2c837
BG
962 (concat
963 (file-name-directory org-current-export-file) "."
964 (file-name-sans-extension
965 (file-name-nondirectory org-current-export-file)) ".orgx")
ed21c5c8
CD
966 (dolist (entry (nreverse index))
967 (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
968
3ab2c837
BG
969(defun org-publish-index-generate-theindex (directory)
970 "Generate the index from all .orgx files in DIRECTORY."
ed21c5c8
CD
971 (require 'find-lisp)
972 (let* ((fulldir (file-name-as-directory
973 (expand-file-name directory)))
974 (full-files (find-lisp-find-files directory "\\.orgx\\'"))
975 (re (concat "\\`" fulldir))
976 (files (mapcar (lambda (f) (if (string-match re f)
977 (substring f (match-end 0))
978 f))
979 full-files))
980 (default-directory directory)
981 index origfile buf target entry ibuffer
86fbb8ca 982 main last-main letter last-letter file sub link tgext)
ed21c5c8
CD
983 ;; `files' contains the list of relative file names
984 (dolist (file files)
3ab2c837 985 (setq origfile (substring file 1 -1))
ed21c5c8
CD
986 (setq buf (find-file-noselect file))
987 (with-current-buffer buf
988 (goto-char (point-min))
989 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
990 (setq target (match-string 1)
991 entry (match-string 2))
992 (push (list entry origfile target) index)))
993 (kill-buffer buf))
994 (setq index (sort index (lambda (a b) (string< (downcase (car a))
995 (downcase (car b))))))
3ab2c837 996 (setq ibuffer (find-file-noselect (expand-file-name "theindex.org" directory)))
ed21c5c8
CD
997 (with-current-buffer ibuffer
998 (erase-buffer)
999 (insert "* Index\n")
1000 (setq last-letter nil)
1001 (dolist (idx index)
1002 (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
86fbb8ca
CD
1003 (if (and (stringp target) (string-match "\\S-" target))
1004 (setq tgext (concat "::#" target))
1005 (setq tgext ""))
ed21c5c8
CD
1006 (setq letter (upcase (substring entry 0 1)))
1007 (when (not (equal letter last-letter))
1008 (insert "** " letter "\n")
1009 (setq last-letter letter))
1010 (if (string-match "!" entry)
1011 (setq main (substring entry 0 (match-beginning 0))
1012 sub (substring entry (match-end 0)))
1013 (setq main nil sub nil last-main nil))
1014 (when (and main (not (equal main last-main)))
1015 (insert " - " main "\n")
1016 (setq last-main main))
86fbb8ca 1017 (setq link (concat "[[file:" file tgext "]"
ed21c5c8
CD
1018 "[" (or sub entry) "]]"))
1019 (if (and main sub)
1020 (insert " - " link "\n")
1021 (insert " - " link "\n")))
1022 (save-buffer))
3ab2c837 1023 (kill-buffer ibuffer)))
4fbd4750 1024
86fbb8ca
CD
1025;; Caching functions:
1026
1027(defun org-publish-write-cache-file (&optional free-cache)
1028 "Write `org-publish-cache' to file.
1029If FREE-CACHE, empty the cache."
1030 (unless org-publish-cache
1031 (error "%s" "`org-publish-write-cache-file' called, but no cache present"))
1032
1033 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1034 (unless cache-file
1035 (error
1036 "%s" "Cannot find cache-file name in `org-publish-write-cache-file'"))
1037 (with-temp-file cache-file
1038 (let ((print-level nil)
1039 (print-length nil))
1040 (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
1041 (maphash (lambda (k v)
1042 (insert
1043 (format (concat "(puthash %S "
1044 (if (or (listp v) (symbolp v))
1045 "'" "")
1046 "%S org-publish-cache)\n") k v)))
1047 org-publish-cache)))
1048 (when free-cache (org-publish-reset-cache))))
1049
1050(defun org-publish-initialize-cache (project-name)
1051 "Initialize the projects cache if not initialized yet and return it."
1052
1053 (unless project-name
1054 (error "%s%s" "Cannot initialize `org-publish-cache' without projects name"
1055 " in `org-publish-initialize-cache'"))
1056
1057 (unless (file-exists-p org-publish-timestamp-directory)
1058 (make-directory org-publish-timestamp-directory t))
1059 (if (not (file-directory-p org-publish-timestamp-directory))
1060 (error "Org publish timestamp: %s is not a directory"
1061 org-publish-timestamp-directory))
1062
1063 (unless (and org-publish-cache
1064 (string= (org-publish-cache-get ":project:") project-name))
1065 (let* ((cache-file (concat
1066 (expand-file-name org-publish-timestamp-directory)
1067 project-name
1068 ".cache"))
1069 (cexists (file-exists-p cache-file)))
1070
1071 (when org-publish-cache
1072 (org-publish-reset-cache))
1073
1074 (if cexists
1075 (load-file cache-file)
1076 (setq org-publish-cache
1077 (make-hash-table :test 'equal :weakness nil :size 100))
1078 (org-publish-cache-set ":project:" project-name)
1079 (org-publish-cache-set ":cache-file:" cache-file))
1080 (unless cexists (org-publish-write-cache-file nil))))
1081 org-publish-cache)
1082
1083(defun org-publish-reset-cache ()
1084 "Empty org-publish-cache and reset it nil."
1085 (message "%s" "Resetting org-publish-cache")
1086 (if (hash-table-p org-publish-cache)
1087 (clrhash org-publish-cache))
1088 (setq org-publish-cache nil))
1089
1090(defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func)
1091 "Check the timestamp of the last publishing of FILENAME.
3ab2c837
BG
1092Return `t', if the file needs publishing. The function also
1093checks if any included files have been more recently published,
1094so that the file including them will be republished as well."
86fbb8ca
CD
1095 (unless org-publish-cache
1096 (error "%s" "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1097 (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func))
3ab2c837
BG
1098 (pstamp (org-publish-cache-get key))
1099 (visiting (find-buffer-visiting filename))
1100 included-files-ctime buf)
1101
1102 (when (equal (file-name-extension filename) "org")
1103 (setq buf (find-file (expand-file-name filename)))
1104 (with-current-buffer buf
1105 (goto-char (point-min))
1106 (while (re-search-forward "^#\\+INCLUDE:[ \t]+\"?\\([^ \t\"]*\\)\"?[ \t]*.*$" nil t)
1107 (let* ((included-file (expand-file-name (match-string 1))))
1108 (add-to-list 'included-files-ctime
1109 (org-publish-cache-ctime-of-src included-file) t))))
1110 ;; FIXME don't kill current buffer
1111 (unless visiting (kill-buffer buf)))
86fbb8ca
CD
1112 (if (null pstamp)
1113 t
1114 (let ((ctime (org-publish-cache-ctime-of-src filename)))
3ab2c837
BG
1115 (or (< pstamp ctime)
1116 (when included-files-ctime
91af3942 1117 (not (null (delq nil (mapcar (lambda(ct) (< ctime ct))
3ab2c837 1118 included-files-ctime))))))))))
86fbb8ca
CD
1119
1120(defun org-publish-cache-set-file-property (filename property value &optional project-name)
1121 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1122Use cache file of PROJECT-NAME. If the entry does not exist, it will be
1123created. Return VALUE."
1124 ;; Evtl. load the requested cache file:
1125 (if project-name (org-publish-initialize-cache project-name))
1126 (let ((pl (org-publish-cache-get filename)))
1127 (if pl
1128 (progn
1129 (plist-put pl property value)
1130 value)
1131 (org-publish-cache-get-file-property
1132 filename property value nil project-name))))
1133
1134(defun org-publish-cache-get-file-property
1135 (filename property &optional default no-create project-name)
1136 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1137Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
1138DEFAULT, if the value does not yet exist.
1139If the entry will be created, unless NO-CREATE is not nil."
1140 ;; Evtl. load the requested cache file:
1141 (if project-name (org-publish-initialize-cache project-name))
1142 (let ((pl (org-publish-cache-get filename))
1143 (retval nil))
1144 (if pl
1145 (if (plist-member pl property)
1146 (setq retval (plist-get pl property))
1147 (setq retval default))
1148 ;; no pl yet:
1149 (unless no-create
1150 (org-publish-cache-set filename (list property default)))
1151 (setq retval default))
1152 retval))
1153
1154(defun org-publish-cache-get (key)
1155 "Return the value stored in `org-publish-cache' for key KEY.
1156Returns nil, if no value or nil is found, or the cache does not
1157exist."
1158 (unless org-publish-cache
1159 (error "%s" "`org-publish-cache-get' called, but no cache present"))
1160 (gethash key org-publish-cache))
1161
1162(defun org-publish-cache-set (key value)
1163 "Store KEY VALUE pair in `org-publish-cache'.
1164Returns value on success, else nil."
1165 (unless org-publish-cache
1166 (error "%s" "`org-publish-cache-set' called, but no cache present"))
1167 (puthash key value org-publish-cache))
1168
1169(defun org-publish-cache-ctime-of-src (filename)
3ab2c837
BG
1170 "Get the FILENAME ctime as an integer."
1171 (let* ((symlink-maybe (or (file-symlink-p filename) filename))
1172 (src-attr (file-attributes (if (file-name-absolute-p symlink-maybe)
1173 symlink-maybe
1174 (expand-file-name
1175 symlink-maybe
1176 (file-name-directory filename))))))
86fbb8ca
CD
1177 (+
1178 (lsh (car (nth 5 src-attr)) 16)
1179 (cadr (nth 5 src-attr)))))
1180
86fbb8ca 1181(provide 'org-publish)
699b9291 1182
5b409b39 1183
b349f79f 1184
9542795a 1185;;; org-publish.el ends here