Clean up spacing to pass Emacs's pre-commit check
[org-mode.git] / lisp / ox-publish.el
blob5170a9a75770c5b21862f49b4eca98e13ea03187
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 (expand-file-name
549 (org-publish-property :base-directory p))))
550 (cond
551 ;; Check if FILENAME is explicitly included in one
552 ;; project.
553 ((member 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 (string-prefix-p base filename) p))
575 ((equal 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 (expand-file-name filename) :crossrefs nil t)
632 :filter-final-output
633 (org-publish--store-crossrefs
634 org-publish-collect-index
635 ,@(plist-get plist :filter-final-output)))))))
636 ;; Remove opened buffer in the process.
637 (unless visiting (kill-buffer work-buffer)))))
639 (defun org-publish-attachment (_plist filename pub-dir)
640 "Publish a file with no transformation of any kind.
642 FILENAME is the filename of the Org file to be published. PLIST
643 is the property list for the given project. PUB-DIR is the
644 publishing directory.
646 Return output file name."
647 (unless (file-directory-p pub-dir)
648 (make-directory pub-dir t))
649 (let ((output (expand-file-name (file-name-nondirectory filename) pub-dir)))
650 (or (equal (expand-file-name (file-name-directory filename))
651 (file-name-as-directory (expand-file-name pub-dir)))
652 (copy-file filename output t))
653 ;; Return file name.
654 output))
658 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
659 ;;; Publishing files, sets of files, and indices
661 (defun org-publish-file (filename &optional project no-cache)
662 "Publish file FILENAME from PROJECT.
663 If NO-CACHE is not nil, do not initialize `org-publish-cache'.
664 This is needed, since this function is used to publish single
665 files, when entire projects are published (see
666 `org-publish-projects')."
667 (let* ((project
668 (or project
669 (or (org-publish-get-project-from-filename filename)
670 (error "File %s not part of any known project"
671 (abbreviate-file-name filename)))))
672 (project-plist (cdr project))
673 (ftname (expand-file-name filename))
674 (publishing-function
675 (let ((fun (plist-get project-plist :publishing-function)))
676 (cond ((null fun) (error "No publishing function chosen"))
677 ((listp fun) fun)
678 (t (list fun)))))
679 (base-dir
680 (file-name-as-directory
681 (expand-file-name
682 (or (plist-get project-plist :base-directory)
683 (error "Project %s does not have :base-directory defined"
684 (car project))))))
685 (pub-dir
686 (file-name-as-directory
687 (file-truename
688 (or (eval (plist-get project-plist :publishing-directory))
689 (error "Project %s does not have :publishing-directory defined"
690 (car project))))))
691 tmp-pub-dir)
693 (unless no-cache (org-publish-initialize-cache (car project)))
695 (setq tmp-pub-dir
696 (file-name-directory
697 (concat pub-dir
698 (and (string-match (regexp-quote base-dir) ftname)
699 (substring ftname (match-end 0))))))
700 ;; Allow chain of publishing functions.
701 (dolist (f publishing-function)
702 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir base-dir)
703 (let ((output (funcall f project-plist filename tmp-pub-dir)))
704 (org-publish-update-timestamp filename pub-dir f base-dir)
705 (run-hook-with-args 'org-publish-after-publishing-hook
706 filename
707 output))))
708 ;; Make sure to write cache to file after successfully publishing
709 ;; a file, so as to minimize impact of a publishing failure.
710 (org-publish-write-cache-file)))
712 (defun org-publish-projects (projects)
713 "Publish all files belonging to the PROJECTS alist.
714 If `:auto-sitemap' is set, publish the sitemap too. If
715 `:makeindex' is set, also produce a file \"theindex.org\"."
716 (dolist (project (org-publish-expand-projects projects))
717 (let ((project-plist (cdr project)))
718 (let ((fun (plist-get project-plist :preparation-function)))
719 (cond ((consp fun) (dolist (f fun) (funcall f project-plist)))
720 ((functionp fun) (funcall fun project-plist))))
721 ;; Each project uses its own cache file.
722 (org-publish-initialize-cache (car project))
723 (when (plist-get project-plist :auto-sitemap)
724 (let ((sitemap-filename
725 (or (plist-get project-plist :sitemap-filename)
726 "sitemap.org"))
727 (sitemap-function
728 (or (plist-get project-plist :sitemap-function)
729 #'org-publish-org-sitemap))
730 (org-publish-sitemap-date-format
731 (or (plist-get project-plist :sitemap-date-format)
732 org-publish-sitemap-date-format))
733 (org-publish-sitemap-file-entry-format
734 (or (plist-get project-plist :sitemap-file-entry-format)
735 org-publish-sitemap-file-entry-format)))
736 (funcall sitemap-function project sitemap-filename)))
737 ;; Publish all files from PROJECT except "theindex.org". Its
738 ;; publishing will be deferred until "theindex.inc" is
739 ;; populated.
740 (let ((theindex
741 (expand-file-name "theindex.org"
742 (plist-get project-plist :base-directory)))
743 (exclude-regexp (plist-get project-plist :exclude)))
744 (dolist (file (org-publish-get-base-files project exclude-regexp))
745 (unless (equal file theindex) (org-publish-file file project t)))
746 ;; Populate "theindex.inc", if needed, and publish
747 ;; "theindex.org".
748 (when (plist-get project-plist :makeindex)
749 (org-publish-index-generate-theindex
750 project (plist-get project-plist :base-directory))
751 (org-publish-file theindex project t)))
752 (let ((fun (plist-get project-plist :completion-function)))
753 (cond ((consp fun) (dolist (f fun) (funcall f project-plist)))
754 ((functionp fun) (funcall fun project-plist))))
755 (org-publish-write-cache-file))))
757 (defun org-publish-org-sitemap (project &optional sitemap-filename)
758 "Create a sitemap of pages in set defined by PROJECT.
759 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
760 Default for SITEMAP-FILENAME is `sitemap.org'."
761 (let* ((project-plist (cdr project))
762 (dir (file-name-as-directory
763 (plist-get project-plist :base-directory)))
764 (localdir (file-name-directory dir))
765 (indent-str (make-string 2 ?\s))
766 (exclude-regexp (plist-get project-plist :exclude))
767 (files (nreverse
768 (org-publish-get-base-files project exclude-regexp)))
769 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
770 (sitemap-title (or (plist-get project-plist :sitemap-title)
771 (concat "Sitemap for project " (car project))))
772 (sitemap-style (or (plist-get project-plist :sitemap-style)
773 'tree))
774 (sitemap-sans-extension
775 (plist-get project-plist :sitemap-sans-extension))
776 (visiting (find-buffer-visiting sitemap-filename))
777 file sitemap-buffer)
778 (with-current-buffer
779 (let ((org-inhibit-startup t))
780 (setq sitemap-buffer
781 (or visiting (find-file sitemap-filename))))
782 (erase-buffer)
783 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
784 (while (setq file (pop files))
785 (let ((link (file-relative-name file dir))
786 (oldlocal localdir))
787 (when sitemap-sans-extension
788 (setq link (file-name-sans-extension link)))
789 ;; sitemap shouldn't list itself
790 (unless (equal (file-truename sitemap-filename)
791 (file-truename file))
792 (if (eq sitemap-style 'list)
793 (message "Generating list-style sitemap for %s" sitemap-title)
794 (message "Generating tree-style sitemap for %s" sitemap-title)
795 (setq localdir (concat (file-name-as-directory dir)
796 (file-name-directory link)))
797 (unless (string= localdir oldlocal)
798 (if (string= localdir dir)
799 (setq indent-str (make-string 2 ?\ ))
800 (let ((subdirs
801 (split-string
802 (directory-file-name
803 (file-name-directory
804 (file-relative-name localdir dir))) "/"))
805 (subdir "")
806 (old-subdirs (split-string
807 (file-relative-name oldlocal dir) "/")))
808 (setq indent-str (make-string 2 ?\ ))
809 (while (string= (car old-subdirs) (car subdirs))
810 (setq indent-str (concat indent-str (make-string 2 ?\ )))
811 (pop old-subdirs)
812 (pop subdirs))
813 (dolist (d subdirs)
814 (setq subdir (concat subdir d "/"))
815 (insert (concat indent-str " + " d "\n"))
816 (setq indent-str (make-string
817 (+ (length indent-str) 2) ?\ )))))))
818 ;; This is common to 'flat and 'tree
819 (let ((entry
820 (org-publish-format-file-entry
821 org-publish-sitemap-file-entry-format file project-plist))
822 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
823 (cond ((string-match-p regexp entry)
824 (string-match regexp entry)
825 (insert (concat indent-str " + " (match-string 1 entry)
826 "[[file:" link "]["
827 (match-string 2 entry)
828 "]]" (match-string 3 entry) "\n")))
830 (insert (concat indent-str " + [[file:" link "]["
831 entry
832 "]]\n"))))))))
833 (save-buffer))
834 (or visiting (kill-buffer sitemap-buffer))))
836 (defun org-publish-format-file-entry (fmt file project-plist)
837 (format-spec
839 `((?t . ,(org-publish-find-title file t))
840 (?d . ,(format-time-string org-publish-sitemap-date-format
841 (org-publish-find-date file)))
842 (?a . ,(or (plist-get project-plist :author) user-full-name)))))
844 (defun org-publish-find-title (file &optional reset)
845 "Find the title of FILE in project."
847 (and (not reset) (org-publish-cache-get-file-property file :title nil t))
848 (let* ((org-inhibit-startup t)
849 (visiting (find-buffer-visiting file))
850 (buffer (or visiting (find-file-noselect file))))
851 (with-current-buffer buffer
852 (let ((title
853 (let ((property
854 (plist-get
855 ;; protect local variables in open buffers
856 (if visiting
857 (org-export-with-buffer-copy (org-export-get-environment))
858 (org-export-get-environment))
859 :title)))
860 (if property
861 (org-no-properties (org-element-interpret-data property))
862 (file-name-nondirectory (file-name-sans-extension file))))))
863 (unless visiting (kill-buffer buffer))
864 (org-publish-cache-set-file-property file :title title)
865 title)))))
867 (defun org-publish-find-date (file)
868 "Find the date of FILE in project.
869 This function assumes FILE is either a directory or an Org file.
870 If FILE is an Org file and provides a DATE keyword use it. In
871 any other case use the file system's modification time. Return
872 time in `current-time' format."
873 (if (file-directory-p file) (nth 5 (file-attributes file))
874 (let* ((org-inhibit-startup t)
875 (visiting (find-buffer-visiting file))
876 (file-buf (or visiting (find-file-noselect file nil)))
877 (date (plist-get
878 (with-current-buffer file-buf
879 (if visiting
880 (org-export-with-buffer-copy
881 (org-export-get-environment))
882 (org-export-get-environment)))
883 :date)))
884 (unless visiting (kill-buffer file-buf))
885 ;; DATE is a secondary string. If it contains a timestamp,
886 ;; convert it to internal format. Otherwise, use FILE
887 ;; modification time.
888 (cond ((let ((ts (and (consp date) (assq 'timestamp date))))
889 (and ts
890 (let ((value (org-element-interpret-data ts)))
891 (and (org-string-nw-p value)
892 (org-time-string-to-time value))))))
893 ((file-exists-p file) (nth 5 (file-attributes file)))
894 (t (error "No such file: \"%s\"" file))))))
898 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
899 ;;; Interactive publishing functions
901 ;;;###autoload
902 (defalias 'org-publish-project 'org-publish)
904 ;;;###autoload
905 (defun org-publish (project &optional force async)
906 "Publish PROJECT.
908 PROJECT is either a project name, as a string, or a project
909 alist (see `org-publish-project-alist' variable).
911 When optional argument FORCE is non-nil, force publishing all
912 files in PROJECT. With a non-nil optional argument ASYNC,
913 publishing will be done asynchronously, in another process."
914 (interactive
915 (list (assoc (completing-read "Publish project: "
916 org-publish-project-alist nil t)
917 org-publish-project-alist)
918 current-prefix-arg))
919 (let ((project (if (not (stringp project)) project
920 ;; If this function is called in batch mode,
921 ;; PROJECT is still a string here.
922 (assoc project org-publish-project-alist))))
923 (cond
924 ((not project))
925 (async
926 (org-export-async-start (lambda (_) nil)
927 `(let ((org-publish-use-timestamps-flag
928 ,(and (not force) org-publish-use-timestamps-flag)))
929 ;; Expand components right now as external process may not
930 ;; be aware of complete `org-publish-project-alist'.
931 (org-publish-projects
932 ',(org-publish-expand-projects (list project))))))
933 (t (save-window-excursion
934 (let ((org-publish-use-timestamps-flag
935 (and (not force) org-publish-use-timestamps-flag)))
936 (org-publish-projects (list project))))))))
938 ;;;###autoload
939 (defun org-publish-all (&optional force async)
940 "Publish all projects.
941 With prefix argument FORCE, remove all files in the timestamp
942 directory and force publishing all projects. With a non-nil
943 optional argument ASYNC, publishing will be done asynchronously,
944 in another process."
945 (interactive "P")
946 (if async
947 (org-export-async-start (lambda (_) nil)
948 `(progn
949 (when ',force (org-publish-remove-all-timestamps))
950 (let ((org-publish-use-timestamps-flag
951 (if ',force nil ,org-publish-use-timestamps-flag)))
952 (org-publish-projects ',org-publish-project-alist))))
953 (when force (org-publish-remove-all-timestamps))
954 (save-window-excursion
955 (let ((org-publish-use-timestamps-flag
956 (if force nil org-publish-use-timestamps-flag)))
957 (org-publish-projects org-publish-project-alist)))))
960 ;;;###autoload
961 (defun org-publish-current-file (&optional force async)
962 "Publish the current file.
963 With prefix argument FORCE, force publish the file. When
964 optional argument ASYNC is non-nil, publishing will be done
965 asynchronously, in another process."
966 (interactive "P")
967 (let ((file (buffer-file-name (buffer-base-buffer))))
968 (if async
969 (org-export-async-start (lambda (_) nil)
970 `(let ((org-publish-use-timestamps-flag
971 (if ',force nil ,org-publish-use-timestamps-flag)))
972 (org-publish-file ,file)))
973 (save-window-excursion
974 (let ((org-publish-use-timestamps-flag
975 (if force nil org-publish-use-timestamps-flag)))
976 (org-publish-file file))))))
978 ;;;###autoload
979 (defun org-publish-current-project (&optional force async)
980 "Publish the project associated with the current file.
981 With a prefix argument, force publishing of all files in
982 the project."
983 (interactive "P")
984 (save-window-excursion
985 (let ((project (org-publish-get-project-from-filename
986 (buffer-file-name (buffer-base-buffer)) 'up)))
987 (if project (org-publish project force async)
988 (error "File %s is not part of any known project"
989 (buffer-file-name (buffer-base-buffer)))))))
993 ;;; Index generation
995 (defun org-publish-collect-index (output _backend info)
996 "Update index for a file in cache.
998 OUTPUT is the output from transcoding current file. BACKEND is
999 the back-end that was used for transcoding. INFO is a plist
1000 containing publishing and export options.
1002 The index relative to current file is stored as an alist. An
1003 association has the following shape: (TERM FILE-NAME PARENT),
1004 where TERM is the indexed term, as a string, FILE-NAME is the
1005 original full path of the file where the term in encountered, and
1006 PARENT is a reference to the headline, if any, containing the
1007 original index keyword. When non-nil, this reference is a cons
1008 cell. Its CAR is a symbol among `id', `custom-id' and `name' and
1009 its CDR is a string."
1010 (let ((file (plist-get info :input-file)))
1011 (org-publish-cache-set-file-property
1012 file :index
1013 (delete-dups
1014 (org-element-map (plist-get info :parse-tree) 'keyword
1015 (lambda (k)
1016 (when (equal (org-element-property :key k) "INDEX")
1017 (let ((parent (org-export-get-parent-headline k)))
1018 (list (org-element-property :value k)
1019 file
1020 (cond
1021 ((not parent) nil)
1022 ((let ((id (org-element-property :ID parent)))
1023 (and id (cons 'id id))))
1024 ((let ((id (org-element-property :CUSTOM_ID parent)))
1025 (and id (cons 'custom-id id))))
1026 (t (cons 'name
1027 ;; Remove statistics cookie.
1028 (replace-regexp-in-string
1029 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
1030 (org-element-property :raw-value parent)))))))))
1031 info))))
1032 ;; Return output unchanged.
1033 output)
1035 (defun org-publish-index-generate-theindex (project directory)
1036 "Retrieve full index from cache and build \"theindex.org\".
1037 PROJECT is the project the index relates to. DIRECTORY is the
1038 publishing directory."
1039 (let ((all-files (org-publish-get-base-files
1040 project (plist-get (cdr project) :exclude)))
1041 full-index)
1042 ;; Compile full index and sort it alphabetically.
1043 (dolist (file all-files
1044 (setq full-index
1045 (sort (nreverse full-index)
1046 (lambda (a b) (string< (downcase (car a))
1047 (downcase (car b)))))))
1048 (let ((index (org-publish-cache-get-file-property file :index)))
1049 (dolist (term index)
1050 (unless (member term full-index) (push term full-index)))))
1051 ;; Write "theindex.inc" in DIRECTORY.
1052 (with-temp-file (expand-file-name "theindex.inc" directory)
1053 (let ((current-letter nil) (last-entry nil))
1054 (dolist (idx full-index)
1055 (let* ((entry (org-split-string (car idx) "!"))
1056 (letter (upcase (substring (car entry) 0 1)))
1057 ;; Transform file into a path relative to publishing
1058 ;; directory.
1059 (file (file-relative-name
1060 (nth 1 idx)
1061 (plist-get (cdr project) :base-directory))))
1062 ;; Check if another letter has to be inserted.
1063 (unless (string= letter current-letter)
1064 (insert (format "* %s\n" letter)))
1065 ;; Compute the first difference between last entry and
1066 ;; current one: it tells the level at which new items
1067 ;; should be added.
1068 (let* ((rank
1069 (if (equal entry last-entry) (1- (length entry))
1070 (cl-loop for n from 0 to (length entry)
1071 unless (equal (nth n entry) (nth n last-entry))
1072 return n)))
1073 (len (length (nthcdr rank entry))))
1074 ;; For each term after the first difference, create
1075 ;; a new sub-list with the term as body. Moreover,
1076 ;; linkify the last term.
1077 (dotimes (n len)
1078 (insert
1079 (concat
1080 (make-string (* (+ rank n) 2) ?\s) " - "
1081 (if (not (= (1- len) n)) (nth (+ rank n) entry)
1082 ;; Last term: Link it to TARGET, if possible.
1083 (let ((target (nth 2 idx)))
1084 (format
1085 "[[%s][%s]]"
1086 ;; Destination.
1087 (pcase (car target)
1088 (`nil (format "file:%s" file))
1089 (`id (format "id:%s" (cdr target)))
1090 (`custom-id (format "file:%s::#%s" file (cdr target)))
1091 (_ (format "file:%s::*%s" file (cdr target))))
1092 ;; Description.
1093 (car (last entry)))))
1094 "\n"))))
1095 (setq current-letter letter last-entry entry))))
1096 ;; Create "theindex.org", if it doesn't exist yet, and provide
1097 ;; a default index file.
1098 (let ((index.org (expand-file-name "theindex.org" directory)))
1099 (unless (file-exists-p index.org)
1100 (with-temp-file index.org
1101 (insert "#+TITLE: Index\n\n#+INCLUDE: \"theindex.inc\"\n\n")))))))
1105 ;;; External Fuzzy Links Resolution
1107 ;; This part implements tools to resolve [[file.org::*Some headline]]
1108 ;; links, where "file.org" belongs to the current project.
1110 (defun org-publish--store-crossrefs (output _backend info)
1111 "Store cross-references for current published file.
1113 OUPUT is the produced output, as a string. BACKEND is the export
1114 back-end used, as a symbol. INFO is the final export state, as
1115 a plist.
1117 This function is meant to be used as a final output filter. See
1118 `org-publish-org-to'."
1119 (org-publish-cache-set-file-property
1120 (plist-get info :input-file) :crossrefs
1121 ;; Update `:crossrefs' so as to remove unused references and search
1122 ;; cells. Actually used references are extracted from
1123 ;; `:internal-references', with references as strings removed. See
1124 ;; `org-export-get-reference' for details.
1125 (cl-remove-if (lambda (pair) (stringp (car pair)))
1126 (plist-get info :internal-references)))
1127 ;; Return output unchanged.
1128 output)
1130 (defun org-publish-resolve-external-link (search file)
1131 "Return reference for element matching string SEARCH in FILE.
1133 Return value is an internal reference, as a string.
1135 This function allows resolving external links with a search
1136 option, e.g.,
1138 [[file.org::*heading][description]]
1139 [[file.org::#custom-id][description]]
1140 [[file.org::fuzzy][description]]
1142 It only makes sense to use this if export back-end builds
1143 references with `org-export-get-reference'."
1144 (if (not org-publish-cache)
1145 (progn
1146 (message "Reference %S in file %S cannot be resolved without publishing"
1147 search
1148 file)
1149 "MissingReference")
1150 (let* ((filename (expand-file-name file))
1151 (crossrefs
1152 (org-publish-cache-get-file-property filename :crossrefs nil t))
1153 (cells (org-export-string-to-search-cell search)))
1155 ;; Look for reference associated to search cells triggered by
1156 ;; LINK. It can match when targeted file has been published
1157 ;; already.
1158 (let ((known (cdr (cl-some (lambda (c) (assoc c crossrefs)) cells))))
1159 (and known (org-export-format-reference known)))
1160 ;; Search cell is unknown so far. Generate a new internal
1161 ;; reference that will be used when the targeted file will be
1162 ;; published.
1163 (let ((new (org-export-new-reference crossrefs)))
1164 (dolist (cell cells) (push (cons cell new) crossrefs))
1165 (org-publish-cache-set-file-property filename :crossrefs crossrefs)
1166 (org-export-format-reference new))))))
1170 ;;; Caching functions
1172 (defun org-publish-write-cache-file (&optional free-cache)
1173 "Write `org-publish-cache' to file.
1174 If FREE-CACHE, empty the cache."
1175 (unless org-publish-cache
1176 (error "`org-publish-write-cache-file' called, but no cache present"))
1178 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1179 (unless cache-file
1180 (error "Cannot find cache-file name in `org-publish-write-cache-file'"))
1181 (with-temp-file cache-file
1182 (let (print-level print-length)
1183 (insert "(setq org-publish-cache \
1184 \(make-hash-table :test 'equal :weakness nil :size 100))\n")
1185 (maphash (lambda (k v)
1186 (insert
1187 (format "(puthash %S %s%S org-publish-cache)\n"
1188 k (if (or (listp v) (symbolp v)) "'" "") v)))
1189 org-publish-cache)))
1190 (when free-cache (org-publish-reset-cache))))
1192 (defun org-publish-initialize-cache (project-name)
1193 "Initialize the projects cache if not initialized yet and return it."
1195 (unless project-name
1196 (error "Cannot initialize `org-publish-cache' without projects name in \
1197 `org-publish-initialize-cache'"))
1199 (unless (file-exists-p org-publish-timestamp-directory)
1200 (make-directory org-publish-timestamp-directory t))
1201 (unless (file-directory-p org-publish-timestamp-directory)
1202 (error "Org publish timestamp: %s is not a directory"
1203 org-publish-timestamp-directory))
1205 (unless (and org-publish-cache
1206 (string= (org-publish-cache-get ":project:") project-name))
1207 (let* ((cache-file
1208 (concat
1209 (expand-file-name org-publish-timestamp-directory)
1210 project-name ".cache"))
1211 (cexists (file-exists-p cache-file)))
1213 (when org-publish-cache (org-publish-reset-cache))
1215 (if cexists (load-file cache-file)
1216 (setq org-publish-cache
1217 (make-hash-table :test 'equal :weakness nil :size 100))
1218 (org-publish-cache-set ":project:" project-name)
1219 (org-publish-cache-set ":cache-file:" cache-file))
1220 (unless cexists (org-publish-write-cache-file nil))))
1221 org-publish-cache)
1223 (defun org-publish-reset-cache ()
1224 "Empty org-publish-cache and reset it nil."
1225 (message "%s" "Resetting org-publish-cache")
1226 (when (hash-table-p org-publish-cache)
1227 (clrhash org-publish-cache))
1228 (setq org-publish-cache nil))
1230 (defun org-publish-cache-file-needs-publishing
1231 (filename &optional pub-dir pub-func _base-dir)
1232 "Check the timestamp of the last publishing of FILENAME.
1233 Return non-nil if the file needs publishing. Also check if
1234 any included files have been more recently published, so that
1235 the file including them will be republished as well."
1236 (unless org-publish-cache
1237 (error
1238 "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1239 (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func))
1240 (pstamp (org-publish-cache-get key))
1241 (org-inhibit-startup t)
1242 included-files-ctime)
1243 (when (equal (file-name-extension filename) "org")
1244 (let ((visiting (find-buffer-visiting filename))
1245 (buf (find-file-noselect filename))
1246 (case-fold-search t))
1247 (unwind-protect
1248 (with-current-buffer buf
1249 (goto-char (point-min))
1250 (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t)
1251 (let* ((element (org-element-at-point))
1252 (included-file
1253 (and (eq (org-element-type element) 'keyword)
1254 (let ((value (org-element-property :value element)))
1255 (and value
1256 (string-match
1257 "\\`\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)"
1258 value)
1259 (let ((m (match-string 1 value)))
1260 (org-unbracket-string
1261 "\"" "\""
1262 ;; Ignore search suffix.
1263 (if (string-match "::.*?\"?\\'" m)
1264 (substring m 0 (match-beginning 0))
1265 m))))))))
1266 (when included-file
1267 (push (org-publish-cache-ctime-of-src
1268 (expand-file-name included-file))
1269 included-files-ctime)))))
1270 (unless visiting (kill-buffer buf)))))
1271 (or (null pstamp)
1272 (let ((ctime (org-publish-cache-ctime-of-src filename)))
1273 (or (< pstamp ctime)
1274 (cl-some (lambda (ct) (< ctime ct)) included-files-ctime))))))
1276 (defun org-publish-cache-set-file-property
1277 (filename property value &optional project-name)
1278 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1279 Use cache file of PROJECT-NAME. If the entry does not exist, it
1280 will be created. Return VALUE."
1281 ;; Evtl. load the requested cache file:
1282 (if project-name (org-publish-initialize-cache project-name))
1283 (let ((pl (org-publish-cache-get filename)))
1284 (if pl (progn (plist-put pl property value) value)
1285 (org-publish-cache-get-file-property
1286 filename property value nil project-name))))
1288 (defun org-publish-cache-get-file-property
1289 (filename property &optional default no-create project-name)
1290 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1291 Use cache file of PROJECT-NAME. Return the value of that PROPERTY,
1292 or DEFAULT, if the value does not yet exist. Create the entry,
1293 if necessary, unless NO-CREATE is non-nil."
1294 ;; Evtl. load the requested cache file:
1295 (if project-name (org-publish-initialize-cache project-name))
1296 (let ((pl (org-publish-cache-get filename)) retval)
1297 (if pl
1298 (if (plist-member pl property)
1299 (setq retval (plist-get pl property))
1300 (setq retval default))
1301 ;; no pl yet:
1302 (unless no-create
1303 (org-publish-cache-set filename (list property default)))
1304 (setq retval default))
1305 retval))
1307 (defun org-publish-cache-get (key)
1308 "Return the value stored in `org-publish-cache' for key KEY.
1309 Return nil, if no value or nil is found. Raise an error if the
1310 cache does not exist."
1311 (unless org-publish-cache
1312 (error "`org-publish-cache-get' called, but no cache present"))
1313 (gethash key org-publish-cache))
1315 (defun org-publish-cache-set (key value)
1316 "Store KEY VALUE pair in `org-publish-cache'.
1317 Returns value on success, else nil. Raise an error if the cache
1318 does not exist."
1319 (unless org-publish-cache
1320 (error "`org-publish-cache-set' called, but no cache present"))
1321 (puthash key value org-publish-cache))
1323 (defun org-publish-cache-ctime-of-src (file)
1324 "Get the ctime of FILE as an integer."
1325 (let ((attr (file-attributes
1326 (expand-file-name (or (file-symlink-p file) file)
1327 (file-name-directory file)))))
1328 (if (not attr) (error "No such file: \"%s\"" file)
1329 (+ (lsh (car (nth 5 attr)) 16)
1330 (cadr (nth 5 attr))))))
1333 (provide 'ox-publish)
1335 ;; Local variables:
1336 ;; generated-autoload-file: "org-loaddefs.el"
1337 ;; End:
1339 ;;; ox-publish.el ends here