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