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