1 ;;; ox-publish.el --- Publish Related Org Mode Files as a Website
2 ;; Copyright (C) 2006-2015 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.
41 (eval-when-compile (require 'cl
))
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 The `:exclude' property may be used to prevent certain files from
114 being published. Its value may be a string or regexp matching
115 file names you don't want to be published.
117 The `:include' property may be used to include extra files. Its
118 value may be a list of filenames to include. The filenames are
119 considered relative to the base directory.
121 When both `:include' and `:exclude' properties are given values,
122 the exclusion step happens first.
124 One special property controls which back-end function to use for
125 publishing files in the project. This can be used to extend the
126 set of file types publishable by `org-publish', as well as the
127 set of output formats.
129 `:publishing-function'
131 Function to publish file. Each back-end may define its
132 own (i.e. `org-latex-publish-to-pdf',
133 `org-html-publish-to-html'). May be a list of functions, in
134 which case each function in the list is invoked in turn.
136 Another property allows you to insert code that prepares
137 a project for publishing. For example, you could call GNU Make
138 on a certain makefile, to ensure published files are built up to
141 `:preparation-function'
143 Function to be called before publishing this project. This
144 may also be a list of functions.
146 `:completion-function'
148 Function to be called after publishing this project. This
149 may also be a list of functions.
151 Some properties control details of the Org publishing process,
152 and are equivalent to the corresponding user variables listed in
153 the right column. Back-end specific properties may also be
154 included. See the back-end documentation for more information.
156 :author `user-full-name'
157 :creator `org-export-creator-string'
158 :email `user-mail-address'
159 :exclude-tags `org-export-exclude-tags'
160 :headline-levels `org-export-headline-levels'
161 :language `org-export-default-language'
162 :preserve-breaks `org-export-preserve-breaks'
163 :section-numbers `org-export-with-section-numbers'
164 :select-tags `org-export-select-tags'
165 :time-stamp-file `org-export-time-stamp-file'
166 :with-archived-trees `org-export-with-archived-trees'
167 :with-author `org-export-with-author'
168 :with-creator `org-export-with-creator'
169 :with-date `org-export-with-date'
170 :with-drawers `org-export-with-drawers'
171 :with-email `org-export-with-email'
172 :with-emphasize `org-export-with-emphasize'
173 :with-entities `org-export-with-entities'
174 :with-fixed-width `org-export-with-fixed-width'
175 :with-footnotes `org-export-with-footnotes'
176 :with-inlinetasks `org-export-with-inlinetasks'
177 :with-latex `org-export-with-latex'
178 :with-planning `org-export-with-planning'
179 :with-priority `org-export-with-priority'
180 :with-properties `org-export-with-properties'
181 :with-smart-quotes `org-export-with-smart-quotes'
182 :with-special-strings `org-export-with-special-strings'
183 :with-statistics-cookies' `org-export-with-statistics-cookies'
184 :with-sub-superscript `org-export-with-sub-superscripts'
185 :with-toc `org-export-with-toc'
186 :with-tables `org-export-with-tables'
187 :with-tags `org-export-with-tags'
188 :with-tasks `org-export-with-tasks'
189 :with-timestamps `org-export-with-timestamps'
190 :with-title `org-export-with-title'
191 :with-todo-keywords `org-export-with-todo-keywords'
193 The following properties may be used to control publishing of
194 a site-map of files or summary page for a given project.
198 Whether to publish a site-map during
199 `org-publish-current-project' or `org-publish-all'.
203 Filename for output of sitemap. Defaults to \"sitemap.org\".
207 Title of site-map page. Defaults to name of file.
211 Plugin function to use for generation of site-map. Defaults
212 to `org-publish-org-sitemap', which generates a plain list of
213 links to all files in the project.
217 Can be `list' (site-map is just an itemized list of the
218 titles of the files involved) or `tree' (the directory
219 structure of the source files is reflected in the site-map).
222 `:sitemap-sans-extension'
224 Remove extension from site-map's file-names. Useful to have
225 cool URIs (see http://www.w3.org/Provider/Style/URI).
228 If you create a site-map file, adjust the sorting like this:
230 `:sitemap-sort-folders'
232 Where folders should appear in the site-map. Set this to
233 `first' (default) or `last' to display folders first or last,
234 respectively. Any other value will mix files and folders.
236 `:sitemap-sort-files'
238 The site map is normally sorted alphabetically. You can
239 change this behavior setting this to `anti-chronologically',
240 `chronologically', or nil.
242 `:sitemap-ignore-case'
244 Should sorting be case-sensitive? Default nil.
246 The following property control the creation of a concept index.
250 Create a concept index. The file containing the index has to
251 be called \"theindex.org\". If it doesn't exist in the
252 project, it will be generated. Contents of the index are
253 stored in the file \"theindex.inc\", which can be included in
256 Other properties affecting publication.
260 Set this to t to publish only the body of the documents."
261 :group
'org-export-publish
264 (defcustom org-publish-use-timestamps-flag t
265 "Non-nil means use timestamp checking to publish only changed files.
266 When nil, do no timestamp checking and always publish all files."
267 :group
'org-export-publish
270 (defcustom org-publish-timestamp-directory
271 (convert-standard-filename "~/.org-timestamps/")
272 "Name of directory in which to store publishing timestamps."
273 :group
'org-export-publish
276 (defcustom org-publish-list-skipped-files t
277 "Non-nil means show message about files *not* published."
278 :group
'org-export-publish
281 (defcustom org-publish-sitemap-sort-files
'alphabetically
282 "Method to sort files in site-maps.
283 Possible values are `alphabetically', `chronologically',
284 `anti-chronologically' and nil.
286 If `alphabetically', files will be sorted alphabetically. If
287 `chronologically', files will be sorted with older modification
288 time first. If `anti-chronologically', files will be sorted with
289 newer modification time first. nil won't sort files.
291 You can overwrite this default per project in your
292 `org-publish-project-alist', using `:sitemap-sort-files'."
293 :group
'org-export-publish
296 (defcustom org-publish-sitemap-sort-folders
'first
297 "A symbol, denoting if folders are sorted first in sitemaps.
298 Possible values are `first', `last', and nil.
299 If `first', folders will be sorted before files.
300 If `last', folders are sorted to the end after the files.
301 Any other value will not mix files and folders.
303 You can overwrite this default per project in your
304 `org-publish-project-alist', using `:sitemap-sort-folders'."
305 :group
'org-export-publish
308 (defcustom org-publish-sitemap-sort-ignore-case nil
309 "Non-nil when site-map sorting should ignore case.
311 You can overwrite this default per project in your
312 `org-publish-project-alist', using `:sitemap-ignore-case'."
313 :group
'org-export-publish
316 (defcustom org-publish-sitemap-date-format
"%Y-%m-%d"
317 "Format for printing a date in the sitemap.
318 See `format-time-string' for allowed formatters."
319 :group
'org-export-publish
322 (defcustom org-publish-sitemap-file-entry-format
"%t"
323 "Format string for site-map file entry.
324 You could use brackets to delimit on what part the link will be.
328 %d is the date formatted using `org-publish-sitemap-date-format'."
329 :group
'org-export-publish
334 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
335 ;;; Timestamp-related functions
337 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func
)
338 "Return path to timestamp file for filename FILENAME."
339 (setq filename
(concat filename
"::" (or pub-dir
"") "::"
340 (format "%s" (or pub-func
""))))
341 (concat "X" (if (fboundp 'sha1
) (sha1 filename
) (md5 filename
))))
343 (defun org-publish-needed-p
344 (filename &optional pub-dir pub-func true-pub-dir base-dir
)
345 "Non-nil if FILENAME should be published in PUB-DIR using PUB-FUNC.
346 TRUE-PUB-DIR is where the file will truly end up. Currently we
347 are not using this - maybe it can eventually be used to check if
348 the file is present at the target location, and how old it is.
349 Right now we cannot do this, because we do not know under what
350 file name the file will be stored - the publishing function can
351 still decide about that independently."
352 (let ((rtn (if (not org-publish-use-timestamps-flag
) t
353 (org-publish-cache-file-needs-publishing
354 filename pub-dir pub-func base-dir
))))
355 (if rtn
(message "Publishing file %s using `%s'" filename pub-func
)
356 (when org-publish-list-skipped-files
357 (message "Skipping unmodified file %s" filename
)))
360 (defun org-publish-update-timestamp
361 (filename &optional pub-dir pub-func base-dir
)
362 "Update publishing timestamp for file FILENAME.
363 If there is no timestamp, create one."
364 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func
))
365 (stamp (org-publish-cache-ctime-of-src filename
)))
366 (org-publish-cache-set key stamp
)))
368 (defun org-publish-remove-all-timestamps ()
369 "Remove all files in the timestamp directory."
370 (let ((dir org-publish-timestamp-directory
)
372 (when (and (file-exists-p dir
) (file-directory-p dir
))
373 (mapc 'delete-file
(directory-files dir
'full
"[^.]\\'"))
374 (org-publish-reset-cache))))
378 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
379 ;;; Getting project information out of `org-publish-project-alist'
381 (defun org-publish-expand-projects (projects-alist)
382 "Expand projects in PROJECTS-ALIST.
383 This splices all the components into the list."
384 (let ((rest projects-alist
) rtn p components
)
385 (while (setq p
(pop rest
))
386 (if (setq components
(plist-get (cdr p
) :components
))
388 (mapcar (lambda (x) (assoc x org-publish-project-alist
))
392 (nreverse (delete-dups (delq nil rtn
)))))
394 (defvar org-publish-sitemap-sort-files
)
395 (defvar org-publish-sitemap-sort-folders
)
396 (defvar org-publish-sitemap-ignore-case
)
397 (defvar org-publish-sitemap-requested
)
398 (defvar org-publish-sitemap-date-format
)
399 (defvar org-publish-sitemap-file-entry-format
)
400 (defun org-publish-compare-directory-files (a b
)
401 "Predicate for `sort', that sorts folders and files for sitemap."
403 (when (or org-publish-sitemap-sort-files org-publish-sitemap-sort-folders
)
404 ;; First we sort files:
405 (when org-publish-sitemap-sort-files
406 (case org-publish-sitemap-sort-files
408 (let* ((adir (file-directory-p a
))
409 (aorg (and (string-match "\\.org$" a
) (not adir
)))
410 (bdir (file-directory-p b
))
411 (borg (and (string-match "\\.org$" b
) (not bdir
)))
412 (A (if aorg
(concat (file-name-directory a
)
413 (org-publish-find-title a
)) a
))
414 (B (if borg
(concat (file-name-directory b
)
415 (org-publish-find-title b
)) b
)))
416 (setq retval
(if org-publish-sitemap-ignore-case
417 (not (string-lessp (upcase B
) (upcase A
)))
418 (not (string-lessp B A
))))))
419 ((anti-chronologically chronologically
)
420 (let* ((adate (org-publish-find-date a
))
421 (bdate (org-publish-find-date b
))
422 (A (+ (lsh (car adate
) 16) (cadr adate
)))
423 (B (+ (lsh (car bdate
) 16) (cadr bdate
))))
425 (if (eq org-publish-sitemap-sort-files
'chronologically
) (<= A B
)
427 ;; Directory-wise wins:
428 (when org-publish-sitemap-sort-folders
429 ;; a is directory, b not:
431 ((and (file-directory-p a
) (not (file-directory-p b
)))
432 (setq retval
(equal org-publish-sitemap-sort-folders
'first
)))
433 ;; a is not a directory, but b is:
434 ((and (not (file-directory-p a
)) (file-directory-p b
))
435 (setq retval
(equal org-publish-sitemap-sort-folders
'last
))))))
438 (defun org-publish-get-base-files-1
439 (base-dir &optional recurse match skip-file skip-dir
)
440 "Set `org-publish-temp-files' with files from BASE-DIR directory.
441 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
442 non-nil, restrict this list to the files matching the regexp
443 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
444 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
445 matching the regexp SKIP-DIR when recursing through BASE-DIR."
447 (let ((fd-p (file-directory-p f
))
448 (fnd (file-name-nondirectory f
)))
449 (if (and fd-p recurse
450 (not (string-match "^\\.+$" fnd
))
451 (if skip-dir
(not (string-match skip-dir fnd
)) t
))
452 (org-publish-get-base-files-1
453 f recurse match skip-file skip-dir
)
454 (unless (or fd-p
;; this is a directory
455 (and skip-file
(string-match skip-file fnd
))
456 (not (file-exists-p (file-truename f
)))
457 (not (string-match match fnd
)))
459 (pushnew f org-publish-temp-files
)))))
460 (let ((all-files (if (not recurse
) (directory-files base-dir t match
)
461 ;; If RECURSE is non-nil, we want all files
462 ;; matching MATCH and sub-directories.
465 (or (file-directory-p file
)
466 (and match
(string-match match file
))))
467 (directory-files base-dir t
)))))
468 (if (not org-publish-sitemap-requested
) all-files
469 (sort all-files
'org-publish-compare-directory-files
)))))
471 (defun org-publish-get-base-files (project &optional exclude-regexp
)
472 "Return a list of all files in PROJECT.
473 If EXCLUDE-REGEXP is set, this will be used to filter out
475 (let* ((project-plist (cdr project
))
476 (base-dir (file-name-as-directory
477 (plist-get project-plist
:base-directory
)))
478 (include-list (plist-get project-plist
:include
))
479 (recurse (plist-get project-plist
:recursive
))
480 (extension (or (plist-get project-plist
:base-extension
) "org"))
481 ;; sitemap-... variables are dynamically scoped for
482 ;; org-publish-compare-directory-files:
483 (org-publish-sitemap-requested
484 (plist-get project-plist
:auto-sitemap
))
486 (or (plist-get project-plist
:sitemap-filename
) "sitemap.org"))
487 (org-publish-sitemap-sort-folders
488 (if (plist-member project-plist
:sitemap-sort-folders
)
489 (plist-get project-plist
:sitemap-sort-folders
)
490 org-publish-sitemap-sort-folders
))
491 (org-publish-sitemap-sort-files
492 (cond ((plist-member project-plist
:sitemap-sort-files
)
493 (plist-get project-plist
:sitemap-sort-files
))
494 ;; For backward compatibility:
495 ((plist-member project-plist
:sitemap-alphabetically
)
496 (if (plist-get project-plist
:sitemap-alphabetically
)
497 'alphabetically nil
))
498 (t org-publish-sitemap-sort-files
)))
499 (org-publish-sitemap-ignore-case
500 (if (plist-member project-plist
:sitemap-ignore-case
)
501 (plist-get project-plist
:sitemap-ignore-case
)
502 org-publish-sitemap-sort-ignore-case
))
503 (match (if (eq extension
'any
) "^[^\\.]"
504 (concat "^[^\\.].*\\.\\(" extension
"\\)$"))))
505 ;; Make sure `org-publish-sitemap-sort-folders' has an accepted
507 (unless (memq org-publish-sitemap-sort-folders
'(first last
))
508 (setq org-publish-sitemap-sort-folders nil
))
510 (setq org-publish-temp-files nil
)
511 (if org-publish-sitemap-requested
512 (pushnew (expand-file-name (concat base-dir sitemap-filename
))
513 org-publish-temp-files
))
514 (org-publish-get-base-files-1 base-dir recurse match
515 ;; FIXME distinguish exclude regexp
516 ;; for skip-file and skip-dir?
517 exclude-regexp exclude-regexp
)
520 (expand-file-name (concat base-dir f
))
521 org-publish-temp-files
))
523 org-publish-temp-files
))
525 (defun org-publish-get-project-from-filename (filename &optional up
)
526 "Return the project that FILENAME belongs to."
527 (let* ((filename (expand-file-name filename
))
531 (dolist (prj org-publish-project-alist
)
532 (unless (plist-get (cdr prj
) :components
)
533 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
534 (let* ((r (plist-get (cdr prj
) :recursive
))
535 (b (expand-file-name (file-name-as-directory
536 (plist-get (cdr prj
) :base-directory
))))
537 (x (or (plist-get (cdr prj
) :base-extension
) "org"))
538 (e (plist-get (cdr prj
) :exclude
))
539 (i (plist-get (cdr prj
) :include
))
540 (xm (concat "^" b
(if r
".+" "[^/]+") "\\.\\(" x
"\\)$")))
544 (mapcar (lambda (file)
545 (expand-file-name file b
))
547 (and (not (and e
(string-match e filename
)))
548 (string-match xm filename
)))
549 (setq project-name
(car prj
))
550 (throw 'p-found project-name
))))))
552 (dolist (prj org-publish-project-alist
)
553 (if (member project-name
(plist-get (cdr prj
) :components
))
554 (setq project-name
(car prj
)))))
555 (assoc project-name org-publish-project-alist
)))
559 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
560 ;;; Tools for publishing functions in back-ends
562 (defun org-publish-org-to (backend filename extension plist
&optional pub-dir
)
563 "Publish an Org file to a specified back-end.
565 BACKEND is a symbol representing the back-end used for
566 transcoding. FILENAME is the filename of the Org file to be
567 published. EXTENSION is the extension used for the output
568 string, with the leading dot. PLIST is the property list for the
571 Optional argument PUB-DIR, when non-nil is the publishing
574 Return output file name."
575 (unless (or (not pub-dir
) (file-exists-p pub-dir
)) (make-directory pub-dir t
))
576 ;; Check if a buffer visiting FILENAME is already open.
577 (let* ((org-inhibit-startup t
)
578 (visitingp (find-buffer-visiting filename
))
579 (work-buffer (or visitingp
(find-file-noselect filename
))))
580 (prog1 (with-current-buffer work-buffer
582 (org-export-output-file-name extension nil pub-dir
))
583 (body-p (plist-get plist
:body-only
)))
584 (org-export-to-file backend output-file
586 ;; Add `org-publish--collect-references' and
587 ;; `org-publish-collect-index' to final output
588 ;; filters. The latter isn't dependent on
589 ;; `:makeindex', since we want to keep it up-to-date
593 `(:filter-final-output
594 ,(cons 'org-publish--collect-references
595 (cons 'org-publish-collect-index
596 (plist-get plist
:filter-final-output
))))))))
597 ;; Remove opened buffer in the process.
598 (unless visitingp
(kill-buffer work-buffer
)))))
600 (defun org-publish-attachment (plist filename pub-dir
)
601 "Publish a file with no transformation of any kind.
603 FILENAME is the filename of the Org file to be published. PLIST
604 is the property list for the given project. PUB-DIR is the
605 publishing directory.
607 Return output file name."
608 (unless (file-directory-p pub-dir
)
609 (make-directory pub-dir t
))
610 (let ((output (expand-file-name (file-name-nondirectory filename
) pub-dir
)))
611 (or (equal (expand-file-name (file-name-directory filename
))
612 (file-name-as-directory (expand-file-name pub-dir
)))
613 (copy-file filename output t
))
619 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
620 ;;; Publishing files, sets of files, and indices
622 (defun org-publish-file (filename &optional project no-cache
)
623 "Publish file FILENAME from PROJECT.
624 If NO-CACHE is not nil, do not initialize org-publish-cache and
625 write it to disk. This is needed, since this function is used to
626 publish single files, when entire projects are published.
627 See `org-publish-projects'."
630 (or (org-publish-get-project-from-filename filename
)
631 (error "File %s not part of any known project"
632 (abbreviate-file-name filename
)))))
633 (project-plist (cdr project
))
634 (ftname (expand-file-name filename
))
636 (let ((fun (plist-get project-plist
:publishing-function
)))
637 (cond ((null fun
) (error "No publishing function chosen"))
641 (file-name-as-directory
643 (or (plist-get project-plist
:base-directory
)
644 (error "Project %s does not have :base-directory defined"
647 (file-name-as-directory
649 (or (eval (plist-get project-plist
:publishing-directory
))
650 (error "Project %s does not have :publishing-directory defined"
654 (unless no-cache
(org-publish-initialize-cache (car project
)))
659 (and (string-match (regexp-quote base-dir
) ftname
)
660 (substring ftname
(match-end 0))))))
661 ;; Allow chain of publishing functions.
662 (dolist (f publishing-function
)
663 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir base-dir
)
664 (let ((output (funcall f project-plist filename tmp-pub-dir
)))
665 (org-publish-update-timestamp filename pub-dir f base-dir
)
666 (run-hook-with-args 'org-publish-after-publishing-hook
669 (unless no-cache
(org-publish-write-cache-file))))
671 (defun org-publish-projects (projects)
672 "Publish all files belonging to the PROJECTS alist.
673 If `:auto-sitemap' is set, publish the sitemap too. If
674 `:makeindex' is set, also produce a file \"theindex.org\"."
675 (dolist (project (org-publish-expand-projects projects
))
676 (let ((project-plist (cdr project
)))
677 (let ((preparation-function
678 (plist-get project-plist
:preparation-function
)))
679 (when preparation-function
(run-hooks 'preparation-function
)))
680 ;; Each project uses its own cache file.
681 (org-publish-initialize-cache (car project
))
682 (when (plist-get project-plist
:auto-sitemap
)
683 (let ((sitemap-filename
684 (or (plist-get project-plist
:sitemap-filename
)
687 (or (plist-get project-plist
:sitemap-function
)
688 #'org-publish-org-sitemap
))
689 (org-publish-sitemap-date-format
690 (or (plist-get project-plist
:sitemap-date-format
)
691 org-publish-sitemap-date-format
))
692 (org-publish-sitemap-file-entry-format
693 (or (plist-get project-plist
:sitemap-file-entry-format
)
694 org-publish-sitemap-file-entry-format
)))
695 (funcall sitemap-function project sitemap-filename
)))
696 ;; Publish all files from PROJECT excepted "theindex.org". Its
697 ;; publishing will be deferred until "theindex.inc" is
700 (expand-file-name "theindex.org"
701 (plist-get project-plist
:base-directory
)))
702 (exclude-regexp (plist-get project-plist
:exclude
)))
703 (dolist (file (org-publish-get-base-files project exclude-regexp
))
704 (unless (equal file theindex
) (org-publish-file file project t
)))
705 ;; Populate "theindex.inc", if needed, and publish
707 (when (plist-get project-plist
:makeindex
)
708 (org-publish-index-generate-theindex
709 project
(plist-get project-plist
:base-directory
))
710 (org-publish-file theindex project t
)))
711 (let ((completion-function
712 (plist-get project-plist
:completion-function
)))
713 (when completion-function
(run-hooks 'completion-function
)))
714 (org-publish-write-cache-file))))
716 (defun org-publish-org-sitemap (project &optional sitemap-filename
)
717 "Create a sitemap of pages in set defined by PROJECT.
718 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
719 Default for SITEMAP-FILENAME is `sitemap.org'."
720 (let* ((project-plist (cdr project
))
721 (dir (file-name-as-directory
722 (plist-get project-plist
:base-directory
)))
723 (localdir (file-name-directory dir
))
724 (indent-str (make-string 2 ?\
))
725 (exclude-regexp (plist-get project-plist
:exclude
))
727 (org-publish-get-base-files project exclude-regexp
)))
728 (sitemap-filename (concat dir
(or sitemap-filename
"sitemap.org")))
729 (sitemap-title (or (plist-get project-plist
:sitemap-title
)
730 (concat "Sitemap for project " (car project
))))
731 (sitemap-style (or (plist-get project-plist
:sitemap-style
)
733 (sitemap-sans-extension
734 (plist-get project-plist
:sitemap-sans-extension
))
735 (visiting (find-buffer-visiting sitemap-filename
))
736 (ifn (file-name-nondirectory sitemap-filename
))
739 (let ((org-inhibit-startup t
))
741 (or visiting
(find-file sitemap-filename
))))
743 (insert (concat "#+TITLE: " sitemap-title
"\n\n"))
744 (while (setq file
(pop files
))
745 (let ((fn (file-name-nondirectory file
))
746 (link (file-relative-name file dir
))
748 (when sitemap-sans-extension
749 (setq link
(file-name-sans-extension link
)))
750 ;; sitemap shouldn't list itself
751 (unless (equal (file-truename sitemap-filename
)
752 (file-truename file
))
753 (if (eq sitemap-style
'list
)
754 (message "Generating list-style sitemap for %s" sitemap-title
)
755 (message "Generating tree-style sitemap for %s" sitemap-title
)
756 (setq localdir
(concat (file-name-as-directory dir
)
757 (file-name-directory link
)))
758 (unless (string= localdir oldlocal
)
759 (if (string= localdir dir
)
760 (setq indent-str
(make-string 2 ?\
))
765 (file-relative-name localdir dir
))) "/"))
767 (old-subdirs (split-string
768 (file-relative-name oldlocal dir
) "/")))
769 (setq indent-str
(make-string 2 ?\
))
770 (while (string= (car old-subdirs
) (car subdirs
))
771 (setq indent-str
(concat indent-str
(make-string 2 ?\
)))
775 (setq subdir
(concat subdir d
"/"))
776 (insert (concat indent-str
" + " d
"\n"))
777 (setq indent-str
(make-string
778 (+ (length indent-str
) 2) ?\
)))))))
779 ;; This is common to 'flat and 'tree
781 (org-publish-format-file-entry
782 org-publish-sitemap-file-entry-format file project-plist
))
783 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
784 (cond ((string-match-p regexp entry
)
785 (string-match regexp entry
)
786 (insert (concat indent-str
" + " (match-string 1 entry
)
788 (match-string 2 entry
)
789 "]]" (match-string 3 entry
) "\n")))
791 (insert (concat indent-str
" + [[file:" link
"]["
795 (or visiting
(kill-buffer sitemap-buffer
))))
797 (defun org-publish-format-file-entry (fmt file project-plist
)
800 `((?t .
,(org-publish-find-title file t
))
801 (?d .
,(format-time-string org-publish-sitemap-date-format
802 (org-publish-find-date file
)))
803 (?a .
,(or (plist-get project-plist
:author
) user-full-name
)))))
805 (defun org-publish-find-title (file &optional reset
)
806 "Find the title of FILE in project."
808 (and (not reset
) (org-publish-cache-get-file-property file
:title nil t
))
809 (let* ((org-inhibit-startup t
)
810 (visiting (find-buffer-visiting file
))
811 (buffer (or visiting
(find-file-noselect file
))))
812 (with-current-buffer buffer
816 ;; protect local variables in open buffers
818 (org-export-with-buffer-copy (org-export-get-environment))
819 (org-export-get-environment))
822 (org-no-properties (org-element-interpret-data property
))
823 (file-name-nondirectory (file-name-sans-extension file
))))))
824 (unless visiting
(kill-buffer buffer
))
825 (org-publish-cache-set-file-property file
:title title
)
828 (defun org-publish-find-date (file)
829 "Find the date of FILE in project.
830 This function assumes FILE is either a directory or an Org file.
831 If FILE is an Org file and provides a DATE keyword use it. In
832 any other case use the file system's modification time. Return
833 time in `current-time' format."
834 (if (file-directory-p file
) (nth 5 (file-attributes file
))
835 (let* ((org-inhibit-startup t
)
836 (visiting (find-buffer-visiting file
))
837 (file-buf (or visiting
(find-file-noselect file nil
)))
839 (with-current-buffer file-buf
841 (org-export-with-buffer-copy
842 (org-export-get-environment))
843 (org-export-get-environment)))
845 (unless visiting
(kill-buffer file-buf
))
846 ;; DATE is a secondary string. If it contains a timestamp,
847 ;; convert it to internal format. Otherwise, use FILE
848 ;; modification time.
849 (cond ((let ((ts (and (consp date
) (assq 'timestamp date
))))
851 (let ((value (org-element-interpret-data ts
)))
852 (and (org-string-nw-p value
)
853 (org-time-string-to-time value
))))))
854 ((file-exists-p file
) (nth 5 (file-attributes file
)))
855 (t (error "No such file: \"%s\"" file
))))))
859 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
860 ;;; Interactive publishing functions
863 (defalias 'org-publish-project
'org-publish
)
866 (defun org-publish (project &optional force async
)
869 PROJECT is either a project name, as a string, or a project
870 alist (see `org-publish-project-alist' variable).
872 When optional argument FORCE is non-nil, force publishing all
873 files in PROJECT. With a non-nil optional argument ASYNC,
874 publishing will be done asynchronously, in another process."
876 (list (assoc (org-icompleting-read "Publish project: "
877 org-publish-project-alist nil t
)
878 org-publish-project-alist
)
880 (let ((project (if (not (stringp project
)) project
881 ;; If this function is called in batch mode,
882 ;; PROJECT is still a string here.
883 (assoc project org-publish-project-alist
))))
887 (org-export-async-start (lambda (results) nil
)
888 `(let ((org-publish-use-timestamps-flag
889 ,(and (not force
) org-publish-use-timestamps-flag
)))
890 ;; Expand components right now as external process may not
891 ;; be aware of complete `org-publish-project-alist'.
892 (org-publish-projects
893 ',(org-publish-expand-projects (list project
))))))
894 (t (save-window-excursion
895 (let ((org-publish-use-timestamps-flag
896 (and (not force
) org-publish-use-timestamps-flag
)))
897 (org-publish-projects (list project
))))))))
900 (defun org-publish-all (&optional force async
)
901 "Publish all projects.
902 With prefix argument FORCE, remove all files in the timestamp
903 directory and force publishing all projects. With a non-nil
904 optional argument ASYNC, publishing will be done asynchronously,
908 (org-export-async-start (lambda (results) nil
)
910 (when ',force
(org-publish-remove-all-timestamps))
911 (let ((org-publish-use-timestamps-flag
912 (if ',force nil
,org-publish-use-timestamps-flag
)))
913 (org-publish-projects ',org-publish-project-alist
))))
914 (when force
(org-publish-remove-all-timestamps))
915 (save-window-excursion
916 (let ((org-publish-use-timestamps-flag
917 (if force nil org-publish-use-timestamps-flag
)))
918 (org-publish-projects org-publish-project-alist
)))))
922 (defun org-publish-current-file (&optional force async
)
923 "Publish the current file.
924 With prefix argument FORCE, force publish the file. When
925 optional argument ASYNC is non-nil, publishing will be done
926 asynchronously, in another process."
928 (let ((file (buffer-file-name (buffer-base-buffer))))
930 (org-export-async-start (lambda (results) nil
)
931 `(let ((org-publish-use-timestamps-flag
932 (if ',force nil
,org-publish-use-timestamps-flag
)))
933 (org-publish-file ,file
)))
934 (save-window-excursion
935 (let ((org-publish-use-timestamps-flag
936 (if force nil org-publish-use-timestamps-flag
)))
937 (org-publish-file file
))))))
940 (defun org-publish-current-project (&optional force async
)
941 "Publish the project associated with the current file.
942 With a prefix argument, force publishing of all files in
945 (save-window-excursion
946 (let ((project (org-publish-get-project-from-filename
947 (buffer-file-name (buffer-base-buffer)) 'up
)))
948 (if project
(org-publish project force async
)
949 (error "File %s is not part of any known project"
950 (buffer-file-name (buffer-base-buffer)))))))
956 (defun org-publish-collect-index (output backend info
)
957 "Update index for a file in cache.
959 OUTPUT is the output from transcoding current file. BACKEND is
960 the back-end that was used for transcoding. INFO is a plist
961 containing publishing and export options.
963 The index relative to current file is stored as an alist. An
964 association has the following shape: (TERM FILE-NAME PARENT),
965 where TERM is the indexed term, as a string, FILE-NAME is the
966 original full path of the file where the term in encountered, and
967 PARENT is a reference to the headline, if any, containing the
968 original index keyword. When non-nil, this reference is a cons
969 cell. Its CAR is a symbol among `id', `custom-id' and `name' and
970 its CDR is a string."
971 (let ((file (plist-get info
:input-file
)))
972 (org-publish-cache-set-file-property
975 (org-element-map (plist-get info
:parse-tree
) 'keyword
977 (when (equal (org-element-property :key k
) "INDEX")
978 (let ((parent (org-export-get-parent-headline k
)))
979 (list (org-element-property :value k
)
983 ((let ((id (org-element-property :ID parent
)))
984 (and id
(cons 'id id
))))
985 ((let ((id (org-element-property :CUSTOM_ID parent
)))
986 (and id
(cons 'custom-id id
))))
988 ;; Remove statistics cookie.
989 (replace-regexp-in-string
990 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
991 (org-element-property :raw-value parent
)))))))))
993 ;; Return output unchanged.
996 (defun org-publish-index-generate-theindex (project directory
)
997 "Retrieve full index from cache and build \"theindex.org\".
998 PROJECT is the project the index relates to. DIRECTORY is the
999 publishing directory."
1000 (let ((all-files (org-publish-get-base-files
1001 project
(plist-get (cdr project
) :exclude
)))
1003 ;; Compile full index and sort it alphabetically.
1004 (dolist (file all-files
1006 (sort (nreverse full-index
)
1007 (lambda (a b
) (string< (downcase (car a
))
1008 (downcase (car b
)))))))
1009 (let ((index (org-publish-cache-get-file-property file
:index
)))
1010 (dolist (term index
)
1011 (unless (member term full-index
) (push term full-index
)))))
1012 ;; Write "theindex.inc" in DIRECTORY.
1013 (with-temp-file (expand-file-name "theindex.inc" directory
)
1014 (let ((current-letter nil
) (last-entry nil
))
1015 (dolist (idx full-index
)
1016 (let* ((entry (org-split-string (car idx
) "!"))
1017 (letter (upcase (substring (car entry
) 0 1)))
1018 ;; Transform file into a path relative to publishing
1020 (file (file-relative-name
1022 (plist-get (cdr project
) :base-directory
))))
1023 ;; Check if another letter has to be inserted.
1024 (unless (string= letter current-letter
)
1025 (insert (format "* %s\n" letter
)))
1026 ;; Compute the first difference between last entry and
1027 ;; current one: it tells the level at which new items
1029 (let* ((rank (if (equal entry last-entry
) (1- (length entry
))
1030 (loop for n from
0 to
(length entry
)
1031 unless
(equal (nth n entry
) (nth n last-entry
))
1033 (len (length (nthcdr rank entry
))))
1034 ;; For each term after the first difference, create
1035 ;; a new sub-list with the term as body. Moreover,
1036 ;; linkify the last term.
1040 (make-string (* (+ rank n
) 2) ?
) " - "
1041 (if (not (= (1- len
) n
)) (nth (+ rank n
) entry
)
1042 ;; Last term: Link it to TARGET, if possible.
1043 (let ((target (nth 2 idx
)))
1048 ('nil
(format "file:%s" file
))
1049 (id (format "id:%s" (cdr target
)))
1050 (custom-id (format "file:%s::#%s" file
(cdr target
)))
1051 (otherwise (format "file:%s::*%s" file
(cdr target
))))
1053 (car (last entry
)))))
1055 (setq current-letter letter last-entry entry
))))
1056 ;; Create "theindex.org", if it doesn't exist yet, and provide
1057 ;; a default index file.
1058 (let ((index.org
(expand-file-name "theindex.org" directory
)))
1059 (unless (file-exists-p index.org
)
1060 (with-temp-file index.org
1061 (insert "#+TITLE: Index\n\n#+INCLUDE: \"theindex.inc\"\n\n")))))))
1065 ;;; External Fuzzy Links Resolution
1067 ;; This part implements tools to resolve [[file.org::*Some headline]]
1068 ;; links, where "file.org" belongs to the current project.
1070 (defun org-publish--collect-references (output backend info
)
1071 "Store headlines references for current published file.
1073 OUPUT is the produced output, as a string. BACKEND is the export
1074 back-end used, as a symbol. INFO is the final export state, as
1077 References are stored as an alist ((TYPE SEARCH) . VALUE) where
1079 TYPE is a symbol among `headline', `custom-id', `target' and
1082 SEARCH is the string a link is expected to match. It is
1084 - headline's title, as a string, with all whitespace
1085 characters and statistics cookies removed, if TYPE is
1088 - CUSTOM_ID value if TYPE is `custom-id'.
1090 - target's or radio-target's name if TYPE is `target'.
1092 - NAME affiliated keyword is TYPE is `other'.
1094 VALUE is an internal reference used in the document, as
1097 This function is meant to be used as a final out filter. See
1098 `org-publish-org-to'."
1099 (org-publish-cache-set-file-property
1100 (plist-get info
:input-file
) :references
1102 (when (hash-table-p (plist-get info
:internal-references
))
1105 (case (org-element-type k
)
1106 ((headline inlinetask
)
1110 (replace-regexp-in-string
1111 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
1112 (org-element-property :raw-value k
))))
1115 (let ((custom-id (org-element-property :CUSTOM_ID k
)))
1117 (push (cons (cons 'custom-id custom-id
) v
) refs
))))
1118 ((radio-target target
)
1121 (org-split-string (org-element-property :value k
)))
1124 ((org-element-property :name k
)
1127 (cons 'other
(org-split-string (org-element-property :name k
)))
1131 (plist-get info
:internal-references
)))
1133 ;; Return output unchanged.
1136 (defun org-publish-resolve-external-link (search file
)
1137 "Return reference for elements or objects matching SEARCH in FILE.
1139 Return value is an internal reference, as a string.
1141 This function allows to resolve external links like:
1143 [[file.org::*fuzzy][description]]
1144 [[file.org::#custom-id][description]]
1145 [[file.org::fuzzy][description]]"
1146 (if (not org-publish-cache
)
1148 (message "Reference \"%s\" in file \"%s\" cannot be resolved without \
1153 (let ((references (org-publish-cache-get-file-property
1154 (expand-file-name file
) :references nil t
)))
1156 ((cdr (case (aref search
0)
1157 (?
* (assoc (cons 'headline
(org-split-string (substring search
1)))
1159 (?
# (assoc (cons 'custom-id
(substring search
1)) references
))
1161 (let ((s (org-split-string search
)))
1162 (or (assoc (cons 'target s
) references
)
1163 (assoc (cons 'other s
) references
)
1164 (assoc (cons 'headline s
) references
)))))))
1165 (t (message "Unknown cross-reference \"%s\" in file \"%s\"" search file
)
1166 "MissingReference")))))
1170 ;;; Caching functions
1172 (defun org-publish-write-cache-file (&optional free-cache
)
1173 "Write `org-publish-cache' to file.
1174 If FREE-CACHE, empty the cache."
1175 (unless org-publish-cache
1176 (error "`org-publish-write-cache-file' called, but no cache present"))
1178 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1180 (error "Cannot find cache-file name in `org-publish-write-cache-file'"))
1181 (with-temp-file cache-file
1182 (let (print-level print-length
)
1183 (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
1184 (maphash (lambda (k v
)
1186 (format (concat "(puthash %S "
1187 (if (or (listp v
) (symbolp v
))
1189 "%S org-publish-cache)\n") k v
)))
1190 org-publish-cache
)))
1191 (when free-cache
(org-publish-reset-cache))))
1193 (defun org-publish-initialize-cache (project-name)
1194 "Initialize the projects cache if not initialized yet and return it."
1196 (unless project-name
1197 (error "Cannot initialize `org-publish-cache' without projects name in `org-publish-initialize-cache'"))
1199 (unless (file-exists-p org-publish-timestamp-directory
)
1200 (make-directory org-publish-timestamp-directory t
))
1201 (unless (file-directory-p org-publish-timestamp-directory
)
1202 (error "Org publish timestamp: %s is not a directory"
1203 org-publish-timestamp-directory
))
1205 (unless (and org-publish-cache
1206 (string= (org-publish-cache-get ":project:") project-name
))
1209 (expand-file-name org-publish-timestamp-directory
)
1210 project-name
".cache"))
1211 (cexists (file-exists-p cache-file
)))
1213 (when org-publish-cache
(org-publish-reset-cache))
1215 (if cexists
(load-file cache-file
)
1216 (setq org-publish-cache
1217 (make-hash-table :test
'equal
:weakness nil
:size
100))
1218 (org-publish-cache-set ":project:" project-name
)
1219 (org-publish-cache-set ":cache-file:" cache-file
))
1220 (unless cexists
(org-publish-write-cache-file nil
))))
1223 (defun org-publish-reset-cache ()
1224 "Empty org-publish-cache and reset it nil."
1225 (message "%s" "Resetting org-publish-cache")
1226 (when (hash-table-p org-publish-cache
)
1227 (clrhash org-publish-cache
))
1228 (setq org-publish-cache nil
))
1230 (defun org-publish-cache-file-needs-publishing
1231 (filename &optional pub-dir pub-func base-dir
)
1232 "Check the timestamp of the last publishing of FILENAME.
1233 Return non-nil if the file needs publishing. Also check if
1234 any included files have been more recently published, so that
1235 the file including them will be republished as well."
1236 (unless org-publish-cache
1238 "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1239 (let* ((case-fold-search t
)
1240 (key (org-publish-timestamp-filename filename pub-dir pub-func
))
1241 (pstamp (org-publish-cache-get key
))
1242 (org-inhibit-startup t
)
1243 (visiting (find-buffer-visiting filename
))
1244 included-files-ctime buf
)
1245 (when (equal (file-name-extension filename
) "org")
1246 (setq buf
(find-file (expand-file-name filename
)))
1247 (with-current-buffer buf
1248 (goto-char (point-min))
1249 (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t
)
1250 (let* ((element (org-element-at-point))
1252 (and (eq (org-element-type element
) 'keyword
)
1253 (let ((value (org-element-property :value element
)))
1255 (string-match "^\\(\".+?\"\\|\\S-+\\)" value
)
1256 (org-remove-double-quotes
1257 (match-string 1 value
)))))))
1259 (add-to-list 'included-files-ctime
1260 (org-publish-cache-ctime-of-src
1261 (expand-file-name included-file
))
1263 (unless visiting
(kill-buffer buf
)))
1265 (let ((ctime (org-publish-cache-ctime-of-src filename
)))
1266 (or (< pstamp ctime
)
1267 (when included-files-ctime
1268 (not (null (delq nil
(mapcar (lambda (ct) (< ctime ct
))
1269 included-files-ctime
))))))))))
1271 (defun org-publish-cache-set-file-property
1272 (filename property value
&optional project-name
)
1273 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1274 Use cache file of PROJECT-NAME. If the entry does not exist, it
1275 will be created. Return VALUE."
1276 ;; Evtl. load the requested cache file:
1277 (if project-name
(org-publish-initialize-cache project-name
))
1278 (let ((pl (org-publish-cache-get filename
)))
1279 (if pl
(progn (plist-put pl property value
) value
)
1280 (org-publish-cache-get-file-property
1281 filename property value nil project-name
))))
1283 (defun org-publish-cache-get-file-property
1284 (filename property
&optional default no-create project-name
)
1285 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1286 Use cache file of PROJECT-NAME. Return the value of that PROPERTY
1287 or DEFAULT, if the value does not yet exist. If the entry will
1288 be created, unless NO-CREATE is not nil."
1289 ;; Evtl. load the requested cache file:
1290 (if project-name
(org-publish-initialize-cache project-name
))
1291 (let ((pl (org-publish-cache-get filename
)) retval
)
1293 (if (plist-member pl property
)
1294 (setq retval
(plist-get pl property
))
1295 (setq retval default
))
1298 (org-publish-cache-set filename
(list property default
)))
1299 (setq retval default
))
1302 (defun org-publish-cache-get (key)
1303 "Return the value stored in `org-publish-cache' for key KEY.
1304 Returns nil, if no value or nil is found, or the cache does not
1306 (unless org-publish-cache
1307 (error "`org-publish-cache-get' called, but no cache present"))
1308 (gethash key org-publish-cache
))
1310 (defun org-publish-cache-set (key value
)
1311 "Store KEY VALUE pair in `org-publish-cache'.
1312 Returns value on success, else nil."
1313 (unless org-publish-cache
1314 (error "`org-publish-cache-set' called, but no cache present"))
1315 (puthash key value org-publish-cache
))
1317 (defun org-publish-cache-ctime-of-src (file)
1318 "Get the ctime of FILE as an integer."
1319 (let ((attr (file-attributes
1320 (expand-file-name (or (file-symlink-p file
) file
)
1321 (file-name-directory file
)))))
1322 (if (not attr
) (error "No such file: \"%s\"" file
)
1323 (+ (lsh (car (nth 5 attr
)) 16)
1324 (cadr (nth 5 attr
))))))
1327 (provide 'ox-publish
)
1330 ;; generated-autoload-file: "org-loaddefs.el"
1333 ;;; ox-publish.el ends here