Fix failing test
[org-mode.git] / lisp / ox-publish.el
blobce51a9cb0b7e5dbb8f0587adf2891b7e6aaa823d
1 ;;; ox-publish.el --- Publish Related Org Mode Files as a Website -*- lexical-binding: t; -*-
2 ;; Copyright (C) 2006-2016 Free Software Foundation, Inc.
4 ;; Author: David O'Toole <dto@gnu.org>
5 ;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
6 ;; Keywords: hypermedia, outlines, wp
8 ;; This file is part of GNU Emacs.
9 ;;
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This program allow configurable publishing of related sets of
26 ;; Org mode files as a complete website.
28 ;; ox-publish.el can do the following:
30 ;; + Publish all one's Org files to a given export back-end
31 ;; + Upload HTML, images, attachments and other files to a web server
32 ;; + Exclude selected private pages from publishing
33 ;; + Publish a clickable sitemap of pages
34 ;; + Manage local timestamps for publishing only changed files
35 ;; + Accept plugin functions to extend range of publishable content
37 ;; Documentation for publishing is in the manual.
39 ;;; Code:
41 (require 'cl-lib)
42 (require 'format-spec)
43 (require 'ox)
47 ;;; Variables
49 (defvar org-publish-temp-files nil
50 "Temporary list of files to be published.")
52 ;; Here, so you find the variable right before it's used the first time:
53 (defvar org-publish-cache nil
54 "This will cache timestamps and titles for files in publishing projects.
55 Blocks could hash sha1 values here.")
57 (defvar org-publish-after-publishing-hook nil
58 "Hook run each time a file is published.
59 Every function in this hook will be called with two arguments:
60 the name of the original file and the name of the file
61 produced.")
63 (defgroup org-publish nil
64 "Options for publishing a set of Org-mode and related files."
65 :tag "Org Publishing"
66 :group 'org)
68 (defcustom org-publish-project-alist nil
69 "Association list to control publishing behavior.
70 Each element of the alist is a publishing project. The CAR of
71 each element is a string, uniquely identifying the project. The
72 CDR of each element is in one of the following forms:
74 1. A well-formed property list with an even number of elements,
75 alternating keys and values, specifying parameters for the
76 publishing process.
78 (:property value :property value ... )
80 2. A meta-project definition, specifying of a list of
81 sub-projects:
83 (:components (\"project-1\" \"project-2\" ...))
85 When the CDR of an element of org-publish-project-alist is in
86 this second form, the elements of the list after `:components'
87 are taken to be components of the project, which group together
88 files requiring different publishing options. When you publish
89 such a project with \\[org-publish], the components all publish.
91 When a property is given a value in `org-publish-project-alist',
92 its setting overrides the value of the corresponding user
93 variable (if any) during publishing. However, options set within
94 a file override everything.
96 Most properties are optional, but some should always be set:
98 `:base-directory'
100 Directory containing publishing source files.
102 `:base-extension'
104 Extension (without the dot!) of source files. This can be
105 a regular expression. If not given, \"org\" will be used as
106 default extension.
108 `:publishing-directory'
110 Directory (possibly remote) where output files will be
111 published.
113 The `:exclude' property may be used to prevent certain files from
114 being published. Its value may be a string or regexp matching
115 file names you don't want to be published.
117 The `:include' property may be used to include extra files. Its
118 value may be a list of filenames to include. The filenames are
119 considered relative to the base directory.
121 When both `:include' and `:exclude' properties are given values,
122 the exclusion step happens first.
124 One special property controls which back-end function to use for
125 publishing files in the project. This can be used to extend the
126 set of file types publishable by `org-publish', as well as the
127 set of output formats.
129 `:publishing-function'
131 Function to publish file. Each back-end may define its
132 own (i.e. `org-latex-publish-to-pdf',
133 `org-html-publish-to-html'). May be a list of functions, in
134 which case each function in the list is invoked in turn.
136 Another property allows you to insert code that prepares
137 a project for publishing. For example, you could call GNU Make
138 on a certain makefile, to ensure published files are built up to
139 date.
141 `:preparation-function'
143 Function to be called before publishing this project. This
144 may also be a list of functions.
146 `:completion-function'
148 Function to be called after publishing this project. This
149 may also be a list of functions.
151 Some properties control details of the Org publishing process,
152 and are equivalent to the corresponding user variables listed in
153 the right column. Back-end specific properties may also be
154 included. See the back-end documentation for more information.
156 :author `user-full-name'
157 :creator `org-export-creator-string'
158 :email `user-mail-address'
159 :exclude-tags `org-export-exclude-tags'
160 :headline-levels `org-export-headline-levels'
161 :language `org-export-default-language'
162 :preserve-breaks `org-export-preserve-breaks'
163 :section-numbers `org-export-with-section-numbers'
164 :select-tags `org-export-select-tags'
165 :time-stamp-file `org-export-time-stamp-file'
166 :with-archived-trees `org-export-with-archived-trees'
167 :with-author `org-export-with-author'
168 :with-creator `org-export-with-creator'
169 :with-date `org-export-with-date'
170 :with-drawers `org-export-with-drawers'
171 :with-email `org-export-with-email'
172 :with-emphasize `org-export-with-emphasize'
173 :with-entities `org-export-with-entities'
174 :with-fixed-width `org-export-with-fixed-width'
175 :with-footnotes `org-export-with-footnotes'
176 :with-inlinetasks `org-export-with-inlinetasks'
177 :with-latex `org-export-with-latex'
178 :with-planning `org-export-with-planning'
179 :with-priority `org-export-with-priority'
180 :with-properties `org-export-with-properties'
181 :with-smart-quotes `org-export-with-smart-quotes'
182 :with-special-strings `org-export-with-special-strings'
183 :with-statistics-cookies' `org-export-with-statistics-cookies'
184 :with-sub-superscript `org-export-with-sub-superscripts'
185 :with-toc `org-export-with-toc'
186 :with-tables `org-export-with-tables'
187 :with-tags `org-export-with-tags'
188 :with-tasks `org-export-with-tasks'
189 :with-timestamps `org-export-with-timestamps'
190 :with-title `org-export-with-title'
191 :with-todo-keywords `org-export-with-todo-keywords'
193 The following properties may be used to control publishing of
194 a site-map of files or summary page for a given project.
196 `:auto-sitemap'
198 Whether to publish a site-map during
199 `org-publish-current-project' or `org-publish-all'.
201 `:sitemap-filename'
203 Filename for output of sitemap. Defaults to \"sitemap.org\".
205 `:sitemap-title'
207 Title of site-map page. Defaults to name of file.
209 `:sitemap-function'
211 Plugin function to use for generation of site-map. Defaults
212 to `org-publish-org-sitemap', which generates a plain list of
213 links to all files in the project.
215 `:sitemap-style'
217 Can be `list' (site-map is just an itemized list of the
218 titles of the files involved) or `tree' (the directory
219 structure of the source files is reflected in the site-map).
220 Defaults to `tree'.
222 `:sitemap-sans-extension'
224 Remove extension from site-map's file-names. Useful to have
225 cool URIs (see http://www.w3.org/Provider/Style/URI).
226 Defaults to nil.
228 If you create a site-map file, adjust the sorting like this:
230 `:sitemap-sort-folders'
232 Where folders should appear in the site-map. Set this to
233 `first' (default) or `last' to display folders first or last,
234 respectively. Any other value will mix files and folders.
236 `:sitemap-sort-files'
238 The site map is normally sorted alphabetically. You can
239 change this behavior setting this to `anti-chronologically',
240 `chronologically', or nil.
242 `:sitemap-ignore-case'
244 Should sorting be case-sensitive? Default nil.
246 The following property control the creation of a concept index.
248 `:makeindex'
250 Create a concept index. The file containing the index has to
251 be called \"theindex.org\". If it doesn't exist in the
252 project, it will be generated. Contents of the index are
253 stored in the file \"theindex.inc\", which can be included in
254 \"theindex.org\".
256 Other properties affecting publication.
258 `:body-only'
260 Set this to t to publish only the body of the documents."
261 :group 'org-export-publish
262 :type 'alist)
264 (defcustom org-publish-use-timestamps-flag t
265 "Non-nil means use timestamp checking to publish only changed files.
266 When nil, do no timestamp checking and always publish all files."
267 :group 'org-export-publish
268 :type 'boolean)
270 (defcustom org-publish-timestamp-directory
271 (convert-standard-filename "~/.org-timestamps/")
272 "Name of directory in which to store publishing timestamps."
273 :group 'org-export-publish
274 :type 'directory)
276 (defcustom org-publish-list-skipped-files t
277 "Non-nil means show message about files *not* published."
278 :group 'org-export-publish
279 :type 'boolean)
281 (defcustom org-publish-sitemap-sort-files 'alphabetically
282 "Method to sort files in site-maps.
283 Possible values are `alphabetically', `chronologically',
284 `anti-chronologically' and nil.
286 If `alphabetically', files will be sorted alphabetically. If
287 `chronologically', files will be sorted with older modification
288 time first. If `anti-chronologically', files will be sorted with
289 newer modification time first. nil won't sort files.
291 You can overwrite this default per project in your
292 `org-publish-project-alist', using `:sitemap-sort-files'."
293 :group 'org-export-publish
294 :type 'symbol)
296 (defcustom org-publish-sitemap-sort-folders 'first
297 "A symbol, denoting if folders are sorted first in sitemaps.
298 Possible values are `first', `last', and nil.
299 If `first', folders will be sorted before files.
300 If `last', folders are sorted to the end after the files.
301 Any other value will not mix files and folders.
303 You can overwrite this default per project in your
304 `org-publish-project-alist', using `:sitemap-sort-folders'."
305 :group 'org-export-publish
306 :type 'symbol)
308 (defcustom org-publish-sitemap-sort-ignore-case nil
309 "Non-nil when site-map sorting should ignore case.
311 You can overwrite this default per project in your
312 `org-publish-project-alist', using `:sitemap-ignore-case'."
313 :group 'org-export-publish
314 :type 'boolean)
316 (defcustom org-publish-sitemap-date-format "%Y-%m-%d"
317 "Format for printing a date in the sitemap.
318 See `format-time-string' for allowed formatters."
319 :group 'org-export-publish
320 :type 'string)
322 (defcustom org-publish-sitemap-file-entry-format "%t"
323 "Format string for site-map file entry.
324 You could use brackets to delimit on what part the link will be.
326 %t is the title.
327 %a is the author.
328 %d is the date formatted using `org-publish-sitemap-date-format'."
329 :group 'org-export-publish
330 :type 'string)
334 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
335 ;;; Timestamp-related functions
337 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
338 "Return path to timestamp file for filename FILENAME."
339 (setq filename (concat filename "::" (or pub-dir "") "::"
340 (format "%s" (or pub-func ""))))
341 (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
343 (defun org-publish-needed-p
344 (filename &optional pub-dir pub-func _true-pub-dir base-dir)
345 "Non-nil if FILENAME should be published in PUB-DIR using PUB-FUNC.
346 TRUE-PUB-DIR is where the file will truly end up. Currently we
347 are not using this - maybe it can eventually be used to check if
348 the file is present at the target location, and how old it is.
349 Right now we cannot do this, because we do not know under what
350 file name the file will be stored - the publishing function can
351 still decide about that independently."
352 (let ((rtn (if (not org-publish-use-timestamps-flag) t
353 (org-publish-cache-file-needs-publishing
354 filename pub-dir pub-func base-dir))))
355 (if rtn (message "Publishing file %s using `%s'" filename pub-func)
356 (when org-publish-list-skipped-files
357 (message "Skipping unmodified file %s" filename)))
358 rtn))
360 (defun org-publish-update-timestamp
361 (filename &optional pub-dir pub-func _base-dir)
362 "Update publishing timestamp for file FILENAME.
363 If there is no timestamp, create one."
364 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func))
365 (stamp (org-publish-cache-ctime-of-src filename)))
366 (org-publish-cache-set key stamp)))
368 (defun org-publish-remove-all-timestamps ()
369 "Remove all files in the timestamp directory."
370 (let ((dir org-publish-timestamp-directory))
371 (when (and (file-exists-p dir) (file-directory-p dir))
372 (mapc #'delete-file (directory-files dir 'full "[^.]\\'"))
373 (org-publish-reset-cache))))
377 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
378 ;;; Getting project information out of `org-publish-project-alist'
380 (defun org-publish-expand-projects (projects-alist)
381 "Expand projects in PROJECTS-ALIST.
382 This splices all the components into the list."
383 (let ((rest projects-alist) rtn p components)
384 (while (setq p (pop rest))
385 (if (setq components (plist-get (cdr p) :components))
386 (setq rest (append
387 (mapcar (lambda (x) (assoc x org-publish-project-alist))
388 components)
389 rest))
390 (push p rtn)))
391 (nreverse (delete-dups (delq nil rtn)))))
393 (defvar org-publish-sitemap-sort-files)
394 (defvar org-publish-sitemap-sort-folders)
395 (defvar org-publish-sitemap-ignore-case)
396 (defvar org-publish-sitemap-requested)
397 (defvar org-publish-sitemap-date-format)
398 (defvar org-publish-sitemap-file-entry-format)
399 (defun org-publish-compare-directory-files (a b)
400 "Predicate for `sort', that sorts folders and files for sitemap."
401 (let ((retval t))
402 (when (or org-publish-sitemap-sort-files org-publish-sitemap-sort-folders)
403 ;; First we sort files:
404 (when org-publish-sitemap-sort-files
405 (pcase org-publish-sitemap-sort-files
406 (`alphabetically
407 (let* ((adir (file-directory-p a))
408 (aorg (and (string-match "\\.org$" a) (not adir)))
409 (bdir (file-directory-p b))
410 (borg (and (string-match "\\.org$" b) (not bdir)))
411 (A (if aorg (concat (file-name-directory a)
412 (org-publish-find-title a)) a))
413 (B (if borg (concat (file-name-directory b)
414 (org-publish-find-title b)) b)))
415 (setq retval (if org-publish-sitemap-ignore-case
416 (not (string-lessp (upcase B) (upcase A)))
417 (not (string-lessp B A))))))
418 ((or `anti-chronologically `chronologically)
419 (let* ((adate (org-publish-find-date a))
420 (bdate (org-publish-find-date b))
421 (A (+ (lsh (car adate) 16) (cadr adate)))
422 (B (+ (lsh (car bdate) 16) (cadr bdate))))
423 (setq retval
424 (if (eq org-publish-sitemap-sort-files 'chronologically)
425 (<= A B)
426 (>= A B)))))))
427 ;; Directory-wise wins:
428 (when org-publish-sitemap-sort-folders
429 ;; a is directory, b not:
430 (cond
431 ((and (file-directory-p a) (not (file-directory-p b)))
432 (setq retval (equal org-publish-sitemap-sort-folders 'first)))
433 ;; a is not a directory, but b is:
434 ((and (not (file-directory-p a)) (file-directory-p b))
435 (setq retval (equal org-publish-sitemap-sort-folders 'last))))))
436 retval))
438 (defun org-publish-get-base-files-1
439 (base-dir &optional recurse match skip-file skip-dir)
440 "Set `org-publish-temp-files' with files from BASE-DIR directory.
441 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
442 non-nil, restrict this list to the files matching the regexp
443 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
444 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
445 matching the regexp SKIP-DIR when recursing through BASE-DIR."
446 (let ((all-files (if (not recurse) (directory-files base-dir t match)
447 ;; If RECURSE is non-nil, we want all files
448 ;; matching MATCH and sub-directories.
449 (cl-remove-if-not
450 (lambda (file)
451 (or (file-directory-p file)
452 (and match (string-match match file))))
453 (directory-files base-dir t)))))
454 (dolist (f (if (not org-publish-sitemap-requested) all-files
455 (sort all-files #'org-publish-compare-directory-files)))
456 (let ((fd-p (file-directory-p f))
457 (fnd (file-name-nondirectory f)))
458 (if (and fd-p recurse
459 (not (string-match "^\\.+$" fnd))
460 (if skip-dir (not (string-match skip-dir fnd)) t))
461 (org-publish-get-base-files-1
462 f recurse match skip-file skip-dir)
463 (unless (or fd-p ; This is a directory.
464 (and skip-file (string-match skip-file fnd))
465 (not (file-exists-p (file-truename f)))
466 (not (string-match match fnd)))
467 (cl-pushnew f org-publish-temp-files)))))))
469 (defun org-publish-get-base-files (project &optional exclude-regexp)
470 "Return a list of all files in PROJECT.
471 If EXCLUDE-REGEXP is set, this will be used to filter out
472 matching filenames."
473 (let* ((project-plist (cdr project))
474 (base-dir (file-name-as-directory
475 (plist-get project-plist :base-directory)))
476 (include-list (plist-get project-plist :include))
477 (recurse (plist-get project-plist :recursive))
478 (extension (or (plist-get project-plist :base-extension) "org"))
479 ;; sitemap-... variables are dynamically scoped for
480 ;; org-publish-compare-directory-files:
481 (org-publish-sitemap-requested
482 (plist-get project-plist :auto-sitemap))
483 (sitemap-filename
484 (or (plist-get project-plist :sitemap-filename) "sitemap.org"))
485 (org-publish-sitemap-sort-folders
486 (if (plist-member project-plist :sitemap-sort-folders)
487 (plist-get project-plist :sitemap-sort-folders)
488 org-publish-sitemap-sort-folders))
489 (org-publish-sitemap-sort-files
490 (cond ((plist-member project-plist :sitemap-sort-files)
491 (plist-get project-plist :sitemap-sort-files))
492 ;; For backward compatibility:
493 ((plist-member project-plist :sitemap-alphabetically)
494 (if (plist-get project-plist :sitemap-alphabetically)
495 'alphabetically nil))
496 (t org-publish-sitemap-sort-files)))
497 (org-publish-sitemap-ignore-case
498 (if (plist-member project-plist :sitemap-ignore-case)
499 (plist-get project-plist :sitemap-ignore-case)
500 org-publish-sitemap-sort-ignore-case))
501 (match (if (eq extension 'any) "^[^\\.]"
502 (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
503 ;; Make sure `org-publish-sitemap-sort-folders' has an accepted
504 ;; value.
505 (unless (memq org-publish-sitemap-sort-folders '(first last))
506 (setq org-publish-sitemap-sort-folders nil))
508 (setq org-publish-temp-files nil)
509 (when org-publish-sitemap-requested
510 (cl-pushnew (expand-file-name (concat base-dir sitemap-filename))
511 org-publish-temp-files))
512 (org-publish-get-base-files-1 base-dir recurse match
513 ;; FIXME distinguish exclude regexp
514 ;; for skip-file and skip-dir?
515 exclude-regexp exclude-regexp)
516 (dolist (f include-list org-publish-temp-files)
517 (cl-pushnew (expand-file-name (concat base-dir f))
518 org-publish-temp-files))))
520 (defun org-publish-get-project-from-filename (filename &optional up)
521 "Return the project that FILENAME belongs to."
522 (let* ((filename (expand-file-name filename))
523 project-name)
525 (catch 'p-found
526 (dolist (prj org-publish-project-alist)
527 (unless (plist-get (cdr prj) :components)
528 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
529 (let* ((r (plist-get (cdr prj) :recursive))
530 (b (expand-file-name (file-name-as-directory
531 (plist-get (cdr prj) :base-directory))))
532 (x (or (plist-get (cdr prj) :base-extension) "org"))
533 (e (plist-get (cdr prj) :exclude))
534 (i (plist-get (cdr prj) :include))
535 (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
536 (when
537 (or (and i
538 (member filename
539 (dolist (file i) (expand-file-name file b))))
540 (and (not (and e (string-match e filename)))
541 (string-match xm filename)))
542 (setq project-name (car prj))
543 (throw 'p-found project-name))))))
544 (when up
545 (dolist (prj org-publish-project-alist)
546 (if (member project-name (plist-get (cdr prj) :components))
547 (setq project-name (car prj)))))
548 (assoc project-name org-publish-project-alist)))
552 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
553 ;;; Tools for publishing functions in back-ends
555 (defun org-publish-org-to (backend filename extension plist &optional pub-dir)
556 "Publish an Org file to a specified back-end.
558 BACKEND is a symbol representing the back-end used for
559 transcoding. FILENAME is the filename of the Org file to be
560 published. EXTENSION is the extension used for the output
561 string, with the leading dot. PLIST is the property list for the
562 given project.
564 Optional argument PUB-DIR, when non-nil is the publishing
565 directory.
567 Return output file name."
568 (unless (or (not pub-dir) (file-exists-p pub-dir)) (make-directory pub-dir t))
569 ;; Check if a buffer visiting FILENAME is already open.
570 (let* ((org-inhibit-startup t)
571 (visiting (find-buffer-visiting filename))
572 (work-buffer (or visiting (find-file-noselect filename))))
573 (unwind-protect
574 (with-current-buffer work-buffer
575 (let ((output (org-export-output-file-name extension nil pub-dir)))
576 (org-export-to-file backend output
577 nil nil nil (plist-get plist :body-only)
578 ;; Add `org-publish--store-crossrefs' and
579 ;; `org-publish-collect-index' to final output filters.
580 ;; The latter isn't dependent on `:makeindex', since we
581 ;; want to keep it up-to-date in cache anyway.
582 (org-combine-plists
583 plist
584 `(:crossrefs
585 ,(org-publish-cache-get-file-property
586 (expand-file-name filename) :crossrefs nil t)
587 :filter-final-output
588 (org-publish--store-crossrefs
589 org-publish-collect-index
590 ,@(plist-get plist :filter-final-output)))))))
591 ;; Remove opened buffer in the process.
592 (unless visiting (kill-buffer work-buffer)))))
594 (defun org-publish-attachment (_plist filename pub-dir)
595 "Publish a file with no transformation of any kind.
597 FILENAME is the filename of the Org file to be published. PLIST
598 is the property list for the given project. PUB-DIR is the
599 publishing directory.
601 Return output file name."
602 (unless (file-directory-p pub-dir)
603 (make-directory pub-dir t))
604 (let ((output (expand-file-name (file-name-nondirectory filename) pub-dir)))
605 (or (equal (expand-file-name (file-name-directory filename))
606 (file-name-as-directory (expand-file-name pub-dir)))
607 (copy-file filename output t))
608 ;; Return file name.
609 output))
613 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
614 ;;; Publishing files, sets of files, and indices
616 (defun org-publish-file (filename &optional project no-cache)
617 "Publish file FILENAME from PROJECT.
618 If NO-CACHE is not nil, do not initialize `org-publish-cache'.
619 This is needed, since this function is used to publish single
620 files, when entire projects are published (see
621 `org-publish-projects')."
622 (let* ((project
623 (or project
624 (or (org-publish-get-project-from-filename filename)
625 (error "File %s not part of any known project"
626 (abbreviate-file-name filename)))))
627 (project-plist (cdr project))
628 (ftname (expand-file-name filename))
629 (publishing-function
630 (let ((fun (plist-get project-plist :publishing-function)))
631 (cond ((null fun) (error "No publishing function chosen"))
632 ((listp fun) fun)
633 (t (list fun)))))
634 (base-dir
635 (file-name-as-directory
636 (expand-file-name
637 (or (plist-get project-plist :base-directory)
638 (error "Project %s does not have :base-directory defined"
639 (car project))))))
640 (pub-dir
641 (file-name-as-directory
642 (file-truename
643 (or (eval (plist-get project-plist :publishing-directory))
644 (error "Project %s does not have :publishing-directory defined"
645 (car project))))))
646 tmp-pub-dir)
648 (unless no-cache (org-publish-initialize-cache (car project)))
650 (setq tmp-pub-dir
651 (file-name-directory
652 (concat pub-dir
653 (and (string-match (regexp-quote base-dir) ftname)
654 (substring ftname (match-end 0))))))
655 ;; Allow chain of publishing functions.
656 (dolist (f publishing-function)
657 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir base-dir)
658 (let ((output (funcall f project-plist filename tmp-pub-dir)))
659 (org-publish-update-timestamp filename pub-dir f base-dir)
660 (run-hook-with-args 'org-publish-after-publishing-hook
661 filename
662 output))))
663 ;; Make sure to write cache to file after successfully publishing
664 ;; a file, so as to minimize impact of a publishing failure.
665 (org-publish-write-cache-file)))
667 (defun org-publish-projects (projects)
668 "Publish all files belonging to the PROJECTS alist.
669 If `:auto-sitemap' is set, publish the sitemap too. If
670 `:makeindex' is set, also produce a file \"theindex.org\"."
671 (dolist (project (org-publish-expand-projects projects))
672 (let ((project-plist (cdr project)))
673 (let ((f (plist-get project-plist :preparation-function)))
674 (cond ((consp f) (mapc #'funcall f))
675 ((functionp f) (funcall f))))
676 ;; Each project uses its own cache file.
677 (org-publish-initialize-cache (car project))
678 (when (plist-get project-plist :auto-sitemap)
679 (let ((sitemap-filename
680 (or (plist-get project-plist :sitemap-filename)
681 "sitemap.org"))
682 (sitemap-function
683 (or (plist-get project-plist :sitemap-function)
684 #'org-publish-org-sitemap))
685 (org-publish-sitemap-date-format
686 (or (plist-get project-plist :sitemap-date-format)
687 org-publish-sitemap-date-format))
688 (org-publish-sitemap-file-entry-format
689 (or (plist-get project-plist :sitemap-file-entry-format)
690 org-publish-sitemap-file-entry-format)))
691 (funcall sitemap-function project sitemap-filename)))
692 ;; Publish all files from PROJECT excepted "theindex.org". Its
693 ;; publishing will be deferred until "theindex.inc" is
694 ;; populated.
695 (let ((theindex
696 (expand-file-name "theindex.org"
697 (plist-get project-plist :base-directory)))
698 (exclude-regexp (plist-get project-plist :exclude)))
699 (dolist (file (org-publish-get-base-files project exclude-regexp))
700 (unless (equal file theindex) (org-publish-file file project t)))
701 ;; Populate "theindex.inc", if needed, and publish
702 ;; "theindex.org".
703 (when (plist-get project-plist :makeindex)
704 (org-publish-index-generate-theindex
705 project (plist-get project-plist :base-directory))
706 (org-publish-file theindex project t)))
707 (let ((f (plist-get project-plist :completion-function)))
708 (cond ((consp f) (mapc #'funcall f))
709 ((functionp f) (funcall f))))
710 (org-publish-write-cache-file))))
712 (defun org-publish-org-sitemap (project &optional sitemap-filename)
713 "Create a sitemap of pages in set defined by PROJECT.
714 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
715 Default for SITEMAP-FILENAME is `sitemap.org'."
716 (let* ((project-plist (cdr project))
717 (dir (file-name-as-directory
718 (plist-get project-plist :base-directory)))
719 (localdir (file-name-directory dir))
720 (indent-str (make-string 2 ?\ ))
721 (exclude-regexp (plist-get project-plist :exclude))
722 (files (nreverse
723 (org-publish-get-base-files project exclude-regexp)))
724 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
725 (sitemap-title (or (plist-get project-plist :sitemap-title)
726 (concat "Sitemap for project " (car project))))
727 (sitemap-style (or (plist-get project-plist :sitemap-style)
728 'tree))
729 (sitemap-sans-extension
730 (plist-get project-plist :sitemap-sans-extension))
731 (visiting (find-buffer-visiting sitemap-filename))
732 file sitemap-buffer)
733 (with-current-buffer
734 (let ((org-inhibit-startup t))
735 (setq sitemap-buffer
736 (or visiting (find-file sitemap-filename))))
737 (erase-buffer)
738 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
739 (while (setq file (pop files))
740 (let ((link (file-relative-name file dir))
741 (oldlocal localdir))
742 (when sitemap-sans-extension
743 (setq link (file-name-sans-extension link)))
744 ;; sitemap shouldn't list itself
745 (unless (equal (file-truename sitemap-filename)
746 (file-truename file))
747 (if (eq sitemap-style 'list)
748 (message "Generating list-style sitemap for %s" sitemap-title)
749 (message "Generating tree-style sitemap for %s" sitemap-title)
750 (setq localdir (concat (file-name-as-directory dir)
751 (file-name-directory link)))
752 (unless (string= localdir oldlocal)
753 (if (string= localdir dir)
754 (setq indent-str (make-string 2 ?\ ))
755 (let ((subdirs
756 (split-string
757 (directory-file-name
758 (file-name-directory
759 (file-relative-name localdir dir))) "/"))
760 (subdir "")
761 (old-subdirs (split-string
762 (file-relative-name oldlocal dir) "/")))
763 (setq indent-str (make-string 2 ?\ ))
764 (while (string= (car old-subdirs) (car subdirs))
765 (setq indent-str (concat indent-str (make-string 2 ?\ )))
766 (pop old-subdirs)
767 (pop subdirs))
768 (dolist (d subdirs)
769 (setq subdir (concat subdir d "/"))
770 (insert (concat indent-str " + " d "\n"))
771 (setq indent-str (make-string
772 (+ (length indent-str) 2) ?\ )))))))
773 ;; This is common to 'flat and 'tree
774 (let ((entry
775 (org-publish-format-file-entry
776 org-publish-sitemap-file-entry-format file project-plist))
777 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
778 (cond ((string-match-p regexp entry)
779 (string-match regexp entry)
780 (insert (concat indent-str " + " (match-string 1 entry)
781 "[[file:" link "]["
782 (match-string 2 entry)
783 "]]" (match-string 3 entry) "\n")))
785 (insert (concat indent-str " + [[file:" link "]["
786 entry
787 "]]\n"))))))))
788 (save-buffer))
789 (or visiting (kill-buffer sitemap-buffer))))
791 (defun org-publish-format-file-entry (fmt file project-plist)
792 (format-spec
794 `((?t . ,(org-publish-find-title file t))
795 (?d . ,(format-time-string org-publish-sitemap-date-format
796 (org-publish-find-date file)))
797 (?a . ,(or (plist-get project-plist :author) user-full-name)))))
799 (defun org-publish-find-title (file &optional reset)
800 "Find the title of FILE in project."
802 (and (not reset) (org-publish-cache-get-file-property file :title nil t))
803 (let* ((org-inhibit-startup t)
804 (visiting (find-buffer-visiting file))
805 (buffer (or visiting (find-file-noselect file))))
806 (with-current-buffer buffer
807 (let ((title
808 (let ((property
809 (plist-get
810 ;; protect local variables in open buffers
811 (if visiting
812 (org-export-with-buffer-copy (org-export-get-environment))
813 (org-export-get-environment))
814 :title)))
815 (if property
816 (org-no-properties (org-element-interpret-data property))
817 (file-name-nondirectory (file-name-sans-extension file))))))
818 (unless visiting (kill-buffer buffer))
819 (org-publish-cache-set-file-property file :title title)
820 title)))))
822 (defun org-publish-find-date (file)
823 "Find the date of FILE in project.
824 This function assumes FILE is either a directory or an Org file.
825 If FILE is an Org file and provides a DATE keyword use it. In
826 any other case use the file system's modification time. Return
827 time in `current-time' format."
828 (if (file-directory-p file) (nth 5 (file-attributes file))
829 (let* ((org-inhibit-startup t)
830 (visiting (find-buffer-visiting file))
831 (file-buf (or visiting (find-file-noselect file nil)))
832 (date (plist-get
833 (with-current-buffer file-buf
834 (if visiting
835 (org-export-with-buffer-copy
836 (org-export-get-environment))
837 (org-export-get-environment)))
838 :date)))
839 (unless visiting (kill-buffer file-buf))
840 ;; DATE is a secondary string. If it contains a timestamp,
841 ;; convert it to internal format. Otherwise, use FILE
842 ;; modification time.
843 (cond ((let ((ts (and (consp date) (assq 'timestamp date))))
844 (and ts
845 (let ((value (org-element-interpret-data ts)))
846 (and (org-string-nw-p value)
847 (org-time-string-to-time value))))))
848 ((file-exists-p file) (nth 5 (file-attributes file)))
849 (t (error "No such file: \"%s\"" file))))))
853 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
854 ;;; Interactive publishing functions
856 ;;;###autoload
857 (defalias 'org-publish-project 'org-publish)
859 ;;;###autoload
860 (defun org-publish (project &optional force async)
861 "Publish PROJECT.
863 PROJECT is either a project name, as a string, or a project
864 alist (see `org-publish-project-alist' variable).
866 When optional argument FORCE is non-nil, force publishing all
867 files in PROJECT. With a non-nil optional argument ASYNC,
868 publishing will be done asynchronously, in another process."
869 (interactive
870 (list (assoc (completing-read "Publish project: "
871 org-publish-project-alist nil t)
872 org-publish-project-alist)
873 current-prefix-arg))
874 (let ((project (if (not (stringp project)) project
875 ;; If this function is called in batch mode,
876 ;; PROJECT is still a string here.
877 (assoc project org-publish-project-alist))))
878 (cond
879 ((not project))
880 (async
881 (org-export-async-start (lambda (_) nil)
882 `(let ((org-publish-use-timestamps-flag
883 ,(and (not force) org-publish-use-timestamps-flag)))
884 ;; Expand components right now as external process may not
885 ;; be aware of complete `org-publish-project-alist'.
886 (org-publish-projects
887 ',(org-publish-expand-projects (list project))))))
888 (t (save-window-excursion
889 (let ((org-publish-use-timestamps-flag
890 (and (not force) org-publish-use-timestamps-flag)))
891 (org-publish-projects (list project))))))))
893 ;;;###autoload
894 (defun org-publish-all (&optional force async)
895 "Publish all projects.
896 With prefix argument FORCE, remove all files in the timestamp
897 directory and force publishing all projects. With a non-nil
898 optional argument ASYNC, publishing will be done asynchronously,
899 in another process."
900 (interactive "P")
901 (if async
902 (org-export-async-start (lambda (_) nil)
903 `(progn
904 (when ',force (org-publish-remove-all-timestamps))
905 (let ((org-publish-use-timestamps-flag
906 (if ',force nil ,org-publish-use-timestamps-flag)))
907 (org-publish-projects ',org-publish-project-alist))))
908 (when force (org-publish-remove-all-timestamps))
909 (save-window-excursion
910 (let ((org-publish-use-timestamps-flag
911 (if force nil org-publish-use-timestamps-flag)))
912 (org-publish-projects org-publish-project-alist)))))
915 ;;;###autoload
916 (defun org-publish-current-file (&optional force async)
917 "Publish the current file.
918 With prefix argument FORCE, force publish the file. When
919 optional argument ASYNC is non-nil, publishing will be done
920 asynchronously, in another process."
921 (interactive "P")
922 (let ((file (buffer-file-name (buffer-base-buffer))))
923 (if async
924 (org-export-async-start (lambda (_) nil)
925 `(let ((org-publish-use-timestamps-flag
926 (if ',force nil ,org-publish-use-timestamps-flag)))
927 (org-publish-file ,file)))
928 (save-window-excursion
929 (let ((org-publish-use-timestamps-flag
930 (if force nil org-publish-use-timestamps-flag)))
931 (org-publish-file file))))))
933 ;;;###autoload
934 (defun org-publish-current-project (&optional force async)
935 "Publish the project associated with the current file.
936 With a prefix argument, force publishing of all files in
937 the project."
938 (interactive "P")
939 (save-window-excursion
940 (let ((project (org-publish-get-project-from-filename
941 (buffer-file-name (buffer-base-buffer)) 'up)))
942 (if project (org-publish project force async)
943 (error "File %s is not part of any known project"
944 (buffer-file-name (buffer-base-buffer)))))))
948 ;;; Index generation
950 (defun org-publish-collect-index (output _backend info)
951 "Update index for a file in cache.
953 OUTPUT is the output from transcoding current file. BACKEND is
954 the back-end that was used for transcoding. INFO is a plist
955 containing publishing and export options.
957 The index relative to current file is stored as an alist. An
958 association has the following shape: (TERM FILE-NAME PARENT),
959 where TERM is the indexed term, as a string, FILE-NAME is the
960 original full path of the file where the term in encountered, and
961 PARENT is a reference to the headline, if any, containing the
962 original index keyword. When non-nil, this reference is a cons
963 cell. Its CAR is a symbol among `id', `custom-id' and `name' and
964 its CDR is a string."
965 (let ((file (plist-get info :input-file)))
966 (org-publish-cache-set-file-property
967 file :index
968 (delete-dups
969 (org-element-map (plist-get info :parse-tree) 'keyword
970 (lambda (k)
971 (when (equal (org-element-property :key k) "INDEX")
972 (let ((parent (org-export-get-parent-headline k)))
973 (list (org-element-property :value k)
974 file
975 (cond
976 ((not parent) nil)
977 ((let ((id (org-element-property :ID parent)))
978 (and id (cons 'id id))))
979 ((let ((id (org-element-property :CUSTOM_ID parent)))
980 (and id (cons 'custom-id id))))
981 (t (cons 'name
982 ;; Remove statistics cookie.
983 (replace-regexp-in-string
984 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
985 (org-element-property :raw-value parent)))))))))
986 info))))
987 ;; Return output unchanged.
988 output)
990 (defun org-publish-index-generate-theindex (project directory)
991 "Retrieve full index from cache and build \"theindex.org\".
992 PROJECT is the project the index relates to. DIRECTORY is the
993 publishing directory."
994 (let ((all-files (org-publish-get-base-files
995 project (plist-get (cdr project) :exclude)))
996 full-index)
997 ;; Compile full index and sort it alphabetically.
998 (dolist (file all-files
999 (setq full-index
1000 (sort (nreverse full-index)
1001 (lambda (a b) (string< (downcase (car a))
1002 (downcase (car b)))))))
1003 (let ((index (org-publish-cache-get-file-property file :index)))
1004 (dolist (term index)
1005 (unless (member term full-index) (push term full-index)))))
1006 ;; Write "theindex.inc" in DIRECTORY.
1007 (with-temp-file (expand-file-name "theindex.inc" directory)
1008 (let ((current-letter nil) (last-entry nil))
1009 (dolist (idx full-index)
1010 (let* ((entry (org-split-string (car idx) "!"))
1011 (letter (upcase (substring (car entry) 0 1)))
1012 ;; Transform file into a path relative to publishing
1013 ;; directory.
1014 (file (file-relative-name
1015 (nth 1 idx)
1016 (plist-get (cdr project) :base-directory))))
1017 ;; Check if another letter has to be inserted.
1018 (unless (string= letter current-letter)
1019 (insert (format "* %s\n" letter)))
1020 ;; Compute the first difference between last entry and
1021 ;; current one: it tells the level at which new items
1022 ;; should be added.
1023 (let* ((rank
1024 (if (equal entry last-entry) (1- (length entry))
1025 (cl-loop for n from 0 to (length entry)
1026 unless (equal (nth n entry) (nth n last-entry))
1027 return n)))
1028 (len (length (nthcdr rank entry))))
1029 ;; For each term after the first difference, create
1030 ;; a new sub-list with the term as body. Moreover,
1031 ;; linkify the last term.
1032 (dotimes (n len)
1033 (insert
1034 (concat
1035 (make-string (* (+ rank n) 2) ? ) " - "
1036 (if (not (= (1- len) n)) (nth (+ rank n) entry)
1037 ;; Last term: Link it to TARGET, if possible.
1038 (let ((target (nth 2 idx)))
1039 (format
1040 "[[%s][%s]]"
1041 ;; Destination.
1042 (pcase (car target)
1043 (`nil (format "file:%s" file))
1044 (`id (format "id:%s" (cdr target)))
1045 (`custom-id (format "file:%s::#%s" file (cdr target)))
1046 (_ (format "file:%s::*%s" file (cdr target))))
1047 ;; Description.
1048 (car (last entry)))))
1049 "\n"))))
1050 (setq current-letter letter last-entry entry))))
1051 ;; Create "theindex.org", if it doesn't exist yet, and provide
1052 ;; a default index file.
1053 (let ((index.org (expand-file-name "theindex.org" directory)))
1054 (unless (file-exists-p index.org)
1055 (with-temp-file index.org
1056 (insert "#+TITLE: Index\n\n#+INCLUDE: \"theindex.inc\"\n\n")))))))
1060 ;;; External Fuzzy Links Resolution
1062 ;; This part implements tools to resolve [[file.org::*Some headline]]
1063 ;; links, where "file.org" belongs to the current project.
1065 (defun org-publish--store-crossrefs (output _backend info)
1066 "Store cross-references for current published file.
1068 OUPUT is the produced output, as a string. BACKEND is the export
1069 back-end used, as a symbol. INFO is the final export state, as
1070 a plist.
1072 This function is meant to be used as a final output filter. See
1073 `org-publish-org-to'."
1074 (org-publish-cache-set-file-property
1075 (plist-get info :input-file) :crossrefs
1076 ;; Update `:crossrefs' so as to remove unused references and search
1077 ;; cells. Actually used references are extracted from
1078 ;; `:internal-references', with references as strings removed. See
1079 ;; `org-export-get-reference' for details.
1080 (cl-remove-if (lambda (pair) (stringp (car pair)))
1081 (plist-get info :internal-references)))
1082 ;; Return output unchanged.
1083 output)
1085 (defun org-publish-resolve-external-link (search file)
1086 "Return reference for element matching string SEARCH in FILE.
1088 Return value is an internal reference, as a string.
1090 This function allows resolving external links with a search
1091 option, e.g.,
1093 [[file.org::*heading][description]]
1094 [[file.org::#custom-id][description]]
1095 [[file.org::fuzzy][description]]
1097 It only makes sense to use this if export back-end builds
1098 references with `org-export-get-reference'."
1099 (if (not org-publish-cache)
1100 (progn
1101 (message "Reference %S in file %S cannot be resolved without publishing"
1102 search
1103 file)
1104 "MissingReference")
1105 (let* ((filename (expand-file-name file))
1106 (crossrefs
1107 (org-publish-cache-get-file-property filename :crossrefs nil t))
1108 (cells (org-export-string-to-search-cell search)))
1110 ;; Look for reference associated to search cells triggered by
1111 ;; LINK. It can match when targeted file has been published
1112 ;; already.
1113 (let ((known (cdr (cl-some (lambda (c) (assoc c crossrefs)) cells))))
1114 (and known (org-export-format-reference known)))
1115 ;; Search cell is unknown so far. Generate a new internal
1116 ;; reference that will be used when the targeted file will be
1117 ;; published.
1118 (let ((new (org-export-new-reference crossrefs)))
1119 (dolist (cell cells) (push (cons cell new) crossrefs))
1120 (org-publish-cache-set-file-property filename :crossrefs crossrefs)
1121 (org-export-format-reference new))))))
1125 ;;; Caching functions
1127 (defun org-publish-write-cache-file (&optional free-cache)
1128 "Write `org-publish-cache' to file.
1129 If FREE-CACHE, empty the cache."
1130 (unless org-publish-cache
1131 (error "`org-publish-write-cache-file' called, but no cache present"))
1133 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1134 (unless cache-file
1135 (error "Cannot find cache-file name in `org-publish-write-cache-file'"))
1136 (with-temp-file cache-file
1137 (let (print-level print-length)
1138 (insert "(setq org-publish-cache \
1139 \(make-hash-table :test 'equal :weakness nil :size 100))\n")
1140 (maphash (lambda (k v)
1141 (insert
1142 (format "(puthash %S %s%S org-publish-cache)\n"
1143 k (if (or (listp v) (symbolp v)) "'" "") v)))
1144 org-publish-cache)))
1145 (when free-cache (org-publish-reset-cache))))
1147 (defun org-publish-initialize-cache (project-name)
1148 "Initialize the projects cache if not initialized yet and return it."
1150 (unless project-name
1151 (error "Cannot initialize `org-publish-cache' without projects name in \
1152 `org-publish-initialize-cache'"))
1154 (unless (file-exists-p org-publish-timestamp-directory)
1155 (make-directory org-publish-timestamp-directory t))
1156 (unless (file-directory-p org-publish-timestamp-directory)
1157 (error "Org publish timestamp: %s is not a directory"
1158 org-publish-timestamp-directory))
1160 (unless (and org-publish-cache
1161 (string= (org-publish-cache-get ":project:") project-name))
1162 (let* ((cache-file
1163 (concat
1164 (expand-file-name org-publish-timestamp-directory)
1165 project-name ".cache"))
1166 (cexists (file-exists-p cache-file)))
1168 (when org-publish-cache (org-publish-reset-cache))
1170 (if cexists (load-file cache-file)
1171 (setq org-publish-cache
1172 (make-hash-table :test 'equal :weakness nil :size 100))
1173 (org-publish-cache-set ":project:" project-name)
1174 (org-publish-cache-set ":cache-file:" cache-file))
1175 (unless cexists (org-publish-write-cache-file nil))))
1176 org-publish-cache)
1178 (defun org-publish-reset-cache ()
1179 "Empty org-publish-cache and reset it nil."
1180 (message "%s" "Resetting org-publish-cache")
1181 (when (hash-table-p org-publish-cache)
1182 (clrhash org-publish-cache))
1183 (setq org-publish-cache nil))
1185 (defun org-publish-cache-file-needs-publishing
1186 (filename &optional pub-dir pub-func _base-dir)
1187 "Check the timestamp of the last publishing of FILENAME.
1188 Return non-nil if the file needs publishing. Also check if
1189 any included files have been more recently published, so that
1190 the file including them will be republished as well."
1191 (unless org-publish-cache
1192 (error
1193 "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1194 (let* ((case-fold-search t)
1195 (key (org-publish-timestamp-filename filename pub-dir pub-func))
1196 (pstamp (org-publish-cache-get key))
1197 (org-inhibit-startup t)
1198 (visiting (find-buffer-visiting filename))
1199 included-files-ctime buf)
1200 (when (equal (file-name-extension filename) "org")
1201 (setq buf (find-file (expand-file-name filename)))
1202 (with-current-buffer buf
1203 (goto-char (point-min))
1204 (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t)
1205 (let* ((element (org-element-at-point))
1206 (included-file
1207 (and (eq (org-element-type element) 'keyword)
1208 (let ((value (org-element-property :value element)))
1209 (and value
1210 (string-match
1211 "\\`\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)"
1212 value)
1213 (let ((m (match-string 1 value)))
1214 (org-remove-double-quotes
1215 ;; Ignore search suffix.
1216 (if (string-match "\\(::\\(.*?\\)\\)\"?\\'" m)
1217 (substring m 0 (match-beginning 0))
1218 m))))))))
1219 (when included-file
1220 (push (org-publish-cache-ctime-of-src
1221 (expand-file-name included-file))
1222 included-files-ctime)))))
1223 (unless visiting (kill-buffer buf)))
1224 (or (null pstamp)
1225 (let ((ctime (org-publish-cache-ctime-of-src filename)))
1226 (or (< pstamp ctime)
1227 (cl-some (lambda (ct) (< ctime ct)) included-files-ctime))))))
1229 (defun org-publish-cache-set-file-property
1230 (filename property value &optional project-name)
1231 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1232 Use cache file of PROJECT-NAME. If the entry does not exist, it
1233 will be created. Return VALUE."
1234 ;; Evtl. load the requested cache file:
1235 (if project-name (org-publish-initialize-cache project-name))
1236 (let ((pl (org-publish-cache-get filename)))
1237 (if pl (progn (plist-put pl property value) value)
1238 (org-publish-cache-get-file-property
1239 filename property value nil project-name))))
1241 (defun org-publish-cache-get-file-property
1242 (filename property &optional default no-create project-name)
1243 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1244 Use cache file of PROJECT-NAME. Return the value of that PROPERTY
1245 or DEFAULT, if the value does not yet exist. If the entry will
1246 be created, unless NO-CREATE is not nil."
1247 ;; Evtl. load the requested cache file:
1248 (if project-name (org-publish-initialize-cache project-name))
1249 (let ((pl (org-publish-cache-get filename)) retval)
1250 (if pl
1251 (if (plist-member pl property)
1252 (setq retval (plist-get pl property))
1253 (setq retval default))
1254 ;; no pl yet:
1255 (unless no-create
1256 (org-publish-cache-set filename (list property default)))
1257 (setq retval default))
1258 retval))
1260 (defun org-publish-cache-get (key)
1261 "Return the value stored in `org-publish-cache' for key KEY.
1262 Returns nil, if no value or nil is found, or the cache does not
1263 exist."
1264 (unless org-publish-cache
1265 (error "`org-publish-cache-get' called, but no cache present"))
1266 (gethash key org-publish-cache))
1268 (defun org-publish-cache-set (key value)
1269 "Store KEY VALUE pair in `org-publish-cache'.
1270 Returns value on success, else nil."
1271 (unless org-publish-cache
1272 (error "`org-publish-cache-set' called, but no cache present"))
1273 (puthash key value org-publish-cache))
1275 (defun org-publish-cache-ctime-of-src (file)
1276 "Get the ctime of FILE as an integer."
1277 (let ((attr (file-attributes
1278 (expand-file-name (or (file-symlink-p file) file)
1279 (file-name-directory file)))))
1280 (if (not attr) (error "No such file: \"%s\"" file)
1281 (+ (lsh (car (nth 5 attr)) 16)
1282 (cadr (nth 5 attr))))))
1285 (provide 'ox-publish)
1287 ;; Local variables:
1288 ;; generated-autoload-file: "org-loaddefs.el"
1289 ;; End:
1291 ;;; ox-publish.el ends here