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 Org-mode and related files."
68 (defcustom org-publish-project-alist nil
69 "Association list to control publishing behavior.
70 Each element of the alist is a publishing project. The CAR of
71 each element is a string, uniquely identifying the project. The
72 CDR of each element is in one of the following forms:
74 1. A well-formed property list with an even number of elements,
75 alternating keys and values, specifying parameters for the
78 (:property value :property value ... )
80 2. A meta-project definition, specifying of a list of
83 (:components (\"project-1\" \"project-2\" ...))
85 When the CDR of an element of org-publish-project-alist is in
86 this second form, the elements of the list after `:components'
87 are taken to be components of the project, which group together
88 files requiring different publishing options. When you publish
89 such a project with \\[org-publish], the components all publish.
91 When a property is given a value in `org-publish-project-alist',
92 its setting overrides the value of the corresponding user
93 variable (if any) during publishing. However, options set within
94 a file override everything.
96 Most properties are optional, but some should always be set:
100 Directory containing publishing source files.
104 Extension (without the dot!) of source files. This can be
105 a regular expression. If not given, \"org\" will be used as
108 `:publishing-directory'
110 Directory (possibly remote) where output files will be
113 If `:recursive' is non-nil files in sub-directories of
114 `:base-directory' are considered.
116 The `:exclude' property may be used to prevent certain files from
117 being published. Its value may be a string or regexp matching
118 file names you don't want to be published.
120 The `:include' property may be used to include extra files. Its
121 value may be a list of filenames to include. The filenames are
122 considered relative to the base directory.
124 When both `:include' and `:exclude' properties are given values,
125 the exclusion step happens first.
127 One special property controls which back-end function to use for
128 publishing files in the project. This can be used to extend the
129 set of file types publishable by `org-publish', as well as the
130 set of output formats.
132 `:publishing-function'
134 Function to publish file. Each back-end may define its
135 own (i.e. `org-latex-publish-to-pdf',
136 `org-html-publish-to-html'). May be a list of functions, in
137 which case each function in the list is invoked in turn.
139 Another property allows you to insert code that prepares
140 a project for publishing. For example, you could call GNU Make
141 on a certain makefile, to ensure published files are built up to
144 `:preparation-function'
146 Function to be called before publishing this project. This
147 may also be a list of functions.
149 `:completion-function'
151 Function to be called after publishing this project. This
152 may also be a list of functions.
154 Some properties control details of the Org publishing process,
155 and are equivalent to the corresponding user variables listed in
156 the right column. Back-end specific properties may also be
157 included. See the back-end documentation for more information.
159 :author `user-full-name'
160 :creator `org-export-creator-string'
161 :email `user-mail-address'
162 :exclude-tags `org-export-exclude-tags'
163 :headline-levels `org-export-headline-levels'
164 :language `org-export-default-language'
165 :preserve-breaks `org-export-preserve-breaks'
166 :section-numbers `org-export-with-section-numbers'
167 :select-tags `org-export-select-tags'
168 :time-stamp-file `org-export-time-stamp-file'
169 :with-archived-trees `org-export-with-archived-trees'
170 :with-author `org-export-with-author'
171 :with-creator `org-export-with-creator'
172 :with-date `org-export-with-date'
173 :with-drawers `org-export-with-drawers'
174 :with-email `org-export-with-email'
175 :with-emphasize `org-export-with-emphasize'
176 :with-entities `org-export-with-entities'
177 :with-fixed-width `org-export-with-fixed-width'
178 :with-footnotes `org-export-with-footnotes'
179 :with-inlinetasks `org-export-with-inlinetasks'
180 :with-latex `org-export-with-latex'
181 :with-planning `org-export-with-planning'
182 :with-priority `org-export-with-priority'
183 :with-properties `org-export-with-properties'
184 :with-smart-quotes `org-export-with-smart-quotes'
185 :with-special-strings `org-export-with-special-strings'
186 :with-statistics-cookies' `org-export-with-statistics-cookies'
187 :with-sub-superscript `org-export-with-sub-superscripts'
188 :with-toc `org-export-with-toc'
189 :with-tables `org-export-with-tables'
190 :with-tags `org-export-with-tags'
191 :with-tasks `org-export-with-tasks'
192 :with-timestamps `org-export-with-timestamps'
193 :with-title `org-export-with-title'
194 :with-todo-keywords `org-export-with-todo-keywords'
196 The following properties may be used to control publishing of
197 a site-map of files or summary page for a given project.
201 Whether to publish a site-map during
202 `org-publish-current-project' or `org-publish-all'.
206 Filename for output of sitemap. Defaults to \"sitemap.org\".
210 Title of site-map page. Defaults to name of file.
214 Plugin function to use for generation of site-map. Defaults
215 to `org-publish-org-sitemap', which generates a plain list of
216 links to all files in the project.
220 Can be `list' (site-map is just an itemized list of the
221 titles of the files involved) or `tree' (the directory
222 structure of the source files is reflected in the site-map).
225 `:sitemap-sans-extension'
227 Remove extension from site-map's file-names. Useful to have
228 cool URIs (see http://www.w3.org/Provider/Style/URI).
231 If you create a site-map file, adjust the sorting like this:
233 `:sitemap-sort-folders'
235 Where folders should appear in the site-map. Set this to
236 `first' (default) or `last' to display folders first or last,
237 respectively. Any other value will mix files and folders.
239 `:sitemap-sort-files'
241 The site map is normally sorted alphabetically. You can
242 change this behavior setting this to `anti-chronologically',
243 `chronologically', or nil.
245 `:sitemap-ignore-case'
247 Should sorting be case-sensitive? Default nil.
249 The following property control the creation of a concept index.
253 Create a concept index. The file containing the index has to
254 be called \"theindex.org\". If it doesn't exist in the
255 project, it will be generated. Contents of the index are
256 stored in the file \"theindex.inc\", which can be included in
259 Other properties affecting publication.
263 Set this to t to publish only the body of the documents."
264 :group
'org-export-publish
267 (defcustom org-publish-use-timestamps-flag t
268 "Non-nil means use timestamp checking to publish only changed files.
269 When nil, do no timestamp checking and always publish all files."
270 :group
'org-export-publish
273 (defcustom org-publish-timestamp-directory
274 (convert-standard-filename "~/.org-timestamps/")
275 "Name of directory in which to store publishing timestamps."
276 :group
'org-export-publish
279 (defcustom org-publish-list-skipped-files t
280 "Non-nil means show message about files *not* published."
281 :group
'org-export-publish
284 (defcustom org-publish-sitemap-sort-files
'alphabetically
285 "Method to sort files in site-maps.
286 Possible values are `alphabetically', `chronologically',
287 `anti-chronologically' and nil.
289 If `alphabetically', files will be sorted alphabetically. If
290 `chronologically', files will be sorted with older modification
291 time first. If `anti-chronologically', files will be sorted with
292 newer modification time first. nil won't sort files.
294 You can overwrite this default per project in your
295 `org-publish-project-alist', using `:sitemap-sort-files'."
296 :group
'org-export-publish
299 (defcustom org-publish-sitemap-sort-folders
'first
300 "A symbol, denoting if folders are sorted first in sitemaps.
301 Possible values are `first', `last', and nil.
302 If `first', folders will be sorted before files.
303 If `last', folders are sorted to the end after the files.
304 Any other value will not mix files and folders.
306 You can overwrite this default per project in your
307 `org-publish-project-alist', using `:sitemap-sort-folders'."
308 :group
'org-export-publish
311 (defcustom org-publish-sitemap-sort-ignore-case nil
312 "Non-nil when site-map sorting should ignore case.
314 You can overwrite this default per project in your
315 `org-publish-project-alist', using `:sitemap-ignore-case'."
316 :group
'org-export-publish
319 (defcustom org-publish-sitemap-date-format
"%Y-%m-%d"
320 "Format for printing a date in the sitemap.
321 See `format-time-string' for allowed formatters."
322 :group
'org-export-publish
325 (defcustom org-publish-sitemap-file-entry-format
"%t"
326 "Format string for site-map file entry.
327 You could use brackets to delimit on what part the link will be.
331 %d is the date formatted using `org-publish-sitemap-date-format'."
332 :group
'org-export-publish
337 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
338 ;;; Timestamp-related functions
340 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func
)
341 "Return path to timestamp file for filename FILENAME."
342 (setq filename
(concat filename
"::" (or pub-dir
"") "::"
343 (format "%s" (or pub-func
""))))
344 (concat "X" (if (fboundp 'sha1
) (sha1 filename
) (md5 filename
))))
346 (defun org-publish-needed-p
347 (filename &optional pub-dir pub-func _true-pub-dir base-dir
)
348 "Non-nil if FILENAME should be published in PUB-DIR using PUB-FUNC.
349 TRUE-PUB-DIR is where the file will truly end up. Currently we
350 are not using this - maybe it can eventually be used to check if
351 the file is present at the target location, and how old it is.
352 Right now we cannot do this, because we do not know under what
353 file name the file will be stored - the publishing function can
354 still decide about that independently."
355 (let ((rtn (if (not org-publish-use-timestamps-flag
) t
356 (org-publish-cache-file-needs-publishing
357 filename pub-dir pub-func base-dir
))))
358 (if rtn
(message "Publishing file %s using `%s'" filename pub-func
)
359 (when org-publish-list-skipped-files
360 (message "Skipping unmodified file %s" filename
)))
363 (defun org-publish-update-timestamp
364 (filename &optional pub-dir pub-func _base-dir
)
365 "Update publishing timestamp for file FILENAME.
366 If there is no timestamp, create one."
367 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func
))
368 (stamp (org-publish-cache-ctime-of-src filename
)))
369 (org-publish-cache-set key stamp
)))
371 (defun org-publish-remove-all-timestamps ()
372 "Remove all files in the timestamp directory."
373 (let ((dir org-publish-timestamp-directory
))
374 (when (and (file-exists-p dir
) (file-directory-p dir
))
375 (mapc #'delete-file
(directory-files dir
'full
"[^.]\\'"))
376 (org-publish-reset-cache))))
380 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
381 ;;; Getting project information out of `org-publish-project-alist'
383 (defun org-publish-expand-projects (projects-alist)
384 "Expand projects in PROJECTS-ALIST.
385 This splices all the components into the list."
386 (let ((rest projects-alist
) rtn p components
)
387 (while (setq p
(pop rest
))
388 (if (setq components
(plist-get (cdr p
) :components
))
390 (mapcar (lambda (x) (assoc x org-publish-project-alist
))
394 (nreverse (delete-dups (delq nil rtn
)))))
396 (defvar org-publish-sitemap-sort-files
)
397 (defvar org-publish-sitemap-sort-folders
)
398 (defvar org-publish-sitemap-ignore-case
)
399 (defvar org-publish-sitemap-requested
)
400 (defvar org-publish-sitemap-date-format
)
401 (defvar org-publish-sitemap-file-entry-format
)
402 (defun org-publish-compare-directory-files (a b
)
403 "Predicate for `sort', that sorts folders and files for sitemap."
405 (when (or org-publish-sitemap-sort-files org-publish-sitemap-sort-folders
)
406 ;; First we sort files:
407 (when org-publish-sitemap-sort-files
408 (pcase org-publish-sitemap-sort-files
410 (let* ((adir (file-directory-p a
))
411 (aorg (and (string-match "\\.org$" a
) (not adir
)))
412 (bdir (file-directory-p b
))
413 (borg (and (string-match "\\.org$" b
) (not bdir
)))
414 (A (if aorg
(concat (file-name-directory a
)
415 (org-publish-find-title a
)) a
))
416 (B (if borg
(concat (file-name-directory b
)
417 (org-publish-find-title b
)) b
)))
418 (setq retval
(if org-publish-sitemap-ignore-case
419 (not (string-lessp (upcase B
) (upcase A
)))
420 (not (string-lessp B A
))))))
421 ((or `anti-chronologically
`chronologically
)
422 (let* ((adate (org-publish-find-date a
))
423 (bdate (org-publish-find-date b
))
424 (A (+ (lsh (car adate
) 16) (cadr adate
)))
425 (B (+ (lsh (car bdate
) 16) (cadr bdate
))))
427 (if (eq org-publish-sitemap-sort-files
'chronologically
)
430 ;; Directory-wise wins:
431 (when org-publish-sitemap-sort-folders
432 ;; a is directory, b not:
434 ((and (file-directory-p a
) (not (file-directory-p b
)))
435 (setq retval
(equal org-publish-sitemap-sort-folders
'first
)))
436 ;; a is not a directory, but b is:
437 ((and (not (file-directory-p a
)) (file-directory-p b
))
438 (setq retval
(equal org-publish-sitemap-sort-folders
'last
))))))
441 (defun org-publish-get-base-files-1
442 (base-dir &optional recurse match skip-file skip-dir
)
443 "Set `org-publish-temp-files' with files from BASE-DIR directory.
444 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
445 non-nil, restrict this list to the files matching the regexp
446 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
447 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
448 matching the regexp SKIP-DIR when recursing through BASE-DIR."
449 (let ((all-files (if (not recurse
) (directory-files base-dir t match
)
450 ;; If RECURSE is non-nil, we want all files
451 ;; matching MATCH and sub-directories.
454 (or (file-directory-p file
)
455 (and match
(string-match match file
))))
456 (directory-files base-dir t
)))))
457 (dolist (f (if (not org-publish-sitemap-requested
) all-files
458 (sort all-files
#'org-publish-compare-directory-files
)))
459 (let ((fd-p (file-directory-p f
))
460 (fnd (file-name-nondirectory f
)))
461 (if (and fd-p recurse
462 (not (string-match "^\\.+$" fnd
))
463 (if skip-dir
(not (string-match skip-dir fnd
)) t
))
464 (org-publish-get-base-files-1
465 f recurse match skip-file skip-dir
)
466 (unless (or fd-p
; This is a directory.
467 (and skip-file
(string-match skip-file fnd
))
468 (not (file-exists-p (file-truename f
)))
469 (not (string-match match fnd
)))
470 (cl-pushnew f org-publish-temp-files
)))))))
472 (defun org-publish-get-base-files (project &optional exclude-regexp
)
473 "Return a list of all files in PROJECT.
474 If EXCLUDE-REGEXP is set, this will be used to filter out
476 (let* ((project-plist (cdr project
))
477 (base-dir (file-name-as-directory
478 (plist-get project-plist
:base-directory
)))
479 (include-list (plist-get project-plist
:include
))
480 (recurse (plist-get project-plist
:recursive
))
481 (extension (or (plist-get project-plist
:base-extension
) "org"))
482 ;; sitemap-... variables are dynamically scoped for
483 ;; org-publish-compare-directory-files:
484 (org-publish-sitemap-requested
485 (plist-get project-plist
:auto-sitemap
))
487 (or (plist-get project-plist
:sitemap-filename
) "sitemap.org"))
488 (org-publish-sitemap-sort-folders
489 (if (plist-member project-plist
:sitemap-sort-folders
)
490 (plist-get project-plist
:sitemap-sort-folders
)
491 org-publish-sitemap-sort-folders
))
492 (org-publish-sitemap-sort-files
493 (cond ((plist-member project-plist
:sitemap-sort-files
)
494 (plist-get project-plist
:sitemap-sort-files
))
495 ;; For backward compatibility:
496 ((plist-member project-plist
:sitemap-alphabetically
)
497 (if (plist-get project-plist
:sitemap-alphabetically
)
498 'alphabetically nil
))
499 (t org-publish-sitemap-sort-files
)))
500 (org-publish-sitemap-ignore-case
501 (if (plist-member project-plist
:sitemap-ignore-case
)
502 (plist-get project-plist
:sitemap-ignore-case
)
503 org-publish-sitemap-sort-ignore-case
))
504 (match (if (eq extension
'any
) "^[^\\.]"
505 (concat "^[^\\.].*\\.\\(" extension
"\\)$"))))
506 ;; Make sure `org-publish-sitemap-sort-folders' has an accepted
508 (unless (memq org-publish-sitemap-sort-folders
'(first last
))
509 (setq org-publish-sitemap-sort-folders nil
))
511 (setq org-publish-temp-files nil
)
512 (when org-publish-sitemap-requested
513 (cl-pushnew (expand-file-name (concat base-dir sitemap-filename
))
514 org-publish-temp-files
))
515 (org-publish-get-base-files-1 base-dir recurse match
516 ;; FIXME distinguish exclude regexp
517 ;; for skip-file and skip-dir?
518 exclude-regexp exclude-regexp
)
519 (dolist (f include-list org-publish-temp-files
)
520 (cl-pushnew (expand-file-name (concat base-dir f
))
521 org-publish-temp-files
))))
523 (defun org-publish-get-project-from-filename (filename &optional up
)
524 "Return the project that FILENAME belongs to."
525 (let* ((filename (expand-file-name filename
))
529 (dolist (prj org-publish-project-alist
)
530 (unless (plist-get (cdr prj
) :components
)
531 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
532 (let* ((r (plist-get (cdr prj
) :recursive
))
533 (b (expand-file-name (file-name-as-directory
534 (plist-get (cdr prj
) :base-directory
))))
535 (x (or (plist-get (cdr prj
) :base-extension
) "org"))
536 (e (plist-get (cdr prj
) :exclude
))
537 (i (plist-get (cdr prj
) :include
))
538 (xm (concat "^" b
(if r
".+" "[^/]+") "\\.\\(" x
"\\)$")))
542 (dolist (file i
) (expand-file-name file b
))))
543 (and (not (and e
(string-match e filename
)))
544 (string-match xm filename
)))
545 (setq project-name
(car prj
))
546 (throw 'p-found project-name
))))))
548 (dolist (prj org-publish-project-alist
)
549 (if (member project-name
(plist-get (cdr prj
) :components
))
550 (setq project-name
(car prj
)))))
551 (assoc project-name org-publish-project-alist
)))
555 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
556 ;;; Tools for publishing functions in back-ends
558 (defun org-publish-org-to (backend filename extension plist
&optional pub-dir
)
559 "Publish an Org file to a specified back-end.
561 BACKEND is a symbol representing the back-end used for
562 transcoding. FILENAME is the filename of the Org file to be
563 published. EXTENSION is the extension used for the output
564 string, with the leading dot. PLIST is the property list for the
567 Optional argument PUB-DIR, when non-nil is the publishing
570 Return output file name."
571 (unless (or (not pub-dir
) (file-exists-p pub-dir
)) (make-directory pub-dir t
))
572 ;; Check if a buffer visiting FILENAME is already open.
573 (let* ((org-inhibit-startup t
)
574 (visiting (find-buffer-visiting filename
))
575 (work-buffer (or visiting
(find-file-noselect filename
))))
577 (with-current-buffer work-buffer
578 (let ((output (org-export-output-file-name extension nil pub-dir
)))
579 (org-export-to-file backend output
580 nil nil nil
(plist-get plist
:body-only
)
581 ;; Add `org-publish--store-crossrefs' and
582 ;; `org-publish-collect-index' to final output filters.
583 ;; The latter isn't dependent on `:makeindex', since we
584 ;; want to keep it up-to-date in cache anyway.
588 ,(org-publish-cache-get-file-property
589 (expand-file-name filename
) :crossrefs nil t
)
591 (org-publish--store-crossrefs
592 org-publish-collect-index
593 ,@(plist-get plist
:filter-final-output
)))))))
594 ;; Remove opened buffer in the process.
595 (unless visiting
(kill-buffer work-buffer
)))))
597 (defun org-publish-attachment (_plist filename pub-dir
)
598 "Publish a file with no transformation of any kind.
600 FILENAME is the filename of the Org file to be published. PLIST
601 is the property list for the given project. PUB-DIR is the
602 publishing directory.
604 Return output file name."
605 (unless (file-directory-p pub-dir
)
606 (make-directory pub-dir t
))
607 (let ((output (expand-file-name (file-name-nondirectory filename
) pub-dir
)))
608 (or (equal (expand-file-name (file-name-directory filename
))
609 (file-name-as-directory (expand-file-name pub-dir
)))
610 (copy-file filename output t
))
616 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
617 ;;; Publishing files, sets of files, and indices
619 (defun org-publish-file (filename &optional project no-cache
)
620 "Publish file FILENAME from PROJECT.
621 If NO-CACHE is not nil, do not initialize `org-publish-cache'.
622 This is needed, since this function is used to publish single
623 files, when entire projects are published (see
624 `org-publish-projects')."
627 (or (org-publish-get-project-from-filename filename
)
628 (error "File %s not part of any known project"
629 (abbreviate-file-name filename
)))))
630 (project-plist (cdr project
))
631 (ftname (expand-file-name filename
))
633 (let ((fun (plist-get project-plist
:publishing-function
)))
634 (cond ((null fun
) (error "No publishing function chosen"))
638 (file-name-as-directory
640 (or (plist-get project-plist
:base-directory
)
641 (error "Project %s does not have :base-directory defined"
644 (file-name-as-directory
646 (or (eval (plist-get project-plist
:publishing-directory
))
647 (error "Project %s does not have :publishing-directory defined"
651 (unless no-cache
(org-publish-initialize-cache (car project
)))
656 (and (string-match (regexp-quote base-dir
) ftname
)
657 (substring ftname
(match-end 0))))))
658 ;; Allow chain of publishing functions.
659 (dolist (f publishing-function
)
660 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir base-dir
)
661 (let ((output (funcall f project-plist filename tmp-pub-dir
)))
662 (org-publish-update-timestamp filename pub-dir f base-dir
)
663 (run-hook-with-args 'org-publish-after-publishing-hook
666 ;; Make sure to write cache to file after successfully publishing
667 ;; a file, so as to minimize impact of a publishing failure.
668 (org-publish-write-cache-file)))
670 (defun org-publish-projects (projects)
671 "Publish all files belonging to the PROJECTS alist.
672 If `:auto-sitemap' is set, publish the sitemap too. If
673 `:makeindex' is set, also produce a file \"theindex.org\"."
674 (dolist (project (org-publish-expand-projects projects
))
675 (let ((project-plist (cdr project
)))
676 (let ((f (plist-get project-plist
:preparation-function
)))
677 (cond ((consp f
) (mapc #'funcall f
))
678 ((functionp f
) (funcall f
))))
679 ;; Each project uses its own cache file.
680 (org-publish-initialize-cache (car project
))
681 (when (plist-get project-plist
:auto-sitemap
)
682 (let ((sitemap-filename
683 (or (plist-get project-plist
:sitemap-filename
)
686 (or (plist-get project-plist
:sitemap-function
)
687 #'org-publish-org-sitemap
))
688 (org-publish-sitemap-date-format
689 (or (plist-get project-plist
:sitemap-date-format
)
690 org-publish-sitemap-date-format
))
691 (org-publish-sitemap-file-entry-format
692 (or (plist-get project-plist
:sitemap-file-entry-format
)
693 org-publish-sitemap-file-entry-format
)))
694 (funcall sitemap-function project sitemap-filename
)))
695 ;; Publish all files from PROJECT excepted "theindex.org". Its
696 ;; publishing will be deferred until "theindex.inc" is
699 (expand-file-name "theindex.org"
700 (plist-get project-plist
:base-directory
)))
701 (exclude-regexp (plist-get project-plist
:exclude
)))
702 (dolist (file (org-publish-get-base-files project exclude-regexp
))
703 (unless (equal file theindex
) (org-publish-file file project t
)))
704 ;; Populate "theindex.inc", if needed, and publish
706 (when (plist-get project-plist
:makeindex
)
707 (org-publish-index-generate-theindex
708 project
(plist-get project-plist
:base-directory
))
709 (org-publish-file theindex project t
)))
710 (let ((f (plist-get project-plist
:completion-function
)))
711 (cond ((consp f
) (mapc #'funcall f
))
712 ((functionp f
) (funcall f
))))
713 (org-publish-write-cache-file))))
715 (defun org-publish-org-sitemap (project &optional sitemap-filename
)
716 "Create a sitemap of pages in set defined by PROJECT.
717 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
718 Default for SITEMAP-FILENAME is `sitemap.org'."
719 (let* ((project-plist (cdr project
))
720 (dir (file-name-as-directory
721 (plist-get project-plist
:base-directory
)))
722 (localdir (file-name-directory dir
))
723 (indent-str (make-string 2 ?\
))
724 (exclude-regexp (plist-get project-plist
:exclude
))
726 (org-publish-get-base-files project exclude-regexp
)))
727 (sitemap-filename (concat dir
(or sitemap-filename
"sitemap.org")))
728 (sitemap-title (or (plist-get project-plist
:sitemap-title
)
729 (concat "Sitemap for project " (car project
))))
730 (sitemap-style (or (plist-get project-plist
:sitemap-style
)
732 (sitemap-sans-extension
733 (plist-get project-plist
:sitemap-sans-extension
))
734 (visiting (find-buffer-visiting sitemap-filename
))
737 (let ((org-inhibit-startup t
))
739 (or visiting
(find-file sitemap-filename
))))
741 (insert (concat "#+TITLE: " sitemap-title
"\n\n"))
742 (while (setq file
(pop files
))
743 (let ((link (file-relative-name file dir
))
745 (when sitemap-sans-extension
746 (setq link
(file-name-sans-extension link
)))
747 ;; sitemap shouldn't list itself
748 (unless (equal (file-truename sitemap-filename
)
749 (file-truename file
))
750 (if (eq sitemap-style
'list
)
751 (message "Generating list-style sitemap for %s" sitemap-title
)
752 (message "Generating tree-style sitemap for %s" sitemap-title
)
753 (setq localdir
(concat (file-name-as-directory dir
)
754 (file-name-directory link
)))
755 (unless (string= localdir oldlocal
)
756 (if (string= localdir dir
)
757 (setq indent-str
(make-string 2 ?\
))
762 (file-relative-name localdir dir
))) "/"))
764 (old-subdirs (split-string
765 (file-relative-name oldlocal dir
) "/")))
766 (setq indent-str
(make-string 2 ?\
))
767 (while (string= (car old-subdirs
) (car subdirs
))
768 (setq indent-str
(concat indent-str
(make-string 2 ?\
)))
772 (setq subdir
(concat subdir d
"/"))
773 (insert (concat indent-str
" + " d
"\n"))
774 (setq indent-str
(make-string
775 (+ (length indent-str
) 2) ?\
)))))))
776 ;; This is common to 'flat and 'tree
778 (org-publish-format-file-entry
779 org-publish-sitemap-file-entry-format file project-plist
))
780 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
781 (cond ((string-match-p regexp entry
)
782 (string-match regexp entry
)
783 (insert (concat indent-str
" + " (match-string 1 entry
)
785 (match-string 2 entry
)
786 "]]" (match-string 3 entry
) "\n")))
788 (insert (concat indent-str
" + [[file:" link
"]["
792 (or visiting
(kill-buffer sitemap-buffer
))))
794 (defun org-publish-format-file-entry (fmt file project-plist
)
797 `((?t .
,(org-publish-find-title file t
))
798 (?d .
,(format-time-string org-publish-sitemap-date-format
799 (org-publish-find-date file
)))
800 (?a .
,(or (plist-get project-plist
:author
) user-full-name
)))))
802 (defun org-publish-find-title (file &optional reset
)
803 "Find the title of FILE in project."
805 (and (not reset
) (org-publish-cache-get-file-property file
:title nil t
))
806 (let* ((org-inhibit-startup t
)
807 (visiting (find-buffer-visiting file
))
808 (buffer (or visiting
(find-file-noselect file
))))
809 (with-current-buffer buffer
813 ;; protect local variables in open buffers
815 (org-export-with-buffer-copy (org-export-get-environment))
816 (org-export-get-environment))
819 (org-no-properties (org-element-interpret-data property
))
820 (file-name-nondirectory (file-name-sans-extension file
))))))
821 (unless visiting
(kill-buffer buffer
))
822 (org-publish-cache-set-file-property file
:title title
)
825 (defun org-publish-find-date (file)
826 "Find the date of FILE in project.
827 This function assumes FILE is either a directory or an Org file.
828 If FILE is an Org file and provides a DATE keyword use it. In
829 any other case use the file system's modification time. Return
830 time in `current-time' format."
831 (if (file-directory-p file
) (nth 5 (file-attributes file
))
832 (let* ((org-inhibit-startup t
)
833 (visiting (find-buffer-visiting file
))
834 (file-buf (or visiting
(find-file-noselect file nil
)))
836 (with-current-buffer file-buf
838 (org-export-with-buffer-copy
839 (org-export-get-environment))
840 (org-export-get-environment)))
842 (unless visiting
(kill-buffer file-buf
))
843 ;; DATE is a secondary string. If it contains a timestamp,
844 ;; convert it to internal format. Otherwise, use FILE
845 ;; modification time.
846 (cond ((let ((ts (and (consp date
) (assq 'timestamp date
))))
848 (let ((value (org-element-interpret-data ts
)))
849 (and (org-string-nw-p value
)
850 (org-time-string-to-time value
))))))
851 ((file-exists-p file
) (nth 5 (file-attributes file
)))
852 (t (error "No such file: \"%s\"" file
))))))
856 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
857 ;;; Interactive publishing functions
860 (defalias 'org-publish-project
'org-publish
)
863 (defun org-publish (project &optional force async
)
866 PROJECT is either a project name, as a string, or a project
867 alist (see `org-publish-project-alist' variable).
869 When optional argument FORCE is non-nil, force publishing all
870 files in PROJECT. With a non-nil optional argument ASYNC,
871 publishing will be done asynchronously, in another process."
873 (list (assoc (completing-read "Publish project: "
874 org-publish-project-alist nil t
)
875 org-publish-project-alist
)
877 (let ((project (if (not (stringp project
)) project
878 ;; If this function is called in batch mode,
879 ;; PROJECT is still a string here.
880 (assoc project org-publish-project-alist
))))
884 (org-export-async-start (lambda (_) nil
)
885 `(let ((org-publish-use-timestamps-flag
886 ,(and (not force
) org-publish-use-timestamps-flag
)))
887 ;; Expand components right now as external process may not
888 ;; be aware of complete `org-publish-project-alist'.
889 (org-publish-projects
890 ',(org-publish-expand-projects (list project
))))))
891 (t (save-window-excursion
892 (let ((org-publish-use-timestamps-flag
893 (and (not force
) org-publish-use-timestamps-flag
)))
894 (org-publish-projects (list project
))))))))
897 (defun org-publish-all (&optional force async
)
898 "Publish all projects.
899 With prefix argument FORCE, remove all files in the timestamp
900 directory and force publishing all projects. With a non-nil
901 optional argument ASYNC, publishing will be done asynchronously,
905 (org-export-async-start (lambda (_) nil
)
907 (when ',force
(org-publish-remove-all-timestamps))
908 (let ((org-publish-use-timestamps-flag
909 (if ',force nil
,org-publish-use-timestamps-flag
)))
910 (org-publish-projects ',org-publish-project-alist
))))
911 (when force
(org-publish-remove-all-timestamps))
912 (save-window-excursion
913 (let ((org-publish-use-timestamps-flag
914 (if force nil org-publish-use-timestamps-flag
)))
915 (org-publish-projects org-publish-project-alist
)))))
919 (defun org-publish-current-file (&optional force async
)
920 "Publish the current file.
921 With prefix argument FORCE, force publish the file. When
922 optional argument ASYNC is non-nil, publishing will be done
923 asynchronously, in another process."
925 (let ((file (buffer-file-name (buffer-base-buffer))))
927 (org-export-async-start (lambda (_) nil
)
928 `(let ((org-publish-use-timestamps-flag
929 (if ',force nil
,org-publish-use-timestamps-flag
)))
930 (org-publish-file ,file
)))
931 (save-window-excursion
932 (let ((org-publish-use-timestamps-flag
933 (if force nil org-publish-use-timestamps-flag
)))
934 (org-publish-file file
))))))
937 (defun org-publish-current-project (&optional force async
)
938 "Publish the project associated with the current file.
939 With a prefix argument, force publishing of all files in
942 (save-window-excursion
943 (let ((project (org-publish-get-project-from-filename
944 (buffer-file-name (buffer-base-buffer)) 'up
)))
945 (if project
(org-publish project force async
)
946 (error "File %s is not part of any known project"
947 (buffer-file-name (buffer-base-buffer)))))))
953 (defun org-publish-collect-index (output _backend info
)
954 "Update index for a file in cache.
956 OUTPUT is the output from transcoding current file. BACKEND is
957 the back-end that was used for transcoding. INFO is a plist
958 containing publishing and export options.
960 The index relative to current file is stored as an alist. An
961 association has the following shape: (TERM FILE-NAME PARENT),
962 where TERM is the indexed term, as a string, FILE-NAME is the
963 original full path of the file where the term in encountered, and
964 PARENT is a reference to the headline, if any, containing the
965 original index keyword. When non-nil, this reference is a cons
966 cell. Its CAR is a symbol among `id', `custom-id' and `name' and
967 its CDR is a string."
968 (let ((file (plist-get info
:input-file
)))
969 (org-publish-cache-set-file-property
972 (org-element-map (plist-get info
:parse-tree
) 'keyword
974 (when (equal (org-element-property :key k
) "INDEX")
975 (let ((parent (org-export-get-parent-headline k
)))
976 (list (org-element-property :value k
)
980 ((let ((id (org-element-property :ID parent
)))
981 (and id
(cons 'id id
))))
982 ((let ((id (org-element-property :CUSTOM_ID parent
)))
983 (and id
(cons 'custom-id id
))))
985 ;; Remove statistics cookie.
986 (replace-regexp-in-string
987 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
988 (org-element-property :raw-value parent
)))))))))
990 ;; Return output unchanged.
993 (defun org-publish-index-generate-theindex (project directory
)
994 "Retrieve full index from cache and build \"theindex.org\".
995 PROJECT is the project the index relates to. DIRECTORY is the
996 publishing directory."
997 (let ((all-files (org-publish-get-base-files
998 project
(plist-get (cdr project
) :exclude
)))
1000 ;; Compile full index and sort it alphabetically.
1001 (dolist (file all-files
1003 (sort (nreverse full-index
)
1004 (lambda (a b
) (string< (downcase (car a
))
1005 (downcase (car b
)))))))
1006 (let ((index (org-publish-cache-get-file-property file
:index
)))
1007 (dolist (term index
)
1008 (unless (member term full-index
) (push term full-index
)))))
1009 ;; Write "theindex.inc" in DIRECTORY.
1010 (with-temp-file (expand-file-name "theindex.inc" directory
)
1011 (let ((current-letter nil
) (last-entry nil
))
1012 (dolist (idx full-index
)
1013 (let* ((entry (org-split-string (car idx
) "!"))
1014 (letter (upcase (substring (car entry
) 0 1)))
1015 ;; Transform file into a path relative to publishing
1017 (file (file-relative-name
1019 (plist-get (cdr project
) :base-directory
))))
1020 ;; Check if another letter has to be inserted.
1021 (unless (string= letter current-letter
)
1022 (insert (format "* %s\n" letter
)))
1023 ;; Compute the first difference between last entry and
1024 ;; current one: it tells the level at which new items
1027 (if (equal entry last-entry
) (1- (length entry
))
1028 (cl-loop for n from
0 to
(length entry
)
1029 unless
(equal (nth n entry
) (nth n last-entry
))
1031 (len (length (nthcdr rank entry
))))
1032 ;; For each term after the first difference, create
1033 ;; a new sub-list with the term as body. Moreover,
1034 ;; linkify the last term.
1038 (make-string (* (+ rank n
) 2) ?
) " - "
1039 (if (not (= (1- len
) n
)) (nth (+ rank n
) entry
)
1040 ;; Last term: Link it to TARGET, if possible.
1041 (let ((target (nth 2 idx
)))
1046 (`nil
(format "file:%s" file
))
1047 (`id
(format "id:%s" (cdr target
)))
1048 (`custom-id
(format "file:%s::#%s" file
(cdr target
)))
1049 (_ (format "file:%s::*%s" file
(cdr target
))))
1051 (car (last entry
)))))
1053 (setq current-letter letter last-entry entry
))))
1054 ;; Create "theindex.org", if it doesn't exist yet, and provide
1055 ;; a default index file.
1056 (let ((index.org
(expand-file-name "theindex.org" directory
)))
1057 (unless (file-exists-p index.org
)
1058 (with-temp-file index.org
1059 (insert "#+TITLE: Index\n\n#+INCLUDE: \"theindex.inc\"\n\n")))))))
1063 ;;; External Fuzzy Links Resolution
1065 ;; This part implements tools to resolve [[file.org::*Some headline]]
1066 ;; links, where "file.org" belongs to the current project.
1068 (defun org-publish--store-crossrefs (output _backend info
)
1069 "Store cross-references for current published file.
1071 OUPUT is the produced output, as a string. BACKEND is the export
1072 back-end used, as a symbol. INFO is the final export state, as
1075 This function is meant to be used as a final output filter. See
1076 `org-publish-org-to'."
1077 (org-publish-cache-set-file-property
1078 (plist-get info
:input-file
) :crossrefs
1079 ;; Update `:crossrefs' so as to remove unused references and search
1080 ;; cells. Actually used references are extracted from
1081 ;; `:internal-references', with references as strings removed. See
1082 ;; `org-export-get-reference' for details.
1083 (cl-remove-if (lambda (pair) (stringp (car pair
)))
1084 (plist-get info
:internal-references
)))
1085 ;; Return output unchanged.
1088 (defun org-publish-resolve-external-link (search file
)
1089 "Return reference for element matching string SEARCH in FILE.
1091 Return value is an internal reference, as a string.
1093 This function allows resolving external links with a search
1096 [[file.org::*heading][description]]
1097 [[file.org::#custom-id][description]]
1098 [[file.org::fuzzy][description]]
1100 It only makes sense to use this if export back-end builds
1101 references with `org-export-get-reference'."
1102 (if (not org-publish-cache
)
1104 (message "Reference %S in file %S cannot be resolved without publishing"
1108 (let* ((filename (expand-file-name file
))
1110 (org-publish-cache-get-file-property filename
:crossrefs nil t
))
1111 (cells (org-export-string-to-search-cell search
)))
1113 ;; Look for reference associated to search cells triggered by
1114 ;; LINK. It can match when targeted file has been published
1116 (let ((known (cdr (cl-some (lambda (c) (assoc c crossrefs
)) cells
))))
1117 (and known
(org-export-format-reference known
)))
1118 ;; Search cell is unknown so far. Generate a new internal
1119 ;; reference that will be used when the targeted file will be
1121 (let ((new (org-export-new-reference crossrefs
)))
1122 (dolist (cell cells
) (push (cons cell new
) crossrefs
))
1123 (org-publish-cache-set-file-property filename
:crossrefs crossrefs
)
1124 (org-export-format-reference new
))))))
1128 ;;; Caching functions
1130 (defun org-publish-write-cache-file (&optional free-cache
)
1131 "Write `org-publish-cache' to file.
1132 If FREE-CACHE, empty the cache."
1133 (unless org-publish-cache
1134 (error "`org-publish-write-cache-file' called, but no cache present"))
1136 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1138 (error "Cannot find cache-file name in `org-publish-write-cache-file'"))
1139 (with-temp-file cache-file
1140 (let (print-level print-length
)
1141 (insert "(setq org-publish-cache \
1142 \(make-hash-table :test 'equal :weakness nil :size 100))\n")
1143 (maphash (lambda (k v
)
1145 (format "(puthash %S %s%S org-publish-cache)\n"
1146 k
(if (or (listp v
) (symbolp v
)) "'" "") v
)))
1147 org-publish-cache
)))
1148 (when free-cache
(org-publish-reset-cache))))
1150 (defun org-publish-initialize-cache (project-name)
1151 "Initialize the projects cache if not initialized yet and return it."
1153 (unless project-name
1154 (error "Cannot initialize `org-publish-cache' without projects name in \
1155 `org-publish-initialize-cache'"))
1157 (unless (file-exists-p org-publish-timestamp-directory
)
1158 (make-directory org-publish-timestamp-directory t
))
1159 (unless (file-directory-p org-publish-timestamp-directory
)
1160 (error "Org publish timestamp: %s is not a directory"
1161 org-publish-timestamp-directory
))
1163 (unless (and org-publish-cache
1164 (string= (org-publish-cache-get ":project:") project-name
))
1167 (expand-file-name org-publish-timestamp-directory
)
1168 project-name
".cache"))
1169 (cexists (file-exists-p cache-file
)))
1171 (when org-publish-cache
(org-publish-reset-cache))
1173 (if cexists
(load-file cache-file
)
1174 (setq org-publish-cache
1175 (make-hash-table :test
'equal
:weakness nil
:size
100))
1176 (org-publish-cache-set ":project:" project-name
)
1177 (org-publish-cache-set ":cache-file:" cache-file
))
1178 (unless cexists
(org-publish-write-cache-file nil
))))
1181 (defun org-publish-reset-cache ()
1182 "Empty org-publish-cache and reset it nil."
1183 (message "%s" "Resetting org-publish-cache")
1184 (when (hash-table-p org-publish-cache
)
1185 (clrhash org-publish-cache
))
1186 (setq org-publish-cache nil
))
1188 (defun org-publish-cache-file-needs-publishing
1189 (filename &optional pub-dir pub-func _base-dir
)
1190 "Check the timestamp of the last publishing of FILENAME.
1191 Return non-nil if the file needs publishing. Also check if
1192 any included files have been more recently published, so that
1193 the file including them will be republished as well."
1194 (unless org-publish-cache
1196 "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1197 (let* ((case-fold-search t
)
1198 (key (org-publish-timestamp-filename filename pub-dir pub-func
))
1199 (pstamp (org-publish-cache-get key
))
1200 (org-inhibit-startup t
)
1201 (visiting (find-buffer-visiting filename
))
1202 included-files-ctime buf
)
1203 (when (equal (file-name-extension filename
) "org")
1204 (setq buf
(find-file (expand-file-name filename
)))
1205 (with-current-buffer buf
1206 (goto-char (point-min))
1207 (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t
)
1208 (let* ((element (org-element-at-point))
1210 (and (eq (org-element-type element
) 'keyword
)
1211 (let ((value (org-element-property :value element
)))
1214 "\\`\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)"
1216 (let ((m (match-string 1 value
)))
1217 (org-remove-double-quotes
1218 ;; Ignore search suffix.
1219 (if (string-match "\\(::\\(.*?\\)\\)\"?\\'" m
)
1220 (substring m
0 (match-beginning 0))
1223 (push (org-publish-cache-ctime-of-src
1224 (expand-file-name included-file
))
1225 included-files-ctime
)))))
1226 (unless visiting
(kill-buffer buf
)))
1228 (let ((ctime (org-publish-cache-ctime-of-src filename
)))
1229 (or (< pstamp ctime
)
1230 (cl-some (lambda (ct) (< ctime ct
)) included-files-ctime
))))))
1232 (defun org-publish-cache-set-file-property
1233 (filename property value
&optional project-name
)
1234 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1235 Use cache file of PROJECT-NAME. If the entry does not exist, it
1236 will be created. Return VALUE."
1237 ;; Evtl. load the requested cache file:
1238 (if project-name
(org-publish-initialize-cache project-name
))
1239 (let ((pl (org-publish-cache-get filename
)))
1240 (if pl
(progn (plist-put pl property value
) value
)
1241 (org-publish-cache-get-file-property
1242 filename property value nil project-name
))))
1244 (defun org-publish-cache-get-file-property
1245 (filename property
&optional default no-create project-name
)
1246 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1247 Use cache file of PROJECT-NAME. Return the value of that PROPERTY
1248 or DEFAULT, if the value does not yet exist. If the entry will
1249 be created, unless NO-CREATE is not nil."
1250 ;; Evtl. load the requested cache file:
1251 (if project-name
(org-publish-initialize-cache project-name
))
1252 (let ((pl (org-publish-cache-get filename
)) retval
)
1254 (if (plist-member pl property
)
1255 (setq retval
(plist-get pl property
))
1256 (setq retval default
))
1259 (org-publish-cache-set filename
(list property default
)))
1260 (setq retval default
))
1263 (defun org-publish-cache-get (key)
1264 "Return the value stored in `org-publish-cache' for key KEY.
1265 Returns nil, if no value or nil is found, or the cache does not
1267 (unless org-publish-cache
1268 (error "`org-publish-cache-get' called, but no cache present"))
1269 (gethash key org-publish-cache
))
1271 (defun org-publish-cache-set (key value
)
1272 "Store KEY VALUE pair in `org-publish-cache'.
1273 Returns value on success, else nil."
1274 (unless org-publish-cache
1275 (error "`org-publish-cache-set' called, but no cache present"))
1276 (puthash key value org-publish-cache
))
1278 (defun org-publish-cache-ctime-of-src (file)
1279 "Get the ctime of FILE as an integer."
1280 (let ((attr (file-attributes
1281 (expand-file-name (or (file-symlink-p file
) file
)
1282 (file-name-directory file
)))))
1283 (if (not attr
) (error "No such file: \"%s\"" file
)
1284 (+ (lsh (car (nth 5 attr
)) 16)
1285 (cadr (nth 5 attr
))))))
1288 (provide 'ox-publish
)
1291 ;; generated-autoload-file: "org-loaddefs.el"
1294 ;;; ox-publish.el ends here