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