Introduce CLOCK_INTO_DRAWER property like the existing LOG_INTO_DRAWER
[org-mode/org-jambu.git] / lisp / org-publish.el
blob56cc80aa1af5f21abb2866d1a79c916ad895670b
1 ;;; org-publish.el --- publish related org-mode files as a website
2 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010
3 ;; Free Software Foundation, Inc.
5 ;; Author: David O'Toole <dto@gnu.org>
6 ;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
7 ;; Keywords: hypermedia, outlines, wp
8 ;; Version: 7.5
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This program allow configurable publishing of related sets of
28 ;; Org-mode files as a complete website.
30 ;; org-publish.el can do the following:
32 ;; + Publish all one's org-files to HTML or PDF
33 ;; + Upload HTML, images, attachments and other files to a web server
34 ;; + Exclude selected private pages from publishing
35 ;; + Publish a clickable sitemap of pages
36 ;; + Manage local timestamps for publishing only changed files
37 ;; + Accept plugin functions to extend range of publishable content
39 ;; Documentation for publishing is in the manual.
41 ;;; Code:
44 (defun org-publish-sanitize-plist (plist)
45 (mapcar (lambda (x)
46 (or (cdr (assq x '((:index-filename . :sitemap-filename)
47 (:index-title . :sitemap-title)
48 (:index-function . :sitemap-function)
49 (:index-style . :sitemap-style)
50 (:auto-index . :auto-sitemap))))
51 x))
52 plist))
54 (eval-when-compile
55 (require 'cl))
56 (require 'org)
57 (require 'org-exp)
58 (require 'format-spec)
60 (eval-and-compile
61 (unless (fboundp 'declare-function)
62 (defmacro declare-function (fn file &optional arglist fileonly))))
64 (defgroup org-publish nil
65 "Options for publishing a set of Org-mode and related files."
66 :tag "Org Publishing"
67 :group 'org)
69 (defcustom org-publish-project-alist nil
70 "Association list to control publishing behavior.
71 Each element of the alist is a publishing 'project.' The CAR of
72 each element is a string, uniquely identifying the project. The
73 CDR of each element is in one of the following forms:
75 1. A well-formed property list with an even number of elements, alternating
76 keys and values, specifying parameters for the publishing process.
78 (:property value :property value ... )
80 2. A meta-project definition, specifying of a list of sub-projects:
82 (:components (\"project-1\" \"project-2\" ...))
84 When the CDR of an element of org-publish-project-alist is in
85 this second form, the elements of the list after :components are
86 taken to be components of the project, which group together files
87 requiring different publishing options. When you publish such a
88 project with \\[org-publish], the components all publish.
90 When a property is given a value in org-publish-project-alist, its
91 setting overrides the value of the corresponding user variable
92 \(if any) during publishing. However, options set within a file
93 override everything.
95 Most properties are optional, but some should always be set:
97 :base-directory Directory containing publishing source files
98 :base-extension Extension (without the dot!) of source files.
99 This can be a regular expression. If not given,
100 \"org\" will be used as default extension.
101 :publishing-directory Directory (possibly remote) where output
102 files will be published
104 The :exclude property may be used to prevent certain files from
105 being published. Its value may be a string or regexp matching
106 file names you don't want to be published.
108 The :include property may be used to include extra files. Its
109 value may be a list of filenames to include. The filenames are
110 considered relative to the base directory.
112 When both :include and :exclude properties are given values, the
113 exclusion step happens first.
115 One special property controls which back-end function to use for
116 publishing files in the project. This can be used to extend the
117 set of file types publishable by org-publish, as well as the set
118 of output formats.
120 :publishing-function Function to publish file. The default is
121 `org-publish-org-to-html', but other
122 values are possible. May also be a
123 list of functions, in which case
124 each function in the list is invoked
125 in turn.
127 Another property allows you to insert code that prepares a
128 project for publishing. For example, you could call GNU Make on a
129 certain makefile, to ensure published files are built up to date.
131 :preparation-function Function to be called before publishing
132 this project. This may also be a list
133 of functions.
134 :completion-function Function to be called after publishing
135 this project. This may also be a list
136 of functions.
138 Some properties control details of the Org publishing process,
139 and are equivalent to the corresponding user variables listed in
140 the right column. See the documentation for those variables to
141 learn more about their use and default values.
143 :language `org-export-default-language'
144 :headline-levels `org-export-headline-levels'
145 :section-numbers `org-export-with-section-numbers'
146 :table-of-contents `org-export-with-toc'
147 :emphasize `org-export-with-emphasize'
148 :sub-superscript `org-export-with-sub-superscripts'
149 :TeX-macros `org-export-with-TeX-macros'
150 :fixed-width `org-export-with-fixed-width'
151 :tables `org-export-with-tables'
152 :table-auto-headline `org-export-highlight-first-table-line'
153 :style `org-export-html-style'
154 :convert-org-links `org-export-html-link-org-files-as-html'
155 :inline-images `org-export-html-inline-images'
156 :expand-quoted-html `org-export-html-expand'
157 :timestamp `org-export-html-with-timestamp'
158 :publishing-directory `org-export-publishing-directory'
159 :html-preamble `org-export-html-preamble'
160 :html-postamble `org-export-html-postamble'
161 :author `user-full-name'
162 :email `user-mail-address'
164 The following properties may be used to control publishing of a
165 sitemap of files or summary page for a given project.
167 :auto-sitemap Whether to publish a sitemap during
168 `org-publish-current-project' or `org-publish-all'.
169 :sitemap-filename Filename for output of sitemap. Defaults
170 to 'sitemap.org' (which becomes 'sitemap.html').
171 :sitemap-title Title of sitemap page. Defaults to name of file.
172 :sitemap-function Plugin function to use for generation of sitemap.
173 Defaults to `org-publish-org-sitemap', which
174 generates a plain list of links to all files
175 in the project.
176 :sitemap-style Can be `list' (sitemap is just an itemized list
177 of the titles of the files involved) or
178 `tree' (the directory structure of the source
179 files is reflected in the sitemap). Defaults to
180 `tree'.
181 :sitemap-sans-extension Remove extension from sitemap's
182 filenames. Useful to have cool
183 URIs (see
184 http://www.w3.org/Provider/Style/URI).
185 Defaults to nil.
187 If you create a sitemap file, adjust the sorting like this:
189 :sitemap-sort-folders Where folders should appear in the sitemap.
190 Set this to `first' (default) or `last' to
191 display folders first or last, respectively.
192 Any other value will mix files and folders.
193 :sitemap-sort-files The site map is normally sorted alphabetically.
194 You can change this behaviour setting this to
195 `chronologically', `anti-chronologically' or nil.
196 :sitemap-ignore-case Should sorting be case-sensitive? Default nil.
198 The following properties control the creation of a concept index.
200 :makeindex Create a concept index.
202 Other properties affecting publication.
204 :body-only Set this to 't' to publish only the body of the
205 documents, excluding everything outside and
206 including the <body> tags in HTML, or
207 \begin{document}..\end{document} in LaTeX."
208 :group 'org-publish
209 :type 'alist)
211 (defcustom org-publish-use-timestamps-flag t
212 "Non-nil means use timestamp checking to publish only changed files.
213 When nil, do no timestamp checking and always publish all files."
214 :group 'org-publish
215 :type 'boolean)
217 (defcustom org-publish-timestamp-directory (convert-standard-filename
218 "~/.org-timestamps/")
219 "Name of directory in which to store publishing timestamps."
220 :group 'org-publish
221 :type 'directory)
223 (defcustom org-publish-list-skipped-files t
224 "Non-nil means show message about files *not* published."
225 :group 'org-publish
226 :type 'boolean)
228 (defcustom org-publish-before-export-hook nil
229 "Hook run before export on the Org file.
230 The hook may modify the file in arbitrary ways before publishing happens.
231 The original version of the buffer will be restored after publishing."
232 :group 'org-publish
233 :type 'hook)
235 (defcustom org-publish-after-export-hook nil
236 "Hook run after export on the exported buffer.
237 Any changes made by this hook will be saved."
238 :group 'org-publish
239 :type 'hook)
241 (defcustom org-publish-sitemap-sort-files 'alphabetically
242 "How sitemaps files should be sorted by default?
243 Possible values are `alphabetically', `chronologically', `anti-chronologically' and nil.
244 If `alphabetically', files will be sorted alphabetically.
245 If `chronologically', files will be sorted with older modification time first.
246 If `anti-chronologically', files will be sorted with newer modification time first.
247 nil won't sort files.
249 You can overwrite this default per project in your
250 `org-publish-project-alist', using `:sitemap-sort-files'."
251 :group 'org-publish
252 :type 'symbol)
254 (defcustom org-publish-sitemap-sort-folders 'first
255 "A symbol, denoting if folders are sorted first in sitemaps.
256 Possible values are `first', `last', and nil.
257 If `first', folders will be sorted before files.
258 If `last', folders are sorted to the end after the files.
259 Any other value will not mix files and folders.
261 You can overwrite this default per project in your
262 `org-publish-project-alist', using `:sitemap-sort-folders'."
263 :group 'org-publish
264 :type 'symbol)
266 (defcustom org-publish-sitemap-sort-ignore-case nil
267 "Sort sitemaps case insensitively by default?
269 You can overwrite this default per project in your
270 `org-publish-project-alist', using `:sitemap-ignore-case'."
271 :group 'org-publish
272 :type 'boolean)
274 (defcustom org-publish-sitemap-date-format "%Y-%m-%d"
275 "Format for `format-time-string' which is used to print a date
276 in the sitemap."
277 :group 'org-publish
278 :type 'string)
280 (defcustom org-publish-sitemap-file-entry-format "%t"
281 "How a sitemap file entry is formated.
282 You could use brackets to delimit on what part the link will be.
284 %t is the title.
285 %a is the author.
286 %d is the date formated using `org-publish-sitemap-date-format'."
287 :group 'org-publish
288 :type 'string)
290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
291 ;;; Timestamp-related functions
293 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
294 "Return path to timestamp file for filename FILENAME."
295 (setq filename (concat filename "::" (or pub-dir "") "::"
296 (format "%s" (or pub-func ""))))
297 (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
299 (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir)
300 "Return t if FILENAME should be published in PUB-DIR using PUB-FUNC.
301 TRUE-PUB-DIR is where the file will truly end up. Currently we are not using
302 this - maybe it can eventually be used to check if the file is present at
303 the target location, and how old it is. Right now we cannot do this, because
304 we do not know under what file name the file will be stored - the publishing
305 function can still decide about that independently."
306 (let ((rtn
307 (if org-publish-use-timestamps-flag
308 (org-publish-cache-file-needs-publishing
309 filename pub-dir pub-func)
310 ;; don't use timestamps, always return t
311 t)))
312 (if rtn
313 (message "Publishing file %s using `%s'" filename pub-func)
314 (when org-publish-list-skipped-files
315 (message "Skipping unmodified file %s" filename)))
316 rtn))
318 (defun org-publish-update-timestamp (filename &optional pub-dir pub-func)
319 "Update publishing timestamp for file FILENAME.
320 If there is no timestamp, create one."
321 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func))
322 (stamp (org-publish-cache-ctime-of-src filename)))
323 (org-publish-cache-set key stamp)))
325 (defun org-publish-remove-all-timestamps ()
326 "Remove all files in the timestamp directory."
327 (let ((dir org-publish-timestamp-directory)
328 files)
329 (when (and (file-exists-p dir)
330 (file-directory-p dir))
331 (mapc 'delete-file (directory-files dir 'full "[^.]\\'"))
332 (org-publish-reset-cache))))
335 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
338 (defvar org-publish-initial-buffer nil
339 "The buffer `org-publish' has been called from.")
340 (defvar org-publish-temp-files nil
341 "Temporary list of files to be published.")
343 ;; Here, so you find the variable right before it's used the first time:
344 (defvar org-publish-cache nil
345 "This will cache timestamps and titles for files in publishing projects.
346 Blocks could hash sha1 values here.")
349 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
350 ;;; Compatibility aliases
352 ;; Delete-dups is not in Emacs <22
353 (if (fboundp 'delete-dups)
354 (defalias 'org-publish-delete-dups 'delete-dups)
355 (defun org-publish-delete-dups (list)
356 "Destructively remove `equal' duplicates from LIST.
357 Store the result in LIST and return it. LIST must be a proper list.
358 Of several `equal' occurrences of an element in LIST, the first
359 one is kept.
361 This is a compatibility function for Emacsen without `delete-dups'."
362 ;; Code from `subr.el' in Emacs 22:
363 (let ((tail list))
364 (while tail
365 (setcdr tail (delete (car tail) (cdr tail)))
366 (setq tail (cdr tail))))
367 list))
369 (declare-function org-publish-delete-dups "org-publish" (list))
370 (declare-function find-lisp-find-files "find-lisp" (directory regexp))
372 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
373 ;;; Getting project information out of org-publish-project-alist
375 (defun org-publish-expand-projects (projects-alist)
376 "Expand projects in PROJECTS-ALIST.
377 This splices all the components into the list."
378 (let ((rest projects-alist) rtn p components)
379 (while (setq p (pop rest))
380 (if (setq components (plist-get (cdr p) :components))
381 (setq rest (append
382 (mapcar (lambda (x) (assoc x org-publish-project-alist))
383 components)
384 rest))
385 (push p rtn)))
386 (nreverse (org-publish-delete-dups (delq nil rtn)))))
389 (defvar sitemap-sort-files)
390 (defvar sitemap-sort-folders)
391 (defvar sitemap-ignore-case)
392 (defvar sitemap-requested)
393 (defvar sitemap-date-format)
394 (defvar sitemap-file-entry-format)
395 (defun org-publish-compare-directory-files (a b)
396 "Predicate for `sort', that sorts folders and files for sitemap."
397 (let ((retval t))
398 (when (or sitemap-sort-files sitemap-sort-folders)
399 ;; First we sort files:
400 (when sitemap-sort-files
401 (cond ((equal sitemap-sort-files 'alphabetically)
402 (let* ((adir (file-directory-p a))
403 (aorg (and (string-match "\\.org$" a) (not adir)))
404 (bdir (file-directory-p b))
405 (borg (and (string-match "\\.org$" b) (not bdir)))
406 (A (if aorg
407 (concat (file-name-directory a)
408 (org-publish-find-title a)) a))
409 (B (if borg
410 (concat (file-name-directory b)
411 (org-publish-find-title b)) b)))
412 (setq retval (if sitemap-ignore-case
413 (not (string-lessp (upcase B) (upcase A)))
414 (not (string-lessp B A))))))
415 ((or (equal sitemap-sort-files 'chronologically)
416 (equal sitemap-sort-files 'anti-chronologically))
417 (let* ((adate (org-publish-find-date a))
418 (bdate (org-publish-find-date b))
419 (A (+ (lsh (car adate) 16) (cadr adate)))
420 (B (+ (lsh (car bdate) 16) (cadr bdate))))
421 (setq retval (if (equal sitemap-sort-files 'chronologically)
422 (<= A B)
423 (>= A B)))))))
424 ;; Directory-wise wins:
425 (when sitemap-sort-folders
426 ;; a is directory, b not:
427 (cond
428 ((and (file-directory-p a) (not (file-directory-p b)))
429 (setq retval (equal sitemap-sort-folders 'first)))
430 ;; a is not a directory, but b is:
431 ((and (not (file-directory-p a)) (file-directory-p b))
432 (setq retval (equal sitemap-sort-folders 'last))))))
433 retval))
435 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
436 "Set `org-publish-temp-files' with files from BASE-DIR directory.
437 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
438 non-nil, restrict this list to the files matching the regexp
439 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
440 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
441 matching the regexp SKIP-DIR when recursing through BASE-DIR."
442 (mapc (lambda (f)
443 (let ((fd-p (file-directory-p f))
444 (fnd (file-name-nondirectory f)))
445 (if (and fd-p recurse
446 (not (string-match "^\\.+$" fnd))
447 (if skip-dir (not (string-match skip-dir fnd)) t))
448 (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
449 (unless (or fd-p ;; this is a directory
450 (and skip-file (string-match skip-file fnd))
451 (not (file-exists-p (file-truename f)))
452 (not (string-match match fnd)))
454 (pushnew f org-publish-temp-files)))))
455 (if sitemap-requested
456 (sort (directory-files base-dir t (unless recurse match))
457 'org-publish-compare-directory-files)
458 (directory-files base-dir t (unless recurse match)))))
460 (defun org-publish-get-base-files (project &optional exclude-regexp)
461 "Return a list of all files in PROJECT.
462 If EXCLUDE-REGEXP is set, this will be used to filter out
463 matching filenames."
464 (let* ((project-plist (cdr project))
465 (base-dir (file-name-as-directory
466 (plist-get project-plist :base-directory)))
467 (include-list (plist-get project-plist :include))
468 (recurse (plist-get project-plist :recursive))
469 (extension (or (plist-get project-plist :base-extension) "org"))
470 ;; sitemap-... variables are dynamically scoped for
471 ;; org-publish-compare-directory-files:
472 (sitemap-requested
473 (plist-get project-plist :auto-sitemap))
474 (sitemap-filename
475 (or (plist-get project-plist :sitemap-filename)
476 "sitemap.org"))
477 (sitemap-sort-folders
478 (if (plist-member project-plist :sitemap-sort-folders)
479 (plist-get project-plist :sitemap-sort-folders)
480 org-publish-sitemap-sort-folders))
481 (sitemap-sort-files
482 (cond ((plist-member project-plist :sitemap-sort-files)
483 (plist-get project-plist :sitemap-sort-files))
484 ;; For backward compatibility:
485 ((plist-member project-plist :sitemap-alphabetically)
486 (if (plist-get project-plist :sitemap-alphabetically)
487 'alphabetically nil))
488 (t org-publish-sitemap-sort-files)))
489 (sitemap-ignore-case
490 (if (plist-member project-plist :sitemap-ignore-case)
491 (plist-get project-plist :sitemap-ignore-case)
492 org-publish-sitemap-sort-ignore-case))
493 (match (if (eq extension 'any)
494 "^[^\\.]"
495 (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
496 ;; Make sure sitemap-sort-folders' has an accepted value
497 (unless (memq sitemap-sort-folders '(first last))
498 (setq sitemap-sort-folders nil))
500 (setq org-publish-temp-files nil)
501 (if sitemap-requested
502 (pushnew (expand-file-name (concat base-dir sitemap-filename))
503 org-publish-temp-files))
504 (org-publish-get-base-files-1 base-dir recurse match
505 ;; FIXME distinguish exclude regexp
506 ;; for skip-file and skip-dir?
507 exclude-regexp exclude-regexp)
508 (mapc (lambda (f)
509 (pushnew
510 (expand-file-name (concat base-dir f))
511 org-publish-temp-files))
512 include-list)
513 org-publish-temp-files))
515 (defun org-publish-get-project-from-filename (filename &optional up)
516 "Return the project that FILENAME belongs to."
517 (let* ((filename (expand-file-name filename))
518 project-name)
520 (catch 'p-found
521 (dolist (prj org-publish-project-alist)
522 (unless (plist-get (cdr prj) :components)
523 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
524 (let* ((r (plist-get (cdr prj) :recursive))
525 (b (expand-file-name (file-name-as-directory
526 (plist-get (cdr prj) :base-directory))))
527 (x (or (plist-get (cdr prj) :base-extension) "org"))
528 (e (plist-get (cdr prj) :exclude))
529 (i (plist-get (cdr prj) :include))
530 (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
531 (when
533 (and
534 i (member filename
535 (mapcar
536 (lambda (file) (expand-file-name file b))
537 i)))
538 (and
539 (not (and e (string-match e filename)))
540 (string-match xm filename)))
541 (setq project-name (car prj))
542 (throw 'p-found project-name))))))
543 (when up
544 (dolist (prj org-publish-project-alist)
545 (if (member project-name (plist-get (cdr prj) :components))
546 (setq project-name (car prj)))))
547 (assoc project-name org-publish-project-alist)))
549 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
550 ;;; Pluggable publishing back-end functions
552 (defun org-publish-org-to (format plist filename pub-dir)
553 "Publish an org file to FORMAT.
554 PLIST is the property list for the given project.
555 FILENAME is the filename of the org file to be published.
556 PUB-DIR is the publishing directory."
557 (require 'org)
558 (unless (file-exists-p pub-dir)
559 (make-directory pub-dir t))
560 (let ((visiting (find-buffer-visiting filename)))
561 (save-excursion
562 (switch-to-buffer (or visiting (find-file filename)))
563 (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
564 (init-buf (current-buffer))
565 (init-point (point))
566 (init-buf-string (buffer-string))
567 export-buf-or-file)
568 ;; run hooks before exporting
569 (run-hooks 'org-publish-before-export-hook)
570 ;; export the possibly modified buffer
571 (setq export-buf-or-file
572 (funcall (intern (concat "org-export-as-" format))
573 (plist-get plist :headline-levels)
574 nil plist nil
575 (plist-get plist :body-only)
576 pub-dir))
577 (when (and (bufferp export-buf-or-file)
578 (buffer-live-p export-buf-or-file))
579 (set-buffer export-buf-or-file)
580 ;; run hooks after export and save export
581 (progn (run-hooks 'org-publish-after-export-hook)
582 (if (buffer-modified-p) (save-buffer)))
583 (kill-buffer export-buf-or-file))
584 ;; maybe restore buffer's content
585 (set-buffer init-buf)
586 (when (buffer-modified-p init-buf)
587 (erase-buffer)
588 (insert init-buf-string)
589 (save-buffer)
590 (goto-char init-point))
591 (unless visiting
592 (kill-buffer init-buf))))))
594 (defmacro org-publish-with-aux-preprocess-maybe (&rest body)
595 "Execute BODY with a modified hook to preprocess for index."
596 `(let ((org-export-preprocess-after-headline-targets-hook
597 (if (plist-get project-plist :makeindex)
598 (cons 'org-publish-aux-preprocess
599 org-export-preprocess-after-headline-targets-hook)
600 org-export-preprocess-after-headline-targets-hook)))
601 ,@body))
603 (defvar project-plist)
604 (defun org-publish-org-to-latex (plist filename pub-dir)
605 "Publish an org file to LaTeX.
606 See `org-publish-org-to' to the list of arguments."
607 (org-publish-with-aux-preprocess-maybe
608 (org-publish-org-to "latex" plist filename pub-dir)))
610 (defun org-publish-org-to-pdf (plist filename pub-dir)
611 "Publish an org file to PDF (via LaTeX).
612 See `org-publish-org-to' to the list of arguments."
613 (org-publish-with-aux-preprocess-maybe
614 (org-publish-org-to "pdf" plist filename pub-dir)))
616 (defun org-publish-org-to-html (plist filename pub-dir)
617 "Publish an org file to HTML.
618 See `org-publish-org-to' to the list of arguments."
619 (org-publish-with-aux-preprocess-maybe
620 (org-publish-org-to "html" plist filename pub-dir)))
622 (defun org-publish-org-to-org (plist filename pub-dir)
623 "Publish an org file to HTML.
624 See `org-publish-org-to' to the list of arguments."
625 (org-publish-org-to "org" plist filename pub-dir))
627 (defun org-publish-org-to-ascii (plist filename pub-dir)
628 "Publish an org file to ASCII.
629 See `org-publish-org-to' to the list of arguments."
630 (org-publish-with-aux-preprocess-maybe
631 (org-publish-org-to "ascii" plist filename pub-dir)))
633 (defun org-publish-org-to-latin1 (plist filename pub-dir)
634 "Publish an org file to Latin-1.
635 See `org-publish-org-to' to the list of arguments."
636 (org-publish-with-aux-preprocess-maybe
637 (org-publish-org-to "latin1" plist filename pub-dir)))
639 (defun org-publish-org-to-utf8 (plist filename pub-dir)
640 "Publish an org file to UTF-8.
641 See `org-publish-org-to' to the list of arguments."
642 (org-publish-with-aux-preprocess-maybe
643 (org-publish-org-to "utf8" plist filename pub-dir)))
645 (defun org-publish-attachment (plist filename pub-dir)
646 "Publish a file with no transformation of any kind.
647 See `org-publish-org-to' to the list of arguments."
648 ;; make sure eshell/cp code is loaded
649 (unless (file-directory-p pub-dir)
650 (make-directory pub-dir t))
651 (or (equal (expand-file-name (file-name-directory filename))
652 (file-name-as-directory (expand-file-name pub-dir)))
653 (copy-file filename
654 (expand-file-name (file-name-nondirectory filename) pub-dir)
655 t)))
657 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
658 ;;; Publishing files, sets of files, and indices
660 (defun org-publish-file (filename &optional project no-cache)
661 "Publish file FILENAME from PROJECT.
662 If NO-CACHE is not nil, do not initialize org-publish-cache and
663 write it to disk. This is needed, since this function is used to
664 publish single files, when entire projects are published.
665 See `org-publish-projects'."
666 (let* ((project
667 (or project
668 (or (org-publish-get-project-from-filename filename)
669 (error "File %s not part of any known project"
670 (abbreviate-file-name filename)))))
671 (project-plist (cdr project))
672 (ftname (expand-file-name filename))
673 (publishing-function
674 (or (plist-get project-plist :publishing-function)
675 'org-publish-org-to-html))
676 (base-dir
677 (file-name-as-directory
678 (expand-file-name
679 (or (plist-get project-plist :base-directory)
680 (error "Project %s does not have :base-directory defined"
681 (car project))))))
682 (pub-dir
683 (file-name-as-directory
684 (file-truename
685 (or (plist-get project-plist :publishing-directory)
686 (error "Project %s does not have :publishing-directory defined"
687 (car project))))))
688 tmp-pub-dir)
690 (unless no-cache
691 (org-publish-initialize-cache (car project)))
693 (setq tmp-pub-dir
694 (file-name-directory
695 (concat pub-dir
696 (and (string-match (regexp-quote base-dir) ftname)
697 (substring ftname (match-end 0))))))
698 (if (listp publishing-function)
699 ;; allow chain of publishing functions
700 (mapc (lambda (f)
701 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir)
702 (funcall f project-plist filename tmp-pub-dir)
703 (org-publish-update-timestamp filename pub-dir f)))
704 publishing-function)
705 (when (org-publish-needed-p filename pub-dir publishing-function
706 tmp-pub-dir)
707 (funcall publishing-function project-plist filename tmp-pub-dir)
708 (org-publish-update-timestamp
709 filename pub-dir publishing-function)))
710 (unless no-cache (org-publish-write-cache-file))))
712 (defun org-publish-projects (projects)
713 "Publish all files belonging to the PROJECTS alist.
714 If :auto-sitemap is set, publish the sitemap too.
715 If :makeindex is set, also produce a file theindex.org."
716 (mapc
717 (lambda (project)
718 ;; Each project uses it's own cache file:
719 (org-publish-initialize-cache (car project))
720 (let*
721 ((project-plist (cdr project))
722 (exclude-regexp (plist-get project-plist :exclude))
723 (sitemap-p (plist-get project-plist :auto-sitemap))
724 (sitemap-filename (or (plist-get project-plist :sitemap-filename)
725 "sitemap.org"))
726 (sitemap-function (or (plist-get project-plist :sitemap-function)
727 'org-publish-org-sitemap))
728 (sitemap-date-format (or (plist-get project-plist :sitemap-date-format)
729 org-publish-sitemap-date-format))
730 (sitemap-file-entry-format (or (plist-get project-plist :sitemap-file-entry-format)
731 org-publish-sitemap-file-entry-format))
732 (preparation-function (plist-get project-plist :preparation-function))
733 (completion-function (plist-get project-plist :completion-function))
734 (files (org-publish-get-base-files project exclude-regexp)) file)
735 (when preparation-function (run-hooks 'preparation-function))
736 (if sitemap-p (funcall sitemap-function project sitemap-filename))
737 (while (setq file (pop files))
738 (org-publish-file file project t))
739 (when (plist-get project-plist :makeindex)
740 (org-publish-index-generate-theindex.inc
741 (plist-get project-plist :base-directory))
742 (org-publish-file (expand-file-name
743 "theindex.org"
744 (plist-get project-plist :base-directory))
745 project t))
746 (when completion-function (run-hooks 'completion-function))
747 (org-publish-write-cache-file)))
748 (org-publish-expand-projects projects)))
750 (defun org-publish-org-sitemap (project &optional sitemap-filename)
751 "Create a sitemap of pages in set defined by PROJECT.
752 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
753 Default for SITEMAP-FILENAME is 'sitemap.org'."
754 (let* ((project-plist (cdr project))
755 (dir (file-name-as-directory
756 (plist-get project-plist :base-directory)))
757 (localdir (file-name-directory dir))
758 (indent-str (make-string 2 ?\ ))
759 (exclude-regexp (plist-get project-plist :exclude))
760 (files (nreverse (org-publish-get-base-files project exclude-regexp)))
761 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
762 (sitemap-title (or (plist-get project-plist :sitemap-title)
763 (concat "Sitemap for project " (car project))))
764 (sitemap-style (or (plist-get project-plist :sitemap-style)
765 'tree))
766 (sitemap-sans-extension (plist-get project-plist :sitemap-sans-extension))
767 (visiting (find-buffer-visiting sitemap-filename))
768 (ifn (file-name-nondirectory sitemap-filename))
769 file sitemap-buffer)
770 (with-current-buffer (setq sitemap-buffer
771 (or visiting (find-file sitemap-filename)))
772 (erase-buffer)
773 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
774 (while (setq file (pop files))
775 (let ((fn (file-name-nondirectory file))
776 (link (file-relative-name file dir))
777 (oldlocal localdir))
778 (when sitemap-sans-extension
779 (setq link (file-name-sans-extension link)))
780 ;; sitemap shouldn't list itself
781 (unless (equal (file-truename sitemap-filename)
782 (file-truename file))
783 (if (eq sitemap-style 'list)
784 (message "Generating list-style sitemap for %s" sitemap-title)
785 (message "Generating tree-style sitemap for %s" sitemap-title)
786 (setq localdir (concat (file-name-as-directory dir)
787 (file-name-directory link)))
788 (unless (string= localdir oldlocal)
789 (if (string= localdir dir)
790 (setq indent-str (make-string 2 ?\ ))
791 (let ((subdirs
792 (split-string
793 (directory-file-name
794 (file-name-directory
795 (file-relative-name localdir dir))) "/"))
796 (subdir "")
797 (old-subdirs (split-string
798 (file-relative-name oldlocal dir) "/")))
799 (setq indent-str (make-string 2 ?\ ))
800 (while (string= (car old-subdirs) (car subdirs))
801 (setq indent-str (concat indent-str (make-string 2 ?\ )))
802 (pop old-subdirs)
803 (pop subdirs))
804 (dolist (d subdirs)
805 (setq subdir (concat subdir d "/"))
806 (insert (concat indent-str " + " d "\n"))
807 (setq indent-str (make-string
808 (+ (length indent-str) 2) ?\ )))))))
809 ;; This is common to 'flat and 'tree
810 (let ((entry
811 (org-publish-format-file-entry sitemap-file-entry-format
812 file project-plist))
813 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
814 (cond ((string-match-p regexp entry)
815 (string-match regexp entry)
816 (insert (concat indent-str " + " (match-string 1 entry)
817 "[[file:" link "]["
818 (match-string 2 entry)
819 "]]" (match-string 3 entry) "\n")))
821 (insert (concat indent-str " + [[file:" link "]["
822 entry
823 "]]\n"))))))))
824 (save-buffer))
825 (or visiting (kill-buffer sitemap-buffer))))
827 (defun org-publish-format-file-entry (fmt file project-plist)
828 (format-spec fmt
829 `((?t . ,(org-publish-find-title file t))
830 (?d . ,(format-time-string sitemap-date-format
831 (org-publish-find-date file)))
832 (?a . ,(or (plist-get project-plist :author) user-full-name)))))
834 (defun org-publish-find-title (file &optional reset)
835 "Find the title of FILE in project."
837 (and (not reset) (org-publish-cache-get-file-property file :title nil t))
838 (let* ((visiting (find-buffer-visiting file))
839 (buffer (or visiting (find-file-noselect file)))
840 title)
841 (with-current-buffer buffer
842 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
843 (org-infile-export-plist))))
844 (setq title
845 (or (plist-get opt-plist :title)
846 (and (not
847 (plist-get opt-plist :skip-before-1st-heading))
848 (org-export-grab-title-from-buffer))
849 (file-name-nondirectory (file-name-sans-extension file))))))
850 (unless visiting
851 (kill-buffer buffer))
852 (org-publish-cache-set-file-property file :title title)
853 title)))
855 (defun org-publish-find-date (file)
856 "Find the date of FILE in project.
857 If FILE provides a #+date keyword use it else use the file
858 system's modification time.
860 It returns time in `current-time' format."
861 (let ((visiting (find-buffer-visiting file)))
862 (save-excursion
863 (switch-to-buffer (or visiting (find-file-noselect file nil t)))
864 (let* ((plist (org-infile-export-plist))
865 (date (plist-get plist :date)))
866 (unless visiting
867 (kill-buffer (current-buffer)))
868 (if date
869 (org-time-string-to-time date)
870 (when (file-exists-p file)
871 (nth 5 (file-attributes file))))))))
873 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
874 ;;; Interactive publishing functions
876 ;;;###autoload
877 (defalias 'org-publish-project 'org-publish)
879 ;;;###autoload
880 (defun org-publish (project &optional force)
881 "Publish PROJECT."
882 (interactive
883 (list
884 (assoc (org-icompleting-read
885 "Publish project: "
886 org-publish-project-alist nil t)
887 org-publish-project-alist)
888 current-prefix-arg))
889 (setq org-publish-initial-buffer (current-buffer))
890 (save-window-excursion
891 (let* ((org-publish-use-timestamps-flag
892 (if force nil org-publish-use-timestamps-flag)))
893 (org-publish-projects
894 (if (stringp project)
895 ;; If this function is called in batch mode,
896 ;; project is still a string here.
897 (list (assoc project org-publish-project-alist))
898 (list project))))))
900 ;;;###autoload
901 (defun org-publish-all (&optional force)
902 "Publish all projects.
903 With prefix argument, remove all files in the timestamp
904 directory and force publishing all files."
905 (interactive "P")
906 (when force
907 (org-publish-remove-all-timestamps))
908 (save-window-excursion
909 (let ((org-publish-use-timestamps-flag
910 (if force nil org-publish-use-timestamps-flag)))
911 (org-publish-projects org-publish-project-alist))))
914 ;;;###autoload
915 (defun org-publish-current-file (&optional force)
916 "Publish the current file.
917 With prefix argument, force publish the file."
918 (interactive "P")
919 (save-window-excursion
920 (let ((org-publish-use-timestamps-flag
921 (if force nil org-publish-use-timestamps-flag)))
922 (org-publish-file (buffer-file-name)))))
924 ;;;###autoload
925 (defun org-publish-current-project (&optional force)
926 "Publish the project associated with the current file.
927 With a prefix argument, force publishing of all files in
928 the project."
929 (interactive "P")
930 (save-window-excursion
931 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up))
932 (org-publish-use-timestamps-flag
933 (if force nil org-publish-use-timestamps-flag)))
934 (if (not project)
935 (error "File %s is not part of any known project" (buffer-file-name)))
936 ;; FIXME: force is not used here?
937 (org-publish project))))
940 ;;; Index generation
942 (defun org-publish-aux-preprocess ()
943 "Find index entries and write them to an .orgx file."
944 (let ((case-fold-search t)
945 entry index target)
946 (goto-char (point-min))
947 (while
948 (and
949 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t)
950 (> (match-end 1) (match-beginning 1)))
951 (setq entry (match-string 1))
952 (when (eq org-export-current-backend 'latex)
953 (replace-match (format "\\index{%s}" entry) t t))
954 (save-excursion
955 (ignore-errors (org-back-to-heading t))
956 (setq target (get-text-property (point) 'target))
957 (setq target (or (cdr (assoc target org-export-preferred-target-alist))
958 (cdr (assoc target org-export-id-target-alist))
959 target ""))
960 (push (cons entry target) index)))
961 (with-temp-file
962 (concat (file-name-sans-extension org-current-export-file) ".orgx")
963 (dolist (entry (nreverse index))
964 (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
966 (defun org-publish-index-generate-theindex.inc (directory)
967 "Generate the index from all .orgx files in the current directory and below."
968 (require 'find-lisp)
969 (let* ((fulldir (file-name-as-directory
970 (expand-file-name directory)))
971 (full-files (find-lisp-find-files directory "\\.orgx\\'"))
972 (re (concat "\\`" fulldir))
973 (files (mapcar (lambda (f) (if (string-match re f)
974 (substring f (match-end 0))
976 full-files))
977 (default-directory directory)
978 index origfile buf target entry ibuffer
979 main last-main letter last-letter file sub link tgext)
980 ;; `files' contains the list of relative file names
981 (dolist (file files)
982 (setq origfile (substring file 0 -1))
983 (setq buf (find-file-noselect file))
984 (with-current-buffer buf
985 (goto-char (point-min))
986 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
987 (setq target (match-string 1)
988 entry (match-string 2))
989 (push (list entry origfile target) index)))
990 (kill-buffer buf))
991 (setq index (sort index (lambda (a b) (string< (downcase (car a))
992 (downcase (car b))))))
993 (setq ibuffer (find-file-noselect (expand-file-name "theindex.inc" directory)))
994 (with-current-buffer ibuffer
995 (erase-buffer)
996 (insert "* Index\n")
997 (setq last-letter nil)
998 (dolist (idx index)
999 (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
1000 (if (and (stringp target) (string-match "\\S-" target))
1001 (setq tgext (concat "::#" target))
1002 (setq tgext ""))
1003 (setq letter (upcase (substring entry 0 1)))
1004 (when (not (equal letter last-letter))
1005 (insert "** " letter "\n")
1006 (setq last-letter letter))
1007 (if (string-match "!" entry)
1008 (setq main (substring entry 0 (match-beginning 0))
1009 sub (substring entry (match-end 0)))
1010 (setq main nil sub nil last-main nil))
1011 (when (and main (not (equal main last-main)))
1012 (insert " - " main "\n")
1013 (setq last-main main))
1014 (setq link (concat "[[file:" file tgext "]"
1015 "[" (or sub entry) "]]"))
1016 (if (and main sub)
1017 (insert " - " link "\n")
1018 (insert " - " link "\n")))
1019 (save-buffer))
1020 (kill-buffer ibuffer)
1022 (let ((index-file (expand-file-name "theindex.org" directory)))
1023 (unless (file-exists-p index-file)
1024 (setq ibuffer (find-file-noselect index-file))
1025 (with-current-buffer ibuffer
1026 (erase-buffer)
1027 (insert "\n\n#+include: \"theindex.inc\"\n\n")
1028 (save-buffer))
1029 (kill-buffer ibuffer)))))
1032 ;; Caching functions:
1034 (defun org-publish-write-cache-file (&optional free-cache)
1035 "Write `org-publish-cache' to file.
1036 If FREE-CACHE, empty the cache."
1037 (unless org-publish-cache
1038 (error "%s" "`org-publish-write-cache-file' called, but no cache present"))
1040 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1041 (unless cache-file
1042 (error
1043 "%s" "Cannot find cache-file name in `org-publish-write-cache-file'"))
1044 (with-temp-file cache-file
1045 (let ((print-level nil)
1046 (print-length nil))
1047 (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
1048 (maphash (lambda (k v)
1049 (insert
1050 (format (concat "(puthash %S "
1051 (if (or (listp v) (symbolp v))
1052 "'" "")
1053 "%S org-publish-cache)\n") k v)))
1054 org-publish-cache)))
1055 (when free-cache (org-publish-reset-cache))))
1057 (defun org-publish-initialize-cache (project-name)
1058 "Initialize the projects cache if not initialized yet and return it."
1060 (unless project-name
1061 (error "%s%s" "Cannot initialize `org-publish-cache' without projects name"
1062 " in `org-publish-initialize-cache'"))
1064 (unless (file-exists-p org-publish-timestamp-directory)
1065 (make-directory org-publish-timestamp-directory t))
1066 (if (not (file-directory-p org-publish-timestamp-directory))
1067 (error "Org publish timestamp: %s is not a directory"
1068 org-publish-timestamp-directory))
1070 (unless (and org-publish-cache
1071 (string= (org-publish-cache-get ":project:") project-name))
1072 (let* ((cache-file (concat
1073 (expand-file-name org-publish-timestamp-directory)
1074 project-name
1075 ".cache"))
1076 (cexists (file-exists-p cache-file)))
1078 (when org-publish-cache
1079 (org-publish-reset-cache))
1081 (if cexists
1082 (load-file cache-file)
1083 (setq org-publish-cache
1084 (make-hash-table :test 'equal :weakness nil :size 100))
1085 (org-publish-cache-set ":project:" project-name)
1086 (org-publish-cache-set ":cache-file:" cache-file))
1087 (unless cexists (org-publish-write-cache-file nil))))
1088 org-publish-cache)
1090 (defun org-publish-reset-cache ()
1091 "Empty org-publish-cache and reset it nil."
1092 (message "%s" "Resetting org-publish-cache")
1093 (if (hash-table-p org-publish-cache)
1094 (clrhash org-publish-cache))
1095 (setq org-publish-cache nil))
1097 (defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func)
1098 "Check the timestamp of the last publishing of FILENAME.
1099 Return `t', if the file needs publishing"
1100 (unless org-publish-cache
1101 (error "%s" "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1102 (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func))
1103 (pstamp (org-publish-cache-get key)))
1104 (if (null pstamp)
1106 (let ((ctime (org-publish-cache-ctime-of-src filename)))
1107 (< pstamp ctime)))))
1109 (defun org-publish-cache-set-file-property (filename property value &optional project-name)
1110 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1111 Use cache file of PROJECT-NAME. If the entry does not exist, it will be
1112 created. Return VALUE."
1113 ;; Evtl. load the requested cache file:
1114 (if project-name (org-publish-initialize-cache project-name))
1115 (let ((pl (org-publish-cache-get filename)))
1116 (if pl
1117 (progn
1118 (plist-put pl property value)
1119 value)
1120 (org-publish-cache-get-file-property
1121 filename property value nil project-name))))
1123 (defun org-publish-cache-get-file-property
1124 (filename property &optional default no-create project-name)
1125 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1126 Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
1127 DEFAULT, if the value does not yet exist.
1128 If the entry will be created, unless NO-CREATE is not nil."
1129 ;; Evtl. load the requested cache file:
1130 (if project-name (org-publish-initialize-cache project-name))
1131 (let ((pl (org-publish-cache-get filename))
1132 (retval nil))
1133 (if pl
1134 (if (plist-member pl property)
1135 (setq retval (plist-get pl property))
1136 (setq retval default))
1137 ;; no pl yet:
1138 (unless no-create
1139 (org-publish-cache-set filename (list property default)))
1140 (setq retval default))
1141 retval))
1143 (defun org-publish-cache-get (key)
1144 "Return the value stored in `org-publish-cache' for key KEY.
1145 Returns nil, if no value or nil is found, or the cache does not
1146 exist."
1147 (unless org-publish-cache
1148 (error "%s" "`org-publish-cache-get' called, but no cache present"))
1149 (gethash key org-publish-cache))
1151 (defun org-publish-cache-set (key value)
1152 "Store KEY VALUE pair in `org-publish-cache'.
1153 Returns value on success, else nil."
1154 (unless org-publish-cache
1155 (error "%s" "`org-publish-cache-set' called, but no cache present"))
1156 (puthash key value org-publish-cache))
1158 (defun org-publish-cache-ctime-of-src (filename)
1159 "Get the FILENAME ctime as an integer."
1160 (let ((src-attr (file-attributes (if (stringp (file-symlink-p filename))
1161 (file-symlink-p filename)
1162 filename))))
1164 (lsh (car (nth 5 src-attr)) 16)
1165 (cadr (nth 5 src-attr)))))
1167 (provide 'org-publish)
1169 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
1171 ;;; org-publish.el ends here