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