Spelling fixes.
[bpt/emacs.git] / lisp / org / org-publish.el
... / ...
CommitLineData
1;;; org-publish.el --- publish related org-mode files as a website
2;; Copyright (C) 2006-2011 Free Software Foundation, Inc.
3
4;; Author: David O'Toole <dto@gnu.org>
5;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
6;; Keywords: hypermedia, outlines, wp
7;; Version: 7.7
8
9;; This file is part of GNU Emacs.
10;;
11;; GNU Emacs is free software: you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24;;; Commentary:
25
26;; This program allow configurable publishing of related sets of
27;; Org-mode files as a complete website.
28;;
29;; org-publish.el can do the following:
30;;
31;; + Publish all one's org-files to HTML or PDF
32;; + Upload HTML, images, attachments and other files to a web server
33;; + Exclude selected private pages from publishing
34;; + Publish a clickable sitemap of pages
35;; + Manage local timestamps for publishing only changed files
36;; + Accept plugin functions to extend range of publishable content
37;;
38;; Documentation for publishing is in the manual.
39
40;;; Code:
41
42
43(eval-when-compile
44 (require 'cl))
45(require 'org)
46(require 'org-exp)
47(require 'format-spec)
48
49(eval-and-compile
50 (unless (fboundp 'declare-function)
51 (defmacro declare-function (fn file &optional arglist fileonly))))
52
53(defvar org-publish-initial-buffer nil
54 "The buffer `org-publish' has been called from.")
55
56(defvar org-publish-temp-files nil
57 "Temporary list of files to be published.")
58
59;; Here, so you find the variable right before it's used the first time:
60(defvar org-publish-cache nil
61 "This will cache timestamps and titles for files in publishing projects.
62Blocks could hash sha1 values here.")
63
64(defgroup org-publish nil
65 "Options for publishing a set of Org-mode and related files."
66 :tag "Org Publishing"
67 :group 'org)
68
69(defcustom org-publish-project-alist nil
70 "Association list to control publishing behavior.
71Each element of the alist is a publishing 'project.' The CAR of
72each element is a string, uniquely identifying the project. The
73CDR of each element is in one of the following forms:
74
751. A well-formed property list with an even number of elements, alternating
76 keys and values, specifying parameters for the publishing process.
77
78 (:property value :property value ... )
79
802. A meta-project definition, specifying of a list of sub-projects:
81
82 (:components (\"project-1\" \"project-2\" ...))
83
84When the CDR of an element of org-publish-project-alist is in
85this second form, the elements of the list after :components are
86taken to be components of the project, which group together files
87requiring different publishing options. When you publish such a
88project with \\[org-publish], the components all publish.
89
90When a property is given a value in org-publish-project-alist, its
91setting overrides the value of the corresponding user variable
92\(if any) during publishing. However, options set within a file
93override everything.
94
95Most properties are optional, but some should always be set:
96
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
103
104The :exclude property may be used to prevent certain files from
105being published. Its value may be a string or regexp matching
106file names you don't want to be published.
107
108The :include property may be used to include extra files. Its
109value may be a list of filenames to include. The filenames are
110considered relative to the base directory.
111
112When both :include and :exclude properties are given values, the
113exclusion step happens first.
114
115One special property controls which back-end function to use for
116publishing files in the project. This can be used to extend the
117set of file types publishable by org-publish, as well as the set
118of output formats.
119
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.
126
127Another property allows you to insert code that prepares a
128project for publishing. For example, you could call GNU Make on a
129certain makefile, to ensure published files are built up to date.
130
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.
137
138Some properties control details of the Org publishing process,
139and are equivalent to the corresponding user variables listed in
140the right column. See the documentation for those variables to
141learn more about their use and default values.
142
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'
163
164The following properties may be used to control publishing of a
165sitemap of files or summary page for a given project.
166
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.
186
187 If you create a sitemap file, adjust the sorting like this:
188
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 behavior setting this to
195 `chronologically', `anti-chronologically' or nil.
196 :sitemap-ignore-case Should sorting be case-sensitive? Default nil.
197
198The following properties control the creation of a concept index.
199
200 :makeindex Create a concept index.
201
202Other properties affecting publication.
203
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)
210
211(defcustom org-publish-use-timestamps-flag t
212 "Non-nil means use timestamp checking to publish only changed files.
213When nil, do no timestamp checking and always publish all files."
214 :group 'org-publish
215 :type 'boolean)
216
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)
222
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)
227
228(defcustom org-publish-before-export-hook nil
229 "Hook run before export on the Org file.
230The hook may modify the file in arbitrary ways before publishing happens.
231The original version of the buffer will be restored after publishing."
232 :group 'org-publish
233 :type 'hook)
234
235(defcustom org-publish-after-export-hook nil
236 "Hook run after export on the exported buffer.
237Any changes made by this hook will be saved."
238 :group 'org-publish
239 :type 'hook)
240
241(defcustom org-publish-sitemap-sort-files 'alphabetically
242 "How sitemaps files should be sorted by default?
243Possible values are `alphabetically', `chronologically', `anti-chronologically' and nil.
244If `alphabetically', files will be sorted alphabetically.
245If `chronologically', files will be sorted with older modification time first.
246If `anti-chronologically', files will be sorted with newer modification time first.
247nil won't sort files.
248
249You can overwrite this default per project in your
250`org-publish-project-alist', using `:sitemap-sort-files'."
251 :group 'org-publish
252 :type 'symbol)
253
254(defcustom org-publish-sitemap-sort-folders 'first
255 "A symbol, denoting if folders are sorted first in sitemaps.
256Possible values are `first', `last', and nil.
257If `first', folders will be sorted before files.
258If `last', folders are sorted to the end after the files.
259Any other value will not mix files and folders.
260
261You can overwrite this default per project in your
262`org-publish-project-alist', using `:sitemap-sort-folders'."
263 :group 'org-publish
264 :type 'symbol)
265
266(defcustom org-publish-sitemap-sort-ignore-case nil
267 "Sort sitemaps case insensitively by default?
268
269You can overwrite this default per project in your
270`org-publish-project-alist', using `:sitemap-ignore-case'."
271 :group 'org-publish
272 :type 'boolean)
273
274(defcustom org-publish-sitemap-date-format "%Y-%m-%d"
275 "Format for `format-time-string' which is used to print a date
276in the sitemap."
277 :group 'org-publish
278 :type 'string)
279
280(defcustom org-publish-sitemap-file-entry-format "%t"
281 "How a sitemap file entry is formated.
282You could use brackets to delimit on what part the link will be.
283
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)
289
290
291;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
292;;; Sanitize-plist (FIXME why?)
293
294(defun org-publish-sanitize-plist (plist)
295 ;; FIXME document
296 (mapcar (lambda (x)
297 (or (cdr (assq x '((:index-filename . :sitemap-filename)
298 (:index-title . :sitemap-title)
299 (:index-function . :sitemap-function)
300 (:index-style . :sitemap-style)
301 (:auto-index . :auto-sitemap))))
302 x))
303 plist))
304
305;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
306;;; Timestamp-related functions
307
308(defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
309 "Return path to timestamp file for filename FILENAME."
310 (setq filename (concat filename "::" (or pub-dir "") "::"
311 (format "%s" (or pub-func ""))))
312 (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
313
314(defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir)
315 "Return t if FILENAME should be published in PUB-DIR using PUB-FUNC.
316TRUE-PUB-DIR is where the file will truly end up. Currently we are not using
317this - maybe it can eventually be used to check if the file is present at
318the target location, and how old it is. Right now we cannot do this, because
319we do not know under what file name the file will be stored - the publishing
320function can still decide about that independently."
321 (let ((rtn
322 (if org-publish-use-timestamps-flag
323 (org-publish-cache-file-needs-publishing
324 filename pub-dir pub-func)
325 ;; don't use timestamps, always return t
326 t)))
327 (if rtn
328 (message "Publishing file %s using `%s'" filename pub-func)
329 (when org-publish-list-skipped-files
330 (message "Skipping unmodified file %s" filename)))
331 rtn))
332
333(defun org-publish-update-timestamp (filename &optional pub-dir pub-func)
334 "Update publishing timestamp for file FILENAME.
335If there is no timestamp, create one."
336 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func))
337 (stamp (org-publish-cache-ctime-of-src filename)))
338 (org-publish-cache-set key stamp)))
339
340(defun org-publish-remove-all-timestamps ()
341 "Remove all files in the timestamp directory."
342 (let ((dir org-publish-timestamp-directory)
343 files)
344 (when (and (file-exists-p dir)
345 (file-directory-p dir))
346 (mapc 'delete-file (directory-files dir 'full "[^.]\\'"))
347 (org-publish-reset-cache))))
348
349
350;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
351;;; Compatibility aliases
352
353;; Delete-dups is not in Emacs <22
354(if (fboundp 'delete-dups)
355 (defalias 'org-publish-delete-dups 'delete-dups)
356 (defun org-publish-delete-dups (list)
357 "Destructively remove `equal' duplicates from LIST.
358Store the result in LIST and return it. LIST must be a proper list.
359Of several `equal' occurrences of an element in LIST, the first
360one is kept.
361
362This is a compatibility function for Emacsen without `delete-dups'."
363 ;; Code from `subr.el' in Emacs 22:
364 (let ((tail list))
365 (while tail
366 (setcdr tail (delete (car tail) (cdr tail)))
367 (setq tail (cdr tail))))
368 list))
369
370(declare-function org-publish-delete-dups "org-publish" (list))
371(declare-function find-lisp-find-files "find-lisp" (directory regexp))
372
373;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
374;;; Getting project information out of org-publish-project-alist
375
376(defun org-publish-expand-projects (projects-alist)
377 "Expand projects in PROJECTS-ALIST.
378This splices all the components into the list."
379 (let ((rest projects-alist) rtn p components)
380 (while (setq p (pop rest))
381 (if (setq components (plist-get (cdr p) :components))
382 (setq rest (append
383 (mapcar (lambda (x) (assoc x org-publish-project-alist))
384 components)
385 rest))
386 (push p rtn)))
387 (nreverse (org-publish-delete-dups (delq nil rtn)))))
388
389(defvar org-sitemap-sort-files)
390(defvar org-sitemap-sort-folders)
391(defvar org-sitemap-ignore-case)
392(defvar org-sitemap-requested)
393(defvar org-sitemap-date-format)
394(defvar org-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 org-sitemap-sort-files org-sitemap-sort-folders)
399 ;; First we sort files:
400 (when org-sitemap-sort-files
401 (cond ((equal org-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 org-sitemap-ignore-case
413 (not (string-lessp (upcase B) (upcase A)))
414 (not (string-lessp B A))))))
415 ((or (equal org-sitemap-sort-files 'chronologically)
416 (equal org-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 org-sitemap-sort-files 'chronologically)
422 (<= A B)
423 (>= A B)))))))
424 ;; Directory-wise wins:
425 (when org-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 org-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 org-sitemap-sort-folders 'last))))))
433 retval))
434
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.
437If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
438non-nil, restrict this list to the files matching the regexp
439MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
440SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
441matching 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)))
453
454 (pushnew f org-publish-temp-files)))))
455 (if org-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)))))
459
460(defun org-publish-get-base-files (project &optional exclude-regexp)
461 "Return a list of all files in PROJECT.
462If EXCLUDE-REGEXP is set, this will be used to filter out
463matching 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 (org-sitemap-requested
473 (plist-get project-plist :auto-sitemap))
474 (sitemap-filename
475 (or (plist-get project-plist :sitemap-filename)
476 "sitemap.org"))
477 (org-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 (org-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 (org-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 `org-sitemap-sort-folders' has an accepted value
497 (unless (memq org-sitemap-sort-folders '(first last))
498 (setq org-sitemap-sort-folders nil))
499
500 (setq org-publish-temp-files nil)
501 (if org-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))
514
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)
519
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
532 (or
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)))
548
549;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
550;;; Pluggable publishing back-end functions
551
552(defun org-publish-org-to (format plist filename pub-dir)
553 "Publish an org file to FORMAT.
554PLIST is the property list for the given project.
555FILENAME is the filename of the org file to be published.
556PUB-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))))))
593
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))
602
603(defvar project-plist)
604(defun org-publish-org-to-latex (plist filename pub-dir)
605 "Publish an org file to LaTeX.
606See `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)))
609
610(defun org-publish-org-to-pdf (plist filename pub-dir)
611 "Publish an org file to PDF (via LaTeX).
612See `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)))
615
616(defun org-publish-org-to-html (plist filename pub-dir)
617 "Publish an org file to HTML.
618See `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)))
621
622(defun org-publish-org-to-org (plist filename pub-dir)
623 "Publish an org file to HTML.
624See `org-publish-org-to' to the list of arguments."
625 (org-publish-org-to "org" plist filename pub-dir))
626
627(defun org-publish-org-to-ascii (plist filename pub-dir)
628 "Publish an org file to ASCII.
629See `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)))
632
633(defun org-publish-org-to-latin1 (plist filename pub-dir)
634 "Publish an org file to Latin-1.
635See `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)))
638
639(defun org-publish-org-to-utf8 (plist filename pub-dir)
640 "Publish an org file to UTF-8.
641See `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)))
644
645(defun org-publish-attachment (plist filename pub-dir)
646 "Publish a file with no transformation of any kind.
647See `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)))
656
657;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
658;;; Publishing files, sets of files, and indices
659
660(defun org-publish-file (filename &optional project no-cache)
661 "Publish file FILENAME from PROJECT.
662If NO-CACHE is not nil, do not initialize org-publish-cache and
663write it to disk. This is needed, since this function is used to
664publish single files, when entire projects are published.
665See `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)
689
690 (unless no-cache
691 (org-publish-initialize-cache (car project)))
692
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))))
711
712(defun org-publish-projects (projects)
713 "Publish all files belonging to the PROJECTS alist.
714If :auto-sitemap is set, publish the sitemap too.
715If :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 (org-sitemap-date-format (or (plist-get project-plist :sitemap-date-format)
729 org-publish-sitemap-date-format))
730 (org-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
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)))
749
750(defun org-publish-org-sitemap (project &optional sitemap-filename)
751 "Create a sitemap of pages in set defined by PROJECT.
752Optionally set the filename of the sitemap with SITEMAP-FILENAME.
753Default 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 org-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")))
820 (t
821 (insert (concat indent-str " + [[file:" link "]["
822 entry
823 "]]\n"))))))))
824 (save-buffer))
825 (or visiting (kill-buffer sitemap-buffer))))
826
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 org-sitemap-date-format
831 (org-publish-find-date file)))
832 (?a . ,(or (plist-get project-plist :author) user-full-name)))))
833
834(defun org-publish-find-title (file &optional reset)
835 "Find the title of FILE in project."
836 (or
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)))
854
855(defun org-publish-find-date (file)
856 "Find the date of FILE in project.
857If FILE provides a #+date keyword use it else use the file
858system's modification time.
859
860It 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))))))))
872
873;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
874;;; Interactive publishing functions
875
876;;;###autoload
877(defalias 'org-publish-project 'org-publish)
878
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))))))
899
900;;;###autoload
901(defun org-publish-all (&optional force)
902 "Publish all projects.
903With prefix argument, remove all files in the timestamp
904directory 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))))
912
913
914;;;###autoload
915(defun org-publish-current-file (&optional force)
916 "Publish the current file.
917With 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)))))
923
924;;;###autoload
925(defun org-publish-current-project (&optional force)
926 "Publish the project associated with the current file.
927With a prefix argument, force publishing of all files in
928the 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))))
938
939
940;;; Index generation
941
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
963 (file-name-directory org-current-export-file) "."
964 (file-name-sans-extension
965 (file-name-nondirectory org-current-export-file)) ".orgx")
966 (dolist (entry (nreverse index))
967 (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
968
969(defun org-publish-index-generate-theindex (directory)
970 "Generate the index from all .orgx files in DIRECTORY."
971 (require 'find-lisp)
972 (let* ((fulldir (file-name-as-directory
973 (expand-file-name directory)))
974 (full-files (find-lisp-find-files directory "\\.orgx\\'"))
975 (re (concat "\\`" fulldir))
976 (files (mapcar (lambda (f) (if (string-match re f)
977 (substring f (match-end 0))
978 f))
979 full-files))
980 (default-directory directory)
981 index origfile buf target entry ibuffer
982 main last-main letter last-letter file sub link tgext)
983 ;; `files' contains the list of relative file names
984 (dolist (file files)
985 (setq origfile (substring file 1 -1))
986 (setq buf (find-file-noselect file))
987 (with-current-buffer buf
988 (goto-char (point-min))
989 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
990 (setq target (match-string 1)
991 entry (match-string 2))
992 (push (list entry origfile target) index)))
993 (kill-buffer buf))
994 (setq index (sort index (lambda (a b) (string< (downcase (car a))
995 (downcase (car b))))))
996 (setq ibuffer (find-file-noselect (expand-file-name "theindex.org" directory)))
997 (with-current-buffer ibuffer
998 (erase-buffer)
999 (insert "* Index\n")
1000 (setq last-letter nil)
1001 (dolist (idx index)
1002 (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
1003 (if (and (stringp target) (string-match "\\S-" target))
1004 (setq tgext (concat "::#" target))
1005 (setq tgext ""))
1006 (setq letter (upcase (substring entry 0 1)))
1007 (when (not (equal letter last-letter))
1008 (insert "** " letter "\n")
1009 (setq last-letter letter))
1010 (if (string-match "!" entry)
1011 (setq main (substring entry 0 (match-beginning 0))
1012 sub (substring entry (match-end 0)))
1013 (setq main nil sub nil last-main nil))
1014 (when (and main (not (equal main last-main)))
1015 (insert " - " main "\n")
1016 (setq last-main main))
1017 (setq link (concat "[[file:" file tgext "]"
1018 "[" (or sub entry) "]]"))
1019 (if (and main sub)
1020 (insert " - " link "\n")
1021 (insert " - " link "\n")))
1022 (save-buffer))
1023 (kill-buffer ibuffer)))
1024
1025;; Caching functions:
1026
1027(defun org-publish-write-cache-file (&optional free-cache)
1028 "Write `org-publish-cache' to file.
1029If FREE-CACHE, empty the cache."
1030 (unless org-publish-cache
1031 (error "%s" "`org-publish-write-cache-file' called, but no cache present"))
1032
1033 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1034 (unless cache-file
1035 (error
1036 "%s" "Cannot find cache-file name in `org-publish-write-cache-file'"))
1037 (with-temp-file cache-file
1038 (let ((print-level nil)
1039 (print-length nil))
1040 (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
1041 (maphash (lambda (k v)
1042 (insert
1043 (format (concat "(puthash %S "
1044 (if (or (listp v) (symbolp v))
1045 "'" "")
1046 "%S org-publish-cache)\n") k v)))
1047 org-publish-cache)))
1048 (when free-cache (org-publish-reset-cache))))
1049
1050(defun org-publish-initialize-cache (project-name)
1051 "Initialize the projects cache if not initialized yet and return it."
1052
1053 (unless project-name
1054 (error "%s%s" "Cannot initialize `org-publish-cache' without projects name"
1055 " in `org-publish-initialize-cache'"))
1056
1057 (unless (file-exists-p org-publish-timestamp-directory)
1058 (make-directory org-publish-timestamp-directory t))
1059 (if (not (file-directory-p org-publish-timestamp-directory))
1060 (error "Org publish timestamp: %s is not a directory"
1061 org-publish-timestamp-directory))
1062
1063 (unless (and org-publish-cache
1064 (string= (org-publish-cache-get ":project:") project-name))
1065 (let* ((cache-file (concat
1066 (expand-file-name org-publish-timestamp-directory)
1067 project-name
1068 ".cache"))
1069 (cexists (file-exists-p cache-file)))
1070
1071 (when org-publish-cache
1072 (org-publish-reset-cache))
1073
1074 (if cexists
1075 (load-file cache-file)
1076 (setq org-publish-cache
1077 (make-hash-table :test 'equal :weakness nil :size 100))
1078 (org-publish-cache-set ":project:" project-name)
1079 (org-publish-cache-set ":cache-file:" cache-file))
1080 (unless cexists (org-publish-write-cache-file nil))))
1081 org-publish-cache)
1082
1083(defun org-publish-reset-cache ()
1084 "Empty org-publish-cache and reset it nil."
1085 (message "%s" "Resetting org-publish-cache")
1086 (if (hash-table-p org-publish-cache)
1087 (clrhash org-publish-cache))
1088 (setq org-publish-cache nil))
1089
1090(defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func)
1091 "Check the timestamp of the last publishing of FILENAME.
1092Return `t', if the file needs publishing. The function also
1093checks if any included files have been more recently published,
1094so that the file including them will be republished as well."
1095 (unless org-publish-cache
1096 (error "%s" "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1097 (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func))
1098 (pstamp (org-publish-cache-get key))
1099 (visiting (find-buffer-visiting filename))
1100 included-files-ctime buf)
1101
1102 (when (equal (file-name-extension filename) "org")
1103 (setq buf (find-file (expand-file-name filename)))
1104 (with-current-buffer buf
1105 (goto-char (point-min))
1106 (while (re-search-forward "^#\\+INCLUDE:[ \t]+\"?\\([^ \t\"]*\\)\"?[ \t]*.*$" nil t)
1107 (let* ((included-file (expand-file-name (match-string 1))))
1108 (add-to-list 'included-files-ctime
1109 (org-publish-cache-ctime-of-src included-file) t))))
1110 ;; FIXME don't kill current buffer
1111 (unless visiting (kill-buffer buf)))
1112 (if (null pstamp)
1113 t
1114 (let ((ctime (org-publish-cache-ctime-of-src filename)))
1115 (or (< pstamp ctime)
1116 (when included-files-ctime
1117 (not (null (delq nil (mapcar (lambda(ct) (< ctime ct))
1118 included-files-ctime))))))))))
1119
1120(defun org-publish-cache-set-file-property (filename property value &optional project-name)
1121 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1122Use cache file of PROJECT-NAME. If the entry does not exist, it will be
1123created. Return VALUE."
1124 ;; Evtl. load the requested cache file:
1125 (if project-name (org-publish-initialize-cache project-name))
1126 (let ((pl (org-publish-cache-get filename)))
1127 (if pl
1128 (progn
1129 (plist-put pl property value)
1130 value)
1131 (org-publish-cache-get-file-property
1132 filename property value nil project-name))))
1133
1134(defun org-publish-cache-get-file-property
1135 (filename property &optional default no-create project-name)
1136 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1137Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
1138DEFAULT, if the value does not yet exist.
1139If the entry will be created, unless NO-CREATE is not nil."
1140 ;; Evtl. load the requested cache file:
1141 (if project-name (org-publish-initialize-cache project-name))
1142 (let ((pl (org-publish-cache-get filename))
1143 (retval nil))
1144 (if pl
1145 (if (plist-member pl property)
1146 (setq retval (plist-get pl property))
1147 (setq retval default))
1148 ;; no pl yet:
1149 (unless no-create
1150 (org-publish-cache-set filename (list property default)))
1151 (setq retval default))
1152 retval))
1153
1154(defun org-publish-cache-get (key)
1155 "Return the value stored in `org-publish-cache' for key KEY.
1156Returns nil, if no value or nil is found, or the cache does not
1157exist."
1158 (unless org-publish-cache
1159 (error "%s" "`org-publish-cache-get' called, but no cache present"))
1160 (gethash key org-publish-cache))
1161
1162(defun org-publish-cache-set (key value)
1163 "Store KEY VALUE pair in `org-publish-cache'.
1164Returns value on success, else nil."
1165 (unless org-publish-cache
1166 (error "%s" "`org-publish-cache-set' called, but no cache present"))
1167 (puthash key value org-publish-cache))
1168
1169(defun org-publish-cache-ctime-of-src (filename)
1170 "Get the FILENAME ctime as an integer."
1171 (let* ((symlink-maybe (or (file-symlink-p filename) filename))
1172 (src-attr (file-attributes (if (file-name-absolute-p symlink-maybe)
1173 symlink-maybe
1174 (expand-file-name
1175 symlink-maybe
1176 (file-name-directory filename))))))
1177 (+
1178 (lsh (car (nth 5 src-attr)) 16)
1179 (cadr (nth 5 src-attr)))))
1180
1181(provide 'org-publish)
1182
1183
1184
1185;;; org-publish.el ends here