1 ;;; ox-publish.el --- Publish Related Org Mode Files as a Website -*- lexical-binding: t; -*-
2 ;; Copyright (C) 2006-2016 Free Software Foundation, Inc.
4 ;; Author: David O'Toole <dto@gnu.org>
5 ;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
6 ;; Keywords: hypermedia, outlines, wp
8 ;; This file is part of GNU Emacs.
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/>.
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.
42 (require 'format-spec
)
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
63 (defgroup org-publish nil
64 "Options for publishing a set of files."
68 (defcustom org-publish-project-alist nil
69 "Association list to control publishing behavior.
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
79 (:property value :property value ... )
81 2. A meta-project definition, specifying of a list of
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:
101 Directory containing publishing source files.
105 Extension (without the dot!) of source files. This can be
106 a regular expression. If not given, \"org\" will be used as
109 `:publishing-directory'
111 Directory (possibly remote) where output files will be
114 If `:recursive' is non-nil files in sub-directories of
115 `:base-directory' are considered.
117 The `:exclude' property may be used to prevent certain files from
118 being published. Its value may be a string or regexp matching
119 file names you don't want to be published.
121 The `:include' property may be used to include extra files. Its
122 value may be a list of filenames to include. The filenames are
123 considered relative to the base directory.
125 When both `:include' and `:exclude' properties are given values,
126 the exclusion step happens first.
128 One special property controls which back-end function to use for
129 publishing files in the project. This can be used to extend the
130 set of file types publishable by `org-publish', as well as the
131 set of output formats.
133 `:publishing-function'
135 Function to publish file. Each back-end may define its
136 own (i.e. `org-latex-publish-to-pdf',
137 `org-html-publish-to-html'). May be a list of functions, in
138 which case each function in the list is invoked in turn.
140 Another property allows you to insert code that prepares
141 a project for publishing. For example, you could call GNU Make
142 on a certain makefile, to ensure published files are built up to
145 `:preparation-function'
147 Function to be called before publishing this project. This
148 may also be a list of functions. Preparation functions are
149 called with the project properties list as their sole
152 `:completion-function'
154 Function to be called after publishing this project. This
155 may also be a list of functions. Completion functions are
156 called with the project properties list as their sole
159 Some properties control details of the Org publishing process,
160 and are equivalent to the corresponding user variables listed in
161 the right column. Back-end specific properties may also be
162 included. See the back-end documentation for more information.
164 :author `user-full-name'
165 :creator `org-export-creator-string'
166 :email `user-mail-address'
167 :exclude-tags `org-export-exclude-tags'
168 :headline-levels `org-export-headline-levels'
169 :language `org-export-default-language'
170 :preserve-breaks `org-export-preserve-breaks'
171 :section-numbers `org-export-with-section-numbers'
172 :select-tags `org-export-select-tags'
173 :time-stamp-file `org-export-time-stamp-file'
174 :with-archived-trees `org-export-with-archived-trees'
175 :with-author `org-export-with-author'
176 :with-creator `org-export-with-creator'
177 :with-date `org-export-with-date'
178 :with-drawers `org-export-with-drawers'
179 :with-email `org-export-with-email'
180 :with-emphasize `org-export-with-emphasize'
181 :with-entities `org-export-with-entities'
182 :with-fixed-width `org-export-with-fixed-width'
183 :with-footnotes `org-export-with-footnotes'
184 :with-inlinetasks `org-export-with-inlinetasks'
185 :with-latex `org-export-with-latex'
186 :with-planning `org-export-with-planning'
187 :with-priority `org-export-with-priority'
188 :with-properties `org-export-with-properties'
189 :with-smart-quotes `org-export-with-smart-quotes'
190 :with-special-strings `org-export-with-special-strings'
191 :with-statistics-cookies' `org-export-with-statistics-cookies'
192 :with-sub-superscript `org-export-with-sub-superscripts'
193 :with-toc `org-export-with-toc'
194 :with-tables `org-export-with-tables'
195 :with-tags `org-export-with-tags'
196 :with-tasks `org-export-with-tasks'
197 :with-timestamps `org-export-with-timestamps'
198 :with-title `org-export-with-title'
199 :with-todo-keywords `org-export-with-todo-keywords'
201 The following properties may be used to control publishing of
202 a site-map of files or summary page for a given project.
206 Whether to publish a site-map during
207 `org-publish-current-project' or `org-publish-all'.
211 Filename for output of sitemap. Defaults to \"sitemap.org\".
215 Title of site-map page. Defaults to name of file.
219 Plugin function to use for generation of site-map. Defaults
220 to `org-publish-org-sitemap', which generates a plain list of
221 links to all files in the project.
225 Can be `list' (site-map is just an itemized list of the
226 titles of the files involved) or `tree' (the directory
227 structure of the source files is reflected in the site-map).
230 `:sitemap-sans-extension'
232 Remove extension from site-map's file-names. Useful to have
233 cool URIs (see http://www.w3.org/Provider/Style/URI).
236 If you create a site-map file, adjust the sorting like this:
238 `:sitemap-sort-folders'
240 Where folders should appear in the site-map. Set this to
241 `first' (default) or `last' to display folders first or last,
242 respectively. Any other value will mix files and folders.
244 `:sitemap-sort-files'
246 The site map is normally sorted alphabetically. You can
247 change this behavior setting this to `anti-chronologically',
248 `chronologically', or nil.
250 `:sitemap-ignore-case'
252 Should sorting be case-sensitive? Default nil.
254 The following property control the creation of a concept index.
258 Create a concept index. The file containing the index has to
259 be called \"theindex.org\". If it doesn't exist in the
260 project, it will be generated. Contents of the index are
261 stored in the file \"theindex.inc\", which can be included in
264 Other properties affecting publication.
268 Set this to t to publish only the body of the documents."
269 :group
'org-export-publish
272 (defcustom org-publish-use-timestamps-flag t
273 "Non-nil means use timestamp checking to publish only changed files.
274 When nil, do no timestamp checking and always publish all files."
275 :group
'org-export-publish
278 (defcustom org-publish-timestamp-directory
279 (convert-standard-filename "~/.org-timestamps/")
280 "Name of directory in which to store publishing timestamps."
281 :group
'org-export-publish
284 (defcustom org-publish-list-skipped-files t
285 "Non-nil means show message about files *not* published."
286 :group
'org-export-publish
289 (defcustom org-publish-sitemap-sort-files
'alphabetically
290 "Method to sort files in site-maps.
291 Possible values are `alphabetically', `chronologically',
292 `anti-chronologically' and nil.
294 If `alphabetically', files will be sorted alphabetically. If
295 `chronologically', files will be sorted with older modification
296 time first. If `anti-chronologically', files will be sorted with
297 newer modification time first. nil won't sort files.
299 You can overwrite this default per project in your
300 `org-publish-project-alist', using `:sitemap-sort-files'."
301 :group
'org-export-publish
304 (defcustom org-publish-sitemap-sort-folders
'first
305 "A symbol, denoting if folders are sorted first in sitemaps.
306 Possible values are `first', `last', and nil.
307 If `first', folders will be sorted before files.
308 If `last', folders are sorted to the end after the files.
309 Any other value will not mix files and folders.
311 You can overwrite this default per project in your
312 `org-publish-project-alist', using `:sitemap-sort-folders'."
313 :group
'org-export-publish
316 (defcustom org-publish-sitemap-sort-ignore-case nil
317 "Non-nil when site-map sorting should ignore case.
319 You can overwrite this default per project in your
320 `org-publish-project-alist', using `:sitemap-ignore-case'."
321 :group
'org-export-publish
324 (defcustom org-publish-sitemap-date-format
"%Y-%m-%d"
325 "Format for printing a date in the sitemap.
326 See `format-time-string' for allowed formatters."
327 :group
'org-export-publish
330 (defcustom org-publish-sitemap-file-entry-format
"%t"
331 "Format string for site-map file entry.
332 You could use brackets to delimit on what part the link will be.
336 %d is the date formatted using `org-publish-sitemap-date-format'."
337 :group
'org-export-publish
342 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
343 ;;; Timestamp-related functions
345 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func
)
346 "Return path to timestamp file for filename FILENAME."
347 (setq filename
(concat filename
"::" (or pub-dir
"") "::"
348 (format "%s" (or pub-func
""))))
349 (concat "X" (if (fboundp 'sha1
) (sha1 filename
) (md5 filename
))))
351 (defun org-publish-needed-p
352 (filename &optional pub-dir pub-func _true-pub-dir base-dir
)
353 "Non-nil if FILENAME should be published in PUB-DIR using PUB-FUNC.
354 TRUE-PUB-DIR is where the file will truly end up. Currently we
355 are not using this - maybe it can eventually be used to check if
356 the file is present at the target location, and how old it is.
357 Right now we cannot do this, because we do not know under what
358 file name the file will be stored - the publishing function can
359 still decide about that independently."
360 (let ((rtn (if (not org-publish-use-timestamps-flag
) t
361 (org-publish-cache-file-needs-publishing
362 filename pub-dir pub-func base-dir
))))
363 (if rtn
(message "Publishing file %s using `%s'" filename pub-func
)
364 (when org-publish-list-skipped-files
365 (message "Skipping unmodified file %s" filename
)))
368 (defun org-publish-update-timestamp
369 (filename &optional pub-dir pub-func _base-dir
)
370 "Update publishing timestamp for file FILENAME.
371 If there is no timestamp, create one."
372 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func
))
373 (stamp (org-publish-cache-ctime-of-src filename
)))
374 (org-publish-cache-set key stamp
)))
376 (defun org-publish-remove-all-timestamps ()
377 "Remove all files in the timestamp directory."
378 (let ((dir org-publish-timestamp-directory
))
379 (when (and (file-exists-p dir
) (file-directory-p dir
))
380 (mapc #'delete-file
(directory-files dir
'full
"[^.]\\'"))
381 (org-publish-reset-cache))))
385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
386 ;;; Getting project information out of `org-publish-project-alist'
388 (defun org-publish-expand-projects (projects-alist)
389 "Expand projects in PROJECTS-ALIST.
390 This splices all the components into the list."
391 (let ((rest projects-alist
) rtn p components
)
392 (while (setq p
(pop rest
))
393 (if (setq components
(plist-get (cdr p
) :components
))
395 (mapcar (lambda (x) (assoc x org-publish-project-alist
))
399 (nreverse (delete-dups (delq nil rtn
)))))
401 (defvar org-publish-sitemap-sort-files
)
402 (defvar org-publish-sitemap-sort-folders
)
403 (defvar org-publish-sitemap-ignore-case
)
404 (defvar org-publish-sitemap-requested
)
405 (defvar org-publish-sitemap-date-format
)
406 (defvar org-publish-sitemap-file-entry-format
)
407 (defun org-publish-compare-directory-files (a b
)
408 "Predicate for `sort', that sorts folders and files for sitemap."
410 (when (or org-publish-sitemap-sort-files org-publish-sitemap-sort-folders
)
411 ;; First we sort files:
412 (when org-publish-sitemap-sort-files
413 (pcase org-publish-sitemap-sort-files
415 (let* ((adir (file-directory-p a
))
416 (aorg (and (string-suffix-p ".org" a
) (not adir
)))
417 (bdir (file-directory-p b
))
418 (borg (and (string-suffix-p ".org" b
) (not bdir
)))
419 (A (if aorg
(concat (file-name-directory a
)
420 (org-publish-find-title a
)) a
))
421 (B (if borg
(concat (file-name-directory b
)
422 (org-publish-find-title b
)) b
)))
423 (setq retval
(if org-publish-sitemap-ignore-case
424 (not (string-lessp (upcase B
) (upcase A
)))
425 (not (string-lessp B A
))))))
426 ((or `anti-chronologically
`chronologically
)
427 (let* ((adate (org-publish-find-date a
))
428 (bdate (org-publish-find-date b
))
429 (A (+ (lsh (car adate
) 16) (cadr adate
)))
430 (B (+ (lsh (car bdate
) 16) (cadr bdate
))))
432 (if (eq org-publish-sitemap-sort-files
'chronologically
)
435 ;; Directory-wise wins:
436 (when org-publish-sitemap-sort-folders
437 ;; a is directory, b not:
439 ((and (file-directory-p a
) (not (file-directory-p b
)))
440 (setq retval
(eq org-publish-sitemap-sort-folders
'first
)))
441 ;; a is not a directory, but b is:
442 ((and (not (file-directory-p a
)) (file-directory-p b
))
443 (setq retval
(eq org-publish-sitemap-sort-folders
'last
))))))
446 (defun org-publish-get-base-files-1
447 (base-dir &optional recurse match skip-file skip-dir
)
448 "Set `org-publish-temp-files' with files from BASE-DIR directory.
449 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
450 non-nil, restrict this list to the files matching the regexp
451 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
452 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
453 matching the regexp SKIP-DIR when recursing through BASE-DIR."
454 (let ((all-files (if (not recurse
) (directory-files base-dir t match
)
455 ;; If RECURSE is non-nil, we want all files
456 ;; matching MATCH and sub-directories.
459 (or (file-directory-p file
)
460 (and match
(string-match match file
))))
461 (directory-files base-dir t
)))))
462 (dolist (f (if (not org-publish-sitemap-requested
) all-files
463 (sort all-files
#'org-publish-compare-directory-files
)))
464 (let ((fd-p (file-directory-p f
))
465 (fnd (file-name-nondirectory f
)))
466 (if (and fd-p recurse
467 (not (string-match "^\\.+$" fnd
))
468 (if skip-dir
(not (string-match skip-dir fnd
)) t
))
469 (org-publish-get-base-files-1
470 f recurse match skip-file skip-dir
)
471 (unless (or fd-p
; This is a directory.
472 (and skip-file
(string-match skip-file fnd
))
473 (not (file-exists-p (file-truename f
)))
474 (not (string-match match fnd
)))
475 (cl-pushnew f org-publish-temp-files
)))))))
477 (defun org-publish-get-base-files (project &optional exclude-regexp
)
478 "Return a list of all files in PROJECT.
479 If EXCLUDE-REGEXP is set, this will be used to filter out
481 (let* ((project-plist (cdr project
))
482 (base-dir (file-name-as-directory
483 (plist-get project-plist
:base-directory
)))
484 (include-list (plist-get project-plist
:include
))
485 (recurse (plist-get project-plist
:recursive
))
486 (extension (or (plist-get project-plist
:base-extension
) "org"))
487 ;; sitemap-... variables are dynamically scoped for
488 ;; org-publish-compare-directory-files:
489 (org-publish-sitemap-requested
490 (plist-get project-plist
:auto-sitemap
))
492 (or (plist-get project-plist
:sitemap-filename
) "sitemap.org"))
493 (org-publish-sitemap-sort-folders
494 (if (plist-member project-plist
:sitemap-sort-folders
)
495 (plist-get project-plist
:sitemap-sort-folders
)
496 org-publish-sitemap-sort-folders
))
497 (org-publish-sitemap-sort-files
498 (cond ((plist-member project-plist
:sitemap-sort-files
)
499 (plist-get project-plist
:sitemap-sort-files
))
500 ;; For backward compatibility:
501 ((plist-member project-plist
:sitemap-alphabetically
)
502 (if (plist-get project-plist
:sitemap-alphabetically
)
503 'alphabetically nil
))
504 (t org-publish-sitemap-sort-files
)))
505 (org-publish-sitemap-ignore-case
506 (if (plist-member project-plist
:sitemap-ignore-case
)
507 (plist-get project-plist
:sitemap-ignore-case
)
508 org-publish-sitemap-sort-ignore-case
))
509 (match (if (eq extension
'any
) "^[^\\.]"
510 (concat "^[^\\.].*\\.\\(" extension
"\\)$"))))
511 ;; Make sure `org-publish-sitemap-sort-folders' has an accepted
513 (unless (memq org-publish-sitemap-sort-folders
'(first last
))
514 (setq org-publish-sitemap-sort-folders nil
))
516 (setq org-publish-temp-files nil
)
517 (when org-publish-sitemap-requested
518 (cl-pushnew (expand-file-name (concat base-dir sitemap-filename
))
519 org-publish-temp-files
))
520 (org-publish-get-base-files-1 base-dir recurse match
521 ;; FIXME distinguish exclude regexp
522 ;; for skip-file and skip-dir?
523 exclude-regexp exclude-regexp
)
524 (dolist (f include-list org-publish-temp-files
)
525 (cl-pushnew (expand-file-name (concat base-dir f
))
526 org-publish-temp-files
))))
528 (defun org-publish-get-project-from-filename (filename &optional up
)
529 "Return the project that FILENAME belongs to."
530 (let* ((filename (expand-file-name filename
))
534 (dolist (prj org-publish-project-alist
)
535 (unless (plist-get (cdr prj
) :components
)
536 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
537 (let* ((r (plist-get (cdr prj
) :recursive
))
538 (b (expand-file-name (file-name-as-directory
539 (plist-get (cdr prj
) :base-directory
))))
540 (x (or (plist-get (cdr prj
) :base-extension
) "org"))
541 (e (plist-get (cdr prj
) :exclude
))
542 (i (plist-get (cdr prj
) :include
))
543 (xm (concat "^" b
(if r
".+" "[^/]+") "\\.\\(" x
"\\)$")))
547 (dolist (file i
) (expand-file-name file b
))))
548 (and (not (and e
(string-match e filename
)))
549 (string-match xm filename
)))
550 (setq project-name
(car prj
))
551 (throw 'p-found project-name
))))))
553 (dolist (prj org-publish-project-alist
)
554 (if (member project-name
(plist-get (cdr prj
) :components
))
555 (setq project-name
(car prj
)))))
556 (assoc project-name org-publish-project-alist
)))
560 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
561 ;;; Tools for publishing functions in back-ends
563 (defun org-publish-org-to (backend filename extension plist
&optional pub-dir
)
564 "Publish an Org file to a specified back-end.
566 BACKEND is a symbol representing the back-end used for
567 transcoding. FILENAME is the filename of the Org file to be
568 published. EXTENSION is the extension used for the output
569 string, with the leading dot. PLIST is the property list for the
572 Optional argument PUB-DIR, when non-nil is the publishing
575 Return output file name."
576 (unless (or (not pub-dir
) (file-exists-p pub-dir
)) (make-directory pub-dir t
))
577 ;; Check if a buffer visiting FILENAME is already open.
578 (let* ((org-inhibit-startup t
)
579 (visiting (find-buffer-visiting filename
))
580 (work-buffer (or visiting
(find-file-noselect filename
))))
582 (with-current-buffer work-buffer
583 (let ((output (org-export-output-file-name extension nil pub-dir
)))
584 (org-export-to-file backend output
585 nil nil nil
(plist-get plist
:body-only
)
586 ;; Add `org-publish--store-crossrefs' and
587 ;; `org-publish-collect-index' to final output filters.
588 ;; The latter isn't dependent on `:makeindex', since we
589 ;; want to keep it up-to-date in cache anyway.
593 ,(org-publish-cache-get-file-property
594 (expand-file-name filename
) :crossrefs nil t
)
596 (org-publish--store-crossrefs
597 org-publish-collect-index
598 ,@(plist-get plist
:filter-final-output
)))))))
599 ;; Remove opened buffer in the process.
600 (unless visiting
(kill-buffer work-buffer
)))))
602 (defun org-publish-attachment (_plist filename pub-dir
)
603 "Publish a file with no transformation of any kind.
605 FILENAME is the filename of the Org file to be published. PLIST
606 is the property list for the given project. PUB-DIR is the
607 publishing directory.
609 Return output file name."
610 (unless (file-directory-p pub-dir
)
611 (make-directory pub-dir t
))
612 (let ((output (expand-file-name (file-name-nondirectory filename
) pub-dir
)))
613 (or (equal (expand-file-name (file-name-directory filename
))
614 (file-name-as-directory (expand-file-name pub-dir
)))
615 (copy-file filename output t
))
621 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
622 ;;; Publishing files, sets of files, and indices
624 (defun org-publish-file (filename &optional project no-cache
)
625 "Publish file FILENAME from PROJECT.
626 If NO-CACHE is not nil, do not initialize `org-publish-cache'.
627 This is needed, since this function is used to publish single
628 files, when entire projects are published (see
629 `org-publish-projects')."
632 (or (org-publish-get-project-from-filename filename
)
633 (error "File %s not part of any known project"
634 (abbreviate-file-name filename
)))))
635 (project-plist (cdr project
))
636 (ftname (expand-file-name filename
))
638 (let ((fun (plist-get project-plist
:publishing-function
)))
639 (cond ((null fun
) (error "No publishing function chosen"))
643 (file-name-as-directory
645 (or (plist-get project-plist
:base-directory
)
646 (error "Project %s does not have :base-directory defined"
649 (file-name-as-directory
651 (or (eval (plist-get project-plist
:publishing-directory
))
652 (error "Project %s does not have :publishing-directory defined"
656 (unless no-cache
(org-publish-initialize-cache (car project
)))
661 (and (string-match (regexp-quote base-dir
) ftname
)
662 (substring ftname
(match-end 0))))))
663 ;; Allow chain of publishing functions.
664 (dolist (f publishing-function
)
665 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir base-dir
)
666 (let ((output (funcall f project-plist filename tmp-pub-dir
)))
667 (org-publish-update-timestamp filename pub-dir f base-dir
)
668 (run-hook-with-args 'org-publish-after-publishing-hook
671 ;; Make sure to write cache to file after successfully publishing
672 ;; a file, so as to minimize impact of a publishing failure.
673 (org-publish-write-cache-file)))
675 (defun org-publish-projects (projects)
676 "Publish all files belonging to the PROJECTS alist.
677 If `:auto-sitemap' is set, publish the sitemap too. If
678 `:makeindex' is set, also produce a file \"theindex.org\"."
679 (dolist (project (org-publish-expand-projects projects
))
680 (let ((project-plist (cdr project
)))
681 (let ((fun (plist-get project-plist
:preparation-function
)))
682 (cond ((consp fun
) (dolist (f fun
) (funcall f project-plist
)))
683 ((functionp fun
) (funcall fun project-plist
))))
684 ;; Each project uses its own cache file.
685 (org-publish-initialize-cache (car project
))
686 (when (plist-get project-plist
:auto-sitemap
)
687 (let ((sitemap-filename
688 (or (plist-get project-plist
:sitemap-filename
)
691 (or (plist-get project-plist
:sitemap-function
)
692 #'org-publish-org-sitemap
))
693 (org-publish-sitemap-date-format
694 (or (plist-get project-plist
:sitemap-date-format
)
695 org-publish-sitemap-date-format
))
696 (org-publish-sitemap-file-entry-format
697 (or (plist-get project-plist
:sitemap-file-entry-format
)
698 org-publish-sitemap-file-entry-format
)))
699 (funcall sitemap-function project sitemap-filename
)))
700 ;; Publish all files from PROJECT excepted "theindex.org". Its
701 ;; publishing will be deferred until "theindex.inc" is
704 (expand-file-name "theindex.org"
705 (plist-get project-plist
:base-directory
)))
706 (exclude-regexp (plist-get project-plist
:exclude
)))
707 (dolist (file (org-publish-get-base-files project exclude-regexp
))
708 (unless (equal file theindex
) (org-publish-file file project t
)))
709 ;; Populate "theindex.inc", if needed, and publish
711 (when (plist-get project-plist
:makeindex
)
712 (org-publish-index-generate-theindex
713 project
(plist-get project-plist
:base-directory
))
714 (org-publish-file theindex project t
)))
715 (let ((fun (plist-get project-plist
:completion-function
)))
716 (cond ((consp fun
) (dolist (f fun
) (funcall f project-plist
)))
717 ((functionp fun
) (funcall fun project-plist
))))
718 (org-publish-write-cache-file))))
720 (defun org-publish-org-sitemap (project &optional sitemap-filename
)
721 "Create a sitemap of pages in set defined by PROJECT.
722 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
723 Default for SITEMAP-FILENAME is `sitemap.org'."
724 (let* ((project-plist (cdr project
))
725 (dir (file-name-as-directory
726 (plist-get project-plist
:base-directory
)))
727 (localdir (file-name-directory dir
))
728 (indent-str (make-string 2 ?\
))
729 (exclude-regexp (plist-get project-plist
:exclude
))
731 (org-publish-get-base-files project exclude-regexp
)))
732 (sitemap-filename (concat dir
(or sitemap-filename
"sitemap.org")))
733 (sitemap-title (or (plist-get project-plist
:sitemap-title
)
734 (concat "Sitemap for project " (car project
))))
735 (sitemap-style (or (plist-get project-plist
:sitemap-style
)
737 (sitemap-sans-extension
738 (plist-get project-plist
:sitemap-sans-extension
))
739 (visiting (find-buffer-visiting sitemap-filename
))
742 (let ((org-inhibit-startup t
))
744 (or visiting
(find-file sitemap-filename
))))
746 (insert (concat "#+TITLE: " sitemap-title
"\n\n"))
747 (while (setq file
(pop files
))
748 (let ((link (file-relative-name file dir
))
750 (when sitemap-sans-extension
751 (setq link
(file-name-sans-extension link
)))
752 ;; sitemap shouldn't list itself
753 (unless (equal (file-truename sitemap-filename
)
754 (file-truename file
))
755 (if (eq sitemap-style
'list
)
756 (message "Generating list-style sitemap for %s" sitemap-title
)
757 (message "Generating tree-style sitemap for %s" sitemap-title
)
758 (setq localdir
(concat (file-name-as-directory dir
)
759 (file-name-directory link
)))
760 (unless (string= localdir oldlocal
)
761 (if (string= localdir dir
)
762 (setq indent-str
(make-string 2 ?\
))
767 (file-relative-name localdir dir
))) "/"))
769 (old-subdirs (split-string
770 (file-relative-name oldlocal dir
) "/")))
771 (setq indent-str
(make-string 2 ?\
))
772 (while (string= (car old-subdirs
) (car subdirs
))
773 (setq indent-str
(concat indent-str
(make-string 2 ?\
)))
777 (setq subdir
(concat subdir d
"/"))
778 (insert (concat indent-str
" + " d
"\n"))
779 (setq indent-str
(make-string
780 (+ (length indent-str
) 2) ?\
)))))))
781 ;; This is common to 'flat and 'tree
783 (org-publish-format-file-entry
784 org-publish-sitemap-file-entry-format file project-plist
))
785 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
786 (cond ((string-match-p regexp entry
)
787 (string-match regexp entry
)
788 (insert (concat indent-str
" + " (match-string 1 entry
)
790 (match-string 2 entry
)
791 "]]" (match-string 3 entry
) "\n")))
793 (insert (concat indent-str
" + [[file:" link
"]["
797 (or visiting
(kill-buffer sitemap-buffer
))))
799 (defun org-publish-format-file-entry (fmt file project-plist
)
802 `((?t .
,(org-publish-find-title file t
))
803 (?d .
,(format-time-string org-publish-sitemap-date-format
804 (org-publish-find-date file
)))
805 (?a .
,(or (plist-get project-plist
:author
) user-full-name
)))))
807 (defun org-publish-find-title (file &optional reset
)
808 "Find the title of FILE in project."
810 (and (not reset
) (org-publish-cache-get-file-property file
:title nil t
))
811 (let* ((org-inhibit-startup t
)
812 (visiting (find-buffer-visiting file
))
813 (buffer (or visiting
(find-file-noselect file
))))
814 (with-current-buffer buffer
818 ;; protect local variables in open buffers
820 (org-export-with-buffer-copy (org-export-get-environment))
821 (org-export-get-environment))
824 (org-no-properties (org-element-interpret-data property
))
825 (file-name-nondirectory (file-name-sans-extension file
))))))
826 (unless visiting
(kill-buffer buffer
))
827 (org-publish-cache-set-file-property file
:title title
)
830 (defun org-publish-find-date (file)
831 "Find the date of FILE in project.
832 This function assumes FILE is either a directory or an Org file.
833 If FILE is an Org file and provides a DATE keyword use it. In
834 any other case use the file system's modification time. Return
835 time in `current-time' format."
836 (if (file-directory-p file
) (nth 5 (file-attributes file
))
837 (let* ((org-inhibit-startup t
)
838 (visiting (find-buffer-visiting file
))
839 (file-buf (or visiting
(find-file-noselect file nil
)))
841 (with-current-buffer file-buf
843 (org-export-with-buffer-copy
844 (org-export-get-environment))
845 (org-export-get-environment)))
847 (unless visiting
(kill-buffer file-buf
))
848 ;; DATE is a secondary string. If it contains a timestamp,
849 ;; convert it to internal format. Otherwise, use FILE
850 ;; modification time.
851 (cond ((let ((ts (and (consp date
) (assq 'timestamp date
))))
853 (let ((value (org-element-interpret-data ts
)))
854 (and (org-string-nw-p value
)
855 (org-time-string-to-time value
))))))
856 ((file-exists-p file
) (nth 5 (file-attributes file
)))
857 (t (error "No such file: \"%s\"" file
))))))
861 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
862 ;;; Interactive publishing functions
865 (defalias 'org-publish-project
'org-publish
)
868 (defun org-publish (project &optional force async
)
871 PROJECT is either a project name, as a string, or a project
872 alist (see `org-publish-project-alist' variable).
874 When optional argument FORCE is non-nil, force publishing all
875 files in PROJECT. With a non-nil optional argument ASYNC,
876 publishing will be done asynchronously, in another process."
878 (list (assoc (completing-read "Publish project: "
879 org-publish-project-alist nil t
)
880 org-publish-project-alist
)
882 (let ((project (if (not (stringp project
)) project
883 ;; If this function is called in batch mode,
884 ;; PROJECT is still a string here.
885 (assoc project org-publish-project-alist
))))
889 (org-export-async-start (lambda (_) nil
)
890 `(let ((org-publish-use-timestamps-flag
891 ,(and (not force
) org-publish-use-timestamps-flag
)))
892 ;; Expand components right now as external process may not
893 ;; be aware of complete `org-publish-project-alist'.
894 (org-publish-projects
895 ',(org-publish-expand-projects (list project
))))))
896 (t (save-window-excursion
897 (let ((org-publish-use-timestamps-flag
898 (and (not force
) org-publish-use-timestamps-flag
)))
899 (org-publish-projects (list project
))))))))
902 (defun org-publish-all (&optional force async
)
903 "Publish all projects.
904 With prefix argument FORCE, remove all files in the timestamp
905 directory and force publishing all projects. With a non-nil
906 optional argument ASYNC, publishing will be done asynchronously,
910 (org-export-async-start (lambda (_) nil
)
912 (when ',force
(org-publish-remove-all-timestamps))
913 (let ((org-publish-use-timestamps-flag
914 (if ',force nil
,org-publish-use-timestamps-flag
)))
915 (org-publish-projects ',org-publish-project-alist
))))
916 (when force
(org-publish-remove-all-timestamps))
917 (save-window-excursion
918 (let ((org-publish-use-timestamps-flag
919 (if force nil org-publish-use-timestamps-flag
)))
920 (org-publish-projects org-publish-project-alist
)))))
924 (defun org-publish-current-file (&optional force async
)
925 "Publish the current file.
926 With prefix argument FORCE, force publish the file. When
927 optional argument ASYNC is non-nil, publishing will be done
928 asynchronously, in another process."
930 (let ((file (buffer-file-name (buffer-base-buffer))))
932 (org-export-async-start (lambda (_) nil
)
933 `(let ((org-publish-use-timestamps-flag
934 (if ',force nil
,org-publish-use-timestamps-flag
)))
935 (org-publish-file ,file
)))
936 (save-window-excursion
937 (let ((org-publish-use-timestamps-flag
938 (if force nil org-publish-use-timestamps-flag
)))
939 (org-publish-file file
))))))
942 (defun org-publish-current-project (&optional force async
)
943 "Publish the project associated with the current file.
944 With a prefix argument, force publishing of all files in
947 (save-window-excursion
948 (let ((project (org-publish-get-project-from-filename
949 (buffer-file-name (buffer-base-buffer)) 'up
)))
950 (if project
(org-publish project force async
)
951 (error "File %s is not part of any known project"
952 (buffer-file-name (buffer-base-buffer)))))))
958 (defun org-publish-collect-index (output _backend info
)
959 "Update index for a file in cache.
961 OUTPUT is the output from transcoding current file. BACKEND is
962 the back-end that was used for transcoding. INFO is a plist
963 containing publishing and export options.
965 The index relative to current file is stored as an alist. An
966 association has the following shape: (TERM FILE-NAME PARENT),
967 where TERM is the indexed term, as a string, FILE-NAME is the
968 original full path of the file where the term in encountered, and
969 PARENT is a reference to the headline, if any, containing the
970 original index keyword. When non-nil, this reference is a cons
971 cell. Its CAR is a symbol among `id', `custom-id' and `name' and
972 its CDR is a string."
973 (let ((file (plist-get info
:input-file
)))
974 (org-publish-cache-set-file-property
977 (org-element-map (plist-get info
:parse-tree
) 'keyword
979 (when (equal (org-element-property :key k
) "INDEX")
980 (let ((parent (org-export-get-parent-headline k
)))
981 (list (org-element-property :value k
)
985 ((let ((id (org-element-property :ID parent
)))
986 (and id
(cons 'id id
))))
987 ((let ((id (org-element-property :CUSTOM_ID parent
)))
988 (and id
(cons 'custom-id id
))))
990 ;; Remove statistics cookie.
991 (replace-regexp-in-string
992 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
993 (org-element-property :raw-value parent
)))))))))
995 ;; Return output unchanged.
998 (defun org-publish-index-generate-theindex (project directory
)
999 "Retrieve full index from cache and build \"theindex.org\".
1000 PROJECT is the project the index relates to. DIRECTORY is the
1001 publishing directory."
1002 (let ((all-files (org-publish-get-base-files
1003 project
(plist-get (cdr project
) :exclude
)))
1005 ;; Compile full index and sort it alphabetically.
1006 (dolist (file all-files
1008 (sort (nreverse full-index
)
1009 (lambda (a b
) (string< (downcase (car a
))
1010 (downcase (car b
)))))))
1011 (let ((index (org-publish-cache-get-file-property file
:index
)))
1012 (dolist (term index
)
1013 (unless (member term full-index
) (push term full-index
)))))
1014 ;; Write "theindex.inc" in DIRECTORY.
1015 (with-temp-file (expand-file-name "theindex.inc" directory
)
1016 (let ((current-letter nil
) (last-entry nil
))
1017 (dolist (idx full-index
)
1018 (let* ((entry (org-split-string (car idx
) "!"))
1019 (letter (upcase (substring (car entry
) 0 1)))
1020 ;; Transform file into a path relative to publishing
1022 (file (file-relative-name
1024 (plist-get (cdr project
) :base-directory
))))
1025 ;; Check if another letter has to be inserted.
1026 (unless (string= letter current-letter
)
1027 (insert (format "* %s\n" letter
)))
1028 ;; Compute the first difference between last entry and
1029 ;; current one: it tells the level at which new items
1032 (if (equal entry last-entry
) (1- (length entry
))
1033 (cl-loop for n from
0 to
(length entry
)
1034 unless
(equal (nth n entry
) (nth n last-entry
))
1036 (len (length (nthcdr rank entry
))))
1037 ;; For each term after the first difference, create
1038 ;; a new sub-list with the term as body. Moreover,
1039 ;; linkify the last term.
1043 (make-string (* (+ rank n
) 2) ?
) " - "
1044 (if (not (= (1- len
) n
)) (nth (+ rank n
) entry
)
1045 ;; Last term: Link it to TARGET, if possible.
1046 (let ((target (nth 2 idx
)))
1051 (`nil
(format "file:%s" file
))
1052 (`id
(format "id:%s" (cdr target
)))
1053 (`custom-id
(format "file:%s::#%s" file
(cdr target
)))
1054 (_ (format "file:%s::*%s" file
(cdr target
))))
1056 (car (last entry
)))))
1058 (setq current-letter letter last-entry entry
))))
1059 ;; Create "theindex.org", if it doesn't exist yet, and provide
1060 ;; a default index file.
1061 (let ((index.org
(expand-file-name "theindex.org" directory
)))
1062 (unless (file-exists-p index.org
)
1063 (with-temp-file index.org
1064 (insert "#+TITLE: Index\n\n#+INCLUDE: \"theindex.inc\"\n\n")))))))
1068 ;;; External Fuzzy Links Resolution
1070 ;; This part implements tools to resolve [[file.org::*Some headline]]
1071 ;; links, where "file.org" belongs to the current project.
1073 (defun org-publish--store-crossrefs (output _backend info
)
1074 "Store cross-references for current published file.
1076 OUPUT is the produced output, as a string. BACKEND is the export
1077 back-end used, as a symbol. INFO is the final export state, as
1080 This function is meant to be used as a final output filter. See
1081 `org-publish-org-to'."
1082 (org-publish-cache-set-file-property
1083 (plist-get info
:input-file
) :crossrefs
1084 ;; Update `:crossrefs' so as to remove unused references and search
1085 ;; cells. Actually used references are extracted from
1086 ;; `:internal-references', with references as strings removed. See
1087 ;; `org-export-get-reference' for details.
1088 (cl-remove-if (lambda (pair) (stringp (car pair
)))
1089 (plist-get info
:internal-references
)))
1090 ;; Return output unchanged.
1093 (defun org-publish-resolve-external-link (search file
)
1094 "Return reference for element matching string SEARCH in FILE.
1096 Return value is an internal reference, as a string.
1098 This function allows resolving external links with a search
1101 [[file.org::*heading][description]]
1102 [[file.org::#custom-id][description]]
1103 [[file.org::fuzzy][description]]
1105 It only makes sense to use this if export back-end builds
1106 references with `org-export-get-reference'."
1107 (if (not org-publish-cache
)
1109 (message "Reference %S in file %S cannot be resolved without publishing"
1113 (let* ((filename (expand-file-name file
))
1115 (org-publish-cache-get-file-property filename
:crossrefs nil t
))
1116 (cells (org-export-string-to-search-cell search
)))
1118 ;; Look for reference associated to search cells triggered by
1119 ;; LINK. It can match when targeted file has been published
1121 (let ((known (cdr (cl-some (lambda (c) (assoc c crossrefs
)) cells
))))
1122 (and known
(org-export-format-reference known
)))
1123 ;; Search cell is unknown so far. Generate a new internal
1124 ;; reference that will be used when the targeted file will be
1126 (let ((new (org-export-new-reference crossrefs
)))
1127 (dolist (cell cells
) (push (cons cell new
) crossrefs
))
1128 (org-publish-cache-set-file-property filename
:crossrefs crossrefs
)
1129 (org-export-format-reference new
))))))
1133 ;;; Caching functions
1135 (defun org-publish-write-cache-file (&optional free-cache
)
1136 "Write `org-publish-cache' to file.
1137 If FREE-CACHE, empty the cache."
1138 (unless org-publish-cache
1139 (error "`org-publish-write-cache-file' called, but no cache present"))
1141 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1143 (error "Cannot find cache-file name in `org-publish-write-cache-file'"))
1144 (with-temp-file cache-file
1145 (let (print-level print-length
)
1146 (insert "(setq org-publish-cache \
1147 \(make-hash-table :test 'equal :weakness nil :size 100))\n")
1148 (maphash (lambda (k v
)
1150 (format "(puthash %S %s%S org-publish-cache)\n"
1151 k
(if (or (listp v
) (symbolp v
)) "'" "") v
)))
1152 org-publish-cache
)))
1153 (when free-cache
(org-publish-reset-cache))))
1155 (defun org-publish-initialize-cache (project-name)
1156 "Initialize the projects cache if not initialized yet and return it."
1158 (unless project-name
1159 (error "Cannot initialize `org-publish-cache' without projects name in \
1160 `org-publish-initialize-cache'"))
1162 (unless (file-exists-p org-publish-timestamp-directory
)
1163 (make-directory org-publish-timestamp-directory t
))
1164 (unless (file-directory-p org-publish-timestamp-directory
)
1165 (error "Org publish timestamp: %s is not a directory"
1166 org-publish-timestamp-directory
))
1168 (unless (and org-publish-cache
1169 (string= (org-publish-cache-get ":project:") project-name
))
1172 (expand-file-name org-publish-timestamp-directory
)
1173 project-name
".cache"))
1174 (cexists (file-exists-p cache-file
)))
1176 (when org-publish-cache
(org-publish-reset-cache))
1178 (if cexists
(load-file cache-file
)
1179 (setq org-publish-cache
1180 (make-hash-table :test
'equal
:weakness nil
:size
100))
1181 (org-publish-cache-set ":project:" project-name
)
1182 (org-publish-cache-set ":cache-file:" cache-file
))
1183 (unless cexists
(org-publish-write-cache-file nil
))))
1186 (defun org-publish-reset-cache ()
1187 "Empty org-publish-cache and reset it nil."
1188 (message "%s" "Resetting org-publish-cache")
1189 (when (hash-table-p org-publish-cache
)
1190 (clrhash org-publish-cache
))
1191 (setq org-publish-cache nil
))
1193 (defun org-publish-cache-file-needs-publishing
1194 (filename &optional pub-dir pub-func _base-dir
)
1195 "Check the timestamp of the last publishing of FILENAME.
1196 Return non-nil if the file needs publishing. Also check if
1197 any included files have been more recently published, so that
1198 the file including them will be republished as well."
1199 (unless org-publish-cache
1201 "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1202 (let* ((case-fold-search t
)
1203 (key (org-publish-timestamp-filename filename pub-dir pub-func
))
1204 (pstamp (org-publish-cache-get key
))
1205 (org-inhibit-startup t
)
1206 (visiting (find-buffer-visiting filename
))
1207 (buf (find-file-noselect (expand-file-name filename
)))
1208 included-files-ctime
)
1209 (when (equal (file-name-extension filename
) "org")
1211 (with-current-buffer buf
1212 (goto-char (point-min))
1213 (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t
)
1214 (let* ((element (org-element-at-point))
1216 (and (eq (org-element-type element
) 'keyword
)
1217 (let ((value (org-element-property :value element
)))
1220 "\\`\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)"
1222 (let ((m (match-string 1 value
)))
1223 (org-unbracket-string
1225 ;; Ignore search suffix.
1226 (if (string-match "\\(::\\(.*?\\)\\)\"?\\'"
1228 (substring m
0 (match-beginning 0))
1231 (push (org-publish-cache-ctime-of-src
1232 (expand-file-name included-file
))
1233 included-files-ctime
)))))
1234 (unless visiting
(kill-buffer buf
))))
1236 (let ((ctime (org-publish-cache-ctime-of-src filename
)))
1237 (or (< pstamp ctime
)
1238 (cl-some (lambda (ct) (< ctime ct
)) included-files-ctime
))))))
1240 (defun org-publish-cache-set-file-property
1241 (filename property value
&optional project-name
)
1242 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1243 Use cache file of PROJECT-NAME. If the entry does not exist, it
1244 will be created. Return VALUE."
1245 ;; Evtl. load the requested cache file:
1246 (if project-name
(org-publish-initialize-cache project-name
))
1247 (let ((pl (org-publish-cache-get filename
)))
1248 (if pl
(progn (plist-put pl property value
) value
)
1249 (org-publish-cache-get-file-property
1250 filename property value nil project-name
))))
1252 (defun org-publish-cache-get-file-property
1253 (filename property
&optional default no-create project-name
)
1254 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1255 Use cache file of PROJECT-NAME. Return the value of that PROPERTY,
1256 or DEFAULT, if the value does not yet exist. Create the entry,
1257 if necessary, unless NO-CREATE is non-nil."
1258 ;; Evtl. load the requested cache file:
1259 (if project-name
(org-publish-initialize-cache project-name
))
1260 (let ((pl (org-publish-cache-get filename
)) retval
)
1262 (if (plist-member pl property
)
1263 (setq retval
(plist-get pl property
))
1264 (setq retval default
))
1267 (org-publish-cache-set filename
(list property default
)))
1268 (setq retval default
))
1271 (defun org-publish-cache-get (key)
1272 "Return the value stored in `org-publish-cache' for key KEY.
1273 Return nil, if no value or nil is found. Raise an error if the
1274 cache does not exist."
1275 (unless org-publish-cache
1276 (error "`org-publish-cache-get' called, but no cache present"))
1277 (gethash key org-publish-cache
))
1279 (defun org-publish-cache-set (key value
)
1280 "Store KEY VALUE pair in `org-publish-cache'.
1281 Returns value on success, else nil. Raise an error if the cache
1283 (unless org-publish-cache
1284 (error "`org-publish-cache-set' called, but no cache present"))
1285 (puthash key value org-publish-cache
))
1287 (defun org-publish-cache-ctime-of-src (file)
1288 "Get the ctime of FILE as an integer."
1289 (let ((attr (file-attributes
1290 (expand-file-name (or (file-symlink-p file
) file
)
1291 (file-name-directory file
)))))
1292 (if (not attr
) (error "No such file: \"%s\"" file
)
1293 (+ (lsh (car (nth 5 attr
)) 16)
1294 (cadr (nth 5 attr
))))))
1297 (provide 'ox-publish
)
1300 ;; generated-autoload-file: "org-loaddefs.el"
1303 ;;; ox-publish.el ends here