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
107 default extension. If it is `any', include all the files,
108 even without extension.
110 `:publishing-directory'
112 Directory (possibly remote) where output files will be
115 If `:recursive' is non-nil files in sub-directories of
116 `:base-directory' are considered.
118 The `:exclude' property may be used to prevent certain files from
119 being published. Its value may be a string or regexp matching
120 file names you don't want to be published.
122 The `:include' property may be used to include extra files. Its
123 value may be a list of filenames to include. The filenames are
124 considered relative to the base directory.
126 When both `:include' and `:exclude' properties are given values,
127 the exclusion step happens first.
129 One special property controls which back-end function to use for
130 publishing files in the project. This can be used to extend the
131 set of file types publishable by `org-publish', as well as the
132 set of output formats.
134 `:publishing-function'
136 Function to publish file. Each back-end may define its
137 own (i.e. `org-latex-publish-to-pdf',
138 `org-html-publish-to-html'). May be a list of functions, in
139 which case each function in the list is invoked in turn.
141 Another property allows you to insert code that prepares
142 a project for publishing. For example, you could call GNU Make
143 on a certain makefile, to ensure published files are built up to
146 `:preparation-function'
148 Function to be called before publishing this project. This
149 may also be a list of functions. Preparation functions are
150 called with the project properties list as their sole
153 `:completion-function'
155 Function to be called after publishing this project. This
156 may also be a list of functions. Completion functions are
157 called with the project properties list as their sole
160 Some properties control details of the Org publishing process,
161 and are equivalent to the corresponding user variables listed in
162 the right column. Back-end specific properties may also be
163 included. See the back-end documentation for more information.
165 :author `user-full-name'
166 :creator `org-export-creator-string'
167 :email `user-mail-address'
168 :exclude-tags `org-export-exclude-tags'
169 :headline-levels `org-export-headline-levels'
170 :language `org-export-default-language'
171 :preserve-breaks `org-export-preserve-breaks'
172 :section-numbers `org-export-with-section-numbers'
173 :select-tags `org-export-select-tags'
174 :time-stamp-file `org-export-time-stamp-file'
175 :with-archived-trees `org-export-with-archived-trees'
176 :with-author `org-export-with-author'
177 :with-creator `org-export-with-creator'
178 :with-date `org-export-with-date'
179 :with-drawers `org-export-with-drawers'
180 :with-email `org-export-with-email'
181 :with-emphasize `org-export-with-emphasize'
182 :with-entities `org-export-with-entities'
183 :with-fixed-width `org-export-with-fixed-width'
184 :with-footnotes `org-export-with-footnotes'
185 :with-inlinetasks `org-export-with-inlinetasks'
186 :with-latex `org-export-with-latex'
187 :with-planning `org-export-with-planning'
188 :with-priority `org-export-with-priority'
189 :with-properties `org-export-with-properties'
190 :with-smart-quotes `org-export-with-smart-quotes'
191 :with-special-strings `org-export-with-special-strings'
192 :with-statistics-cookies' `org-export-with-statistics-cookies'
193 :with-sub-superscript `org-export-with-sub-superscripts'
194 :with-toc `org-export-with-toc'
195 :with-tables `org-export-with-tables'
196 :with-tags `org-export-with-tags'
197 :with-tasks `org-export-with-tasks'
198 :with-timestamps `org-export-with-timestamps'
199 :with-title `org-export-with-title'
200 :with-todo-keywords `org-export-with-todo-keywords'
202 The following properties may be used to control publishing of
203 a site-map of files or summary page for a given project.
207 Whether to publish a site-map during
208 `org-publish-current-project' or `org-publish-all'.
212 Filename for output of sitemap. Defaults to \"sitemap.org\".
216 Title of site-map page. Defaults to name of file.
220 Plugin function to use for generation of site-map. Defaults
221 to `org-publish-org-sitemap', which generates a plain list of
222 links to all files in the project.
226 Can be `list' (site-map is just an itemized list of the
227 titles of the files involved) or `tree' (the directory
228 structure of the source files is reflected in the site-map).
231 `:sitemap-sans-extension'
233 Remove extension from site-map's file-names. Useful to have
234 cool URIs (see http://www.w3.org/Provider/Style/URI).
237 If you create a site-map file, adjust the sorting like this:
239 `:sitemap-sort-folders'
241 Where folders should appear in the site-map. Set this to
242 `first' (default) or `last' to display folders first or last,
243 respectively. Any other value will mix files and folders.
245 `:sitemap-sort-files'
247 The site map is normally sorted alphabetically. You can
248 change this behavior setting this to `anti-chronologically',
249 `chronologically', or nil.
251 `:sitemap-ignore-case'
253 Should sorting be case-sensitive? Default nil.
255 The following property control the creation of a concept index.
259 Create a concept index. The file containing the index has to
260 be called \"theindex.org\". If it doesn't exist in the
261 project, it will be generated. Contents of the index are
262 stored in the file \"theindex.inc\", which can be included in
265 Other properties affecting publication.
269 Set this to t to publish only the body of the documents."
270 :group
'org-export-publish
273 (defcustom org-publish-use-timestamps-flag t
274 "Non-nil means use timestamp checking to publish only changed files.
275 When nil, do no timestamp checking and always publish all files."
276 :group
'org-export-publish
279 (defcustom org-publish-timestamp-directory
280 (convert-standard-filename "~/.org-timestamps/")
281 "Name of directory in which to store publishing timestamps."
282 :group
'org-export-publish
285 (defcustom org-publish-list-skipped-files t
286 "Non-nil means show message about files *not* published."
287 :group
'org-export-publish
290 (defcustom org-publish-sitemap-sort-files
'alphabetically
291 "Method to sort files in site-maps.
292 Possible values are `alphabetically', `chronologically',
293 `anti-chronologically' and nil.
295 If `alphabetically', files will be sorted alphabetically. If
296 `chronologically', files will be sorted with older modification
297 time first. If `anti-chronologically', files will be sorted with
298 newer modification time first. nil won't sort files.
300 You can overwrite this default per project in your
301 `org-publish-project-alist', using `:sitemap-sort-files'."
302 :group
'org-export-publish
305 (defcustom org-publish-sitemap-sort-folders
'first
306 "A symbol, denoting if folders are sorted first in sitemaps.
307 Possible values are `first', `last', and nil.
308 If `first', folders will be sorted before files.
309 If `last', folders are sorted to the end after the files.
310 Any other value will not mix files and folders.
312 You can overwrite this default per project in your
313 `org-publish-project-alist', using `:sitemap-sort-folders'."
314 :group
'org-export-publish
317 (defcustom org-publish-sitemap-sort-ignore-case nil
318 "Non-nil when site-map sorting should ignore case.
320 You can overwrite this default per project in your
321 `org-publish-project-alist', using `:sitemap-ignore-case'."
322 :group
'org-export-publish
325 (defcustom org-publish-sitemap-date-format
"%Y-%m-%d"
326 "Format for printing a date in the sitemap.
327 See `format-time-string' for allowed formatters."
328 :group
'org-export-publish
331 (defcustom org-publish-sitemap-file-entry-format
"%t"
332 "Format string for site-map file entry.
333 You could use brackets to delimit on what part the link will be.
337 %d is the date formatted using `org-publish-sitemap-date-format'."
338 :group
'org-export-publish
343 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
344 ;;; Timestamp-related functions
346 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func
)
347 "Return path to timestamp file for filename FILENAME."
348 (setq filename
(concat filename
"::" (or pub-dir
"") "::"
349 (format "%s" (or pub-func
""))))
350 (concat "X" (if (fboundp 'sha1
) (sha1 filename
) (md5 filename
))))
352 (defun org-publish-needed-p
353 (filename &optional pub-dir pub-func _true-pub-dir base-dir
)
354 "Non-nil if FILENAME should be published in PUB-DIR using PUB-FUNC.
355 TRUE-PUB-DIR is where the file will truly end up. Currently we
356 are not using this - maybe it can eventually be used to check if
357 the file is present at the target location, and how old it is.
358 Right now we cannot do this, because we do not know under what
359 file name the file will be stored - the publishing function can
360 still decide about that independently."
361 (let ((rtn (if (not org-publish-use-timestamps-flag
) t
362 (org-publish-cache-file-needs-publishing
363 filename pub-dir pub-func base-dir
))))
364 (if rtn
(message "Publishing file %s using `%s'" filename pub-func
)
365 (when org-publish-list-skipped-files
366 (message "Skipping unmodified file %s" filename
)))
369 (defun org-publish-update-timestamp
370 (filename &optional pub-dir pub-func _base-dir
)
371 "Update publishing timestamp for file FILENAME.
372 If there is no timestamp, create one."
373 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func
))
374 (stamp (org-publish-cache-ctime-of-src filename
)))
375 (org-publish-cache-set key stamp
)))
377 (defun org-publish-remove-all-timestamps ()
378 "Remove all files in the timestamp directory."
379 (let ((dir org-publish-timestamp-directory
))
380 (when (and (file-exists-p dir
) (file-directory-p dir
))
381 (mapc #'delete-file
(directory-files dir
'full
"[^.]\\'"))
382 (org-publish-reset-cache))))
386 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
387 ;;; Getting project information out of `org-publish-project-alist'
389 (defun org-publish-expand-projects (projects-alist)
390 "Expand projects in PROJECTS-ALIST.
391 This splices all the components into the list."
392 (let ((rest projects-alist
) rtn p components
)
393 (while (setq p
(pop rest
))
394 (if (setq components
(plist-get (cdr p
) :components
))
396 (mapcar (lambda (x) (assoc x org-publish-project-alist
))
400 (nreverse (delete-dups (delq nil rtn
)))))
402 (defvar org-publish-sitemap-sort-files
)
403 (defvar org-publish-sitemap-sort-folders
)
404 (defvar org-publish-sitemap-ignore-case
)
405 (defvar org-publish-sitemap-requested
)
406 (defvar org-publish-sitemap-date-format
)
407 (defvar org-publish-sitemap-file-entry-format
)
408 (defun org-publish-compare-directory-files (a b
)
409 "Predicate for `sort', that sorts folders and files for sitemap."
411 (when (or org-publish-sitemap-sort-files org-publish-sitemap-sort-folders
)
412 ;; First we sort files:
413 (when org-publish-sitemap-sort-files
414 (pcase org-publish-sitemap-sort-files
416 (let* ((adir (file-directory-p a
))
417 (aorg (and (string-suffix-p ".org" a
) (not adir
)))
418 (bdir (file-directory-p b
))
419 (borg (and (string-suffix-p ".org" b
) (not bdir
)))
420 (A (if aorg
(concat (file-name-directory a
)
421 (org-publish-find-title a
)) a
))
422 (B (if borg
(concat (file-name-directory b
)
423 (org-publish-find-title b
)) b
)))
424 (setq retval
(if org-publish-sitemap-ignore-case
425 (not (string-lessp (upcase B
) (upcase A
)))
426 (not (string-lessp B A
))))))
427 ((or `anti-chronologically
`chronologically
)
428 (let* ((adate (org-publish-find-date a
))
429 (bdate (org-publish-find-date b
))
430 (A (+ (lsh (car adate
) 16) (cadr adate
)))
431 (B (+ (lsh (car bdate
) 16) (cadr bdate
))))
433 (if (eq org-publish-sitemap-sort-files
'chronologically
)
436 ;; Directory-wise wins:
437 (when org-publish-sitemap-sort-folders
438 ;; a is directory, b not:
440 ((and (file-directory-p a
) (not (file-directory-p b
)))
441 (setq retval
(eq org-publish-sitemap-sort-folders
'first
)))
442 ;; a is not a directory, but b is:
443 ((and (not (file-directory-p a
)) (file-directory-p b
))
444 (setq retval
(eq org-publish-sitemap-sort-folders
'last
))))))
447 (defun org-publish-get-base-files-1
448 (base-dir &optional recurse match skip-file skip-dir
)
449 "Set `org-publish-temp-files' with files from BASE-DIR directory.
450 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
451 non-nil, restrict this list to the files matching the regexp
452 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
453 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
454 matching the regexp SKIP-DIR when recursing through BASE-DIR."
455 (let ((all-files (if (not recurse
) (directory-files base-dir t match
)
456 ;; If RECURSE is non-nil, we want all files
457 ;; matching MATCH and sub-directories.
460 (or (file-directory-p file
)
461 (and match
(string-match match file
))))
462 (directory-files base-dir t
)))))
463 (dolist (f (if (not org-publish-sitemap-requested
) all-files
464 (sort all-files
#'org-publish-compare-directory-files
)))
465 (let ((fd-p (file-directory-p f
))
466 (fnd (file-name-nondirectory f
)))
467 (if (and fd-p recurse
468 (not (string-match "^\\.+$" fnd
))
469 (if skip-dir
(not (string-match skip-dir fnd
)) t
))
470 (org-publish-get-base-files-1
471 f recurse match skip-file skip-dir
)
472 (unless (or fd-p
; This is a directory.
473 (and skip-file
(string-match skip-file fnd
))
474 (not (file-exists-p (file-truename f
)))
475 (not (string-match match fnd
)))
476 (cl-pushnew f org-publish-temp-files
)))))))
478 (defun org-publish-get-base-files (project &optional exclude-regexp
)
479 "Return a list of all files in PROJECT.
480 If EXCLUDE-REGEXP is set, this will be used to filter out
482 (let* ((project-plist (cdr project
))
483 (base-dir (file-name-as-directory
484 (plist-get project-plist
:base-directory
)))
485 (include-list (plist-get project-plist
:include
))
486 (recurse (plist-get project-plist
:recursive
))
487 (extension (or (plist-get project-plist
:base-extension
) "org"))
488 ;; sitemap-... variables are dynamically scoped for
489 ;; org-publish-compare-directory-files:
490 (org-publish-sitemap-requested
491 (plist-get project-plist
:auto-sitemap
))
493 (or (plist-get project-plist
:sitemap-filename
) "sitemap.org"))
494 (org-publish-sitemap-sort-folders
495 (if (plist-member project-plist
:sitemap-sort-folders
)
496 (plist-get project-plist
:sitemap-sort-folders
)
497 org-publish-sitemap-sort-folders
))
498 (org-publish-sitemap-sort-files
499 (cond ((plist-member project-plist
:sitemap-sort-files
)
500 (plist-get project-plist
:sitemap-sort-files
))
501 ;; For backward compatibility:
502 ((plist-member project-plist
:sitemap-alphabetically
)
503 (if (plist-get project-plist
:sitemap-alphabetically
)
504 'alphabetically nil
))
505 (t org-publish-sitemap-sort-files
)))
506 (org-publish-sitemap-ignore-case
507 (if (plist-member project-plist
:sitemap-ignore-case
)
508 (plist-get project-plist
:sitemap-ignore-case
)
509 org-publish-sitemap-sort-ignore-case
))
510 (match (if (eq extension
'any
) "^[^\\.]"
511 (concat "^[^\\.].*\\.\\(" extension
"\\)$"))))
512 ;; Make sure `org-publish-sitemap-sort-folders' has an accepted
514 (unless (memq org-publish-sitemap-sort-folders
'(first last
))
515 (setq org-publish-sitemap-sort-folders nil
))
517 (setq org-publish-temp-files nil
)
518 (when org-publish-sitemap-requested
519 (cl-pushnew (expand-file-name (concat base-dir sitemap-filename
))
520 org-publish-temp-files
))
521 (org-publish-get-base-files-1 base-dir recurse match
522 ;; FIXME distinguish exclude regexp
523 ;; for skip-file and skip-dir?
524 exclude-regexp exclude-regexp
)
525 (dolist (f include-list org-publish-temp-files
)
526 (cl-pushnew (expand-file-name (concat base-dir f
))
527 org-publish-temp-files
))))
529 (defun org-publish-get-project-from-filename (filename &optional up
)
530 "Return the project that FILENAME belongs to."
531 (let* ((filename (expand-file-name filename
))
535 (dolist (prj org-publish-project-alist
)
536 (unless (plist-get (cdr prj
) :components
)
537 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
538 (let* ((r (plist-get (cdr prj
) :recursive
))
539 (b (expand-file-name (file-name-as-directory
540 (plist-get (cdr prj
) :base-directory
))))
541 (x (or (plist-get (cdr prj
) :base-extension
) "org"))
542 (e (plist-get (cdr prj
) :exclude
))
543 (i (plist-get (cdr prj
) :include
))
546 (and (not (eq x
'any
))
547 (format "\\.\\(%s\\)\\'" x
)))))
551 (dolist (file i
) (expand-file-name file b
))))
552 (and (not (and e
(string-match e filename
)))
553 (string-match xm filename
)))
554 (setq project-name
(car prj
))
555 (throw 'p-found project-name
))))))
557 (dolist (prj org-publish-project-alist
)
558 (if (member project-name
(plist-get (cdr prj
) :components
))
559 (setq project-name
(car prj
)))))
560 (assoc project-name org-publish-project-alist
)))
564 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
565 ;;; Tools for publishing functions in back-ends
567 (defun org-publish-org-to (backend filename extension plist
&optional pub-dir
)
568 "Publish an Org file to a specified back-end.
570 BACKEND is a symbol representing the back-end used for
571 transcoding. FILENAME is the filename of the Org file to be
572 published. EXTENSION is the extension used for the output
573 string, with the leading dot. PLIST is the property list for the
576 Optional argument PUB-DIR, when non-nil is the publishing
579 Return output file name."
580 (unless (or (not pub-dir
) (file-exists-p pub-dir
)) (make-directory pub-dir t
))
581 ;; Check if a buffer visiting FILENAME is already open.
582 (let* ((org-inhibit-startup t
)
583 (visiting (find-buffer-visiting filename
))
584 (work-buffer (or visiting
(find-file-noselect filename
))))
586 (with-current-buffer work-buffer
587 (let ((output (org-export-output-file-name extension nil pub-dir
)))
588 (org-export-to-file backend output
589 nil nil nil
(plist-get plist
:body-only
)
590 ;; Add `org-publish--store-crossrefs' and
591 ;; `org-publish-collect-index' to final output filters.
592 ;; The latter isn't dependent on `:makeindex', since we
593 ;; want to keep it up-to-date in cache anyway.
597 ,(org-publish-cache-get-file-property
598 (expand-file-name filename
) :crossrefs nil t
)
600 (org-publish--store-crossrefs
601 org-publish-collect-index
602 ,@(plist-get plist
:filter-final-output
)))))))
603 ;; Remove opened buffer in the process.
604 (unless visiting
(kill-buffer work-buffer
)))))
606 (defun org-publish-attachment (_plist filename pub-dir
)
607 "Publish a file with no transformation of any kind.
609 FILENAME is the filename of the Org file to be published. PLIST
610 is the property list for the given project. PUB-DIR is the
611 publishing directory.
613 Return output file name."
614 (unless (file-directory-p pub-dir
)
615 (make-directory pub-dir t
))
616 (let ((output (expand-file-name (file-name-nondirectory filename
) pub-dir
)))
617 (or (equal (expand-file-name (file-name-directory filename
))
618 (file-name-as-directory (expand-file-name pub-dir
)))
619 (copy-file filename output t
))
625 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
626 ;;; Publishing files, sets of files, and indices
628 (defun org-publish-file (filename &optional project no-cache
)
629 "Publish file FILENAME from PROJECT.
630 If NO-CACHE is not nil, do not initialize `org-publish-cache'.
631 This is needed, since this function is used to publish single
632 files, when entire projects are published (see
633 `org-publish-projects')."
636 (or (org-publish-get-project-from-filename filename
)
637 (error "File %s not part of any known project"
638 (abbreviate-file-name filename
)))))
639 (project-plist (cdr project
))
640 (ftname (expand-file-name filename
))
642 (let ((fun (plist-get project-plist
:publishing-function
)))
643 (cond ((null fun
) (error "No publishing function chosen"))
647 (file-name-as-directory
649 (or (plist-get project-plist
:base-directory
)
650 (error "Project %s does not have :base-directory defined"
653 (file-name-as-directory
655 (or (eval (plist-get project-plist
:publishing-directory
))
656 (error "Project %s does not have :publishing-directory defined"
660 (unless no-cache
(org-publish-initialize-cache (car project
)))
665 (and (string-match (regexp-quote base-dir
) ftname
)
666 (substring ftname
(match-end 0))))))
667 ;; Allow chain of publishing functions.
668 (dolist (f publishing-function
)
669 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir base-dir
)
670 (let ((output (funcall f project-plist filename tmp-pub-dir
)))
671 (org-publish-update-timestamp filename pub-dir f base-dir
)
672 (run-hook-with-args 'org-publish-after-publishing-hook
675 ;; Make sure to write cache to file after successfully publishing
676 ;; a file, so as to minimize impact of a publishing failure.
677 (org-publish-write-cache-file)))
679 (defun org-publish-projects (projects)
680 "Publish all files belonging to the PROJECTS alist.
681 If `:auto-sitemap' is set, publish the sitemap too. If
682 `:makeindex' is set, also produce a file \"theindex.org\"."
683 (dolist (project (org-publish-expand-projects projects
))
684 (let ((project-plist (cdr project
)))
685 (let ((fun (plist-get project-plist
:preparation-function
)))
686 (cond ((consp fun
) (dolist (f fun
) (funcall f project-plist
)))
687 ((functionp fun
) (funcall fun project-plist
))))
688 ;; Each project uses its own cache file.
689 (org-publish-initialize-cache (car project
))
690 (when (plist-get project-plist
:auto-sitemap
)
691 (let ((sitemap-filename
692 (or (plist-get project-plist
:sitemap-filename
)
695 (or (plist-get project-plist
:sitemap-function
)
696 #'org-publish-org-sitemap
))
697 (org-publish-sitemap-date-format
698 (or (plist-get project-plist
:sitemap-date-format
)
699 org-publish-sitemap-date-format
))
700 (org-publish-sitemap-file-entry-format
701 (or (plist-get project-plist
:sitemap-file-entry-format
)
702 org-publish-sitemap-file-entry-format
)))
703 (funcall sitemap-function project sitemap-filename
)))
704 ;; Publish all files from PROJECT except "theindex.org". Its
705 ;; publishing will be deferred until "theindex.inc" is
708 (expand-file-name "theindex.org"
709 (plist-get project-plist
:base-directory
)))
710 (exclude-regexp (plist-get project-plist
:exclude
)))
711 (dolist (file (org-publish-get-base-files project exclude-regexp
))
712 (unless (equal file theindex
) (org-publish-file file project t
)))
713 ;; Populate "theindex.inc", if needed, and publish
715 (when (plist-get project-plist
:makeindex
)
716 (org-publish-index-generate-theindex
717 project
(plist-get project-plist
:base-directory
))
718 (org-publish-file theindex project t
)))
719 (let ((fun (plist-get project-plist
:completion-function
)))
720 (cond ((consp fun
) (dolist (f fun
) (funcall f project-plist
)))
721 ((functionp fun
) (funcall fun project-plist
))))
722 (org-publish-write-cache-file))))
724 (defun org-publish-org-sitemap (project &optional sitemap-filename
)
725 "Create a sitemap of pages in set defined by PROJECT.
726 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
727 Default for SITEMAP-FILENAME is `sitemap.org'."
728 (let* ((project-plist (cdr project
))
729 (dir (file-name-as-directory
730 (plist-get project-plist
:base-directory
)))
731 (localdir (file-name-directory dir
))
732 (indent-str (make-string 2 ?\s
))
733 (exclude-regexp (plist-get project-plist
:exclude
))
735 (org-publish-get-base-files project exclude-regexp
)))
736 (sitemap-filename (concat dir
(or sitemap-filename
"sitemap.org")))
737 (sitemap-title (or (plist-get project-plist
:sitemap-title
)
738 (concat "Sitemap for project " (car project
))))
739 (sitemap-style (or (plist-get project-plist
:sitemap-style
)
741 (sitemap-sans-extension
742 (plist-get project-plist
:sitemap-sans-extension
))
743 (visiting (find-buffer-visiting sitemap-filename
))
746 (let ((org-inhibit-startup t
))
748 (or visiting
(find-file sitemap-filename
))))
750 (insert (concat "#+TITLE: " sitemap-title
"\n\n"))
751 (while (setq file
(pop files
))
752 (let ((link (file-relative-name file dir
))
754 (when sitemap-sans-extension
755 (setq link
(file-name-sans-extension link
)))
756 ;; sitemap shouldn't list itself
757 (unless (equal (file-truename sitemap-filename
)
758 (file-truename file
))
759 (if (eq sitemap-style
'list
)
760 (message "Generating list-style sitemap for %s" sitemap-title
)
761 (message "Generating tree-style sitemap for %s" sitemap-title
)
762 (setq localdir
(concat (file-name-as-directory dir
)
763 (file-name-directory link
)))
764 (unless (string= localdir oldlocal
)
765 (if (string= localdir dir
)
766 (setq indent-str
(make-string 2 ?\
))
771 (file-relative-name localdir dir
))) "/"))
773 (old-subdirs (split-string
774 (file-relative-name oldlocal dir
) "/")))
775 (setq indent-str
(make-string 2 ?\
))
776 (while (string= (car old-subdirs
) (car subdirs
))
777 (setq indent-str
(concat indent-str
(make-string 2 ?\
)))
781 (setq subdir
(concat subdir d
"/"))
782 (insert (concat indent-str
" + " d
"\n"))
783 (setq indent-str
(make-string
784 (+ (length indent-str
) 2) ?\
)))))))
785 ;; This is common to 'flat and 'tree
787 (org-publish-format-file-entry
788 org-publish-sitemap-file-entry-format file project-plist
))
789 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
790 (cond ((string-match-p regexp entry
)
791 (string-match regexp entry
)
792 (insert (concat indent-str
" + " (match-string 1 entry
)
794 (match-string 2 entry
)
795 "]]" (match-string 3 entry
) "\n")))
797 (insert (concat indent-str
" + [[file:" link
"]["
801 (or visiting
(kill-buffer sitemap-buffer
))))
803 (defun org-publish-format-file-entry (fmt file project-plist
)
806 `((?t .
,(org-publish-find-title file t
))
807 (?d .
,(format-time-string org-publish-sitemap-date-format
808 (org-publish-find-date file
)))
809 (?a .
,(or (plist-get project-plist
:author
) user-full-name
)))))
811 (defun org-publish-find-title (file &optional reset
)
812 "Find the title of FILE in project."
814 (and (not reset
) (org-publish-cache-get-file-property file
:title nil t
))
815 (let* ((org-inhibit-startup t
)
816 (visiting (find-buffer-visiting file
))
817 (buffer (or visiting
(find-file-noselect file
))))
818 (with-current-buffer buffer
822 ;; protect local variables in open buffers
824 (org-export-with-buffer-copy (org-export-get-environment))
825 (org-export-get-environment))
828 (org-no-properties (org-element-interpret-data property
))
829 (file-name-nondirectory (file-name-sans-extension file
))))))
830 (unless visiting
(kill-buffer buffer
))
831 (org-publish-cache-set-file-property file
:title title
)
834 (defun org-publish-find-date (file)
835 "Find the date of FILE in project.
836 This function assumes FILE is either a directory or an Org file.
837 If FILE is an Org file and provides a DATE keyword use it. In
838 any other case use the file system's modification time. Return
839 time in `current-time' format."
840 (if (file-directory-p file
) (nth 5 (file-attributes file
))
841 (let* ((org-inhibit-startup t
)
842 (visiting (find-buffer-visiting file
))
843 (file-buf (or visiting
(find-file-noselect file nil
)))
845 (with-current-buffer file-buf
847 (org-export-with-buffer-copy
848 (org-export-get-environment))
849 (org-export-get-environment)))
851 (unless visiting
(kill-buffer file-buf
))
852 ;; DATE is a secondary string. If it contains a timestamp,
853 ;; convert it to internal format. Otherwise, use FILE
854 ;; modification time.
855 (cond ((let ((ts (and (consp date
) (assq 'timestamp date
))))
857 (let ((value (org-element-interpret-data ts
)))
858 (and (org-string-nw-p value
)
859 (org-time-string-to-time value
))))))
860 ((file-exists-p file
) (nth 5 (file-attributes file
)))
861 (t (error "No such file: \"%s\"" file
))))))
865 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
866 ;;; Interactive publishing functions
869 (defalias 'org-publish-project
'org-publish
)
872 (defun org-publish (project &optional force async
)
875 PROJECT is either a project name, as a string, or a project
876 alist (see `org-publish-project-alist' variable).
878 When optional argument FORCE is non-nil, force publishing all
879 files in PROJECT. With a non-nil optional argument ASYNC,
880 publishing will be done asynchronously, in another process."
882 (list (assoc (completing-read "Publish project: "
883 org-publish-project-alist nil t
)
884 org-publish-project-alist
)
886 (let ((project (if (not (stringp project
)) project
887 ;; If this function is called in batch mode,
888 ;; PROJECT is still a string here.
889 (assoc project org-publish-project-alist
))))
893 (org-export-async-start (lambda (_) nil
)
894 `(let ((org-publish-use-timestamps-flag
895 ,(and (not force
) org-publish-use-timestamps-flag
)))
896 ;; Expand components right now as external process may not
897 ;; be aware of complete `org-publish-project-alist'.
898 (org-publish-projects
899 ',(org-publish-expand-projects (list project
))))))
900 (t (save-window-excursion
901 (let ((org-publish-use-timestamps-flag
902 (and (not force
) org-publish-use-timestamps-flag
)))
903 (org-publish-projects (list project
))))))))
906 (defun org-publish-all (&optional force async
)
907 "Publish all projects.
908 With prefix argument FORCE, remove all files in the timestamp
909 directory and force publishing all projects. With a non-nil
910 optional argument ASYNC, publishing will be done asynchronously,
914 (org-export-async-start (lambda (_) nil
)
916 (when ',force
(org-publish-remove-all-timestamps))
917 (let ((org-publish-use-timestamps-flag
918 (if ',force nil
,org-publish-use-timestamps-flag
)))
919 (org-publish-projects ',org-publish-project-alist
))))
920 (when force
(org-publish-remove-all-timestamps))
921 (save-window-excursion
922 (let ((org-publish-use-timestamps-flag
923 (if force nil org-publish-use-timestamps-flag
)))
924 (org-publish-projects org-publish-project-alist
)))))
928 (defun org-publish-current-file (&optional force async
)
929 "Publish the current file.
930 With prefix argument FORCE, force publish the file. When
931 optional argument ASYNC is non-nil, publishing will be done
932 asynchronously, in another process."
934 (let ((file (buffer-file-name (buffer-base-buffer))))
936 (org-export-async-start (lambda (_) nil
)
937 `(let ((org-publish-use-timestamps-flag
938 (if ',force nil
,org-publish-use-timestamps-flag
)))
939 (org-publish-file ,file
)))
940 (save-window-excursion
941 (let ((org-publish-use-timestamps-flag
942 (if force nil org-publish-use-timestamps-flag
)))
943 (org-publish-file file
))))))
946 (defun org-publish-current-project (&optional force async
)
947 "Publish the project associated with the current file.
948 With a prefix argument, force publishing of all files in
951 (save-window-excursion
952 (let ((project (org-publish-get-project-from-filename
953 (buffer-file-name (buffer-base-buffer)) 'up
)))
954 (if project
(org-publish project force async
)
955 (error "File %s is not part of any known project"
956 (buffer-file-name (buffer-base-buffer)))))))
962 (defun org-publish-collect-index (output _backend info
)
963 "Update index for a file in cache.
965 OUTPUT is the output from transcoding current file. BACKEND is
966 the back-end that was used for transcoding. INFO is a plist
967 containing publishing and export options.
969 The index relative to current file is stored as an alist. An
970 association has the following shape: (TERM FILE-NAME PARENT),
971 where TERM is the indexed term, as a string, FILE-NAME is the
972 original full path of the file where the term in encountered, and
973 PARENT is a reference to the headline, if any, containing the
974 original index keyword. When non-nil, this reference is a cons
975 cell. Its CAR is a symbol among `id', `custom-id' and `name' and
976 its CDR is a string."
977 (let ((file (plist-get info
:input-file
)))
978 (org-publish-cache-set-file-property
981 (org-element-map (plist-get info
:parse-tree
) 'keyword
983 (when (equal (org-element-property :key k
) "INDEX")
984 (let ((parent (org-export-get-parent-headline k
)))
985 (list (org-element-property :value k
)
989 ((let ((id (org-element-property :ID parent
)))
990 (and id
(cons 'id id
))))
991 ((let ((id (org-element-property :CUSTOM_ID parent
)))
992 (and id
(cons 'custom-id id
))))
994 ;; Remove statistics cookie.
995 (replace-regexp-in-string
996 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
997 (org-element-property :raw-value parent
)))))))))
999 ;; Return output unchanged.
1002 (defun org-publish-index-generate-theindex (project directory
)
1003 "Retrieve full index from cache and build \"theindex.org\".
1004 PROJECT is the project the index relates to. DIRECTORY is the
1005 publishing directory."
1006 (let ((all-files (org-publish-get-base-files
1007 project
(plist-get (cdr project
) :exclude
)))
1009 ;; Compile full index and sort it alphabetically.
1010 (dolist (file all-files
1012 (sort (nreverse full-index
)
1013 (lambda (a b
) (string< (downcase (car a
))
1014 (downcase (car b
)))))))
1015 (let ((index (org-publish-cache-get-file-property file
:index
)))
1016 (dolist (term index
)
1017 (unless (member term full-index
) (push term full-index
)))))
1018 ;; Write "theindex.inc" in DIRECTORY.
1019 (with-temp-file (expand-file-name "theindex.inc" directory
)
1020 (let ((current-letter nil
) (last-entry nil
))
1021 (dolist (idx full-index
)
1022 (let* ((entry (org-split-string (car idx
) "!"))
1023 (letter (upcase (substring (car entry
) 0 1)))
1024 ;; Transform file into a path relative to publishing
1026 (file (file-relative-name
1028 (plist-get (cdr project
) :base-directory
))))
1029 ;; Check if another letter has to be inserted.
1030 (unless (string= letter current-letter
)
1031 (insert (format "* %s\n" letter
)))
1032 ;; Compute the first difference between last entry and
1033 ;; current one: it tells the level at which new items
1036 (if (equal entry last-entry
) (1- (length entry
))
1037 (cl-loop for n from
0 to
(length entry
)
1038 unless
(equal (nth n entry
) (nth n last-entry
))
1040 (len (length (nthcdr rank entry
))))
1041 ;; For each term after the first difference, create
1042 ;; a new sub-list with the term as body. Moreover,
1043 ;; linkify the last term.
1047 (make-string (* (+ rank n
) 2) ?\s
) " - "
1048 (if (not (= (1- len
) n
)) (nth (+ rank n
) entry
)
1049 ;; Last term: Link it to TARGET, if possible.
1050 (let ((target (nth 2 idx
)))
1055 (`nil
(format "file:%s" file
))
1056 (`id
(format "id:%s" (cdr target
)))
1057 (`custom-id
(format "file:%s::#%s" file
(cdr target
)))
1058 (_ (format "file:%s::*%s" file
(cdr target
))))
1060 (car (last entry
)))))
1062 (setq current-letter letter last-entry entry
))))
1063 ;; Create "theindex.org", if it doesn't exist yet, and provide
1064 ;; a default index file.
1065 (let ((index.org
(expand-file-name "theindex.org" directory
)))
1066 (unless (file-exists-p index.org
)
1067 (with-temp-file index.org
1068 (insert "#+TITLE: Index\n\n#+INCLUDE: \"theindex.inc\"\n\n")))))))
1072 ;;; External Fuzzy Links Resolution
1074 ;; This part implements tools to resolve [[file.org::*Some headline]]
1075 ;; links, where "file.org" belongs to the current project.
1077 (defun org-publish--store-crossrefs (output _backend info
)
1078 "Store cross-references for current published file.
1080 OUPUT is the produced output, as a string. BACKEND is the export
1081 back-end used, as a symbol. INFO is the final export state, as
1084 This function is meant to be used as a final output filter. See
1085 `org-publish-org-to'."
1086 (org-publish-cache-set-file-property
1087 (plist-get info
:input-file
) :crossrefs
1088 ;; Update `:crossrefs' so as to remove unused references and search
1089 ;; cells. Actually used references are extracted from
1090 ;; `:internal-references', with references as strings removed. See
1091 ;; `org-export-get-reference' for details.
1092 (cl-remove-if (lambda (pair) (stringp (car pair
)))
1093 (plist-get info
:internal-references
)))
1094 ;; Return output unchanged.
1097 (defun org-publish-resolve-external-link (search file
)
1098 "Return reference for element matching string SEARCH in FILE.
1100 Return value is an internal reference, as a string.
1102 This function allows resolving external links with a search
1105 [[file.org::*heading][description]]
1106 [[file.org::#custom-id][description]]
1107 [[file.org::fuzzy][description]]
1109 It only makes sense to use this if export back-end builds
1110 references with `org-export-get-reference'."
1111 (if (not org-publish-cache
)
1113 (message "Reference %S in file %S cannot be resolved without publishing"
1117 (let* ((filename (expand-file-name file
))
1119 (org-publish-cache-get-file-property filename
:crossrefs nil t
))
1120 (cells (org-export-string-to-search-cell search
)))
1122 ;; Look for reference associated to search cells triggered by
1123 ;; LINK. It can match when targeted file has been published
1125 (let ((known (cdr (cl-some (lambda (c) (assoc c crossrefs
)) cells
))))
1126 (and known
(org-export-format-reference known
)))
1127 ;; Search cell is unknown so far. Generate a new internal
1128 ;; reference that will be used when the targeted file will be
1130 (let ((new (org-export-new-reference crossrefs
)))
1131 (dolist (cell cells
) (push (cons cell new
) crossrefs
))
1132 (org-publish-cache-set-file-property filename
:crossrefs crossrefs
)
1133 (org-export-format-reference new
))))))
1137 ;;; Caching functions
1139 (defun org-publish-write-cache-file (&optional free-cache
)
1140 "Write `org-publish-cache' to file.
1141 If FREE-CACHE, empty the cache."
1142 (unless org-publish-cache
1143 (error "`org-publish-write-cache-file' called, but no cache present"))
1145 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1147 (error "Cannot find cache-file name in `org-publish-write-cache-file'"))
1148 (with-temp-file cache-file
1149 (let (print-level print-length
)
1150 (insert "(setq org-publish-cache \
1151 \(make-hash-table :test 'equal :weakness nil :size 100))\n")
1152 (maphash (lambda (k v
)
1154 (format "(puthash %S %s%S org-publish-cache)\n"
1155 k
(if (or (listp v
) (symbolp v
)) "'" "") v
)))
1156 org-publish-cache
)))
1157 (when free-cache
(org-publish-reset-cache))))
1159 (defun org-publish-initialize-cache (project-name)
1160 "Initialize the projects cache if not initialized yet and return it."
1162 (unless project-name
1163 (error "Cannot initialize `org-publish-cache' without projects name in \
1164 `org-publish-initialize-cache'"))
1166 (unless (file-exists-p org-publish-timestamp-directory
)
1167 (make-directory org-publish-timestamp-directory t
))
1168 (unless (file-directory-p org-publish-timestamp-directory
)
1169 (error "Org publish timestamp: %s is not a directory"
1170 org-publish-timestamp-directory
))
1172 (unless (and org-publish-cache
1173 (string= (org-publish-cache-get ":project:") project-name
))
1176 (expand-file-name org-publish-timestamp-directory
)
1177 project-name
".cache"))
1178 (cexists (file-exists-p cache-file
)))
1180 (when org-publish-cache
(org-publish-reset-cache))
1182 (if cexists
(load-file cache-file
)
1183 (setq org-publish-cache
1184 (make-hash-table :test
'equal
:weakness nil
:size
100))
1185 (org-publish-cache-set ":project:" project-name
)
1186 (org-publish-cache-set ":cache-file:" cache-file
))
1187 (unless cexists
(org-publish-write-cache-file nil
))))
1190 (defun org-publish-reset-cache ()
1191 "Empty org-publish-cache and reset it nil."
1192 (message "%s" "Resetting org-publish-cache")
1193 (when (hash-table-p org-publish-cache
)
1194 (clrhash org-publish-cache
))
1195 (setq org-publish-cache nil
))
1197 (defun org-publish-cache-file-needs-publishing
1198 (filename &optional pub-dir pub-func _base-dir
)
1199 "Check the timestamp of the last publishing of FILENAME.
1200 Return non-nil if the file needs publishing. Also check if
1201 any included files have been more recently published, so that
1202 the file including them will be republished as well."
1203 (unless org-publish-cache
1205 "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1206 (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func
))
1207 (pstamp (org-publish-cache-get key
))
1208 (org-inhibit-startup t
)
1209 included-files-ctime
)
1210 (when (equal (file-name-extension filename
) "org")
1211 (let ((visiting (find-buffer-visiting filename
))
1212 (buf (find-file-noselect filename
))
1213 (case-fold-search t
))
1215 (with-current-buffer buf
1216 (goto-char (point-min))
1217 (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t
)
1218 (let* ((element (org-element-at-point))
1220 (and (eq (org-element-type element
) 'keyword
)
1221 (let ((value (org-element-property :value element
)))
1224 "\\`\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)"
1226 (let ((m (match-string 1 value
)))
1227 (org-unbracket-string
1229 ;; Ignore search suffix.
1230 (if (string-match "::.*?\"?\\'" m
)
1231 (substring m
0 (match-beginning 0))
1234 (push (org-publish-cache-ctime-of-src
1235 (expand-file-name included-file
))
1236 included-files-ctime
)))))
1237 (unless visiting
(kill-buffer buf
)))))
1239 (let ((ctime (org-publish-cache-ctime-of-src filename
)))
1240 (or (< pstamp ctime
)
1241 (cl-some (lambda (ct) (< ctime ct
)) included-files-ctime
))))))
1243 (defun org-publish-cache-set-file-property
1244 (filename property value
&optional project-name
)
1245 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1246 Use cache file of PROJECT-NAME. If the entry does not exist, it
1247 will be created. Return VALUE."
1248 ;; Evtl. load the requested cache file:
1249 (if project-name
(org-publish-initialize-cache project-name
))
1250 (let ((pl (org-publish-cache-get filename
)))
1251 (if pl
(progn (plist-put pl property value
) value
)
1252 (org-publish-cache-get-file-property
1253 filename property value nil project-name
))))
1255 (defun org-publish-cache-get-file-property
1256 (filename property
&optional default no-create project-name
)
1257 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1258 Use cache file of PROJECT-NAME. Return the value of that PROPERTY,
1259 or DEFAULT, if the value does not yet exist. Create the entry,
1260 if necessary, unless NO-CREATE is non-nil."
1261 ;; Evtl. load the requested cache file:
1262 (if project-name
(org-publish-initialize-cache project-name
))
1263 (let ((pl (org-publish-cache-get filename
)) retval
)
1265 (if (plist-member pl property
)
1266 (setq retval
(plist-get pl property
))
1267 (setq retval default
))
1270 (org-publish-cache-set filename
(list property default
)))
1271 (setq retval default
))
1274 (defun org-publish-cache-get (key)
1275 "Return the value stored in `org-publish-cache' for key KEY.
1276 Return nil, if no value or nil is found. Raise an error if the
1277 cache does not exist."
1278 (unless org-publish-cache
1279 (error "`org-publish-cache-get' called, but no cache present"))
1280 (gethash key org-publish-cache
))
1282 (defun org-publish-cache-set (key value
)
1283 "Store KEY VALUE pair in `org-publish-cache'.
1284 Returns value on success, else nil. Raise an error if the cache
1286 (unless org-publish-cache
1287 (error "`org-publish-cache-set' called, but no cache present"))
1288 (puthash key value org-publish-cache
))
1290 (defun org-publish-cache-ctime-of-src (file)
1291 "Get the ctime of FILE as an integer."
1292 (let ((attr (file-attributes
1293 (expand-file-name (or (file-symlink-p file
) file
)
1294 (file-name-directory file
)))))
1295 (if (not attr
) (error "No such file: \"%s\"" file
)
1296 (+ (lsh (car (nth 5 attr
)) 16)
1297 (cadr (nth 5 attr
))))))
1300 (provide 'ox-publish
)
1303 ;; generated-autoload-file: "org-loaddefs.el"
1306 ;;; ox-publish.el ends here