Fix typos
[org-mode.git] / lisp / org-publish.el
blob328d9611cf0fa7e028746c61009fd2f755b342e0
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: 6.35trans
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)
59 (eval-and-compile
60 (unless (fboundp 'declare-function)
61 (defmacro declare-function (fn file &optional arglist fileonly))))
63 (defgroup org-publish nil
64 "Options for publishing a set of Org-mode and related files."
65 :tag "Org Publishing"
66 :group 'org)
68 (defcustom org-publish-project-alist nil
69 "Association list to control publishing behavior.
70 Each element of the alist is a publishing 'project.' The CAR of
71 each element is a string, uniquely identifying the project. The
72 CDR of each element is in one of the following forms:
74 (:property value :property value ... )
76 OR,
78 (:components (\"project-1\" \"project-2\" ...))
80 When the CDR of an element of org-publish-project-alist is in
81 this second form, the elements of the list after :components are
82 taken to be components of the project, which group together files
83 requiring different publishing options. When you publish such a
84 project with \\[org-publish], the components all publish.
86 When a property is given a value in org-publish-project-alist, its
87 setting overrides the value of the corresponding user variable
88 \(if any) during publishing. However, options set within a file
89 override everything.
91 Most properties are optional, but some should always be set:
93 :base-directory Directory containing publishing source files
94 :base-extension Extension (without the dot!) of source files.
95 This can be a regular expression.
96 :publishing-directory Directory (possibly remote) where output
97 files will be published
99 The :exclude property may be used to prevent certain files from
100 being published. Its value may be a string or regexp matching
101 file names you don't want to be published.
103 The :include property may be used to include extra files. Its
104 value may be a list of filenames to include. The filenames are
105 considered relative to the base directory.
107 When both :include and :exclude properties are given values, the
108 exclusion step happens first.
110 One special property controls which back-end function to use for
111 publishing files in the project. This can be used to extend the
112 set of file types publishable by org-publish, as well as the set
113 of output formats.
115 :publishing-function Function to publish file. The default is
116 `org-publish-org-to-html', but other
117 values are possible. May also be a
118 list of functions, in which case
119 each function in the list is invoked
120 in turn.
122 Another property allows you to insert code that prepares a
123 project for publishing. For example, you could call GNU Make on a
124 certain makefile, to ensure published files are built up to date.
126 :preparation-function Function to be called before publishing
127 this project. This may also be a list
128 of functions.
129 :completion-function Function to be called after publishing
130 this project. This may also be a list
131 of functions.
133 Some properties control details of the Org publishing process,
134 and are equivalent to the corresponding user variables listed in
135 the right column. See the documentation for those variables to
136 learn more about their use and default values.
138 :language `org-export-default-language'
139 :headline-levels `org-export-headline-levels'
140 :section-numbers `org-export-with-section-numbers'
141 :table-of-contents `org-export-with-toc'
142 :emphasize `org-export-with-emphasize'
143 :sub-superscript `org-export-with-sub-superscripts'
144 :TeX-macros `org-export-with-TeX-macros'
145 :fixed-width `org-export-with-fixed-width'
146 :tables `org-export-with-tables'
147 :table-auto-headline `org-export-highlight-first-table-line'
148 :style `org-export-html-style'
149 :convert-org-links `org-export-html-link-org-files-as-html'
150 :inline-images `org-export-html-inline-images'
151 :expand-quoted-html `org-export-html-expand'
152 :timestamp `org-export-html-with-timestamp'
153 :publishing-directory `org-export-publishing-directory'
154 :preamble `org-export-html-preamble'
155 :postamble `org-export-html-postamble'
156 :auto-preamble `org-export-html-auto-preamble'
157 :auto-postamble `org-export-html-auto-postamble'
158 :author `user-full-name'
159 :email `user-mail-address'
161 The following properties may be used to control publishing of a
162 sitemap of files or summary page for a given project.
164 :auto-sitemap Whether to publish a sitemap during
165 `org-publish-current-project' or `org-publish-all'.
166 :sitemap-filename Filename for output of sitemap. Defaults
167 to 'sitemap.org' (which becomes 'sitemap.html').
168 :sitemap-title Title of sitemap page. Defaults to name of file.
169 :sitemap-function Plugin function to use for generation of sitemap.
170 Defaults to `org-publish-org-sitemap', which
171 generates a plain list of links to all files
172 in the project.
173 :sitemap-style Can be `list' (sitemap is just an itemized list
174 of the titles of the files involved) or
175 `tree' (the directory structure of the source
176 files is reflected in the sitemap). Defaults to
177 `tree'.
179 If you create a sitemap file, adjust the sorting like this:
181 :sitemap-sort-folders Where folders should appear in the sitemap.
182 Set this to `first' (default) or `last' to
183 display folders first or last, respectively.
184 Any other value will mix files and folders.
185 :sitemap-alphabetically The site map is normally sorted alphabetically.
186 Set this explicitly to nil to turn off sorting.
187 :sitemap-ignore-case Should sorting be case-sensitive? Default nil.
189 The following properties control the creation of a concept index.
191 :makeindex Create a concept index."
192 :group 'org-publish
193 :type 'alist)
195 (defcustom org-publish-use-timestamps-flag t
196 "When non-nil, use timestamp checking to publish only changed files.
197 When nil, do no timestamp checking and always publish all files."
198 :group 'org-publish
199 :type 'boolean)
201 (defcustom org-publish-timestamp-directory (convert-standard-filename
202 "~/.org-timestamps/")
203 "Name of directory in which to store publishing timestamps."
204 :group 'org-publish
205 :type 'directory)
207 (defcustom org-publish-list-skipped-files t
208 "Non-nil means show message about files *not* published."
209 :group 'org-publish
210 :type 'boolean)
212 (defcustom org-publish-before-export-hook nil
213 "Hook run before export on the Org file.
214 The hook may modify the file in arbitrary ways before publishing happens.
215 The original version of the buffer will be restored after publishing."
216 :group 'org-publish
217 :type 'hook)
219 (defcustom org-publish-after-export-hook nil
220 "Hook run after export on the exported buffer.
221 Any changes made by this hook will be saved."
222 :group 'org-publish
223 :type 'hook)
225 (defcustom org-publish-sitemap-sort-alphabetically t
226 "Should sitemaps be sorted alphabetically by default?
228 You can overwrite this default per project in your
229 `org-publish-project-alist', using `:sitemap-alphabetically'."
230 :group 'org-publish
231 :type 'boolean)
233 (defcustom org-publish-sitemap-sort-folders 'first
234 "A symbol, denoting if folders are sorted first in sitemaps.
235 Possible values are `first', `last', and nil.
236 If `first', folders will be sorted before files.
237 If `last', folders are sorted to the end after the files.
238 Any other value will not mix files and folders.
240 You can overwrite this default per project in your
241 `org-publish-project-alist', using `:sitemap-sort-folders'."
242 :group 'org-publish
243 :type 'symbol)
245 (defcustom org-publish-sitemap-sort-ignore-case nil
246 "Sort sitemaps case insensitively by default?
248 You can overwrite this default per project in your
249 `org-publish-project-alist', using `:sitemap-ignore-case'."
250 :group 'org-publish
251 :type 'boolean)
253 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
254 ;;; Timestamp-related functions
256 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
257 "Return path to timestamp file for filename FILENAME."
258 (setq filename (concat filename "::" (or pub-dir "") "::"
259 (format "%s" (or pub-func ""))))
260 (concat (file-name-as-directory org-publish-timestamp-directory)
261 "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
263 (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir)
264 "Return `t' if FILENAME should be published in PUB-DIR using PUB-FUNC.
265 TRUE-PUB-DIR is there the file will truely end up. Currently we are not using
266 this - maybe it can eventually be used to check if the file is present at
267 the target location, and how old it is. Right ow we cannot do this, because
268 we do not know under what file name the file will be stored - the publishing
269 function can still decide about that independently."
270 (let ((rtn
271 (if org-publish-use-timestamps-flag
272 (if (file-exists-p org-publish-timestamp-directory)
273 ;; first handle possible wrong timestamp directory
274 (if (not (file-directory-p org-publish-timestamp-directory))
275 (error "Org publish timestamp: %s is not a directory"
276 org-publish-timestamp-directory)
277 ;; there is a timestamp, check if FILENAME is newer
278 (file-newer-than-file-p
279 filename (org-publish-timestamp-filename
280 filename pub-dir pub-func)))
281 (make-directory org-publish-timestamp-directory)
283 ;; don't use timestamps, always return t
284 t)))
285 (if rtn
286 (message "Publishing file %s using `%s'" filename pub-func)
287 (when org-publish-list-skipped-files
288 (message "Skipping unmodified file %s" filename)))
289 rtn))
291 (defun org-publish-update-timestamp (filename &optional pub-dir pub-func)
292 "Update publishing timestamp for file FILENAME.
293 If there is no timestamp, create one."
294 (let ((timestamp-file (org-publish-timestamp-filename
295 filename pub-dir pub-func))
296 newly-created-timestamp)
297 (if (not (file-exists-p timestamp-file))
298 ;; create timestamp file if needed
299 (with-temp-buffer
300 (make-directory (file-name-directory timestamp-file) t)
301 (write-file timestamp-file)
302 (setq newly-created-timestamp t)))
303 ;; Emacs 21 doesn't have `set-file-times'
304 (if (and (fboundp 'set-file-times)
305 (not newly-created-timestamp))
306 (set-file-times timestamp-file)
307 (call-process "touch" nil 0 nil (expand-file-name timestamp-file)))))
309 (defun org-publish-remove-all-timestamps ()
310 "Remove all files in the timstamp directory."
311 (let ((dir org-publish-timestamp-directory)
312 files)
313 (when (and (file-exists-p dir)
314 (file-directory-p dir))
315 (mapc 'delete-file (directory-files dir 'full "[^.]\\'")))))
318 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
319 ;;; Mapping files to project names
321 (defvar org-publish-files-alist nil
322 "Alist of files and their parent projects.
323 Each element of this alist is of the form:
325 (file-name . project-name)")
327 (defvar org-publish-initial-buffer nil
328 "The buffer `org-publish' has been called from.")
329 (defvar org-publish-temp-files nil
330 "Temporary list of files to be published.")
332 ;; Here, so you find the variable right before it's used the first time:
333 (defvar org-publish-file-title-cache nil
334 "List of absolute filenames and titles.")
336 (defun org-publish-initialize-files-alist (&optional refresh)
337 "Set `org-publish-files-alist' if it is not set.
338 Also set it if the optional argument REFRESH is non-nil."
339 (interactive "P")
340 (when (or refresh (not org-publish-files-alist))
341 (setq org-publish-file-title-cache nil)
342 (setq org-publish-files-alist
343 (org-publish-get-files org-publish-project-alist))))
345 (defun org-publish-validate-link (link &optional directory)
346 "Check if LINK points to a file in the current project."
347 (assoc (expand-file-name link directory) org-publish-files-alist))
349 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
350 ;;; Compatibility aliases
352 ;; Delete-dups is not in Emacs <22
353 (if (fboundp 'delete-dups)
354 (defalias 'org-publish-delete-dups 'delete-dups)
355 (defun org-publish-delete-dups (list)
356 "Destructively remove `equal' duplicates from LIST.
357 Store the result in LIST and return it. LIST must be a proper list.
358 Of several `equal' occurrences of an element in LIST, the first
359 one is kept.
361 This is a compatibility function for Emacsen without `delete-dups'."
362 ;; Code from `subr.el' in Emacs 22:
363 (let ((tail list))
364 (while tail
365 (setcdr tail (delete (car tail) (cdr tail)))
366 (setq tail (cdr tail))))
367 list))
369 (declare-function org-publish-delete-dups "org-publish" (list))
370 (declare-function find-lisp-find-files "find-lisp" (directory regexp))
372 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
373 ;;; Getting project information out of org-publish-project-alist
375 (defun org-publish-get-files (projects-alist &optional no-exclusion)
376 "Return the list of all publishable files for PROJECTS-ALIST.
377 If NO-EXCLUSION is non-nil, don't exclude files."
378 (let (all-files)
379 ;; add all projects
380 (mapc
381 (lambda(p)
382 (let* ((exclude (plist-get (cdr p) :exclude))
383 (files (and p (org-publish-get-base-files p exclude))))
384 ;; add all files from this project
385 (mapc (lambda(f)
386 (add-to-list 'all-files
387 (cons (expand-file-name f) (car p))))
388 files)))
389 (org-publish-expand-projects projects-alist))
390 all-files))
392 (defun org-publish-expand-projects (projects-alist)
393 "Expand projects in PROJECTS-ALIST.
394 This splices all the components into the list."
395 (let ((rest projects-alist) rtn p components)
396 (while (setq p (pop rest))
397 (if (setq components (plist-get (cdr p) :components))
398 (setq rest (append
399 (mapcar (lambda (x) (assoc x org-publish-project-alist))
400 components)
401 rest))
402 (push p rtn)))
403 (nreverse (org-publish-delete-dups (delq nil rtn)))))
405 (defun org-publish-compare-directory-files (a b)
406 "Predicate for `sort', that sorts folders-first/last and
407 eventually alphabetically."
408 (let ((retval t))
409 (when (or sitemap-alphabetically sitemap-sort-folders)
410 ;; First we sort alphabetically:
411 (when sitemap-alphabetically
412 (let* ((adir (file-directory-p a))
413 (aorg (and (string-match "\\.org$" a) (not adir)))
414 (bdir (file-directory-p b))
415 (borg (and (string-match "\\.org$" b) (not bdir)))
416 (A (if aorg
417 (concat (file-name-directory a)
418 (org-publish-find-title a)) a))
419 (B (if borg
420 (concat (file-name-directory b)
421 (org-publish-find-title b)) b)))
422 (setq retval (if sitemap-ignore-case
423 (not (string-lessp (upcase B) (upcase A)))
424 (not (string-lessp B A))))))
426 ;; Directory-wise wins:
427 (when sitemap-sort-folders
428 ;; a is directory, b not:
429 (cond
430 ((and (file-directory-p a) (not (file-directory-p b)))
431 (setq retval (equal sitemap-sort-folders 'first)))
432 ;; a is not a directory, but b is:
433 ((and (not (file-directory-p a)) (file-directory-p b))
434 (setq retval (equal sitemap-sort-folders 'last))))))
435 retval))
437 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
438 "Set `org-publish-temp-files' with files from BASE-DIR directory.
439 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
440 non-nil, restrict this list to the files matching the regexp
441 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
442 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
443 matching the regexp SKIP-DIR when recursing through BASE-DIR."
444 (mapc (lambda (f)
445 (let ((fd-p (file-directory-p f))
446 (fnd (file-name-nondirectory f)))
447 (if (and fd-p recurse
448 (not (string-match "^\\.+$" fnd))
449 (if skip-dir (not (string-match skip-dir fnd)) t))
450 (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
451 (unless (or fd-p ;; this is a directory
452 (and skip-file (string-match skip-file fnd))
453 (not (file-exists-p (file-truename f)))
454 (not (string-match match fnd)))
455 (pushnew f org-publish-temp-files)))))
456 (sort (directory-files base-dir t (unless recurse match))
457 'org-publish-compare-directory-files)))
459 (defun org-publish-get-base-files (project &optional exclude-regexp)
460 "Return a list of all files in PROJECT.
461 If EXCLUDE-REGEXP is set, this will be used to filter out
462 matching filenames."
463 (let* ((project-plist (cdr project))
464 (base-dir (file-name-as-directory
465 (plist-get project-plist :base-directory)))
466 (include-list (plist-get project-plist :include))
467 (recurse (plist-get project-plist :recursive))
468 (extension (or (plist-get project-plist :base-extension) "org"))
469 ;; sitemap-... variables are dynamically scoped for
470 ;; org-publish-compare-directory-files:
471 (sitemap-sort-folders
472 (if (plist-member project-plist :sitemap-sort-folders)
473 (plist-get project-plist :sitemap-sort-folders)
474 org-publish-sitemap-sort-folders))
475 (sitemap-alphabetically
476 (if (plist-member project-plist :sitemap-alphabetically)
477 (plist-get project-plist :sitemap-alphabetically)
478 org-publish-sitemap-sort-alphabetically))
479 (sitemap-ignore-case
480 (if (plist-member project-plist :sitemap-ignore-case)
481 (plist-get project-plist :sitemap-ignore-case)
482 org-publish-sitemap-sort-ignore-case))
483 (match (if (eq extension 'any)
484 "^[^\\.]"
485 (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
486 ;; Make sure sitemap-sort-folders' has an accepted value
487 (unless (memq sitemap-sort-folders '(first last))
488 (setq sitemap-sort-folders nil))
490 (setq org-publish-temp-files nil)
491 (org-publish-get-base-files-1 base-dir recurse match
492 ;; FIXME distinguish exclude regexp
493 ;; for skip-file and skip-dir?
494 exclude-regexp exclude-regexp)
495 (mapc (lambda (f)
496 (pushnew
497 (expand-file-name (concat base-dir f))
498 org-publish-temp-files))
499 include-list)
500 org-publish-temp-files))
502 (defun org-publish-get-project-from-filename (filename &optional up)
503 "Return the project FILENAME belongs."
504 (let* ((project-name (cdr (assoc (expand-file-name filename)
505 org-publish-files-alist))))
506 (when up
507 (dolist (prj org-publish-project-alist)
508 (if (member project-name (plist-get (cdr prj) :components))
509 (setq project-name (car prj)))))
510 (assoc project-name org-publish-project-alist)))
512 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
513 ;;; Pluggable publishing back-end functions
515 (defun org-publish-org-to (format plist filename pub-dir)
516 "Publish an org file to FORMAT.
517 PLIST is the property list for the given project.
518 FILENAME is the filename of the org file to be published.
519 PUB-DIR is the publishing directory."
520 (require 'org)
521 (unless (file-exists-p pub-dir)
522 (make-directory pub-dir t))
523 (let ((visiting (find-buffer-visiting filename)))
524 (save-excursion
525 (switch-to-buffer (or visiting (find-file filename)))
526 (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
527 (init-buf (current-buffer))
528 (init-point (point))
529 (init-buf-string (buffer-string))
530 export-buf-or-file)
531 ;; run hooks before exporting
532 (run-hooks 'org-publish-before-export-hook)
533 ;; export the possibly modified buffer
534 (setq export-buf-or-file
535 (funcall (intern (concat "org-export-as-" format))
536 (plist-get plist :headline-levels)
537 nil plist nil nil pub-dir))
538 (when (and (bufferp export-buf-or-file)
539 (buffer-live-p export-buf-or-file))
540 (set-buffer export-buf-or-file)
541 ;; run hooks after export and save export
542 (progn (run-hooks 'org-publish-after-export-hook)
543 (if (buffer-modified-p) (save-buffer)))
544 (kill-buffer export-buf-or-file))
545 ;; maybe restore buffer's content
546 (set-buffer init-buf)
547 (when (buffer-modified-p init-buf)
548 (erase-buffer)
549 (insert init-buf-string)
550 (save-buffer)
551 (goto-char init-point))
552 (unless visiting
553 (kill-buffer init-buf))))))
555 (defmacro org-publish-with-aux-preprocess-maybe (&rest body)
556 "Execute BODY with a modified hook to preprocess for index."
557 `(let ((org-export-preprocess-after-headline-targets-hook
558 (if (plist-get project-plist :makeindex)
559 (cons 'org-publish-aux-preprocess
560 org-export-preprocess-after-headline-targets-hook)
561 org-export-preprocess-after-headline-targets-hook)))
562 ,@body))
564 (defvar project-plist)
565 (defun org-publish-org-to-latex (plist filename pub-dir)
566 "Publish an org file to LaTeX.
567 See `org-publish-org-to' to the list of arguments."
568 (org-publish-with-aux-preprocess-maybe
569 (org-publish-org-to "latex" plist filename pub-dir)))
571 (defun org-publish-org-to-pdf (plist filename pub-dir)
572 "Publish an org file to PDF (via LaTeX).
573 See `org-publish-org-to' to the list of arguments."
574 (org-publish-with-aux-preprocess-maybe
575 (org-publish-org-to "pdf" plist filename pub-dir)))
577 (defun org-publish-org-to-html (plist filename pub-dir)
578 "Publish an org file to HTML.
579 See `org-publish-org-to' to the list of arguments."
580 (org-publish-with-aux-preprocess-maybe
581 (org-publish-org-to "html" plist filename pub-dir)))
583 (defun org-publish-org-to-org (plist filename pub-dir)
584 "Publish an org file to HTML.
585 See `org-publish-org-to' to the list of arguments."
586 (org-publish-org-to "org" plist filename pub-dir))
588 (defun org-publish-attachment (plist filename pub-dir)
589 "Publish a file with no transformation of any kind.
590 See `org-publish-org-to' to the list of arguments."
591 ;; make sure eshell/cp code is loaded
592 (unless (file-directory-p pub-dir)
593 (make-directory pub-dir t))
594 (or (equal (expand-file-name (file-name-directory filename))
595 (file-name-as-directory (expand-file-name pub-dir)))
596 (copy-file filename
597 (expand-file-name (file-name-nondirectory filename) pub-dir)
598 t)))
600 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
601 ;;; Publishing files, sets of files, and indices
603 (defun org-publish-file (filename &optional project)
604 "Publish file FILENAME from PROJECT."
605 (let* ((project
606 (or project
607 (or (org-publish-get-project-from-filename filename)
608 (if (y-or-n-p
609 (format "%s is not in a project. Re-read the list of projects files? "
610 (abbreviate-file-name filename)))
611 ;; If requested, re-initialize the list of projects files
612 (progn (org-publish-initialize-files-alist t)
613 (or (org-publish-get-project-from-filename filename)
614 (error "File %s not part of any known project"
615 (abbreviate-file-name filename))))
616 (error "Can't publish file outside of a project")))))
617 (project-plist (cdr project))
618 (ftname (file-truename filename))
619 (publishing-function
620 (or (plist-get project-plist :publishing-function)
621 'org-publish-org-to-html))
622 (base-dir (file-name-as-directory
623 (file-truename (plist-get project-plist :base-directory))))
624 (pub-dir (file-name-as-directory
625 (file-truename (plist-get project-plist :publishing-directory))))
626 tmp-pub-dir)
627 (setq tmp-pub-dir
628 (file-name-directory
629 (concat pub-dir
630 (and (string-match (regexp-quote base-dir) ftname)
631 (substring ftname (match-end 0))))))
632 (if (listp publishing-function)
633 ;; allow chain of publishing functions
634 (mapc (lambda (f)
635 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir)
636 (funcall f project-plist filename tmp-pub-dir)
637 (org-publish-update-timestamp filename pub-dir f)))
638 publishing-function)
639 (when (org-publish-needed-p filename pub-dir publishing-function
640 tmp-pub-dir)
641 (funcall publishing-function project-plist filename tmp-pub-dir)
642 (org-publish-update-timestamp
643 filename pub-dir publishing-function)))))
645 (defun org-publish-projects (projects)
646 "Publish all files belonging to the PROJECTS alist.
647 If :auto-sitemap is set, publish the sitemap too.
648 If :makeindex is set, also produce a file theindex.org."
649 (mapc
650 (lambda (project)
651 (let*
652 ((project-plist (cdr project))
653 (exclude-regexp (plist-get project-plist :exclude))
654 (sitemap-p (plist-get project-plist :auto-sitemap))
655 (sitemap-filename (or (plist-get project-plist :sitemap-filename)
656 "sitemap.org"))
657 (sitemap-function (or (plist-get project-plist :sitemap-function)
658 'org-publish-org-sitemap))
659 (preparation-function (plist-get project-plist :preparation-function))
660 (completion-function (plist-get project-plist :completion-function))
661 (files (org-publish-get-base-files project exclude-regexp)) file)
662 (when preparation-function (run-hooks 'preparation-function))
663 (if sitemap-p (funcall sitemap-function project sitemap-filename))
664 (while (setq file (pop files))
665 (org-publish-file file project))
666 (when (plist-get project-plist :makeindex)
667 (org-publish-index-generate-theindex.inc
668 (plist-get project-plist :base-directory))
669 (org-publish-file (expand-file-name
670 "theindex.org"
671 (plist-get project-plist :base-directory))
672 project))
673 (when completion-function (run-hooks 'completion-function))))
674 (org-publish-expand-projects projects)))
676 (defun org-publish-org-sitemap (project &optional sitemap-filename)
677 "Create an sitemap of pages in set defined by PROJECT.
678 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
679 Default for SITEMAP-FILENAME is 'sitemap.org'."
680 (let* ((project-plist (cdr project))
681 (dir (file-name-as-directory
682 (plist-get project-plist :base-directory)))
683 (localdir (file-name-directory dir))
684 (indent-str (make-string 2 ?\ ))
685 (exclude-regexp (plist-get project-plist :exclude))
686 (files (nreverse (org-publish-get-base-files project exclude-regexp)))
687 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
688 (sitemap-title (or (plist-get project-plist :sitemap-title)
689 (concat "Sitemap for project " (car project))))
690 (sitemap-style (or (plist-get project-plist :sitemap-style)
691 'tree))
692 (visiting (find-buffer-visiting sitemap-filename))
693 (ifn (file-name-nondirectory sitemap-filename))
694 file sitemap-buffer)
695 (with-current-buffer (setq sitemap-buffer
696 (or visiting (find-file sitemap-filename)))
697 (erase-buffer)
698 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
699 (while (setq file (pop files))
700 (let ((fn (file-name-nondirectory file))
701 (link (file-relative-name file dir))
702 (oldlocal localdir))
703 ;; sitemap shouldn't list itself
704 (unless (equal (file-truename sitemap-filename)
705 (file-truename file))
706 (if (eq sitemap-style 'list)
707 (message "Generating list-style sitemap for %s" sitemap-title)
708 (message "Generating tree-style sitemap for %s" sitemap-title)
709 (setq localdir (concat (file-name-as-directory dir)
710 (file-name-directory link)))
711 (unless (string= localdir oldlocal)
712 (if (string= localdir dir)
713 (setq indent-str (make-string 2 ?\ ))
714 (let ((subdirs
715 (split-string
716 (directory-file-name
717 (file-name-directory
718 (file-relative-name localdir dir))) "/"))
719 (subdir "")
720 (old-subdirs (split-string
721 (file-relative-name oldlocal dir) "/")))
722 (setq indent-str (make-string 2 ?\ ))
723 (while (string= (car old-subdirs) (car subdirs))
724 (setq indent-str (concat indent-str (make-string 2 ?\ )))
725 (pop old-subdirs)
726 (pop subdirs))
727 (dolist (d subdirs)
728 (setq subdir (concat subdir d "/"))
729 (insert (concat indent-str " + " d "\n"))
730 (setq indent-str (make-string
731 (+ (length indent-str) 2) ?\ )))))))
732 ;; This is common to 'flat and 'tree
733 (insert (concat indent-str " + [[file:" link "]["
734 (org-publish-find-title file)
735 "]]\n")))))
736 (save-buffer))
737 (or visiting (kill-buffer sitemap-buffer))))
739 (defun org-publish-find-title (file)
740 "Find the title of file in project."
741 (if (member file org-publish-file-title-cache)
742 (cadr (member file org-publish-file-title-cache))
743 (let* ((visiting (find-buffer-visiting file))
744 (buffer (or visiting (find-file-noselect file)))
745 title)
746 (with-current-buffer buffer
747 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
748 (org-infile-export-plist))))
749 (setq title
750 (or (plist-get opt-plist :title)
751 (and (not
752 (plist-get opt-plist :skip-before-1st-heading))
753 (org-export-grab-title-from-buffer))
754 (file-name-nondirectory (file-name-sans-extension file))))))
755 (unless visiting
756 (kill-buffer buffer))
757 (setq org-publish-file-title-cache
758 (append org-publish-file-title-cache (list file title)))
759 title)))
761 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
762 ;;; Interactive publishing functions
764 ;;;###autoload
765 (defalias 'org-publish-project 'org-publish)
767 ;;;###autoload
768 (defun org-publish (project &optional force)
769 "Publish PROJECT."
770 (interactive
771 (list
772 (assoc (org-icompleting-read
773 "Publish project: "
774 org-publish-project-alist nil t)
775 org-publish-project-alist)
776 current-prefix-arg))
777 (setq org-publish-initial-buffer (current-buffer))
778 (save-window-excursion
779 (let* ((org-publish-use-timestamps-flag
780 (if force nil org-publish-use-timestamps-flag)))
781 (org-publish-projects (list project)))))
783 ;;;###autoload
784 (defun org-publish-all (&optional force)
785 "Publish all projects.
786 With prefix argument, remove all files in the timestamp
787 directory and force publishing all files."
788 (interactive "P")
789 (when force
790 (org-publish-remove-all-timestamps))
791 (org-publish-initialize-files-alist force)
792 (save-window-excursion
793 (let ((org-publish-use-timestamps-flag
794 (if force nil org-publish-use-timestamps-flag)))
795 (org-publish-projects org-publish-project-alist))))
798 ;;;###autoload
799 (defun org-publish-current-file (&optional force)
800 "Publish the current file.
801 With prefix argument, force publish the file."
802 (interactive "P")
803 (org-publish-initialize-files-alist force)
804 (save-window-excursion
805 (let ((org-publish-use-timestamps-flag
806 (if force nil org-publish-use-timestamps-flag)))
807 (org-publish-file (buffer-file-name)))))
809 ;;;###autoload
810 (defun org-publish-current-project (&optional force)
811 "Publish the project associated with the current file.
812 With a prefix argument, force publishing of all files in
813 the project."
814 (interactive "P")
815 (org-publish-initialize-files-alist force)
816 (save-window-excursion
817 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up))
818 (org-publish-use-timestamps-flag
819 (if force nil org-publish-use-timestamps-flag)))
820 (if (not project)
821 (error "File %s is not part of any known project" (buffer-file-name)))
822 (org-publish project))))
825 ;;; Index generation
827 (defvar backend) ; dynamically scoped
828 (defun org-publish-aux-preprocess ()
829 "Find index entries and write them to an .orgx file."
830 (let ((case-fold-search t)
831 entry index target)
832 (goto-char (point-min))
833 (while
834 (and
835 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t)
836 (> (match-end 1) (match-beginning 1)))
837 (setq entry (match-string 1))
838 (when (eq backend 'latex)
839 (replace-match (format "\\index{%s}" entry) t t))
840 (save-excursion
841 (ignore-errors (org-back-to-heading t))
842 (setq target (get-text-property (point) 'target))
843 (setq target (or (cdr (assoc target org-export-preferred-target-alist))
844 (cdr (assoc target org-export-id-target-alist))
845 target ""))
846 (push (cons entry target) index)))
847 (with-temp-file
848 (concat (file-name-sans-extension org-current-export-file) ".orgx")
849 (dolist (entry (nreverse index))
850 (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
852 (defun org-publish-index-generate-theindex.inc (directory)
853 "Generate the index from all .orgx files in the current directory and below."
854 (require 'find-lisp)
855 (let* ((fulldir (file-name-as-directory
856 (expand-file-name directory)))
857 (full-files (find-lisp-find-files directory "\\.orgx\\'"))
858 (re (concat "\\`" fulldir))
859 (files (mapcar (lambda (f) (if (string-match re f)
860 (substring f (match-end 0))
862 full-files))
863 (default-directory directory)
864 index origfile buf target entry ibuffer
865 main last-main letter last-letter file sub link tgext)
866 ;; `files' contains the list of relative file names
867 (dolist (file files)
868 (setq origfile (substring file 0 -1))
869 (setq buf (find-file-noselect file))
870 (with-current-buffer buf
871 (goto-char (point-min))
872 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
873 (setq target (match-string 1)
874 entry (match-string 2))
875 (push (list entry origfile target) index)))
876 (kill-buffer buf))
877 (setq index (sort index (lambda (a b) (string< (downcase (car a))
878 (downcase (car b))))))
879 (setq ibuffer (find-file-noselect (expand-file-name "theindex.inc" directory)))
880 (with-current-buffer ibuffer
881 (erase-buffer)
882 (insert "* Index\n")
883 (setq last-letter nil)
884 (dolist (idx index)
885 (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
886 (if (and (stringp target) (string-match "\\S-" target))
887 (setq tgext (concat "::#" target))
888 (setq tgext ""))
889 (setq letter (upcase (substring entry 0 1)))
890 (when (not (equal letter last-letter))
891 (insert "** " letter "\n")
892 (setq last-letter letter))
893 (if (string-match "!" entry)
894 (setq main (substring entry 0 (match-beginning 0))
895 sub (substring entry (match-end 0)))
896 (setq main nil sub nil last-main nil))
897 (when (and main (not (equal main last-main)))
898 (insert " - " main "\n")
899 (setq last-main main))
900 (setq link (concat "[[file:" file tgext "]"
901 "[" (or sub entry) "]]"))
902 (if (and main sub)
903 (insert " - " link "\n")
904 (insert " - " link "\n")))
905 (save-buffer))
906 (kill-buffer ibuffer)
908 (let ((index-file (expand-file-name "theindex.org" directory)))
909 (unless (file-exists-p index-file)
910 (setq ibuffer (find-file-noselect index-file))
911 (with-current-buffer ibuffer
912 (erase-buffer)
913 (insert "\n\n#+include: \"theindex.inc\"\n\n")
914 (save-buffer))
915 (kill-buffer ibuffer)))))
917 (provide 'org-publish)
920 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
922 ;;; org-publish.el ends here