org.texi (Built-in table editor): Tiny enhancement
[org-mode.git] / contrib / lisp / org-e-publish.el
blob3b6d5c357726e35649e84f70c4d408d6ce287076
1 ;;; org-e-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 not 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 ;; org-e-publish.el can do the following:
30 ;; + Publish all one's Org files to HTML or PDF
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 'org-export)
47 ;;; Variables
49 (defvar org-e-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-e-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-e-publish nil
58 "Options for publishing a set of Org-mode and related files."
59 :tag "Org Publishing"
60 :group 'org)
62 (defcustom org-e-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-e-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-e-publish], the components all
84 publish.
86 When a property is given a value in
87 `org-e-publish-project-alist', its setting overrides the value of
88 the corresponding user variable \(if any) during publishing.
89 However, options set within a file override everything.
91 Most properties are optional, but some should always be set:
93 `:base-directory'
95 Directory containing publishing source files.
97 `:base-extension'
99 Extension \(without the dot!) of source files. This can be
100 a regular expression. If not given, \"org\" will be used as
101 default extension.
103 `:publishing-directory'
105 Directory \(possibly remote) where output files will be
106 published.
108 The `:exclude' property may be used to prevent certain files from
109 being published. Its value may be a string or regexp matching
110 file names you don't want to be published.
112 The `:include' property may be used to include extra files. Its
113 value may be a list of filenames to include. The filenames are
114 considered relative to the base directory.
116 When both `:include' and `:exclude' properties are given values,
117 the exclusion step happens first.
119 One special property controls which back-end function to use for
120 publishing files in the project. This can be used to extend the
121 set of file types publishable by `org-e-publish', as well as the
122 set of output formats.
124 `:publishing-function'
126 Function to publish file. Each back-end may define its
127 own (i.e. `org-e-latex-publish-to-pdf',
128 `org-e-html-publish-to-html'). May be a list of functions,
129 in which case each function in the list is invoked in turn.
131 Another property allows you to insert code that prepares
132 a project for publishing. For example, you could call GNU Make
133 on a certain makefile, to ensure published files are built up to
134 date.
136 `:preparation-function'
138 Function to be called before publishing this project. This
139 may also be a list of functions.
141 `:completion-function'
143 Function to be called after publishing this project. This
144 may also be a list of functions.
146 Some properties control details of the Org publishing process,
147 and are equivalent to the corresponding user variables listed in
148 the right column. Back-end specific properties may also be
149 included. See the back-end documentation for more information.
151 :author `user-full-name'
152 :creator `org-export-creator-string'
153 :email `user-mail-address'
154 :exclude-tags `org-export-exclude-tags'
155 :headline-levels `org-export-headline-levels'
156 :language `org-export-default-language'
157 :preserve-breaks `org-export-preserve-breaks'
158 :section-numbers `org-export-with-section-numbers'
159 :select-tags `org-export-select-tags'
160 :time-stamp-file `org-export-time-stamp-file'
161 :with-archived-trees `org-export-with-archived-trees'
162 :with-author `org-export-with-author'
163 :with-creator `org-export-with-creator'
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-priority `org-export-with-priority'
171 :with-special-strings `org-export-with-special-strings'
172 :with-sub-superscript `org-export-with-sub-superscripts'
173 :with-toc `org-export-with-toc'
174 :with-tables `org-export-with-tables'
175 :with-tags `org-export-with-tags'
176 :with-tasks `org-export-with-tasks'
177 :with-timestamps `org-export-with-timestamps'
178 :with-todo-keywords `org-export-with-todo-keywords'
180 The following properties may be used to control publishing of
181 a site-map of files or summary page for a given project.
183 `:auto-sitemap'
185 Whether to publish a site-map during
186 `org-e-publish-current-project' or `org-e-publish-all'.
188 `:sitemap-filename'
190 Filename for output of sitemap. Defaults to \"sitemap.org\".
192 `:sitemap-title'
194 Title of site-map page. Defaults to name of file.
196 `:sitemap-function'
198 Plugin function to use for generation of site-map. Defaults to
199 `org-e-publish-org-sitemap', which generates a plain list of
200 links to all files in the project.
202 `:sitemap-style'
204 Can be `list' \(site-map is just an itemized list of the
205 titles of the files involved) or `tree' \(the directory
206 structure of the source files is reflected in the site-map).
207 Defaults to `tree'.
209 `:sitemap-sans-extension'
211 Remove extension from site-map's file-names. Useful to have
212 cool URIs \(see http://www.w3.org/Provider/Style/URI).
213 Defaults to nil.
215 If you create a site-map file, adjust the sorting like this:
217 `:sitemap-sort-folders'
219 Where folders should appear in the site-map. Set this to
220 `first' \(default) or `last' to display folders first or
221 last, respectively. Any other value will mix files and
222 folders.
224 `:sitemap-sort-files'
226 The site map is normally sorted alphabetically. You can
227 change this behaviour setting this to `anti-chronologically',
228 `chronologically', or nil.
230 `:sitemap-ignore-case'
232 Should sorting be case-sensitive? Default nil.
234 The following properties control the creation of a concept index.
236 `:makeindex'
238 Create a concept index.
240 Other properties affecting publication.
242 `:body-only'
244 Set this to t to publish only the body of the documents."
245 :group 'org-e-publish
246 :type 'alist)
248 (defcustom org-e-publish-use-timestamps-flag t
249 "Non-nil means use timestamp checking to publish only changed files.
250 When nil, do no timestamp checking and always publish all files."
251 :group 'org-e-publish
252 :type 'boolean)
254 (defcustom org-e-publish-timestamp-directory
255 (convert-standard-filename "~/.org-timestamps/")
256 "Name of directory in which to store publishing timestamps."
257 :group 'org-e-publish
258 :type 'directory)
260 (defcustom org-e-publish-list-skipped-files t
261 "Non-nil means show message about files *not* published."
262 :group 'org-e-publish
263 :type 'boolean)
265 (defcustom org-e-publish-sitemap-sort-files 'alphabetically
266 "Method to sort files in site-maps.
267 Possible values are `alphabetically', `chronologically',
268 `anti-chronologically' and nil.
270 If `alphabetically', files will be sorted alphabetically. If
271 `chronologically', files will be sorted with older modification
272 time first. If `anti-chronologically', files will be sorted with
273 newer modification time first. nil won't sort files.
275 You can overwrite this default per project in your
276 `org-e-publish-project-alist', using `:sitemap-sort-files'."
277 :group 'org-e-publish
278 :type 'symbol)
280 (defcustom org-e-publish-sitemap-sort-folders 'first
281 "A symbol, denoting if folders are sorted first in sitemaps.
282 Possible values are `first', `last', and nil.
283 If `first', folders will be sorted before files.
284 If `last', folders are sorted to the end after the files.
285 Any other value will not mix files and folders.
287 You can overwrite this default per project in your
288 `org-e-publish-project-alist', using `:sitemap-sort-folders'."
289 :group 'org-e-publish
290 :type 'symbol)
292 (defcustom org-e-publish-sitemap-sort-ignore-case nil
293 "Non-nil when site-map sorting should ignore case.
295 You can overwrite this default per project in your
296 `org-e-publish-project-alist', using `:sitemap-ignore-case'."
297 :group 'org-e-publish
298 :type 'boolean)
300 (defcustom org-e-publish-sitemap-date-format "%Y-%m-%d"
301 "Format for `format-time-string' which is used to print a date
302 in the sitemap."
303 :group 'org-e-publish
304 :type 'string)
306 (defcustom org-e-publish-sitemap-file-entry-format "%t"
307 "Format string for site-map file entry.
308 You could use brackets to delimit on what part the link will be.
310 %t is the title.
311 %a is the author.
312 %d is the date formatted using `org-e-publish-sitemap-date-format'."
313 :group 'org-e-publish
314 :type 'string)
318 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
319 ;;; Timestamp-related functions
321 (defun org-e-publish-timestamp-filename (filename &optional pub-dir pub-func)
322 "Return path to timestamp file for filename FILENAME."
323 (setq filename (concat filename "::" (or pub-dir "") "::"
324 (format "%s" (or pub-func ""))))
325 (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
327 (defun org-e-publish-needed-p
328 (filename &optional pub-dir pub-func true-pub-dir base-dir)
329 "Non-nil if FILENAME should be published in PUB-DIR using PUB-FUNC.
330 TRUE-PUB-DIR is where the file will truly end up. Currently we
331 are not using this - maybe it can eventually be used to check if
332 the file is present at the target location, and how old it is.
333 Right now we cannot do this, because we do not know under what
334 file name the file will be stored - the publishing function can
335 still decide about that independently."
336 (let ((rtn (if (not org-e-publish-use-timestamps-flag) t
337 (org-e-publish-cache-file-needs-publishing
338 filename pub-dir pub-func base-dir))))
339 (if rtn (message "Publishing file %s using `%s'" filename pub-func)
340 (when org-e-publish-list-skipped-files
341 (message "Skipping unmodified file %s" filename)))
342 rtn))
344 (defun org-e-publish-update-timestamp
345 (filename &optional pub-dir pub-func base-dir)
346 "Update publishing timestamp for file FILENAME.
347 If there is no timestamp, create one."
348 (let ((key (org-e-publish-timestamp-filename filename pub-dir pub-func))
349 (stamp (org-e-publish-cache-ctime-of-src filename)))
350 (org-e-publish-cache-set key stamp)))
352 (defun org-e-publish-remove-all-timestamps ()
353 "Remove all files in the timestamp directory."
354 (let ((dir org-e-publish-timestamp-directory)
355 files)
356 (when (and (file-exists-p dir) (file-directory-p dir))
357 (mapc 'delete-file (directory-files dir 'full "[^.]\\'"))
358 (org-e-publish-reset-cache))))
362 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
363 ;;; Getting project information out of `org-e-publish-project-alist'
365 (defun org-e-publish-expand-projects (projects-alist)
366 "Expand projects in PROJECTS-ALIST.
367 This splices all the components into the list."
368 (let ((rest projects-alist) rtn p components)
369 (while (setq p (pop rest))
370 (if (setq components (plist-get (cdr p) :components))
371 (setq rest (append
372 (mapcar (lambda (x) (assoc x org-e-publish-project-alist))
373 components)
374 rest))
375 (push p rtn)))
376 (nreverse (delete-dups (delq nil rtn)))))
378 (defvar org-sitemap-sort-files)
379 (defvar org-sitemap-sort-folders)
380 (defvar org-sitemap-ignore-case)
381 (defvar org-sitemap-requested)
382 (defvar org-sitemap-date-format)
383 (defvar org-sitemap-file-entry-format)
384 (defun org-e-publish-compare-directory-files (a b)
385 "Predicate for `sort', that sorts folders and files for sitemap."
386 (let ((retval t))
387 (when (or org-sitemap-sort-files org-sitemap-sort-folders)
388 ;; First we sort files:
389 (when org-sitemap-sort-files
390 (case org-sitemap-sort-files
391 (alphabetically
392 (let* ((adir (file-directory-p a))
393 (aorg (and (string-match "\\.org$" a) (not adir)))
394 (bdir (file-directory-p b))
395 (borg (and (string-match "\\.org$" b) (not bdir)))
396 (A (if aorg (concat (file-name-directory a)
397 (org-e-publish-find-title a)) a))
398 (B (if borg (concat (file-name-directory b)
399 (org-e-publish-find-title b)) b)))
400 (setq retval (if org-sitemap-ignore-case
401 (not (string-lessp (upcase B) (upcase A)))
402 (not (string-lessp B A))))))
403 ((anti-chronologically chronologically)
404 (let* ((adate (org-e-publish-find-date a))
405 (bdate (org-e-publish-find-date b))
406 (A (+ (lsh (car adate) 16) (cadr adate)))
407 (B (+ (lsh (car bdate) 16) (cadr bdate))))
408 (setq retval
409 (if (eq org-sitemap-sort-files 'chronologically) (<= A B)
410 (>= A B)))))))
411 ;; Directory-wise wins:
412 (when org-sitemap-sort-folders
413 ;; a is directory, b not:
414 (cond
415 ((and (file-directory-p a) (not (file-directory-p b)))
416 (setq retval (equal org-sitemap-sort-folders 'first)))
417 ;; a is not a directory, but b is:
418 ((and (not (file-directory-p a)) (file-directory-p b))
419 (setq retval (equal org-sitemap-sort-folders 'last))))))
420 retval))
422 (defun org-e-publish-get-base-files-1
423 (base-dir &optional recurse match skip-file skip-dir)
424 "Set `org-e-publish-temp-files' with files from BASE-DIR directory.
425 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
426 non-nil, restrict this list to the files matching the regexp
427 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
428 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
429 matching the regexp SKIP-DIR when recursing through BASE-DIR."
430 (mapc (lambda (f)
431 (let ((fd-p (file-directory-p f))
432 (fnd (file-name-nondirectory f)))
433 (if (and fd-p recurse
434 (not (string-match "^\\.+$" fnd))
435 (if skip-dir (not (string-match skip-dir fnd)) t))
436 (org-e-publish-get-base-files-1
437 f recurse match skip-file skip-dir)
438 (unless (or fd-p ;; this is a directory
439 (and skip-file (string-match skip-file fnd))
440 (not (file-exists-p (file-truename f)))
441 (not (string-match match fnd)))
443 (pushnew f org-e-publish-temp-files)))))
444 (if org-sitemap-requested
445 (sort (directory-files base-dir t (unless recurse match))
446 'org-e-publish-compare-directory-files)
447 (directory-files base-dir t (unless recurse match)))))
449 (defun org-e-publish-get-base-files (project &optional exclude-regexp)
450 "Return a list of all files in PROJECT.
451 If EXCLUDE-REGEXP is set, this will be used to filter out
452 matching filenames."
453 (let* ((project-plist (cdr project))
454 (base-dir (file-name-as-directory
455 (plist-get project-plist :base-directory)))
456 (include-list (plist-get project-plist :include))
457 (recurse (plist-get project-plist :recursive))
458 (extension (or (plist-get project-plist :base-extension) "org"))
459 ;; sitemap-... variables are dynamically scoped for
460 ;; org-e-publish-compare-directory-files:
461 (org-sitemap-requested
462 (plist-get project-plist :auto-sitemap))
463 (sitemap-filename
464 (or (plist-get project-plist :sitemap-filename) "sitemap.org"))
465 (org-sitemap-sort-folders
466 (if (plist-member project-plist :sitemap-sort-folders)
467 (plist-get project-plist :sitemap-sort-folders)
468 org-e-publish-sitemap-sort-folders))
469 (org-sitemap-sort-files
470 (cond ((plist-member project-plist :sitemap-sort-files)
471 (plist-get project-plist :sitemap-sort-files))
472 ;; For backward compatibility:
473 ((plist-member project-plist :sitemap-alphabetically)
474 (if (plist-get project-plist :sitemap-alphabetically)
475 'alphabetically nil))
476 (t org-e-publish-sitemap-sort-files)))
477 (org-sitemap-ignore-case
478 (if (plist-member project-plist :sitemap-ignore-case)
479 (plist-get project-plist :sitemap-ignore-case)
480 org-e-publish-sitemap-sort-ignore-case))
481 (match (if (eq extension 'any) "^[^\\.]"
482 (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
483 ;; Make sure `org-sitemap-sort-folders' has an accepted value
484 (unless (memq org-sitemap-sort-folders '(first last))
485 (setq org-sitemap-sort-folders nil))
487 (setq org-e-publish-temp-files nil)
488 (if org-sitemap-requested
489 (pushnew (expand-file-name (concat base-dir sitemap-filename))
490 org-e-publish-temp-files))
491 (org-e-publish-get-base-files-1 base-dir recurse match
492 ;; FIXME distinguish exclude regexp
493 ;; for skip-file and skip-dir?
494 exclude-regexp exclude-regexp)
495 (mapc (lambda (f)
496 (pushnew
497 (expand-file-name (concat base-dir f))
498 org-e-publish-temp-files))
499 include-list)
500 org-e-publish-temp-files))
502 (defun org-e-publish-get-project-from-filename (filename &optional up)
503 "Return the project that FILENAME belongs to."
504 (let* ((filename (expand-file-name filename))
505 project-name)
507 (catch 'p-found
508 (dolist (prj org-e-publish-project-alist)
509 (unless (plist-get (cdr prj) :components)
510 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
511 (let* ((r (plist-get (cdr prj) :recursive))
512 (b (expand-file-name (file-name-as-directory
513 (plist-get (cdr prj) :base-directory))))
514 (x (or (plist-get (cdr prj) :base-extension) "org"))
515 (e (plist-get (cdr prj) :exclude))
516 (i (plist-get (cdr prj) :include))
517 (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
518 (when
519 (or (and i
520 (member filename
521 (mapcar (lambda (file)
522 (expand-file-name file b))
523 i)))
524 (and (not (and e (string-match e filename)))
525 (string-match xm filename)))
526 (setq project-name (car prj))
527 (throw 'p-found project-name))))))
528 (when up
529 (dolist (prj org-e-publish-project-alist)
530 (if (member project-name (plist-get (cdr prj) :components))
531 (setq project-name (car prj)))))
532 (assoc project-name org-e-publish-project-alist)))
536 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
537 ;;; Tools for publishing functions in back-ends
539 (defun org-e-publish-org-to (backend filename extension plist &optional pub-dir)
540 "Publish an Org file to a specified back-end.
542 BACKEND is a symbol representing the back-end used for
543 transcoding. FILENAME is the filename of the Org file to be
544 published. EXTENSION is the extension used for the output
545 string, with the leading dot. PLIST is the property list for the
546 given project.
548 Optional argument PUB-DIR, when non-nil is the publishing
549 directory.
551 Return output file name."
552 (unless (or (not pub-dir) (file-exists-p pub-dir)) (make-directory pub-dir t))
553 ;; Check if a buffer visiting FILENAME is already open.
554 (let* ((visitingp (find-buffer-visiting filename))
555 (work-buffer (or visitingp (find-file-noselect filename))))
556 (prog1 (with-current-buffer work-buffer
557 (let ((output-file
558 (org-export-output-file-name extension nil pub-dir))
559 (body-p (plist-get plist :body-only)))
560 (org-export-to-file
561 backend output-file nil nil body-p
562 ;; Install `org-e-publish-collect-index' in parse tree
563 ;; filters. It isn't dependent on `:makeindex', since
564 ;; we want to keep it up-to-date in cache anyway.
565 (org-combine-plists
566 plist `(:filter-parse-tree
567 (org-e-publish-collect-index
568 ,@(plist-get plist :filter-parse-tree)))))))
569 ;; Remove opened buffer in the process.
570 (unless visitingp (kill-buffer work-buffer)))))
572 (defvar project-plist)
574 (defun org-e-publish-attachment (plist filename pub-dir)
575 "Publish a file with no transformation of any kind.
577 FILENAME is the filename of the Org file to be published. PLIST
578 is the property list for the given project. PUB-DIR is the
579 publishing directory.
581 Return output file name."
582 (unless (file-directory-p pub-dir)
583 (make-directory pub-dir t))
584 (or (equal (expand-file-name (file-name-directory filename))
585 (file-name-as-directory (expand-file-name pub-dir)))
586 (copy-file filename
587 (expand-file-name (file-name-nondirectory filename) pub-dir)
588 t)))
592 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
593 ;;; Publishing files, sets of files, and indices
595 (defun org-e-publish-file (filename &optional project no-cache)
596 "Publish file FILENAME from PROJECT.
597 If NO-CACHE is not nil, do not initialize org-e-publish-cache and
598 write it to disk. This is needed, since this function is used to
599 publish single files, when entire projects are published.
600 See `org-e-publish-projects'."
601 (let* ((project
602 (or project
603 (or (org-e-publish-get-project-from-filename filename)
604 (error "File %s not part of any known project"
605 (abbreviate-file-name filename)))))
606 (project-plist (cdr project))
607 (ftname (expand-file-name filename))
608 (publishing-function
609 (or (plist-get project-plist :publishing-function)
610 (error "No publishing function chosen")))
611 (base-dir
612 (file-name-as-directory
613 (expand-file-name
614 (or (plist-get project-plist :base-directory)
615 (error "Project %s does not have :base-directory defined"
616 (car project))))))
617 (pub-dir
618 (file-name-as-directory
619 (file-truename
620 (or (eval (plist-get project-plist :publishing-directory))
621 (error "Project %s does not have :publishing-directory defined"
622 (car project))))))
623 tmp-pub-dir)
625 (unless no-cache (org-e-publish-initialize-cache (car project)))
627 (setq tmp-pub-dir
628 (file-name-directory
629 (concat pub-dir
630 (and (string-match (regexp-quote base-dir) ftname)
631 (substring ftname (match-end 0))))))
632 (if (listp publishing-function)
633 ;; allow chain of publishing functions
634 (mapc (lambda (f)
635 (when (org-e-publish-needed-p
636 filename pub-dir f tmp-pub-dir base-dir)
637 (funcall f project-plist filename tmp-pub-dir)
638 (org-e-publish-update-timestamp filename pub-dir f base-dir)))
639 publishing-function)
640 (when (org-e-publish-needed-p
641 filename pub-dir publishing-function tmp-pub-dir base-dir)
642 (funcall publishing-function project-plist filename tmp-pub-dir)
643 (org-e-publish-update-timestamp
644 filename pub-dir publishing-function base-dir)))
645 (unless no-cache (org-e-publish-write-cache-file))))
647 (defun org-e-publish-projects (projects)
648 "Publish all files belonging to the PROJECTS alist.
649 If `:auto-sitemap' is set, publish the sitemap too. If
650 `:makeindex' is set, also produce a file theindex.org."
651 (mapc
652 (lambda (project)
653 ;; Each project uses its own cache file:
654 (org-e-publish-initialize-cache (car project))
655 (let* ((project-plist (cdr project))
656 (exclude-regexp (plist-get project-plist :exclude))
657 (sitemap-p (plist-get project-plist :auto-sitemap))
658 (sitemap-filename (or (plist-get project-plist :sitemap-filename)
659 "sitemap.org"))
660 (sitemap-function (or (plist-get project-plist :sitemap-function)
661 'org-e-publish-org-sitemap))
662 (org-sitemap-date-format
663 (or (plist-get project-plist :sitemap-date-format)
664 org-e-publish-sitemap-date-format))
665 (org-sitemap-file-entry-format
666 (or (plist-get project-plist :sitemap-file-entry-format)
667 org-e-publish-sitemap-file-entry-format))
668 (preparation-function
669 (plist-get project-plist :preparation-function))
670 (completion-function (plist-get project-plist :completion-function))
671 (files (org-e-publish-get-base-files project exclude-regexp)) file)
672 (when preparation-function (run-hooks 'preparation-function))
673 (if sitemap-p (funcall sitemap-function project sitemap-filename))
674 (dolist (file files) (org-e-publish-file file project t))
675 (when (plist-get project-plist :makeindex)
676 (org-e-publish-index-generate-theindex
677 project (plist-get project-plist :base-directory))
678 (org-e-publish-file
679 (expand-file-name
680 "theindex.org" (plist-get project-plist :base-directory))
681 project t))
682 (when completion-function (run-hooks 'completion-function))
683 (org-e-publish-write-cache-file)))
684 (org-e-publish-expand-projects projects)))
686 (defun org-e-publish-org-sitemap (project &optional sitemap-filename)
687 "Create a sitemap of pages in set defined by PROJECT.
688 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
689 Default for SITEMAP-FILENAME is 'sitemap.org'."
690 (let* ((project-plist (cdr project))
691 (dir (file-name-as-directory
692 (plist-get project-plist :base-directory)))
693 (localdir (file-name-directory dir))
694 (indent-str (make-string 2 ?\ ))
695 (exclude-regexp (plist-get project-plist :exclude))
696 (files (nreverse
697 (org-e-publish-get-base-files project exclude-regexp)))
698 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
699 (sitemap-title (or (plist-get project-plist :sitemap-title)
700 (concat "Sitemap for project " (car project))))
701 (sitemap-style (or (plist-get project-plist :sitemap-style)
702 'tree))
703 (sitemap-sans-extension
704 (plist-get project-plist :sitemap-sans-extension))
705 (visiting (find-buffer-visiting sitemap-filename))
706 (ifn (file-name-nondirectory sitemap-filename))
707 file sitemap-buffer)
708 (with-current-buffer (setq sitemap-buffer
709 (or visiting (find-file sitemap-filename)))
710 (erase-buffer)
711 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
712 (while (setq file (pop files))
713 (let ((fn (file-name-nondirectory file))
714 (link (file-relative-name file dir))
715 (oldlocal localdir))
716 (when sitemap-sans-extension
717 (setq link (file-name-sans-extension link)))
718 ;; sitemap shouldn't list itself
719 (unless (equal (file-truename sitemap-filename)
720 (file-truename file))
721 (if (eq sitemap-style 'list)
722 (message "Generating list-style sitemap for %s" sitemap-title)
723 (message "Generating tree-style sitemap for %s" sitemap-title)
724 (setq localdir (concat (file-name-as-directory dir)
725 (file-name-directory link)))
726 (unless (string= localdir oldlocal)
727 (if (string= localdir dir)
728 (setq indent-str (make-string 2 ?\ ))
729 (let ((subdirs
730 (split-string
731 (directory-file-name
732 (file-name-directory
733 (file-relative-name localdir dir))) "/"))
734 (subdir "")
735 (old-subdirs (split-string
736 (file-relative-name oldlocal dir) "/")))
737 (setq indent-str (make-string 2 ?\ ))
738 (while (string= (car old-subdirs) (car subdirs))
739 (setq indent-str (concat indent-str (make-string 2 ?\ )))
740 (pop old-subdirs)
741 (pop subdirs))
742 (dolist (d subdirs)
743 (setq subdir (concat subdir d "/"))
744 (insert (concat indent-str " + " d "\n"))
745 (setq indent-str (make-string
746 (+ (length indent-str) 2) ?\ )))))))
747 ;; This is common to 'flat and 'tree
748 (let ((entry
749 (org-e-publish-format-file-entry
750 org-sitemap-file-entry-format file project-plist))
751 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
752 (cond ((string-match-p regexp entry)
753 (string-match regexp entry)
754 (insert (concat indent-str " + " (match-string 1 entry)
755 "[[file:" link "]["
756 (match-string 2 entry)
757 "]]" (match-string 3 entry) "\n")))
759 (insert (concat indent-str " + [[file:" link "]["
760 entry
761 "]]\n"))))))))
762 (save-buffer))
763 (or visiting (kill-buffer sitemap-buffer))))
765 (defun org-e-publish-format-file-entry (fmt file project-plist)
766 (format-spec fmt
767 `((?t . ,(org-e-publish-find-title file t))
768 (?d . ,(format-time-string org-sitemap-date-format
769 (org-e-publish-find-date file)))
770 (?a . ,(or (plist-get project-plist :author) user-full-name)))))
772 (defun org-e-publish-find-title (file &optional reset)
773 "Find the title of FILE in project."
775 (and (not reset) (org-e-publish-cache-get-file-property file :title nil t))
776 (let* ((visiting (find-buffer-visiting file))
777 (buffer (or visiting (find-file-noselect file)))
778 title)
779 (with-current-buffer buffer
780 (org-mode)
781 (setq title
782 (or (org-element-interpret-data
783 (plist-get (org-export-get-environment) :title))
784 (file-name-nondirectory (file-name-sans-extension file)))))
785 (unless visiting (kill-buffer buffer))
786 (org-e-publish-cache-set-file-property file :title title)
787 title)))
789 (defun org-e-publish-find-date (file)
790 "Find the date of FILE in project.
791 If FILE provides a #+date keyword use it else use the file
792 system's modification time.
794 It returns time in `current-time' format."
795 (let* ((visiting (find-buffer-visiting file))
796 (file-buf (or visiting (find-file-noselect file nil)))
797 (date (plist-get
798 (with-current-buffer file-buf
799 (org-mode)
800 (org-export--get-inbuffer-options))
801 :date)))
802 (unless visiting (kill-buffer file-buf))
803 (if date (org-time-string-to-time date)
804 (when (file-exists-p file)
805 (nth 5 (file-attributes file))))))
809 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
810 ;;; Interactive publishing functions
812 ;;;###autoload
813 (defalias 'org-e-publish-project 'org-e-publish)
815 ;;;###autoload
816 (defun org-e-publish (project &optional force async)
817 "Publish PROJECT.
819 PROJECT is either a project name, as a string, or a project
820 alist (see `org-e-publish-project-alist' variable).
822 When optional argument FORCE is non-nil, force publishing all
823 files in PROJECT. With a non-nil optional argument ASYNC,
824 publishing will be done asynchronously, in another process."
825 (interactive
826 (list
827 (assoc (org-icompleting-read
828 "Publish project: "
829 org-e-publish-project-alist nil t)
830 org-e-publish-project-alist)
831 current-prefix-arg))
832 (let ((project-alist (if (not (stringp project)) (list project)
833 ;; If this function is called in batch mode,
834 ;; project is still a string here.
835 (list (assoc project org-e-publish-project-alist)))))
836 (if async
837 (org-export-async-start 'ignore
838 `(let ((org-e-publish-use-timestamps-flag
839 (if ',force nil ,org-e-publish-use-timestamps-flag)))
840 (org-e-publish-projects ',project-alist)))
841 (save-window-excursion
842 (let* ((org-e-publish-use-timestamps-flag
843 (if force nil org-e-publish-use-timestamps-flag)))
844 (org-e-publish-projects project-alist))))))
846 ;;;###autoload
847 (defun org-e-publish-all (&optional force async)
848 "Publish all projects.
849 With prefix argument FORCE, remove all files in the timestamp
850 directory and force publishing all projects. With a non-nil
851 optional argument ASYNC, publishing will be done asynchronously,
852 in another process."
853 (interactive "P")
854 (if async
855 (org-export-async-start 'ignore
856 `(when ',force (org-e-publish-remove-all-timestamps))
857 `(let ((org-e-publish-use-timestamps-flag
858 (if ',force nil ,org-e-publish-use-timestamps-flag)))
859 (org-e-publish-projects ',org-e-publish-project-alist)))
860 (when force (org-e-publish-remove-all-timestamps))
861 (save-window-excursion
862 (let ((org-e-publish-use-timestamps-flag
863 (if force nil org-e-publish-use-timestamps-flag)))
864 (org-e-publish-projects org-e-publish-project-alist)))))
867 ;;;###autoload
868 (defun org-e-publish-current-file (&optional force async)
869 "Publish the current file.
870 With prefix argument FORCE, force publish the file. When
871 optional argument ASYNC is non-nil, publishing will be done
872 asynchronously, in another process."
873 (interactive "P")
874 (let ((file (buffer-file-name (buffer-base-buffer))))
875 (if async
876 (org-export-async-start 'ignore
877 `(let ((org-e-publish-use-timestamps-flag
878 (if ',force nil ,org-e-publish-use-timestamps-flag)))
879 (org-e-publish-file ,file)))
880 (save-window-excursion
881 (let ((org-e-publish-use-timestamps-flag
882 (if force nil org-e-publish-use-timestamps-flag)))
883 (org-e-publish-file file))))))
885 ;;;###autoload
886 (defun org-e-publish-current-project (&optional force async)
887 "Publish the project associated with the current file.
888 With a prefix argument, force publishing of all files in
889 the project."
890 (interactive "P")
891 (save-window-excursion
892 (let ((project (org-e-publish-get-project-from-filename
893 (buffer-file-name (buffer-base-buffer)) 'up)))
894 (if project (org-e-publish project force async)
895 (error "File %s is not part of any known project"
896 (buffer-file-name (buffer-base-buffer)))))))
900 ;;; Index generation
902 (defun org-e-publish-collect-index (tree backend info)
903 "Update index for a file with TREE in cache.
905 BACKEND is the back-end being used for transcoding. INFO is
906 a plist containing publishing options.
908 The index relative to current file is stored as an alist. An
909 association has the following shape: (TERM FILE-NAME PARENT),
910 where TERM is the indexed term, as a string, FILE-NAME is the
911 original full path of the file where the term in encountered, and
912 PARENT is a reference to the headline, if any, containing the
913 original index keyword. When non-nil, this reference is a cons
914 cell. Its CAR is a symbol among `id', `custom-id' and `name' and
915 its CDR is a string."
916 (let ((file (plist-get info :input-file)))
917 (org-e-publish-cache-set-file-property
918 file :index
919 (delete-dups
920 (org-element-map
921 tree 'keyword
922 (lambda (k)
923 (when (equal (upcase (org-element-property :key k)) "INDEX")
924 (let ((parent (org-export-get-parent-headline k)))
925 (list (org-element-property :value k)
926 file
927 (cond
928 ((not parent) nil)
929 ((let ((id (org-element-property :id parent)))
930 (and id (cons 'id id))))
931 ((let ((id (org-element-property :custom-id parent)))
932 (and id (cons 'custom-id id))))
933 (t (cons 'name
934 (org-element-property :raw-value parent))))))))
935 info))))
936 ;; Return parse-tree to avoid altering output.
937 tree)
939 (defun org-e-publish-index-generate-theindex (project directory)
940 "Retrieve full index from cache and build \"theindex.org\".
941 PROJECT is the project the index relates to. DIRECTORY is the
942 publishing directory."
943 (let ((all-files (org-e-publish-get-base-files
944 project (plist-get (cdr project) :exclude)))
945 full-index)
946 ;; Compile full index.
947 (mapc
948 (lambda (file)
949 (let ((index (org-e-publish-cache-get-file-property file :index)))
950 (dolist (term index)
951 (unless (member term full-index) (push term full-index)))))
952 all-files)
953 ;; Sort it alphabetically.
954 (setq full-index
955 (sort full-index (lambda (a b) (string< (downcase (car a))
956 (downcase (car b))))))
957 ;; Fill "theindex.org".
958 (with-temp-buffer
959 (insert "#+TITLE: Index\n#+OPTIONS: num:nil author:nil\n")
960 (let ((current-letter nil) (last-entry nil))
961 (dolist (idx full-index)
962 (let* ((entry (org-split-string (car idx) "!"))
963 (letter (upcase (substring (car entry) 0 1)))
964 ;; Transform file into a path relative to publishing
965 ;; directory.
966 (file (file-relative-name
967 (nth 1 idx)
968 (plist-get (cdr project) :base-directory))))
969 ;; Check if another letter has to be inserted.
970 (unless (string= letter current-letter)
971 (insert (format "* %s\n" letter)))
972 ;; Compute the first difference between last entry and
973 ;; current one: it tells the level at which new items
974 ;; should be added.
975 (let* ((rank (loop for n from 0 to (length entry)
976 unless (equal (nth n entry) (nth n last-entry))
977 return n))
978 (len (length (nthcdr rank entry))))
979 ;; For each term after the first difference, create
980 ;; a new sub-list with the term as body. Moreover,
981 ;; linkify the last term.
982 (dotimes (n len)
983 (insert
984 (concat
985 (make-string (* (+ rank n) 2) ? ) " - "
986 (if (not (= (1- len) n)) (nth (+ rank n) entry)
987 ;; Last term: Link it to TARGET, if possible.
988 (let ((target (nth 2 idx)))
989 (format
990 "[[%s][%s]]"
991 ;; Destination.
992 (case (car target)
993 ('nil (format "file:%s" file))
994 (id (format "id:%s" (cdr target)))
995 (custom-id (format "file:%s::#%s" file (cdr target)))
996 (otherwise (format "file:%s::*%s" file (cdr target))))
997 ;; Description.
998 (car (last entry)))))
999 "\n"))))
1000 (setq current-letter letter last-entry entry))))
1001 ;; Write index.
1002 (write-file (expand-file-name "theindex.org" directory)))))
1006 ;;; Caching functions
1008 (defun org-e-publish-write-cache-file (&optional free-cache)
1009 "Write `org-e-publish-cache' to file.
1010 If FREE-CACHE, empty the cache."
1011 (unless org-e-publish-cache
1012 (error "`org-e-publish-write-cache-file' called, but no cache present"))
1014 (let ((cache-file (org-e-publish-cache-get ":cache-file:")))
1015 (unless cache-file
1016 (error "Cannot find cache-file name in `org-e-publish-write-cache-file'"))
1017 (with-temp-file cache-file
1018 (let (print-level print-length)
1019 (insert "(setq org-e-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
1020 (maphash (lambda (k v)
1021 (insert
1022 (format (concat "(puthash %S "
1023 (if (or (listp v) (symbolp v))
1024 "'" "")
1025 "%S org-e-publish-cache)\n") k v)))
1026 org-e-publish-cache)))
1027 (when free-cache (org-e-publish-reset-cache))))
1029 (defun org-e-publish-initialize-cache (project-name)
1030 "Initialize the projects cache if not initialized yet and return it."
1032 (unless project-name
1033 (error "Cannot initialize `org-e-publish-cache' without projects name in `org-e-publish-initialize-cache'"))
1035 (unless (file-exists-p org-e-publish-timestamp-directory)
1036 (make-directory org-e-publish-timestamp-directory t))
1037 (unless (file-directory-p org-e-publish-timestamp-directory)
1038 (error "Org publish timestamp: %s is not a directory"
1039 org-e-publish-timestamp-directory))
1041 (unless (and org-e-publish-cache
1042 (string= (org-e-publish-cache-get ":project:") project-name))
1043 (let* ((cache-file
1044 (concat
1045 (expand-file-name org-e-publish-timestamp-directory)
1046 project-name ".cache"))
1047 (cexists (file-exists-p cache-file)))
1049 (when org-e-publish-cache (org-e-publish-reset-cache))
1051 (if cexists (load-file cache-file)
1052 (setq org-e-publish-cache
1053 (make-hash-table :test 'equal :weakness nil :size 100))
1054 (org-e-publish-cache-set ":project:" project-name)
1055 (org-e-publish-cache-set ":cache-file:" cache-file))
1056 (unless cexists (org-e-publish-write-cache-file nil))))
1057 org-e-publish-cache)
1059 (defun org-e-publish-reset-cache ()
1060 "Empty org-e-publish-cache and reset it nil."
1061 (message "%s" "Resetting org-e-publish-cache")
1062 (when (hash-table-p org-e-publish-cache)
1063 (clrhash org-e-publish-cache))
1064 (setq org-e-publish-cache nil))
1066 (defun org-e-publish-cache-file-needs-publishing
1067 (filename &optional pub-dir pub-func base-dir)
1068 "Check the timestamp of the last publishing of FILENAME.
1069 Non-nil if the file needs publishing. The function also checks
1070 if any included files have been more recently published, so that
1071 the file including them will be republished as well."
1072 (unless org-e-publish-cache
1073 (error
1074 "`org-e-publish-cache-file-needs-publishing' called, but no cache present"))
1075 (let* ((case-fold-search t)
1076 (key (org-e-publish-timestamp-filename filename pub-dir pub-func))
1077 (pstamp (org-e-publish-cache-get key))
1078 (visiting (find-buffer-visiting filename))
1079 included-files-ctime buf)
1081 (when (equal (file-name-extension filename) "org")
1082 (setq buf (find-file (expand-file-name filename)))
1083 (with-current-buffer buf
1084 (goto-char (point-min))
1085 (while (re-search-forward
1086 "^#\\+INCLUDE:[ \t]+\"\\([^\t\n\r\"]*\\)\"[ \t]*.*$" nil t)
1087 (let* ((included-file (expand-file-name (match-string 1))))
1088 (add-to-list 'included-files-ctime
1089 (org-e-publish-cache-ctime-of-src included-file) t))))
1090 (unless visiting (kill-buffer buf)))
1091 (if (null pstamp) t
1092 (let ((ctime (org-e-publish-cache-ctime-of-src filename)))
1093 (or (< pstamp ctime)
1094 (when included-files-ctime
1095 (not (null (delq nil (mapcar (lambda(ct) (< ctime ct))
1096 included-files-ctime))))))))))
1098 (defun org-e-publish-cache-set-file-property
1099 (filename property value &optional project-name)
1100 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1101 Use cache file of PROJECT-NAME. If the entry does not exist, it
1102 will be created. Return VALUE."
1103 ;; Evtl. load the requested cache file:
1104 (if project-name (org-e-publish-initialize-cache project-name))
1105 (let ((pl (org-e-publish-cache-get filename)))
1106 (if pl (progn (plist-put pl property value) value)
1107 (org-e-publish-cache-get-file-property
1108 filename property value nil project-name))))
1110 (defun org-e-publish-cache-get-file-property
1111 (filename property &optional default no-create project-name)
1112 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1113 Use cache file of PROJECT-NAME. Return the value of that PROPERTY
1114 or DEFAULT, if the value does not yet exist. If the entry will
1115 be created, unless NO-CREATE is not nil."
1116 ;; Evtl. load the requested cache file:
1117 (if project-name (org-e-publish-initialize-cache project-name))
1118 (let ((pl (org-e-publish-cache-get filename)) retval)
1119 (if pl
1120 (if (plist-member pl property)
1121 (setq retval (plist-get pl property))
1122 (setq retval default))
1123 ;; no pl yet:
1124 (unless no-create
1125 (org-e-publish-cache-set filename (list property default)))
1126 (setq retval default))
1127 retval))
1129 (defun org-e-publish-cache-get (key)
1130 "Return the value stored in `org-e-publish-cache' for key KEY.
1131 Returns nil, if no value or nil is found, or the cache does not
1132 exist."
1133 (unless org-e-publish-cache
1134 (error "`org-e-publish-cache-get' called, but no cache present"))
1135 (gethash key org-e-publish-cache))
1137 (defun org-e-publish-cache-set (key value)
1138 "Store KEY VALUE pair in `org-e-publish-cache'.
1139 Returns value on success, else nil."
1140 (unless org-e-publish-cache
1141 (error "`org-e-publish-cache-set' called, but no cache present"))
1142 (puthash key value org-e-publish-cache))
1144 (defun org-e-publish-cache-ctime-of-src (file)
1145 "Get the ctime of FILE as an integer."
1146 (let ((attr (file-attributes
1147 (expand-file-name (or (file-symlink-p file) file)
1148 (file-name-directory file)))))
1149 (+ (lsh (car (nth 5 attr)) 16)
1150 (cadr (nth 5 attr)))))
1153 (provide 'org-e-publish)
1155 ;;; org-e-publish.el ends here