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