babel: replace org-babel-*-read-string functions with single version.
[org-mode.git] / lisp / ox-publish.el
blob912cb2558bc6c21b7ddf4ac477aea84be087dad1
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 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 If `:recursive' is non-nil files in sub-directories of
114 `:base-directory' are considered.
116 The `:exclude' property may be used to prevent certain files from
117 being published. Its value may be a string or regexp matching
118 file names you don't want to be published.
120 The `:include' property may be used to include extra files. Its
121 value may be a list of filenames to include. The filenames are
122 considered relative to the base directory.
124 When both `:include' and `:exclude' properties are given values,
125 the exclusion step happens first.
127 One special property controls which back-end function to use for
128 publishing files in the project. This can be used to extend the
129 set of file types publishable by `org-publish', as well as the
130 set of output formats.
132 `:publishing-function'
134 Function to publish file. Each back-end may define its
135 own (i.e. `org-latex-publish-to-pdf',
136 `org-html-publish-to-html'). May be a list of functions, in
137 which case each function in the list is invoked in turn.
139 Another property allows you to insert code that prepares
140 a project for publishing. For example, you could call GNU Make
141 on a certain makefile, to ensure published files are built up to
142 date.
144 `:preparation-function'
146 Function to be called before publishing this project. This
147 may also be a list of functions. Preparation functions are
148 called with the project properties list as their sole
149 argument.
151 `:completion-function'
153 Function to be called after publishing this project. This
154 may also be a list of functions. Completion functions are
155 called with the project properties list as their sole
156 argument.
158 Some properties control details of the Org publishing process,
159 and are equivalent to the corresponding user variables listed in
160 the right column. Back-end specific properties may also be
161 included. See the back-end documentation for more information.
163 :author `user-full-name'
164 :creator `org-export-creator-string'
165 :email `user-mail-address'
166 :exclude-tags `org-export-exclude-tags'
167 :headline-levels `org-export-headline-levels'
168 :language `org-export-default-language'
169 :preserve-breaks `org-export-preserve-breaks'
170 :section-numbers `org-export-with-section-numbers'
171 :select-tags `org-export-select-tags'
172 :time-stamp-file `org-export-time-stamp-file'
173 :with-archived-trees `org-export-with-archived-trees'
174 :with-author `org-export-with-author'
175 :with-creator `org-export-with-creator'
176 :with-date `org-export-with-date'
177 :with-drawers `org-export-with-drawers'
178 :with-email `org-export-with-email'
179 :with-emphasize `org-export-with-emphasize'
180 :with-entities `org-export-with-entities'
181 :with-fixed-width `org-export-with-fixed-width'
182 :with-footnotes `org-export-with-footnotes'
183 :with-inlinetasks `org-export-with-inlinetasks'
184 :with-latex `org-export-with-latex'
185 :with-planning `org-export-with-planning'
186 :with-priority `org-export-with-priority'
187 :with-properties `org-export-with-properties'
188 :with-smart-quotes `org-export-with-smart-quotes'
189 :with-special-strings `org-export-with-special-strings'
190 :with-statistics-cookies' `org-export-with-statistics-cookies'
191 :with-sub-superscript `org-export-with-sub-superscripts'
192 :with-toc `org-export-with-toc'
193 :with-tables `org-export-with-tables'
194 :with-tags `org-export-with-tags'
195 :with-tasks `org-export-with-tasks'
196 :with-timestamps `org-export-with-timestamps'
197 :with-title `org-export-with-title'
198 :with-todo-keywords `org-export-with-todo-keywords'
200 The following properties may be used to control publishing of
201 a site-map of files or summary page for a given project.
203 `:auto-sitemap'
205 Whether to publish a site-map during
206 `org-publish-current-project' or `org-publish-all'.
208 `:sitemap-filename'
210 Filename for output of sitemap. Defaults to \"sitemap.org\".
212 `:sitemap-title'
214 Title of site-map page. Defaults to name of file.
216 `:sitemap-function'
218 Plugin function to use for generation of site-map. Defaults
219 to `org-publish-org-sitemap', which generates a plain list of
220 links to all files in the project.
222 `:sitemap-style'
224 Can be `list' (site-map is just an itemized list of the
225 titles of the files involved) or `tree' (the directory
226 structure of the source files is reflected in the site-map).
227 Defaults to `tree'.
229 `:sitemap-sans-extension'
231 Remove extension from site-map's file-names. Useful to have
232 cool URIs (see http://www.w3.org/Provider/Style/URI).
233 Defaults to nil.
235 If you create a site-map file, adjust the sorting like this:
237 `:sitemap-sort-folders'
239 Where folders should appear in the site-map. Set this to
240 `first' (default) or `last' to display folders first or last,
241 respectively. Any other value will mix files and folders.
243 `:sitemap-sort-files'
245 The site map is normally sorted alphabetically. You can
246 change this behavior setting this to `anti-chronologically',
247 `chronologically', or nil.
249 `:sitemap-ignore-case'
251 Should sorting be case-sensitive? Default nil.
253 The following property control the creation of a concept index.
255 `:makeindex'
257 Create a concept index. The file containing the index has to
258 be called \"theindex.org\". If it doesn't exist in the
259 project, it will be generated. Contents of the index are
260 stored in the file \"theindex.inc\", which can be included in
261 \"theindex.org\".
263 Other properties affecting publication.
265 `:body-only'
267 Set this to t to publish only the body of the documents."
268 :group 'org-export-publish
269 :type 'alist)
271 (defcustom org-publish-use-timestamps-flag t
272 "Non-nil means use timestamp checking to publish only changed files.
273 When nil, do no timestamp checking and always publish all files."
274 :group 'org-export-publish
275 :type 'boolean)
277 (defcustom org-publish-timestamp-directory
278 (convert-standard-filename "~/.org-timestamps/")
279 "Name of directory in which to store publishing timestamps."
280 :group 'org-export-publish
281 :type 'directory)
283 (defcustom org-publish-list-skipped-files t
284 "Non-nil means show message about files *not* published."
285 :group 'org-export-publish
286 :type 'boolean)
288 (defcustom org-publish-sitemap-sort-files 'alphabetically
289 "Method to sort files in site-maps.
290 Possible values are `alphabetically', `chronologically',
291 `anti-chronologically' and nil.
293 If `alphabetically', files will be sorted alphabetically. If
294 `chronologically', files will be sorted with older modification
295 time first. If `anti-chronologically', files will be sorted with
296 newer modification time first. nil won't sort files.
298 You can overwrite this default per project in your
299 `org-publish-project-alist', using `:sitemap-sort-files'."
300 :group 'org-export-publish
301 :type 'symbol)
303 (defcustom org-publish-sitemap-sort-folders 'first
304 "A symbol, denoting if folders are sorted first in sitemaps.
305 Possible values are `first', `last', and nil.
306 If `first', folders will be sorted before files.
307 If `last', folders are sorted to the end after the files.
308 Any other value will not mix files and folders.
310 You can overwrite this default per project in your
311 `org-publish-project-alist', using `:sitemap-sort-folders'."
312 :group 'org-export-publish
313 :type 'symbol)
315 (defcustom org-publish-sitemap-sort-ignore-case nil
316 "Non-nil when site-map sorting should ignore case.
318 You can overwrite this default per project in your
319 `org-publish-project-alist', using `:sitemap-ignore-case'."
320 :group 'org-export-publish
321 :type 'boolean)
323 (defcustom org-publish-sitemap-date-format "%Y-%m-%d"
324 "Format for printing a date in the sitemap.
325 See `format-time-string' for allowed formatters."
326 :group 'org-export-publish
327 :type 'string)
329 (defcustom org-publish-sitemap-file-entry-format "%t"
330 "Format string for site-map file entry.
331 You could use brackets to delimit on what part the link will be.
333 %t is the title.
334 %a is the author.
335 %d is the date formatted using `org-publish-sitemap-date-format'."
336 :group 'org-export-publish
337 :type 'string)
341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 ;;; Timestamp-related functions
344 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
345 "Return path to timestamp file for filename FILENAME."
346 (setq filename (concat filename "::" (or pub-dir "") "::"
347 (format "%s" (or pub-func ""))))
348 (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
350 (defun org-publish-needed-p
351 (filename &optional pub-dir pub-func _true-pub-dir base-dir)
352 "Non-nil if FILENAME should be published in PUB-DIR using PUB-FUNC.
353 TRUE-PUB-DIR is where the file will truly end up. Currently we
354 are not using this - maybe it can eventually be used to check if
355 the file is present at the target location, and how old it is.
356 Right now we cannot do this, because we do not know under what
357 file name the file will be stored - the publishing function can
358 still decide about that independently."
359 (let ((rtn (if (not org-publish-use-timestamps-flag) t
360 (org-publish-cache-file-needs-publishing
361 filename pub-dir pub-func base-dir))))
362 (if rtn (message "Publishing file %s using `%s'" filename pub-func)
363 (when org-publish-list-skipped-files
364 (message "Skipping unmodified file %s" filename)))
365 rtn))
367 (defun org-publish-update-timestamp
368 (filename &optional pub-dir pub-func _base-dir)
369 "Update publishing timestamp for file FILENAME.
370 If there is no timestamp, create one."
371 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func))
372 (stamp (org-publish-cache-ctime-of-src filename)))
373 (org-publish-cache-set key stamp)))
375 (defun org-publish-remove-all-timestamps ()
376 "Remove all files in the timestamp directory."
377 (let ((dir org-publish-timestamp-directory))
378 (when (and (file-exists-p dir) (file-directory-p dir))
379 (mapc #'delete-file (directory-files dir 'full "[^.]\\'"))
380 (org-publish-reset-cache))))
384 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
385 ;;; Getting project information out of `org-publish-project-alist'
387 (defun org-publish-expand-projects (projects-alist)
388 "Expand projects in PROJECTS-ALIST.
389 This splices all the components into the list."
390 (let ((rest projects-alist) rtn p components)
391 (while (setq p (pop rest))
392 (if (setq components (plist-get (cdr p) :components))
393 (setq rest (append
394 (mapcar (lambda (x) (assoc x org-publish-project-alist))
395 components)
396 rest))
397 (push p rtn)))
398 (nreverse (delete-dups (delq nil rtn)))))
400 (defvar org-publish-sitemap-sort-files)
401 (defvar org-publish-sitemap-sort-folders)
402 (defvar org-publish-sitemap-ignore-case)
403 (defvar org-publish-sitemap-requested)
404 (defvar org-publish-sitemap-date-format)
405 (defvar org-publish-sitemap-file-entry-format)
406 (defun org-publish-compare-directory-files (a b)
407 "Predicate for `sort', that sorts folders and files for sitemap."
408 (let ((retval t))
409 (when (or org-publish-sitemap-sort-files org-publish-sitemap-sort-folders)
410 ;; First we sort files:
411 (when org-publish-sitemap-sort-files
412 (pcase org-publish-sitemap-sort-files
413 (`alphabetically
414 (let* ((adir (file-directory-p a))
415 (aorg (and (string-suffix-p ".org" a) (not adir)))
416 (bdir (file-directory-p b))
417 (borg (and (string-suffix-p ".org" b) (not bdir)))
418 (A (if aorg (concat (file-name-directory a)
419 (org-publish-find-title a)) a))
420 (B (if borg (concat (file-name-directory b)
421 (org-publish-find-title b)) b)))
422 (setq retval (if org-publish-sitemap-ignore-case
423 (not (string-lessp (upcase B) (upcase A)))
424 (not (string-lessp B A))))))
425 ((or `anti-chronologically `chronologically)
426 (let* ((adate (org-publish-find-date a))
427 (bdate (org-publish-find-date b))
428 (A (+ (lsh (car adate) 16) (cadr adate)))
429 (B (+ (lsh (car bdate) 16) (cadr bdate))))
430 (setq retval
431 (if (eq org-publish-sitemap-sort-files 'chronologically)
432 (<= A B)
433 (>= A B)))))))
434 ;; Directory-wise wins:
435 (when org-publish-sitemap-sort-folders
436 ;; a is directory, b not:
437 (cond
438 ((and (file-directory-p a) (not (file-directory-p b)))
439 (setq retval (equal org-publish-sitemap-sort-folders 'first)))
440 ;; a is not a directory, but b is:
441 ((and (not (file-directory-p a)) (file-directory-p b))
442 (setq retval (equal org-publish-sitemap-sort-folders 'last))))))
443 retval))
445 (defun org-publish-get-base-files-1
446 (base-dir &optional recurse match skip-file skip-dir)
447 "Set `org-publish-temp-files' with files from BASE-DIR directory.
448 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
449 non-nil, restrict this list to the files matching the regexp
450 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
451 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
452 matching the regexp SKIP-DIR when recursing through BASE-DIR."
453 (let ((all-files (if (not recurse) (directory-files base-dir t match)
454 ;; If RECURSE is non-nil, we want all files
455 ;; matching MATCH and sub-directories.
456 (cl-remove-if-not
457 (lambda (file)
458 (or (file-directory-p file)
459 (and match (string-match match file))))
460 (directory-files base-dir t)))))
461 (dolist (f (if (not org-publish-sitemap-requested) all-files
462 (sort all-files #'org-publish-compare-directory-files)))
463 (let ((fd-p (file-directory-p f))
464 (fnd (file-name-nondirectory f)))
465 (if (and fd-p recurse
466 (not (string-match "^\\.+$" fnd))
467 (if skip-dir (not (string-match skip-dir fnd)) t))
468 (org-publish-get-base-files-1
469 f recurse match skip-file skip-dir)
470 (unless (or fd-p ; This is a directory.
471 (and skip-file (string-match skip-file fnd))
472 (not (file-exists-p (file-truename f)))
473 (not (string-match match fnd)))
474 (cl-pushnew f org-publish-temp-files)))))))
476 (defun org-publish-get-base-files (project &optional exclude-regexp)
477 "Return a list of all files in PROJECT.
478 If EXCLUDE-REGEXP is set, this will be used to filter out
479 matching filenames."
480 (let* ((project-plist (cdr project))
481 (base-dir (file-name-as-directory
482 (plist-get project-plist :base-directory)))
483 (include-list (plist-get project-plist :include))
484 (recurse (plist-get project-plist :recursive))
485 (extension (or (plist-get project-plist :base-extension) "org"))
486 ;; sitemap-... variables are dynamically scoped for
487 ;; org-publish-compare-directory-files:
488 (org-publish-sitemap-requested
489 (plist-get project-plist :auto-sitemap))
490 (sitemap-filename
491 (or (plist-get project-plist :sitemap-filename) "sitemap.org"))
492 (org-publish-sitemap-sort-folders
493 (if (plist-member project-plist :sitemap-sort-folders)
494 (plist-get project-plist :sitemap-sort-folders)
495 org-publish-sitemap-sort-folders))
496 (org-publish-sitemap-sort-files
497 (cond ((plist-member project-plist :sitemap-sort-files)
498 (plist-get project-plist :sitemap-sort-files))
499 ;; For backward compatibility:
500 ((plist-member project-plist :sitemap-alphabetically)
501 (if (plist-get project-plist :sitemap-alphabetically)
502 'alphabetically nil))
503 (t org-publish-sitemap-sort-files)))
504 (org-publish-sitemap-ignore-case
505 (if (plist-member project-plist :sitemap-ignore-case)
506 (plist-get project-plist :sitemap-ignore-case)
507 org-publish-sitemap-sort-ignore-case))
508 (match (if (eq extension 'any) "^[^\\.]"
509 (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
510 ;; Make sure `org-publish-sitemap-sort-folders' has an accepted
511 ;; value.
512 (unless (memq org-publish-sitemap-sort-folders '(first last))
513 (setq org-publish-sitemap-sort-folders nil))
515 (setq org-publish-temp-files nil)
516 (when org-publish-sitemap-requested
517 (cl-pushnew (expand-file-name (concat base-dir sitemap-filename))
518 org-publish-temp-files))
519 (org-publish-get-base-files-1 base-dir recurse match
520 ;; FIXME distinguish exclude regexp
521 ;; for skip-file and skip-dir?
522 exclude-regexp exclude-regexp)
523 (dolist (f include-list org-publish-temp-files)
524 (cl-pushnew (expand-file-name (concat base-dir f))
525 org-publish-temp-files))))
527 (defun org-publish-get-project-from-filename (filename &optional up)
528 "Return the project that FILENAME belongs to."
529 (let* ((filename (expand-file-name filename))
530 project-name)
532 (catch 'p-found
533 (dolist (prj org-publish-project-alist)
534 (unless (plist-get (cdr prj) :components)
535 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
536 (let* ((r (plist-get (cdr prj) :recursive))
537 (b (expand-file-name (file-name-as-directory
538 (plist-get (cdr prj) :base-directory))))
539 (x (or (plist-get (cdr prj) :base-extension) "org"))
540 (e (plist-get (cdr prj) :exclude))
541 (i (plist-get (cdr prj) :include))
542 (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
543 (when
544 (or (and i
545 (member filename
546 (dolist (file i) (expand-file-name file b))))
547 (and (not (and e (string-match e filename)))
548 (string-match xm filename)))
549 (setq project-name (car prj))
550 (throw 'p-found project-name))))))
551 (when up
552 (dolist (prj org-publish-project-alist)
553 (if (member project-name (plist-get (cdr prj) :components))
554 (setq project-name (car prj)))))
555 (assoc project-name org-publish-project-alist)))
559 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
560 ;;; Tools for publishing functions in back-ends
562 (defun org-publish-org-to (backend filename extension plist &optional pub-dir)
563 "Publish an Org file to a specified back-end.
565 BACKEND is a symbol representing the back-end used for
566 transcoding. FILENAME is the filename of the Org file to be
567 published. EXTENSION is the extension used for the output
568 string, with the leading dot. PLIST is the property list for the
569 given project.
571 Optional argument PUB-DIR, when non-nil is the publishing
572 directory.
574 Return output file name."
575 (unless (or (not pub-dir) (file-exists-p pub-dir)) (make-directory pub-dir t))
576 ;; Check if a buffer visiting FILENAME is already open.
577 (let* ((org-inhibit-startup t)
578 (visiting (find-buffer-visiting filename))
579 (work-buffer (or visiting (find-file-noselect filename))))
580 (unwind-protect
581 (with-current-buffer work-buffer
582 (let ((output (org-export-output-file-name extension nil pub-dir)))
583 (org-export-to-file backend output
584 nil nil nil (plist-get plist :body-only)
585 ;; Add `org-publish--store-crossrefs' and
586 ;; `org-publish-collect-index' to final output filters.
587 ;; The latter isn't dependent on `:makeindex', since we
588 ;; want to keep it up-to-date in cache anyway.
589 (org-combine-plists
590 plist
591 `(:crossrefs
592 ,(org-publish-cache-get-file-property
593 (expand-file-name filename) :crossrefs nil t)
594 :filter-final-output
595 (org-publish--store-crossrefs
596 org-publish-collect-index
597 ,@(plist-get plist :filter-final-output)))))))
598 ;; Remove opened buffer in the process.
599 (unless visiting (kill-buffer work-buffer)))))
601 (defun org-publish-attachment (_plist filename pub-dir)
602 "Publish a file with no transformation of any kind.
604 FILENAME is the filename of the Org file to be published. PLIST
605 is the property list for the given project. PUB-DIR is the
606 publishing directory.
608 Return output file name."
609 (unless (file-directory-p pub-dir)
610 (make-directory pub-dir t))
611 (let ((output (expand-file-name (file-name-nondirectory filename) pub-dir)))
612 (or (equal (expand-file-name (file-name-directory filename))
613 (file-name-as-directory (expand-file-name pub-dir)))
614 (copy-file filename output t))
615 ;; Return file name.
616 output))
620 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
621 ;;; Publishing files, sets of files, and indices
623 (defun org-publish-file (filename &optional project no-cache)
624 "Publish file FILENAME from PROJECT.
625 If NO-CACHE is not nil, do not initialize `org-publish-cache'.
626 This is needed, since this function is used to publish single
627 files, when entire projects are published (see
628 `org-publish-projects')."
629 (let* ((project
630 (or project
631 (or (org-publish-get-project-from-filename filename)
632 (error "File %s not part of any known project"
633 (abbreviate-file-name filename)))))
634 (project-plist (cdr project))
635 (ftname (expand-file-name filename))
636 (publishing-function
637 (let ((fun (plist-get project-plist :publishing-function)))
638 (cond ((null fun) (error "No publishing function chosen"))
639 ((listp fun) fun)
640 (t (list fun)))))
641 (base-dir
642 (file-name-as-directory
643 (expand-file-name
644 (or (plist-get project-plist :base-directory)
645 (error "Project %s does not have :base-directory defined"
646 (car project))))))
647 (pub-dir
648 (file-name-as-directory
649 (file-truename
650 (or (eval (plist-get project-plist :publishing-directory))
651 (error "Project %s does not have :publishing-directory defined"
652 (car project))))))
653 tmp-pub-dir)
655 (unless no-cache (org-publish-initialize-cache (car project)))
657 (setq tmp-pub-dir
658 (file-name-directory
659 (concat pub-dir
660 (and (string-match (regexp-quote base-dir) ftname)
661 (substring ftname (match-end 0))))))
662 ;; Allow chain of publishing functions.
663 (dolist (f publishing-function)
664 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir base-dir)
665 (let ((output (funcall f project-plist filename tmp-pub-dir)))
666 (org-publish-update-timestamp filename pub-dir f base-dir)
667 (run-hook-with-args 'org-publish-after-publishing-hook
668 filename
669 output))))
670 ;; Make sure to write cache to file after successfully publishing
671 ;; a file, so as to minimize impact of a publishing failure.
672 (org-publish-write-cache-file)))
674 (defun org-publish-projects (projects)
675 "Publish all files belonging to the PROJECTS alist.
676 If `:auto-sitemap' is set, publish the sitemap too. If
677 `:makeindex' is set, also produce a file \"theindex.org\"."
678 (dolist (project (org-publish-expand-projects projects))
679 (let ((project-plist (cdr project)))
680 (let ((fun (plist-get project-plist :preparation-function)))
681 (cond ((consp fun) (dolist (f fun) (funcall f project-plist)))
682 ((functionp fun) (funcall fun project-plist))))
683 ;; Each project uses its own cache file.
684 (org-publish-initialize-cache (car project))
685 (when (plist-get project-plist :auto-sitemap)
686 (let ((sitemap-filename
687 (or (plist-get project-plist :sitemap-filename)
688 "sitemap.org"))
689 (sitemap-function
690 (or (plist-get project-plist :sitemap-function)
691 #'org-publish-org-sitemap))
692 (org-publish-sitemap-date-format
693 (or (plist-get project-plist :sitemap-date-format)
694 org-publish-sitemap-date-format))
695 (org-publish-sitemap-file-entry-format
696 (or (plist-get project-plist :sitemap-file-entry-format)
697 org-publish-sitemap-file-entry-format)))
698 (funcall sitemap-function project sitemap-filename)))
699 ;; Publish all files from PROJECT excepted "theindex.org". Its
700 ;; publishing will be deferred until "theindex.inc" is
701 ;; populated.
702 (let ((theindex
703 (expand-file-name "theindex.org"
704 (plist-get project-plist :base-directory)))
705 (exclude-regexp (plist-get project-plist :exclude)))
706 (dolist (file (org-publish-get-base-files project exclude-regexp))
707 (unless (equal file theindex) (org-publish-file file project t)))
708 ;; Populate "theindex.inc", if needed, and publish
709 ;; "theindex.org".
710 (when (plist-get project-plist :makeindex)
711 (org-publish-index-generate-theindex
712 project (plist-get project-plist :base-directory))
713 (org-publish-file theindex project t)))
714 (let ((fun (plist-get project-plist :completion-function)))
715 (cond ((consp fun) (dolist (f fun) (funcall f project-plist)))
716 ((functionp fun) (funcall fun project-plist))))
717 (org-publish-write-cache-file))))
719 (defun org-publish-org-sitemap (project &optional sitemap-filename)
720 "Create a sitemap of pages in set defined by PROJECT.
721 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
722 Default for SITEMAP-FILENAME is `sitemap.org'."
723 (let* ((project-plist (cdr project))
724 (dir (file-name-as-directory
725 (plist-get project-plist :base-directory)))
726 (localdir (file-name-directory dir))
727 (indent-str (make-string 2 ?\ ))
728 (exclude-regexp (plist-get project-plist :exclude))
729 (files (nreverse
730 (org-publish-get-base-files project exclude-regexp)))
731 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
732 (sitemap-title (or (plist-get project-plist :sitemap-title)
733 (concat "Sitemap for project " (car project))))
734 (sitemap-style (or (plist-get project-plist :sitemap-style)
735 'tree))
736 (sitemap-sans-extension
737 (plist-get project-plist :sitemap-sans-extension))
738 (visiting (find-buffer-visiting sitemap-filename))
739 file sitemap-buffer)
740 (with-current-buffer
741 (let ((org-inhibit-startup t))
742 (setq sitemap-buffer
743 (or visiting (find-file sitemap-filename))))
744 (erase-buffer)
745 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
746 (while (setq file (pop files))
747 (let ((link (file-relative-name file dir))
748 (oldlocal localdir))
749 (when sitemap-sans-extension
750 (setq link (file-name-sans-extension link)))
751 ;; sitemap shouldn't list itself
752 (unless (equal (file-truename sitemap-filename)
753 (file-truename file))
754 (if (eq sitemap-style 'list)
755 (message "Generating list-style sitemap for %s" sitemap-title)
756 (message "Generating tree-style sitemap for %s" sitemap-title)
757 (setq localdir (concat (file-name-as-directory dir)
758 (file-name-directory link)))
759 (unless (string= localdir oldlocal)
760 (if (string= localdir dir)
761 (setq indent-str (make-string 2 ?\ ))
762 (let ((subdirs
763 (split-string
764 (directory-file-name
765 (file-name-directory
766 (file-relative-name localdir dir))) "/"))
767 (subdir "")
768 (old-subdirs (split-string
769 (file-relative-name oldlocal dir) "/")))
770 (setq indent-str (make-string 2 ?\ ))
771 (while (string= (car old-subdirs) (car subdirs))
772 (setq indent-str (concat indent-str (make-string 2 ?\ )))
773 (pop old-subdirs)
774 (pop subdirs))
775 (dolist (d subdirs)
776 (setq subdir (concat subdir d "/"))
777 (insert (concat indent-str " + " d "\n"))
778 (setq indent-str (make-string
779 (+ (length indent-str) 2) ?\ )))))))
780 ;; This is common to 'flat and 'tree
781 (let ((entry
782 (org-publish-format-file-entry
783 org-publish-sitemap-file-entry-format file project-plist))
784 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
785 (cond ((string-match-p regexp entry)
786 (string-match regexp entry)
787 (insert (concat indent-str " + " (match-string 1 entry)
788 "[[file:" link "]["
789 (match-string 2 entry)
790 "]]" (match-string 3 entry) "\n")))
792 (insert (concat indent-str " + [[file:" link "]["
793 entry
794 "]]\n"))))))))
795 (save-buffer))
796 (or visiting (kill-buffer sitemap-buffer))))
798 (defun org-publish-format-file-entry (fmt file project-plist)
799 (format-spec
801 `((?t . ,(org-publish-find-title file t))
802 (?d . ,(format-time-string org-publish-sitemap-date-format
803 (org-publish-find-date file)))
804 (?a . ,(or (plist-get project-plist :author) user-full-name)))))
806 (defun org-publish-find-title (file &optional reset)
807 "Find the title of FILE in project."
809 (and (not reset) (org-publish-cache-get-file-property file :title nil t))
810 (let* ((org-inhibit-startup t)
811 (visiting (find-buffer-visiting file))
812 (buffer (or visiting (find-file-noselect file))))
813 (with-current-buffer buffer
814 (let ((title
815 (let ((property
816 (plist-get
817 ;; protect local variables in open buffers
818 (if visiting
819 (org-export-with-buffer-copy (org-export-get-environment))
820 (org-export-get-environment))
821 :title)))
822 (if property
823 (org-no-properties (org-element-interpret-data property))
824 (file-name-nondirectory (file-name-sans-extension file))))))
825 (unless visiting (kill-buffer buffer))
826 (org-publish-cache-set-file-property file :title title)
827 title)))))
829 (defun org-publish-find-date (file)
830 "Find the date of FILE in project.
831 This function assumes FILE is either a directory or an Org file.
832 If FILE is an Org file and provides a DATE keyword use it. In
833 any other case use the file system's modification time. Return
834 time in `current-time' format."
835 (if (file-directory-p file) (nth 5 (file-attributes file))
836 (let* ((org-inhibit-startup t)
837 (visiting (find-buffer-visiting file))
838 (file-buf (or visiting (find-file-noselect file nil)))
839 (date (plist-get
840 (with-current-buffer file-buf
841 (if visiting
842 (org-export-with-buffer-copy
843 (org-export-get-environment))
844 (org-export-get-environment)))
845 :date)))
846 (unless visiting (kill-buffer file-buf))
847 ;; DATE is a secondary string. If it contains a timestamp,
848 ;; convert it to internal format. Otherwise, use FILE
849 ;; modification time.
850 (cond ((let ((ts (and (consp date) (assq 'timestamp date))))
851 (and ts
852 (let ((value (org-element-interpret-data ts)))
853 (and (org-string-nw-p value)
854 (org-time-string-to-time value))))))
855 ((file-exists-p file) (nth 5 (file-attributes file)))
856 (t (error "No such file: \"%s\"" file))))))
860 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
861 ;;; Interactive publishing functions
863 ;;;###autoload
864 (defalias 'org-publish-project 'org-publish)
866 ;;;###autoload
867 (defun org-publish (project &optional force async)
868 "Publish PROJECT.
870 PROJECT is either a project name, as a string, or a project
871 alist (see `org-publish-project-alist' variable).
873 When optional argument FORCE is non-nil, force publishing all
874 files in PROJECT. With a non-nil optional argument ASYNC,
875 publishing will be done asynchronously, in another process."
876 (interactive
877 (list (assoc (completing-read "Publish project: "
878 org-publish-project-alist nil t)
879 org-publish-project-alist)
880 current-prefix-arg))
881 (let ((project (if (not (stringp project)) project
882 ;; If this function is called in batch mode,
883 ;; PROJECT is still a string here.
884 (assoc project org-publish-project-alist))))
885 (cond
886 ((not project))
887 (async
888 (org-export-async-start (lambda (_) nil)
889 `(let ((org-publish-use-timestamps-flag
890 ,(and (not force) org-publish-use-timestamps-flag)))
891 ;; Expand components right now as external process may not
892 ;; be aware of complete `org-publish-project-alist'.
893 (org-publish-projects
894 ',(org-publish-expand-projects (list project))))))
895 (t (save-window-excursion
896 (let ((org-publish-use-timestamps-flag
897 (and (not force) org-publish-use-timestamps-flag)))
898 (org-publish-projects (list project))))))))
900 ;;;###autoload
901 (defun org-publish-all (&optional force async)
902 "Publish all projects.
903 With prefix argument FORCE, remove all files in the timestamp
904 directory and force publishing all projects. With a non-nil
905 optional argument ASYNC, publishing will be done asynchronously,
906 in another process."
907 (interactive "P")
908 (if async
909 (org-export-async-start (lambda (_) nil)
910 `(progn
911 (when ',force (org-publish-remove-all-timestamps))
912 (let ((org-publish-use-timestamps-flag
913 (if ',force nil ,org-publish-use-timestamps-flag)))
914 (org-publish-projects ',org-publish-project-alist))))
915 (when force (org-publish-remove-all-timestamps))
916 (save-window-excursion
917 (let ((org-publish-use-timestamps-flag
918 (if force nil org-publish-use-timestamps-flag)))
919 (org-publish-projects org-publish-project-alist)))))
922 ;;;###autoload
923 (defun org-publish-current-file (&optional force async)
924 "Publish the current file.
925 With prefix argument FORCE, force publish the file. When
926 optional argument ASYNC is non-nil, publishing will be done
927 asynchronously, in another process."
928 (interactive "P")
929 (let ((file (buffer-file-name (buffer-base-buffer))))
930 (if async
931 (org-export-async-start (lambda (_) nil)
932 `(let ((org-publish-use-timestamps-flag
933 (if ',force nil ,org-publish-use-timestamps-flag)))
934 (org-publish-file ,file)))
935 (save-window-excursion
936 (let ((org-publish-use-timestamps-flag
937 (if force nil org-publish-use-timestamps-flag)))
938 (org-publish-file file))))))
940 ;;;###autoload
941 (defun org-publish-current-project (&optional force async)
942 "Publish the project associated with the current file.
943 With a prefix argument, force publishing of all files in
944 the project."
945 (interactive "P")
946 (save-window-excursion
947 (let ((project (org-publish-get-project-from-filename
948 (buffer-file-name (buffer-base-buffer)) 'up)))
949 (if project (org-publish project force async)
950 (error "File %s is not part of any known project"
951 (buffer-file-name (buffer-base-buffer)))))))
955 ;;; Index generation
957 (defun org-publish-collect-index (output _backend info)
958 "Update index for a file in cache.
960 OUTPUT is the output from transcoding current file. BACKEND is
961 the back-end that was used for transcoding. INFO is a plist
962 containing publishing and export options.
964 The index relative to current file is stored as an alist. An
965 association has the following shape: (TERM FILE-NAME PARENT),
966 where TERM is the indexed term, as a string, FILE-NAME is the
967 original full path of the file where the term in encountered, and
968 PARENT is a reference to the headline, if any, containing the
969 original index keyword. When non-nil, this reference is a cons
970 cell. Its CAR is a symbol among `id', `custom-id' and `name' and
971 its CDR is a string."
972 (let ((file (plist-get info :input-file)))
973 (org-publish-cache-set-file-property
974 file :index
975 (delete-dups
976 (org-element-map (plist-get info :parse-tree) 'keyword
977 (lambda (k)
978 (when (equal (org-element-property :key k) "INDEX")
979 (let ((parent (org-export-get-parent-headline k)))
980 (list (org-element-property :value k)
981 file
982 (cond
983 ((not parent) nil)
984 ((let ((id (org-element-property :ID parent)))
985 (and id (cons 'id id))))
986 ((let ((id (org-element-property :CUSTOM_ID parent)))
987 (and id (cons 'custom-id id))))
988 (t (cons 'name
989 ;; Remove statistics cookie.
990 (replace-regexp-in-string
991 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
992 (org-element-property :raw-value parent)))))))))
993 info))))
994 ;; Return output unchanged.
995 output)
997 (defun org-publish-index-generate-theindex (project directory)
998 "Retrieve full index from cache and build \"theindex.org\".
999 PROJECT is the project the index relates to. DIRECTORY is the
1000 publishing directory."
1001 (let ((all-files (org-publish-get-base-files
1002 project (plist-get (cdr project) :exclude)))
1003 full-index)
1004 ;; Compile full index and sort it alphabetically.
1005 (dolist (file all-files
1006 (setq full-index
1007 (sort (nreverse full-index)
1008 (lambda (a b) (string< (downcase (car a))
1009 (downcase (car b)))))))
1010 (let ((index (org-publish-cache-get-file-property file :index)))
1011 (dolist (term index)
1012 (unless (member term full-index) (push term full-index)))))
1013 ;; Write "theindex.inc" in DIRECTORY.
1014 (with-temp-file (expand-file-name "theindex.inc" directory)
1015 (let ((current-letter nil) (last-entry nil))
1016 (dolist (idx full-index)
1017 (let* ((entry (org-split-string (car idx) "!"))
1018 (letter (upcase (substring (car entry) 0 1)))
1019 ;; Transform file into a path relative to publishing
1020 ;; directory.
1021 (file (file-relative-name
1022 (nth 1 idx)
1023 (plist-get (cdr project) :base-directory))))
1024 ;; Check if another letter has to be inserted.
1025 (unless (string= letter current-letter)
1026 (insert (format "* %s\n" letter)))
1027 ;; Compute the first difference between last entry and
1028 ;; current one: it tells the level at which new items
1029 ;; should be added.
1030 (let* ((rank
1031 (if (equal entry last-entry) (1- (length entry))
1032 (cl-loop for n from 0 to (length entry)
1033 unless (equal (nth n entry) (nth n last-entry))
1034 return n)))
1035 (len (length (nthcdr rank entry))))
1036 ;; For each term after the first difference, create
1037 ;; a new sub-list with the term as body. Moreover,
1038 ;; linkify the last term.
1039 (dotimes (n len)
1040 (insert
1041 (concat
1042 (make-string (* (+ rank n) 2) ? ) " - "
1043 (if (not (= (1- len) n)) (nth (+ rank n) entry)
1044 ;; Last term: Link it to TARGET, if possible.
1045 (let ((target (nth 2 idx)))
1046 (format
1047 "[[%s][%s]]"
1048 ;; Destination.
1049 (pcase (car target)
1050 (`nil (format "file:%s" file))
1051 (`id (format "id:%s" (cdr target)))
1052 (`custom-id (format "file:%s::#%s" file (cdr target)))
1053 (_ (format "file:%s::*%s" file (cdr target))))
1054 ;; Description.
1055 (car (last entry)))))
1056 "\n"))))
1057 (setq current-letter letter last-entry entry))))
1058 ;; Create "theindex.org", if it doesn't exist yet, and provide
1059 ;; a default index file.
1060 (let ((index.org (expand-file-name "theindex.org" directory)))
1061 (unless (file-exists-p index.org)
1062 (with-temp-file index.org
1063 (insert "#+TITLE: Index\n\n#+INCLUDE: \"theindex.inc\"\n\n")))))))
1067 ;;; External Fuzzy Links Resolution
1069 ;; This part implements tools to resolve [[file.org::*Some headline]]
1070 ;; links, where "file.org" belongs to the current project.
1072 (defun org-publish--store-crossrefs (output _backend info)
1073 "Store cross-references for current published file.
1075 OUPUT is the produced output, as a string. BACKEND is the export
1076 back-end used, as a symbol. INFO is the final export state, as
1077 a plist.
1079 This function is meant to be used as a final output filter. See
1080 `org-publish-org-to'."
1081 (org-publish-cache-set-file-property
1082 (plist-get info :input-file) :crossrefs
1083 ;; Update `:crossrefs' so as to remove unused references and search
1084 ;; cells. Actually used references are extracted from
1085 ;; `:internal-references', with references as strings removed. See
1086 ;; `org-export-get-reference' for details.
1087 (cl-remove-if (lambda (pair) (stringp (car pair)))
1088 (plist-get info :internal-references)))
1089 ;; Return output unchanged.
1090 output)
1092 (defun org-publish-resolve-external-link (search file)
1093 "Return reference for element matching string SEARCH in FILE.
1095 Return value is an internal reference, as a string.
1097 This function allows resolving external links with a search
1098 option, e.g.,
1100 [[file.org::*heading][description]]
1101 [[file.org::#custom-id][description]]
1102 [[file.org::fuzzy][description]]
1104 It only makes sense to use this if export back-end builds
1105 references with `org-export-get-reference'."
1106 (if (not org-publish-cache)
1107 (progn
1108 (message "Reference %S in file %S cannot be resolved without publishing"
1109 search
1110 file)
1111 "MissingReference")
1112 (let* ((filename (expand-file-name file))
1113 (crossrefs
1114 (org-publish-cache-get-file-property filename :crossrefs nil t))
1115 (cells (org-export-string-to-search-cell search)))
1117 ;; Look for reference associated to search cells triggered by
1118 ;; LINK. It can match when targeted file has been published
1119 ;; already.
1120 (let ((known (cdr (cl-some (lambda (c) (assoc c crossrefs)) cells))))
1121 (and known (org-export-format-reference known)))
1122 ;; Search cell is unknown so far. Generate a new internal
1123 ;; reference that will be used when the targeted file will be
1124 ;; published.
1125 (let ((new (org-export-new-reference crossrefs)))
1126 (dolist (cell cells) (push (cons cell new) crossrefs))
1127 (org-publish-cache-set-file-property filename :crossrefs crossrefs)
1128 (org-export-format-reference new))))))
1132 ;;; Caching functions
1134 (defun org-publish-write-cache-file (&optional free-cache)
1135 "Write `org-publish-cache' to file.
1136 If FREE-CACHE, empty the cache."
1137 (unless org-publish-cache
1138 (error "`org-publish-write-cache-file' called, but no cache present"))
1140 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1141 (unless cache-file
1142 (error "Cannot find cache-file name in `org-publish-write-cache-file'"))
1143 (with-temp-file cache-file
1144 (let (print-level print-length)
1145 (insert "(setq org-publish-cache \
1146 \(make-hash-table :test 'equal :weakness nil :size 100))\n")
1147 (maphash (lambda (k v)
1148 (insert
1149 (format "(puthash %S %s%S org-publish-cache)\n"
1150 k (if (or (listp v) (symbolp v)) "'" "") v)))
1151 org-publish-cache)))
1152 (when free-cache (org-publish-reset-cache))))
1154 (defun org-publish-initialize-cache (project-name)
1155 "Initialize the projects cache if not initialized yet and return it."
1157 (unless project-name
1158 (error "Cannot initialize `org-publish-cache' without projects name in \
1159 `org-publish-initialize-cache'"))
1161 (unless (file-exists-p org-publish-timestamp-directory)
1162 (make-directory org-publish-timestamp-directory t))
1163 (unless (file-directory-p org-publish-timestamp-directory)
1164 (error "Org publish timestamp: %s is not a directory"
1165 org-publish-timestamp-directory))
1167 (unless (and org-publish-cache
1168 (string= (org-publish-cache-get ":project:") project-name))
1169 (let* ((cache-file
1170 (concat
1171 (expand-file-name org-publish-timestamp-directory)
1172 project-name ".cache"))
1173 (cexists (file-exists-p cache-file)))
1175 (when org-publish-cache (org-publish-reset-cache))
1177 (if cexists (load-file cache-file)
1178 (setq org-publish-cache
1179 (make-hash-table :test 'equal :weakness nil :size 100))
1180 (org-publish-cache-set ":project:" project-name)
1181 (org-publish-cache-set ":cache-file:" cache-file))
1182 (unless cexists (org-publish-write-cache-file nil))))
1183 org-publish-cache)
1185 (defun org-publish-reset-cache ()
1186 "Empty org-publish-cache and reset it nil."
1187 (message "%s" "Resetting org-publish-cache")
1188 (when (hash-table-p org-publish-cache)
1189 (clrhash org-publish-cache))
1190 (setq org-publish-cache nil))
1192 (defun org-publish-cache-file-needs-publishing
1193 (filename &optional pub-dir pub-func _base-dir)
1194 "Check the timestamp of the last publishing of FILENAME.
1195 Return non-nil if the file needs publishing. Also check if
1196 any included files have been more recently published, so that
1197 the file including them will be republished as well."
1198 (unless org-publish-cache
1199 (error
1200 "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1201 (let* ((case-fold-search t)
1202 (key (org-publish-timestamp-filename filename pub-dir pub-func))
1203 (pstamp (org-publish-cache-get key))
1204 (org-inhibit-startup t)
1205 (visiting (find-buffer-visiting filename))
1206 included-files-ctime buf)
1207 (when (equal (file-name-extension filename) "org")
1208 (setq buf (find-file (expand-file-name filename)))
1209 (with-current-buffer buf
1210 (goto-char (point-min))
1211 (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t)
1212 (let* ((element (org-element-at-point))
1213 (included-file
1214 (and (eq (org-element-type element) 'keyword)
1215 (let ((value (org-element-property :value element)))
1216 (and value
1217 (string-match
1218 "\\`\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)"
1219 value)
1220 (let ((m (match-string 1 value)))
1221 (org-remove-double-quotes
1222 ;; Ignore search suffix.
1223 (if (string-match "\\(::\\(.*?\\)\\)\"?\\'" m)
1224 (substring m 0 (match-beginning 0))
1225 m))))))))
1226 (when included-file
1227 (push (org-publish-cache-ctime-of-src
1228 (expand-file-name included-file))
1229 included-files-ctime)))))
1230 (unless visiting (kill-buffer buf)))
1231 (or (null pstamp)
1232 (let ((ctime (org-publish-cache-ctime-of-src filename)))
1233 (or (< pstamp ctime)
1234 (cl-some (lambda (ct) (< ctime ct)) included-files-ctime))))))
1236 (defun org-publish-cache-set-file-property
1237 (filename property value &optional project-name)
1238 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1239 Use cache file of PROJECT-NAME. If the entry does not exist, it
1240 will be created. Return VALUE."
1241 ;; Evtl. load the requested cache file:
1242 (if project-name (org-publish-initialize-cache project-name))
1243 (let ((pl (org-publish-cache-get filename)))
1244 (if pl (progn (plist-put pl property value) value)
1245 (org-publish-cache-get-file-property
1246 filename property value nil project-name))))
1248 (defun org-publish-cache-get-file-property
1249 (filename property &optional default no-create project-name)
1250 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1251 Use cache file of PROJECT-NAME. Return the value of that PROPERTY
1252 or DEFAULT, if the value does not yet exist. If the entry will
1253 be created, unless NO-CREATE is not nil."
1254 ;; Evtl. load the requested cache file:
1255 (if project-name (org-publish-initialize-cache project-name))
1256 (let ((pl (org-publish-cache-get filename)) retval)
1257 (if pl
1258 (if (plist-member pl property)
1259 (setq retval (plist-get pl property))
1260 (setq retval default))
1261 ;; no pl yet:
1262 (unless no-create
1263 (org-publish-cache-set filename (list property default)))
1264 (setq retval default))
1265 retval))
1267 (defun org-publish-cache-get (key)
1268 "Return the value stored in `org-publish-cache' for key KEY.
1269 Returns nil, if no value or nil is found, or the cache does not
1270 exist."
1271 (unless org-publish-cache
1272 (error "`org-publish-cache-get' called, but no cache present"))
1273 (gethash key org-publish-cache))
1275 (defun org-publish-cache-set (key value)
1276 "Store KEY VALUE pair in `org-publish-cache'.
1277 Returns value on success, else nil."
1278 (unless org-publish-cache
1279 (error "`org-publish-cache-set' called, but no cache present"))
1280 (puthash key value org-publish-cache))
1282 (defun org-publish-cache-ctime-of-src (file)
1283 "Get the ctime of FILE as an integer."
1284 (let ((attr (file-attributes
1285 (expand-file-name (or (file-symlink-p file) file)
1286 (file-name-directory file)))))
1287 (if (not attr) (error "No such file: \"%s\"" file)
1288 (+ (lsh (car (nth 5 attr)) 16)
1289 (cadr (nth 5 attr))))))
1292 (provide 'ox-publish)
1294 ;; Local variables:
1295 ;; generated-autoload-file: "org-loaddefs.el"
1296 ;; End:
1298 ;;; ox-publish.el ends here