org-element: Fix "*" itemized plain lists interpretation
[org-mode/org-tableheadings.git] / lisp / ox-publish.el
blobcf428d1a0ef20838cc0123494f4dcc1d6a905c1b
1 ;;; ox-publish.el --- Publish Related Org Mode Files as a Website
2 ;; Copyright (C) 2006-2013 Free Software Foundation, Inc.
4 ;; Author: David O'Toole <dto@gnu.org>
5 ;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
6 ;; Keywords: hypermedia, outlines, wp
8 ;; This file is part of GNU Emacs.
9 ;;
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This program allow configurable publishing of related sets of
26 ;; Org mode files as a complete website.
28 ;; ox-publish.el can do the following:
30 ;; + Publish all one's Org files to a given export back-end
31 ;; + Upload HTML, images, attachments and other files to a web server
32 ;; + Exclude selected private pages from publishing
33 ;; + Publish a clickable sitemap of pages
34 ;; + Manage local timestamps for publishing only changed files
35 ;; + Accept plugin functions to extend range of publishable content
37 ;; Documentation for publishing is in the manual.
39 ;;; Code:
41 (eval-when-compile (require 'cl))
42 (require 'format-spec)
43 (require 'ox)
47 ;;; Variables
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 (defgroup org-publish nil
58 "Options for publishing a set of Org-mode and related files."
59 :tag "Org Publishing"
60 :group 'org)
62 (defcustom org-publish-project-alist nil
63 "Association list to control publishing behavior.
64 Each element of the alist is a publishing 'project.' The CAR of
65 each element is a string, uniquely identifying the project. The
66 CDR of each element is in one of the following forms:
68 1. A well-formed property list with an even number of elements,
69 alternating keys and values, specifying parameters for the
70 publishing process.
72 \(:property value :property value ... )
74 2. A meta-project definition, specifying of a list of
75 sub-projects:
77 \(:components (\"project-1\" \"project-2\" ...))
79 When the CDR of an element of org-publish-project-alist is in
80 this second form, the elements of the list after `:components'
81 are taken to be components of the project, which group together
82 files requiring different publishing options. When you publish
83 such a project with \\[org-publish], the components all publish.
85 When a property is given a value in `org-publish-project-alist',
86 its setting overrides the value of the corresponding user
87 variable (if any) during publishing. However, options set within
88 a file override everything.
90 Most properties are optional, but some should always be set:
92 `:base-directory'
94 Directory containing publishing source files.
96 `:base-extension'
98 Extension (without the dot!) of source files. This can be
99 a regular expression. If not given, \"org\" will be used as
100 default extension.
102 `:publishing-directory'
104 Directory (possibly remote) where output files will be
105 published.
107 The `:exclude' property may be used to prevent certain files from
108 being published. Its value may be a string or regexp matching
109 file names you don't want to be published.
111 The `:include' property may be used to include extra files. Its
112 value may be a list of filenames to include. The filenames are
113 considered relative to the base directory.
115 When both `:include' and `:exclude' properties are given values,
116 the exclusion step happens first.
118 One special property controls which back-end function to use for
119 publishing files in the project. This can be used to extend the
120 set of file types publishable by `org-publish', as well as the
121 set of output formats.
123 `:publishing-function'
125 Function to publish file. Each back-end may define its
126 own (i.e. `org-latex-publish-to-pdf',
127 `org-html-publish-to-html'). May be a list of functions, in
128 which case each function in the list is invoked in turn.
130 Another property allows you to insert code that prepares
131 a project for publishing. For example, you could call GNU Make
132 on a certain makefile, to ensure published files are built up to
133 date.
135 `:preparation-function'
137 Function to be called before publishing this project. This
138 may also be a list of functions.
140 `:completion-function'
142 Function to be called after publishing this project. This
143 may also be a list of functions.
145 Some properties control details of the Org publishing process,
146 and are equivalent to the corresponding user variables listed in
147 the right column. Back-end specific properties may also be
148 included. See the back-end documentation for more information.
150 :author `user-full-name'
151 :creator `org-export-creator-string'
152 :email `user-mail-address'
153 :exclude-tags `org-export-exclude-tags'
154 :headline-levels `org-export-headline-levels'
155 :language `org-export-default-language'
156 :preserve-breaks `org-export-preserve-breaks'
157 :section-numbers `org-export-with-section-numbers'
158 :select-tags `org-export-select-tags'
159 :time-stamp-file `org-export-time-stamp-file'
160 :with-archived-trees `org-export-with-archived-trees'
161 :with-author `org-export-with-author'
162 :with-creator `org-export-with-creator'
163 :with-date `org-export-with-date'
164 :with-drawers `org-export-with-drawers'
165 :with-email `org-export-with-email'
166 :with-emphasize `org-export-with-emphasize'
167 :with-entities `org-export-with-entities'
168 :with-fixed-width `org-export-with-fixed-width'
169 :with-footnotes `org-export-with-footnotes'
170 :with-inlinetasks `org-export-with-inlinetasks'
171 :with-latex `org-export-with-latex'
172 :with-priority `org-export-with-priority'
173 :with-smart-quotes `org-export-with-smart-quotes'
174 :with-special-strings `org-export-with-special-strings'
175 :with-statistics-cookies' `org-export-with-statistics-cookies'
176 :with-sub-superscript `org-export-with-sub-superscripts'
177 :with-toc `org-export-with-toc'
178 :with-tables `org-export-with-tables'
179 :with-tags `org-export-with-tags'
180 :with-tasks `org-export-with-tasks'
181 :with-timestamps `org-export-with-timestamps'
182 :with-planning `org-export-with-planning'
183 :with-todo-keywords `org-export-with-todo-keywords'
185 The following properties may be used to control publishing of
186 a site-map of files or summary page for a given project.
188 `:auto-sitemap'
190 Whether to publish a site-map during
191 `org-publish-current-project' or `org-publish-all'.
193 `:sitemap-filename'
195 Filename for output of sitemap. Defaults to \"sitemap.org\".
197 `:sitemap-title'
199 Title of site-map page. Defaults to name of file.
201 `:sitemap-function'
203 Plugin function to use for generation of site-map. Defaults
204 to `org-publish-org-sitemap', which generates a plain list of
205 links to all files in the project.
207 `:sitemap-style'
209 Can be `list' (site-map is just an itemized list of the
210 titles of the files involved) or `tree' (the directory
211 structure of the source files is reflected in the site-map).
212 Defaults to `tree'.
214 `:sitemap-sans-extension'
216 Remove extension from site-map's file-names. Useful to have
217 cool URIs (see http://www.w3.org/Provider/Style/URI).
218 Defaults to nil.
220 If you create a site-map file, adjust the sorting like this:
222 `:sitemap-sort-folders'
224 Where folders should appear in the site-map. Set this to
225 `first' (default) or `last' to display folders first or last,
226 respectively. Any other value will mix files and folders.
228 `:sitemap-sort-files'
230 The site map is normally sorted alphabetically. You can
231 change this behaviour setting this to `anti-chronologically',
232 `chronologically', or nil.
234 `:sitemap-ignore-case'
236 Should sorting be case-sensitive? Default nil.
238 The following property control the creation of a concept index.
240 `:makeindex'
242 Create a concept index. The file containing the index has to
243 be called \"theindex.org\". If it doesn't exist in the
244 project, it will be generated. Contents of the index are
245 stored in the file \"theindex.inc\", which can be included in
246 \"theindex.org\".
248 Other properties affecting publication.
250 `:body-only'
252 Set this to t to publish only the body of the documents."
253 :group 'org-export-publish
254 :type 'alist)
256 (defcustom org-publish-use-timestamps-flag t
257 "Non-nil means use timestamp checking to publish only changed files.
258 When nil, do no timestamp checking and always publish all files."
259 :group 'org-export-publish
260 :type 'boolean)
262 (defcustom org-publish-timestamp-directory
263 (convert-standard-filename "~/.org-timestamps/")
264 "Name of directory in which to store publishing timestamps."
265 :group 'org-export-publish
266 :type 'directory)
268 (defcustom org-publish-list-skipped-files t
269 "Non-nil means show message about files *not* published."
270 :group 'org-export-publish
271 :type 'boolean)
273 (defcustom org-publish-sitemap-sort-files 'alphabetically
274 "Method to sort files in site-maps.
275 Possible values are `alphabetically', `chronologically',
276 `anti-chronologically' and nil.
278 If `alphabetically', files will be sorted alphabetically. If
279 `chronologically', files will be sorted with older modification
280 time first. If `anti-chronologically', files will be sorted with
281 newer modification time first. nil won't sort files.
283 You can overwrite this default per project in your
284 `org-publish-project-alist', using `:sitemap-sort-files'."
285 :group 'org-export-publish
286 :type 'symbol)
288 (defcustom org-publish-sitemap-sort-folders 'first
289 "A symbol, denoting if folders are sorted first in sitemaps.
290 Possible values are `first', `last', and nil.
291 If `first', folders will be sorted before files.
292 If `last', folders are sorted to the end after the files.
293 Any other value will not mix files and folders.
295 You can overwrite this default per project in your
296 `org-publish-project-alist', using `:sitemap-sort-folders'."
297 :group 'org-export-publish
298 :type 'symbol)
300 (defcustom org-publish-sitemap-sort-ignore-case nil
301 "Non-nil when site-map sorting should ignore case.
303 You can overwrite this default per project in your
304 `org-publish-project-alist', using `:sitemap-ignore-case'."
305 :group 'org-export-publish
306 :type 'boolean)
308 (defcustom org-publish-sitemap-date-format "%Y-%m-%d"
309 "Format for printing a date in the sitemap.
310 See `format-time-string' for allowed formatters."
311 :group 'org-export-publish
312 :type 'string)
314 (defcustom org-publish-sitemap-file-entry-format "%t"
315 "Format string for site-map file entry.
316 You could use brackets to delimit on what part the link will be.
318 %t is the title.
319 %a is the author.
320 %d is the date formatted using `org-publish-sitemap-date-format'."
321 :group 'org-export-publish
322 :type 'string)
326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
327 ;;; Timestamp-related functions
329 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
330 "Return path to timestamp file for filename FILENAME."
331 (setq filename (concat filename "::" (or pub-dir "") "::"
332 (format "%s" (or pub-func ""))))
333 (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
335 (defun org-publish-needed-p
336 (filename &optional pub-dir pub-func true-pub-dir base-dir)
337 "Non-nil if FILENAME should be published in PUB-DIR using PUB-FUNC.
338 TRUE-PUB-DIR is where the file will truly end up. Currently we
339 are not using this - maybe it can eventually be used to check if
340 the file is present at the target location, and how old it is.
341 Right now we cannot do this, because we do not know under what
342 file name the file will be stored - the publishing function can
343 still decide about that independently."
344 (let ((rtn (if (not org-publish-use-timestamps-flag) t
345 (org-publish-cache-file-needs-publishing
346 filename pub-dir pub-func base-dir))))
347 (if rtn (message "Publishing file %s using `%s'" filename pub-func)
348 (when org-publish-list-skipped-files
349 (message "Skipping unmodified file %s" filename)))
350 rtn))
352 (defun org-publish-update-timestamp
353 (filename &optional pub-dir pub-func base-dir)
354 "Update publishing timestamp for file FILENAME.
355 If there is no timestamp, create one."
356 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func))
357 (stamp (org-publish-cache-ctime-of-src filename)))
358 (org-publish-cache-set key stamp)))
360 (defun org-publish-remove-all-timestamps ()
361 "Remove all files in the timestamp directory."
362 (let ((dir org-publish-timestamp-directory)
363 files)
364 (when (and (file-exists-p dir) (file-directory-p dir))
365 (mapc 'delete-file (directory-files dir 'full "[^.]\\'"))
366 (org-publish-reset-cache))))
370 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
371 ;;; Getting project information out of `org-publish-project-alist'
373 (defun org-publish-expand-projects (projects-alist)
374 "Expand projects in PROJECTS-ALIST.
375 This splices all the components into the list."
376 (let ((rest projects-alist) rtn p components)
377 (while (setq p (pop rest))
378 (if (setq components (plist-get (cdr p) :components))
379 (setq rest (append
380 (mapcar (lambda (x) (assoc x org-publish-project-alist))
381 components)
382 rest))
383 (push p rtn)))
384 (nreverse (delete-dups (delq nil rtn)))))
386 (defvar org-sitemap-sort-files)
387 (defvar org-sitemap-sort-folders)
388 (defvar org-sitemap-ignore-case)
389 (defvar org-sitemap-requested)
390 (defvar org-sitemap-date-format)
391 (defvar org-sitemap-file-entry-format)
392 (defun org-publish-compare-directory-files (a b)
393 "Predicate for `sort', that sorts folders and files for sitemap."
394 (let ((retval t))
395 (when (or org-sitemap-sort-files org-sitemap-sort-folders)
396 ;; First we sort files:
397 (when org-sitemap-sort-files
398 (case org-sitemap-sort-files
399 (alphabetically
400 (let* ((adir (file-directory-p a))
401 (aorg (and (string-match "\\.org$" a) (not adir)))
402 (bdir (file-directory-p b))
403 (borg (and (string-match "\\.org$" b) (not bdir)))
404 (A (if aorg (concat (file-name-directory a)
405 (org-publish-find-title a)) a))
406 (B (if borg (concat (file-name-directory b)
407 (org-publish-find-title b)) b)))
408 (setq retval (if org-sitemap-ignore-case
409 (not (string-lessp (upcase B) (upcase A)))
410 (not (string-lessp B A))))))
411 ((anti-chronologically chronologically)
412 (let* ((adate (org-publish-find-date a))
413 (bdate (org-publish-find-date b))
414 (A (+ (lsh (car adate) 16) (cadr adate)))
415 (B (+ (lsh (car bdate) 16) (cadr bdate))))
416 (setq retval
417 (if (eq org-sitemap-sort-files 'chronologically) (<= A B)
418 (>= A B)))))))
419 ;; Directory-wise wins:
420 (when org-sitemap-sort-folders
421 ;; a is directory, b not:
422 (cond
423 ((and (file-directory-p a) (not (file-directory-p b)))
424 (setq retval (equal org-sitemap-sort-folders 'first)))
425 ;; a is not a directory, but b is:
426 ((and (not (file-directory-p a)) (file-directory-p b))
427 (setq retval (equal org-sitemap-sort-folders 'last))))))
428 retval))
430 (defun org-publish-get-base-files-1
431 (base-dir &optional recurse match skip-file skip-dir)
432 "Set `org-publish-temp-files' with files from BASE-DIR directory.
433 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
434 non-nil, restrict this list to the files matching the regexp
435 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
436 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
437 matching the regexp SKIP-DIR when recursing through BASE-DIR."
438 (mapc (lambda (f)
439 (let ((fd-p (file-directory-p f))
440 (fnd (file-name-nondirectory f)))
441 (if (and fd-p recurse
442 (not (string-match "^\\.+$" fnd))
443 (if skip-dir (not (string-match skip-dir fnd)) t))
444 (org-publish-get-base-files-1
445 f recurse match skip-file skip-dir)
446 (unless (or fd-p ;; this is a directory
447 (and skip-file (string-match skip-file fnd))
448 (not (file-exists-p (file-truename f)))
449 (not (string-match match fnd)))
451 (pushnew f org-publish-temp-files)))))
452 (if org-sitemap-requested
453 (sort (directory-files base-dir t (unless recurse match))
454 'org-publish-compare-directory-files)
455 (directory-files base-dir t (unless recurse match)))))
457 (defun org-publish-get-base-files (project &optional exclude-regexp)
458 "Return a list of all files in PROJECT.
459 If EXCLUDE-REGEXP is set, this will be used to filter out
460 matching filenames."
461 (let* ((project-plist (cdr project))
462 (base-dir (file-name-as-directory
463 (plist-get project-plist :base-directory)))
464 (include-list (plist-get project-plist :include))
465 (recurse (plist-get project-plist :recursive))
466 (extension (or (plist-get project-plist :base-extension) "org"))
467 ;; sitemap-... variables are dynamically scoped for
468 ;; org-publish-compare-directory-files:
469 (org-sitemap-requested
470 (plist-get project-plist :auto-sitemap))
471 (sitemap-filename
472 (or (plist-get project-plist :sitemap-filename) "sitemap.org"))
473 (org-sitemap-sort-folders
474 (if (plist-member project-plist :sitemap-sort-folders)
475 (plist-get project-plist :sitemap-sort-folders)
476 org-publish-sitemap-sort-folders))
477 (org-sitemap-sort-files
478 (cond ((plist-member project-plist :sitemap-sort-files)
479 (plist-get project-plist :sitemap-sort-files))
480 ;; For backward compatibility:
481 ((plist-member project-plist :sitemap-alphabetically)
482 (if (plist-get project-plist :sitemap-alphabetically)
483 'alphabetically nil))
484 (t org-publish-sitemap-sort-files)))
485 (org-sitemap-ignore-case
486 (if (plist-member project-plist :sitemap-ignore-case)
487 (plist-get project-plist :sitemap-ignore-case)
488 org-publish-sitemap-sort-ignore-case))
489 (match (if (eq extension 'any) "^[^\\.]"
490 (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
491 ;; Make sure `org-sitemap-sort-folders' has an accepted value
492 (unless (memq org-sitemap-sort-folders '(first last))
493 (setq org-sitemap-sort-folders nil))
495 (setq org-publish-temp-files nil)
496 (if org-sitemap-requested
497 (pushnew (expand-file-name (concat base-dir sitemap-filename))
498 org-publish-temp-files))
499 (org-publish-get-base-files-1 base-dir recurse match
500 ;; FIXME distinguish exclude regexp
501 ;; for skip-file and skip-dir?
502 exclude-regexp exclude-regexp)
503 (mapc (lambda (f)
504 (pushnew
505 (expand-file-name (concat base-dir f))
506 org-publish-temp-files))
507 include-list)
508 org-publish-temp-files))
510 (defun org-publish-get-project-from-filename (filename &optional up)
511 "Return the project that FILENAME belongs to."
512 (let* ((filename (expand-file-name filename))
513 project-name)
515 (catch 'p-found
516 (dolist (prj org-publish-project-alist)
517 (unless (plist-get (cdr prj) :components)
518 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
519 (let* ((r (plist-get (cdr prj) :recursive))
520 (b (expand-file-name (file-name-as-directory
521 (plist-get (cdr prj) :base-directory))))
522 (x (or (plist-get (cdr prj) :base-extension) "org"))
523 (e (plist-get (cdr prj) :exclude))
524 (i (plist-get (cdr prj) :include))
525 (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
526 (when
527 (or (and i
528 (member filename
529 (mapcar (lambda (file)
530 (expand-file-name file b))
531 i)))
532 (and (not (and e (string-match e filename)))
533 (string-match xm filename)))
534 (setq project-name (car prj))
535 (throw 'p-found project-name))))))
536 (when up
537 (dolist (prj org-publish-project-alist)
538 (if (member project-name (plist-get (cdr prj) :components))
539 (setq project-name (car prj)))))
540 (assoc project-name org-publish-project-alist)))
544 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
545 ;;; Tools for publishing functions in back-ends
547 (defun org-publish-org-to (backend filename extension plist &optional pub-dir)
548 "Publish an Org file to a specified back-end.
550 BACKEND is a symbol representing the back-end used for
551 transcoding. FILENAME is the filename of the Org file to be
552 published. EXTENSION is the extension used for the output
553 string, with the leading dot. PLIST is the property list for the
554 given project.
556 Optional argument PUB-DIR, when non-nil is the publishing
557 directory.
559 Return output file name."
560 (unless (or (not pub-dir) (file-exists-p pub-dir)) (make-directory pub-dir t))
561 ;; Check if a buffer visiting FILENAME is already open.
562 (let* ((org-inhibit-startup t)
563 (visitingp (find-buffer-visiting filename))
564 (work-buffer (or visitingp (find-file-noselect filename))))
565 (prog1 (with-current-buffer work-buffer
566 (let ((output-file
567 (org-export-output-file-name extension nil pub-dir))
568 (body-p (plist-get plist :body-only)))
569 (org-export-to-file
570 backend output-file nil nil body-p
571 ;; Add `org-publish-collect-numbering' and
572 ;; `org-publish-collect-index' to final output
573 ;; filters. The latter isn't dependent on
574 ;; `:makeindex', since we want to keep it up-to-date
575 ;; in cache anyway.
576 (org-combine-plists
577 plist
578 `(:filter-final-output
579 ,(cons 'org-publish-collect-numbering
580 (cons 'org-publish-collect-index
581 (plist-get plist :filter-final-output))))))))
582 ;; Remove opened buffer in the process.
583 (unless visitingp (kill-buffer work-buffer)))))
585 (defvar project-plist)
587 (defun org-publish-attachment (plist filename pub-dir)
588 "Publish a file with no transformation of any kind.
590 FILENAME is the filename of the Org file to be published. PLIST
591 is the property list for the given project. PUB-DIR is the
592 publishing directory.
594 Return output file name."
595 (unless (file-directory-p pub-dir)
596 (make-directory pub-dir t))
597 (or (equal (expand-file-name (file-name-directory filename))
598 (file-name-as-directory (expand-file-name pub-dir)))
599 (copy-file filename
600 (expand-file-name (file-name-nondirectory filename) pub-dir)
601 t)))
605 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
606 ;;; Publishing files, sets of files, and indices
608 (defun org-publish-file (filename &optional project no-cache)
609 "Publish file FILENAME from PROJECT.
610 If NO-CACHE is not nil, do not initialize org-publish-cache and
611 write it to disk. This is needed, since this function is used to
612 publish single files, when entire projects are published.
613 See `org-publish-projects'."
614 (let* ((project
615 (or project
616 (or (org-publish-get-project-from-filename filename)
617 (error "File %s not part of any known project"
618 (abbreviate-file-name filename)))))
619 (project-plist (cdr project))
620 (ftname (expand-file-name filename))
621 (publishing-function
622 (or (plist-get project-plist :publishing-function)
623 (error "No publishing function chosen")))
624 (base-dir
625 (file-name-as-directory
626 (expand-file-name
627 (or (plist-get project-plist :base-directory)
628 (error "Project %s does not have :base-directory defined"
629 (car project))))))
630 (pub-dir
631 (file-name-as-directory
632 (file-truename
633 (or (eval (plist-get project-plist :publishing-directory))
634 (error "Project %s does not have :publishing-directory defined"
635 (car project))))))
636 tmp-pub-dir)
638 (unless no-cache (org-publish-initialize-cache (car project)))
640 (setq tmp-pub-dir
641 (file-name-directory
642 (concat pub-dir
643 (and (string-match (regexp-quote base-dir) ftname)
644 (substring ftname (match-end 0))))))
645 (if (listp publishing-function)
646 ;; allow chain of publishing functions
647 (mapc (lambda (f)
648 (when (org-publish-needed-p
649 filename pub-dir f tmp-pub-dir base-dir)
650 (funcall f project-plist filename tmp-pub-dir)
651 (org-publish-update-timestamp filename pub-dir f base-dir)))
652 publishing-function)
653 (when (org-publish-needed-p
654 filename pub-dir publishing-function tmp-pub-dir base-dir)
655 (funcall publishing-function project-plist filename tmp-pub-dir)
656 (org-publish-update-timestamp
657 filename pub-dir publishing-function base-dir)))
658 (unless no-cache (org-publish-write-cache-file))))
660 (defun org-publish-projects (projects)
661 "Publish all files belonging to the PROJECTS alist.
662 If `:auto-sitemap' is set, publish the sitemap too. If
663 `:makeindex' is set, also produce a file theindex.org."
664 (mapc
665 (lambda (project)
666 ;; Each project uses its own cache file:
667 (org-publish-initialize-cache (car project))
668 (let* ((project-plist (cdr project))
669 (exclude-regexp (plist-get project-plist :exclude))
670 (sitemap-p (plist-get project-plist :auto-sitemap))
671 (sitemap-filename (or (plist-get project-plist :sitemap-filename)
672 "sitemap.org"))
673 (sitemap-function (or (plist-get project-plist :sitemap-function)
674 'org-publish-org-sitemap))
675 (org-sitemap-date-format
676 (or (plist-get project-plist :sitemap-date-format)
677 org-publish-sitemap-date-format))
678 (org-sitemap-file-entry-format
679 (or (plist-get project-plist :sitemap-file-entry-format)
680 org-publish-sitemap-file-entry-format))
681 (preparation-function
682 (plist-get project-plist :preparation-function))
683 (completion-function (plist-get project-plist :completion-function))
684 (files (org-publish-get-base-files project exclude-regexp))
685 (theindex
686 (expand-file-name "theindex.org"
687 (plist-get project-plist :base-directory))))
688 (when preparation-function (run-hooks 'preparation-function))
689 (if sitemap-p (funcall sitemap-function project sitemap-filename))
690 ;; Publish all files from PROJECT excepted "theindex.org". Its
691 ;; publishing will be deferred until "theindex.inc" is
692 ;; populated.
693 (dolist (file files)
694 (unless (equal file theindex)
695 (org-publish-file file project t)))
696 ;; Populate "theindex.inc", if needed, and publish
697 ;; "theindex.org".
698 (when (plist-get project-plist :makeindex)
699 (org-publish-index-generate-theindex
700 project (plist-get project-plist :base-directory))
701 (org-publish-file theindex project t))
702 (when completion-function (run-hooks 'completion-function))
703 (org-publish-write-cache-file)))
704 (org-publish-expand-projects projects)))
706 (defun org-publish-org-sitemap (project &optional sitemap-filename)
707 "Create a sitemap of pages in set defined by PROJECT.
708 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
709 Default for SITEMAP-FILENAME is 'sitemap.org'."
710 (let* ((project-plist (cdr project))
711 (dir (file-name-as-directory
712 (plist-get project-plist :base-directory)))
713 (localdir (file-name-directory dir))
714 (indent-str (make-string 2 ?\ ))
715 (exclude-regexp (plist-get project-plist :exclude))
716 (files (nreverse
717 (org-publish-get-base-files project exclude-regexp)))
718 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
719 (sitemap-title (or (plist-get project-plist :sitemap-title)
720 (concat "Sitemap for project " (car project))))
721 (sitemap-style (or (plist-get project-plist :sitemap-style)
722 'tree))
723 (sitemap-sans-extension
724 (plist-get project-plist :sitemap-sans-extension))
725 (visiting (find-buffer-visiting sitemap-filename))
726 (ifn (file-name-nondirectory sitemap-filename))
727 file sitemap-buffer)
728 (with-current-buffer
729 (let ((org-inhibit-startup t))
730 (setq sitemap-buffer
731 (or visiting (find-file sitemap-filename))))
732 (erase-buffer)
733 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
734 (while (setq file (pop files))
735 (let ((fn (file-name-nondirectory file))
736 (link (file-relative-name file dir))
737 (oldlocal localdir))
738 (when sitemap-sans-extension
739 (setq link (file-name-sans-extension link)))
740 ;; sitemap shouldn't list itself
741 (unless (equal (file-truename sitemap-filename)
742 (file-truename file))
743 (if (eq sitemap-style 'list)
744 (message "Generating list-style sitemap for %s" sitemap-title)
745 (message "Generating tree-style sitemap for %s" sitemap-title)
746 (setq localdir (concat (file-name-as-directory dir)
747 (file-name-directory link)))
748 (unless (string= localdir oldlocal)
749 (if (string= localdir dir)
750 (setq indent-str (make-string 2 ?\ ))
751 (let ((subdirs
752 (split-string
753 (directory-file-name
754 (file-name-directory
755 (file-relative-name localdir dir))) "/"))
756 (subdir "")
757 (old-subdirs (split-string
758 (file-relative-name oldlocal dir) "/")))
759 (setq indent-str (make-string 2 ?\ ))
760 (while (string= (car old-subdirs) (car subdirs))
761 (setq indent-str (concat indent-str (make-string 2 ?\ )))
762 (pop old-subdirs)
763 (pop subdirs))
764 (dolist (d subdirs)
765 (setq subdir (concat subdir d "/"))
766 (insert (concat indent-str " + " d "\n"))
767 (setq indent-str (make-string
768 (+ (length indent-str) 2) ?\ )))))))
769 ;; This is common to 'flat and 'tree
770 (let ((entry
771 (org-publish-format-file-entry
772 org-sitemap-file-entry-format file project-plist))
773 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
774 (cond ((string-match-p regexp entry)
775 (string-match regexp entry)
776 (insert (concat indent-str " + " (match-string 1 entry)
777 "[[file:" link "]["
778 (match-string 2 entry)
779 "]]" (match-string 3 entry) "\n")))
781 (insert (concat indent-str " + [[file:" link "]["
782 entry
783 "]]\n"))))))))
784 (save-buffer))
785 (or visiting (kill-buffer sitemap-buffer))))
787 (defun org-publish-format-file-entry (fmt file project-plist)
788 (format-spec fmt
789 `((?t . ,(org-publish-find-title file t))
790 (?d . ,(format-time-string org-sitemap-date-format
791 (org-publish-find-date file)))
792 (?a . ,(or (plist-get project-plist :author) user-full-name)))))
794 (defun org-publish-find-title (file &optional reset)
795 "Find the title of FILE in project."
797 (and (not reset) (org-publish-cache-get-file-property file :title nil t))
798 (let* ((org-inhibit-startup t)
799 (visiting (find-buffer-visiting file))
800 (buffer (or visiting (find-file-noselect file)))
801 title)
802 (with-current-buffer buffer
803 (org-mode)
804 (setq title
805 (or (org-element-interpret-data
806 (plist-get (org-export-get-environment) :title))
807 (file-name-nondirectory (file-name-sans-extension file)))))
808 (unless visiting (kill-buffer buffer))
809 (org-publish-cache-set-file-property file :title title)
810 title)))
812 (defun org-publish-find-date (file)
813 "Find the date of FILE in project.
814 If FILE provides a DATE keyword use it else use the file system's
815 modification time. Return time in `current-time' format."
816 (let* ((org-inhibit-startup t)
817 (visiting (find-buffer-visiting file))
818 (file-buf (or visiting (find-file-noselect file nil)))
819 (date (plist-get
820 (with-current-buffer file-buf
821 (org-mode)
822 (org-export-get-environment))
823 :date)))
824 (unless visiting (kill-buffer file-buf))
825 ;; DATE is either a timestamp object or a secondary string. If it
826 ;; is a timestamp or if the secondary string contains a timestamp,
827 ;; convert it to internal format. Otherwise, use FILE
828 ;; modification time.
829 (cond ((eq (org-element-type date) 'timestamp)
830 (org-time-string-to-time (org-element-interpret-data date)))
831 ((let ((ts (and (consp date) (assq 'timestamp date))))
832 (and ts
833 (let ((value (org-element-interpret-data ts)))
834 (and (org-string-nw-p value)
835 (org-time-string-to-time value))))))
836 ((file-exists-p file) (nth 5 (file-attributes file)))
837 (t (error "No such file: \"%s\"" file)))))
841 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
842 ;;; Interactive publishing functions
844 ;;;###autoload
845 (defalias 'org-publish-project 'org-publish)
847 ;;;###autoload
848 (defun org-publish (project &optional force async)
849 "Publish PROJECT.
851 PROJECT is either a project name, as a string, or a project
852 alist (see `org-publish-project-alist' variable).
854 When optional argument FORCE is non-nil, force publishing all
855 files in PROJECT. With a non-nil optional argument ASYNC,
856 publishing will be done asynchronously, in another process."
857 (interactive
858 (list
859 (assoc (org-icompleting-read
860 "Publish project: "
861 org-publish-project-alist nil t)
862 org-publish-project-alist)
863 current-prefix-arg))
864 (let ((project-alist (if (not (stringp project)) (list project)
865 ;; If this function is called in batch mode,
866 ;; project is still a string here.
867 (list (assoc project org-publish-project-alist)))))
868 (if async
869 (org-export-async-start 'ignore
870 `(let ((org-publish-use-timestamps-flag
871 (if ',force nil ,org-publish-use-timestamps-flag)))
872 (org-publish-projects ',project-alist)))
873 (save-window-excursion
874 (let* ((org-publish-use-timestamps-flag
875 (if force nil org-publish-use-timestamps-flag)))
876 (org-publish-projects project-alist))))))
878 ;;;###autoload
879 (defun org-publish-all (&optional force async)
880 "Publish all projects.
881 With prefix argument FORCE, remove all files in the timestamp
882 directory and force publishing all projects. With a non-nil
883 optional argument ASYNC, publishing will be done asynchronously,
884 in another process."
885 (interactive "P")
886 (if async
887 (org-export-async-start 'ignore
888 `(when ',force (org-publish-remove-all-timestamps))
889 `(let ((org-publish-use-timestamps-flag
890 (if ',force nil ,org-publish-use-timestamps-flag)))
891 (org-publish-projects ',org-publish-project-alist)))
892 (when force (org-publish-remove-all-timestamps))
893 (save-window-excursion
894 (let ((org-publish-use-timestamps-flag
895 (if force nil org-publish-use-timestamps-flag)))
896 (org-publish-projects org-publish-project-alist)))))
899 ;;;###autoload
900 (defun org-publish-current-file (&optional force async)
901 "Publish the current file.
902 With prefix argument FORCE, force publish the file. When
903 optional argument ASYNC is non-nil, publishing will be done
904 asynchronously, in another process."
905 (interactive "P")
906 (let ((file (buffer-file-name (buffer-base-buffer))))
907 (if async
908 (org-export-async-start 'ignore
909 `(let ((org-publish-use-timestamps-flag
910 (if ',force nil ,org-publish-use-timestamps-flag)))
911 (org-publish-file ,file)))
912 (save-window-excursion
913 (let ((org-publish-use-timestamps-flag
914 (if force nil org-publish-use-timestamps-flag)))
915 (org-publish-file file))))))
917 ;;;###autoload
918 (defun org-publish-current-project (&optional force async)
919 "Publish the project associated with the current file.
920 With a prefix argument, force publishing of all files in
921 the project."
922 (interactive "P")
923 (save-window-excursion
924 (let ((project (org-publish-get-project-from-filename
925 (buffer-file-name (buffer-base-buffer)) 'up)))
926 (if project (org-publish project force async)
927 (error "File %s is not part of any known project"
928 (buffer-file-name (buffer-base-buffer)))))))
932 ;;; Index generation
934 (defun org-publish-collect-index (output backend info)
935 "Update index for a file in cache.
937 OUTPUT is the output from transcoding current file. BACKEND is
938 the back-end that was used for transcoding. INFO is a plist
939 containing publishing and export options.
941 The index relative to current file is stored as an alist. An
942 association has the following shape: (TERM FILE-NAME PARENT),
943 where TERM is the indexed term, as a string, FILE-NAME is the
944 original full path of the file where the term in encountered, and
945 PARENT is a reference to the headline, if any, containing the
946 original index keyword. When non-nil, this reference is a cons
947 cell. Its CAR is a symbol among `id', `custom-id' and `name' and
948 its CDR is a string."
949 (let ((file (plist-get info :input-file)))
950 (org-publish-cache-set-file-property
951 file :index
952 (delete-dups
953 (org-element-map (plist-get info :parse-tree) 'keyword
954 (lambda (k)
955 (when (equal (org-element-property :key k) "INDEX")
956 (let ((parent (org-export-get-parent-headline k)))
957 (list (org-element-property :value k)
958 file
959 (cond
960 ((not parent) nil)
961 ((let ((id (org-element-property :ID parent)))
962 (and id (cons 'id id))))
963 ((let ((id (org-element-property :CUSTOM_ID parent)))
964 (and id (cons 'custom-id id))))
965 (t (cons 'name
966 ;; Remove statistics cookie.
967 (replace-regexp-in-string
968 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
969 (org-element-property :raw-value parent)))))))))
970 info))))
971 ;; Return output unchanged.
972 output)
974 (defun org-publish-index-generate-theindex (project directory)
975 "Retrieve full index from cache and build \"theindex.org\".
976 PROJECT is the project the index relates to. DIRECTORY is the
977 publishing directory."
978 (let ((all-files (org-publish-get-base-files
979 project (plist-get (cdr project) :exclude)))
980 full-index)
981 ;; Compile full index and sort it alphabetically.
982 (dolist (file all-files
983 (setq full-index
984 (sort (nreverse full-index)
985 (lambda (a b) (string< (downcase (car a))
986 (downcase (car b)))))))
987 (let ((index (org-publish-cache-get-file-property file :index)))
988 (dolist (term index)
989 (unless (member term full-index) (push term full-index)))))
990 ;; Write "theindex.inc" in DIRECTORY.
991 (with-temp-file (expand-file-name "theindex.inc" directory)
992 (let ((current-letter nil) (last-entry nil))
993 (dolist (idx full-index)
994 (let* ((entry (org-split-string (car idx) "!"))
995 (letter (upcase (substring (car entry) 0 1)))
996 ;; Transform file into a path relative to publishing
997 ;; directory.
998 (file (file-relative-name
999 (nth 1 idx)
1000 (plist-get (cdr project) :base-directory))))
1001 ;; Check if another letter has to be inserted.
1002 (unless (string= letter current-letter)
1003 (insert (format "* %s\n" letter)))
1004 ;; Compute the first difference between last entry and
1005 ;; current one: it tells the level at which new items
1006 ;; should be added.
1007 (let* ((rank (if (equal entry last-entry) (1- (length entry))
1008 (loop for n from 0 to (length entry)
1009 unless (equal (nth n entry) (nth n last-entry))
1010 return n)))
1011 (len (length (nthcdr rank entry))))
1012 ;; For each term after the first difference, create
1013 ;; a new sub-list with the term as body. Moreover,
1014 ;; linkify the last term.
1015 (dotimes (n len)
1016 (insert
1017 (concat
1018 (make-string (* (+ rank n) 2) ? ) " - "
1019 (if (not (= (1- len) n)) (nth (+ rank n) entry)
1020 ;; Last term: Link it to TARGET, if possible.
1021 (let ((target (nth 2 idx)))
1022 (format
1023 "[[%s][%s]]"
1024 ;; Destination.
1025 (case (car target)
1026 ('nil (format "file:%s" file))
1027 (id (format "id:%s" (cdr target)))
1028 (custom-id (format "file:%s::#%s" file (cdr target)))
1029 (otherwise (format "file:%s::*%s" file (cdr target))))
1030 ;; Description.
1031 (car (last entry)))))
1032 "\n"))))
1033 (setq current-letter letter last-entry entry))))
1034 ;; Create "theindex.org", if it doesn't exist yet, and provide
1035 ;; a default index file.
1036 (let ((index.org (expand-file-name "theindex.org" directory)))
1037 (unless (file-exists-p index.org)
1038 (with-temp-file index.org
1039 (insert "#+TITLE: Index\n\n#+INCLUDE: \"theindex.inc\"\n\n")))))))
1043 ;;; External Fuzzy Links Resolution
1045 ;; This part implements tools to resolve [[file.org::*Some headline]]
1046 ;; links, where "file.org" belongs to the current project.
1048 (defun org-publish-collect-numbering (output backend info)
1049 (org-publish-cache-set-file-property
1050 (plist-get info :input-file) :numbering
1051 (mapcar (lambda (entry)
1052 (cons (org-split-string
1053 (replace-regexp-in-string
1054 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
1055 (org-element-property :raw-value (car entry))))
1056 (cdr entry)))
1057 (plist-get info :headline-numbering)))
1058 ;; Return output unchanged.
1059 output)
1061 (defun org-publish-resolve-external-fuzzy-link (file fuzzy)
1062 "Return numbering for headline matching FUZZY search in FILE.
1064 Return value is a list of numbers, or nil. This function allows
1065 to resolve external fuzzy links like:
1067 [[file.org::*fuzzy][description]"
1068 (when org-publish-cache
1069 (cdr (assoc (org-split-string
1070 (if (eq (aref fuzzy 0) ?*) (substring fuzzy 1) fuzzy))
1071 (org-publish-cache-get-file-property
1072 (expand-file-name file) :numbering nil t)))))
1076 ;;; Caching functions
1078 (defun org-publish-write-cache-file (&optional free-cache)
1079 "Write `org-publish-cache' to file.
1080 If FREE-CACHE, empty the cache."
1081 (unless org-publish-cache
1082 (error "`org-publish-write-cache-file' called, but no cache present"))
1084 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1085 (unless cache-file
1086 (error "Cannot find cache-file name in `org-publish-write-cache-file'"))
1087 (with-temp-file cache-file
1088 (let (print-level print-length)
1089 (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
1090 (maphash (lambda (k v)
1091 (insert
1092 (format (concat "(puthash %S "
1093 (if (or (listp v) (symbolp v))
1094 "'" "")
1095 "%S org-publish-cache)\n") k v)))
1096 org-publish-cache)))
1097 (when free-cache (org-publish-reset-cache))))
1099 (defun org-publish-initialize-cache (project-name)
1100 "Initialize the projects cache if not initialized yet and return it."
1102 (unless project-name
1103 (error "Cannot initialize `org-publish-cache' without projects name in `org-publish-initialize-cache'"))
1105 (unless (file-exists-p org-publish-timestamp-directory)
1106 (make-directory org-publish-timestamp-directory t))
1107 (unless (file-directory-p org-publish-timestamp-directory)
1108 (error "Org publish timestamp: %s is not a directory"
1109 org-publish-timestamp-directory))
1111 (unless (and org-publish-cache
1112 (string= (org-publish-cache-get ":project:") project-name))
1113 (let* ((cache-file
1114 (concat
1115 (expand-file-name org-publish-timestamp-directory)
1116 project-name ".cache"))
1117 (cexists (file-exists-p cache-file)))
1119 (when org-publish-cache (org-publish-reset-cache))
1121 (if cexists (load-file cache-file)
1122 (setq org-publish-cache
1123 (make-hash-table :test 'equal :weakness nil :size 100))
1124 (org-publish-cache-set ":project:" project-name)
1125 (org-publish-cache-set ":cache-file:" cache-file))
1126 (unless cexists (org-publish-write-cache-file nil))))
1127 org-publish-cache)
1129 (defun org-publish-reset-cache ()
1130 "Empty org-publish-cache and reset it nil."
1131 (message "%s" "Resetting org-publish-cache")
1132 (when (hash-table-p org-publish-cache)
1133 (clrhash org-publish-cache))
1134 (setq org-publish-cache nil))
1136 (defun org-publish-cache-file-needs-publishing
1137 (filename &optional pub-dir pub-func base-dir)
1138 "Check the timestamp of the last publishing of FILENAME.
1139 Return non-nil if the file needs publishing. Also check if
1140 any included files have been more recently published, so that
1141 the file including them will be republished as well."
1142 (unless org-publish-cache
1143 (error
1144 "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1145 (let* ((case-fold-search t)
1146 (key (org-publish-timestamp-filename filename pub-dir pub-func))
1147 (pstamp (org-publish-cache-get key))
1148 (org-inhibit-startup t)
1149 (visiting (find-buffer-visiting filename))
1150 included-files-ctime buf)
1152 (when (equal (file-name-extension filename) "org")
1153 (setq buf (find-file (expand-file-name filename)))
1154 (with-current-buffer buf
1155 (goto-char (point-min))
1156 (while (re-search-forward
1157 "^#\\+INCLUDE:[ \t]+\"\\([^\t\n\r\"]*\\)\"[ \t]*.*$" nil t)
1158 (let* ((included-file (expand-file-name (match-string 1))))
1159 (add-to-list 'included-files-ctime
1160 (org-publish-cache-ctime-of-src included-file) t))))
1161 (unless visiting (kill-buffer buf)))
1162 (if (null pstamp) t
1163 (let ((ctime (org-publish-cache-ctime-of-src filename)))
1164 (or (< pstamp ctime)
1165 (when included-files-ctime
1166 (not (null (delq nil (mapcar (lambda(ct) (< ctime ct))
1167 included-files-ctime))))))))))
1169 (defun org-publish-cache-set-file-property
1170 (filename property value &optional project-name)
1171 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1172 Use cache file of PROJECT-NAME. If the entry does not exist, it
1173 will be created. Return VALUE."
1174 ;; Evtl. load the requested cache file:
1175 (if project-name (org-publish-initialize-cache project-name))
1176 (let ((pl (org-publish-cache-get filename)))
1177 (if pl (progn (plist-put pl property value) value)
1178 (org-publish-cache-get-file-property
1179 filename property value nil project-name))))
1181 (defun org-publish-cache-get-file-property
1182 (filename property &optional default no-create project-name)
1183 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1184 Use cache file of PROJECT-NAME. Return the value of that PROPERTY
1185 or DEFAULT, if the value does not yet exist. If the entry will
1186 be created, unless NO-CREATE is not nil."
1187 ;; Evtl. load the requested cache file:
1188 (if project-name (org-publish-initialize-cache project-name))
1189 (let ((pl (org-publish-cache-get filename)) retval)
1190 (if pl
1191 (if (plist-member pl property)
1192 (setq retval (plist-get pl property))
1193 (setq retval default))
1194 ;; no pl yet:
1195 (unless no-create
1196 (org-publish-cache-set filename (list property default)))
1197 (setq retval default))
1198 retval))
1200 (defun org-publish-cache-get (key)
1201 "Return the value stored in `org-publish-cache' for key KEY.
1202 Returns nil, if no value or nil is found, or the cache does not
1203 exist."
1204 (unless org-publish-cache
1205 (error "`org-publish-cache-get' called, but no cache present"))
1206 (gethash key org-publish-cache))
1208 (defun org-publish-cache-set (key value)
1209 "Store KEY VALUE pair in `org-publish-cache'.
1210 Returns value on success, else nil."
1211 (unless org-publish-cache
1212 (error "`org-publish-cache-set' called, but no cache present"))
1213 (puthash key value org-publish-cache))
1215 (defun org-publish-cache-ctime-of-src (file)
1216 "Get the ctime of FILE as an integer."
1217 (let ((attr (file-attributes
1218 (expand-file-name (or (file-symlink-p file) file)
1219 (file-name-directory file)))))
1220 (+ (lsh (car (nth 5 attr)) 16)
1221 (cadr (nth 5 attr)))))
1224 (provide 'ox-publish)
1226 ;; Local variables:
1227 ;; generated-autoload-file: "org-loaddefs.el"
1228 ;; End:
1230 ;;; ox-publish.el ends here