Release 6.10a.
[org-mode.git] / lisp / org-publish.el
blob68bdd79e8c33a3f675c29fe839d3a81979966ba0
1 ;;; org-publish.el --- publish related org-mode files as a website
2 ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
4 ;; Author: David O'Toole <dto@gnu.org>
5 ;; Maintainer: Bastien Guerry <bzg AT altern DOT org>
6 ;; Keywords: hypermedia, outlines, wp
7 ;; Version: 6.10a
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
26 ;; This program allow configurable publishing of related sets of
27 ;; Org-mode files as a complete website.
29 ;; org-publish.el can do the following:
31 ;; + Publish all one's org-files to HTML or LaTeX
32 ;; + Upload HTML, images, attachments and other files to a web server
33 ;; + Exclude selected private pages from publishing
34 ;; + Publish a clickable index of pages
35 ;; + Manage local timestamps for publishing only changed files
36 ;; + Accept plugin functions to extend range of publishable content
38 ;; Special thanks to the org-mode maintainer Carsten Dominik for his
39 ;; ideas, enthusiasm, and cooperation.
41 ;;; Installation:
43 ;; Put org-publish.el in your load path, byte-compile it, and then add
44 ;; the following lines to your emacs initialization file:
46 ;; (autoload 'org-publish "org-publish" nil t)
47 ;; (autoload 'org-publish "org-publish-all" nil t)
48 ;; (autoload 'org-publish "org-publish-current-file" nil t)
49 ;; (autoload 'org-publish "org-publish-current-project" nil t)
51 ;; NOTE: When org-publish.el is included with org.el, those forms are
52 ;; already in the file org-install.el, and hence don't need to be put
53 ;; in your emacs initialization file in this case.
55 ;;; Usage:
57 ;; The program's main configuration variable is
58 ;; `org-publish-project-alist'. See below for example configurations
59 ;; with commentary.
61 ;; The main interactive functions are:
63 ;; M-x org-publish
64 ;; M-x org-publish-all
65 ;; M-x org-publish-current-file
66 ;; M-x org-publish-current-project
68 ;;;; Simple example configuration:
70 ;; (setq org-publish-project-alist
71 ;; (list
72 ;; '("org" . (:base-directory "~/org/"
73 ;; :base-extension "org"
74 ;; :publishing-directory "~/public_html"
75 ;; :with-section-numbers nil
76 ;; :table-of-contents nil
77 ;; :recursive t
78 ;; :style "<link rel=stylesheet href=\"../other/mystyle.css\" type=\"text/css\">")))
80 ;;;; More complex example configuration:
82 ;; Imagine your *.org files are kept in ~/org, your images in
83 ;; ~/images, and stylesheets in ~/other. Now imagine you want to
84 ;; publish the files through an ssh connection to a remote host, via
85 ;; Tramp-mode. To maintain relative links from *.org files to /images
86 ;; and /other, we should replicate the same directory structure in
87 ;; your web server account's designated html root (in this case,
88 ;; assumed to be ~/html)
90 ;; Once you've done created the proper directories, you can adapt the
91 ;; following example configuration to your specific paths, run M-x
92 ;; org-publish-all, and it should publish the files to the correct
93 ;; directories on the web server, transforming the *.org files into
94 ;; HTML, and leaving other files alone.
96 ;; (setq org-publish-project-alist
97 ;; (list
98 ;; '("orgfiles" :base-directory "~/org/"
99 ;; :base-extension "org"
100 ;; :publishing-directory "/ssh:user@host:~/html/notebook/"
101 ;; :publishing-function org-publish-org-to-html
102 ;; :exclude "PrivatePage.org" ;; regexp
103 ;; :headline-levels 3
104 ;; :with-section-numbers nil
105 ;; :table-of-contents nil
106 ;; :style "<link rel=stylesheet href=\"../other/mystyle.css\" type=\"text/css\">"
107 ;; :auto-preamble t
108 ;; :auto-postamble nil)
109 ;; ("images" :base-directory "~/images/"
110 ;; :base-extension "jpg\\|gif\\|png"
111 ;; :publishing-directory "/ssh:user@host:~/html/images/"
112 ;; :publishing-function org-publish-attachment)
113 ;; ("other" :base-directory "~/other/"
114 ;; :base-extension "css"
115 ;; :publishing-directory "/ssh:user@host:~/html/other/"
116 ;; :publishing-function org-publish-attachment)
117 ;; ("website" :components ("orgfiles" "images" "other"))))
119 ;; For more information, see the documentation for the variable
120 ;; `org-publish-project-alist'.
122 ;; Of course, you don't have to publish to remote directories from
123 ;; within emacs. You can always just publish to local folders, and
124 ;; then use the synchronization/upload tool of your choice.
126 ;;; List of user-visible changes since version 1.27
128 ;; 1.78: Allow list-valued :publishing-function
129 ;; 1.77: Added :preparation-function, this allows you to use GNU Make etc.
130 ;; 1.65: Remove old "composite projects". They're redundant.
131 ;; 1.64: Allow meta-projects with :components
132 ;; 1.57: Timestamps flag is now called "org-publish-use-timestamps-flag"
133 ;; 1.52: Properly set default for :index-filename
134 ;; 1.48: Composite projects allowed.
135 ;; :include keyword allowed.
136 ;; 1.43: Index no longer includes itself in the index.
137 ;; 1.42: Fix "function definition is void" error
138 ;; when :publishing-function not set in org-publish-current-file.
139 ;; 1.41: Fixed bug where index isn't published on first try.
140 ;; 1.37: Added interactive function "org-publish". Prompts for particular
141 ;; project name to publish.
142 ;; 1.34: Added force-publish option to all interactive functions.
143 ;; 1.32: Fixed "index.org has changed on disk" error during index publishing.
144 ;; 1.30: Fixed startup error caused by (require 'em-unix)
146 ;;; Code:
148 (eval-when-compile
149 (require 'cl))
150 (require 'org)
151 (require 'org-exp)
153 (eval-and-compile
154 (unless (fboundp 'declare-function)
155 (defmacro declare-function (fn file &optional arglist fileonly))))
157 (defgroup org-publish nil
158 "Options for publishing a set of Org-mode and related files."
159 :tag "Org Publishing"
160 :group 'org)
162 (defcustom org-publish-project-alist nil
163 "Association list to control publishing behavior.
164 Each element of the alist is a publishing 'project.' The CAR of
165 each element is a string, uniquely identifying the project. The
166 CDR of each element is in one of the following forms:
168 (:property value :property value ... )
172 (:components (\"project-1\" \"project-2\" ...))
174 When the CDR of an element of org-publish-project-alist is in
175 this second form, the elements of the list after :components are
176 taken to be components of the project, which group together files
177 requiring different publishing options. When you publish such a
178 project with \\[org-publish], the components all publish.
180 When a property is given a value in org-publish-project-alist, its
181 setting overrides the value of the corresponding user variable
182 \(if any) during publishing. However, options set within a file
183 override everything.
185 Most properties are optional, but some should always be set:
187 :base-directory Directory containing publishing source files
188 :base-extension Extension (without the dot!) of source files.
189 This can be a regular expression.
190 :publishing-directory Directory (possibly remote) where output
191 files will be published
193 The :exclude property may be used to prevent certain files from
194 being published. Its value may be a string or regexp matching
195 file names you don't want to be published.
197 The :include property may be used to include extra files. Its
198 value may be a list of filenames to include. The filenames are
199 considered relative to the base directory.
201 When both :include and :exclude properties are given values, the
202 exclusion step happens first.
204 One special property controls which back-end function to use for
205 publishing files in the project. This can be used to extend the
206 set of file types publishable by org-publish, as well as the set
207 of output formats.
209 :publishing-function Function to publish file. The default is
210 `org-publish-org-to-html', but other
211 values are possible. May also be a
212 list of functions, in which case
213 each function in the list is invoked
214 in turn.
216 Another property allows you to insert code that prepares a
217 project for publishing. For example, you could call GNU Make on a
218 certain makefile, to ensure published files are built up to date.
220 :preparation-function Function to be called before publishing
221 this project.
222 :completion-function Function to be called after publishing
223 this project.
225 Some properties control details of the Org publishing process,
226 and are equivalent to the corresponding user variables listed in
227 the right column. See the documentation for those variables to
228 learn more about their use and default values.
230 :language `org-export-default-language'
231 :headline-levels `org-export-headline-levels'
232 :section-numbers `org-export-with-section-numbers'
233 :table-of-contents `org-export-with-toc'
234 :emphasize `org-export-with-emphasize'
235 :sub-superscript `org-export-with-sub-superscripts'
236 :TeX-macros `org-export-with-TeX-macros'
237 :fixed-width `org-export-with-fixed-width'
238 :tables `org-export-with-tables'
239 :table-auto-headline `org-export-highlight-first-table-line'
240 :style `org-export-html-style'
241 :convert-org-links `org-export-html-link-org-files-as-html'
242 :inline-images `org-export-html-inline-images'
243 :expand-quoted-html `org-export-html-expand'
244 :timestamp `org-export-html-with-timestamp'
245 :publishing-directory `org-export-publishing-directory'
246 :preamble `org-export-html-preamble'
247 :postamble `org-export-html-postamble'
248 :auto-preamble `org-export-html-auto-preamble'
249 :auto-postamble `org-export-html-auto-postamble'
250 :author `user-full-name'
251 :email `user-mail-address'
253 The following properties may be used to control publishing of an
254 index of files or summary page for a given project.
256 :auto-index Whether to publish an index during
257 `org-publish-current-project' or `org-publish-all'.
258 :index-filename Filename for output of index. Defaults
259 to 'index.org' (which becomes 'index.html').
260 :index-title Title of index page. Defaults to name of file.
261 :index-function Plugin function to use for generation of index.
262 Defaults to `org-publish-org-index', which
263 generates a plain list of links to all files
264 in the project.
265 :index-style Can be `list' (index is just an itemized list
266 of the titles of the files involved) or
267 `tree' (the directory structure of the source
268 files is reflected in the index). Defaults to
269 `tree'."
270 :group 'org-publish
271 :type 'alist)
273 (defcustom org-publish-use-timestamps-flag t
274 "When non-nil, use timestamp checking to publish only changed files.
275 When nil, do no timestamp checking and always publish all files."
276 :group 'org-publish
277 :type 'boolean)
279 (defcustom org-publish-timestamp-directory (convert-standard-filename
280 "~/.org-timestamps/")
281 "Name of directory in which to store publishing timestamps."
282 :group 'org-publish
283 :type 'directory)
285 (defcustom org-publish-before-export-hook nil
286 "Hook run before export on the Org file.
287 If the functions in this hook modify the original Org buffer, the
288 modified buffer will be used for export, but the buffer will be
289 restored and saved back to its initial state after export."
290 :group 'org-publish
291 :type 'hook)
293 (defcustom org-publish-after-export-hook nil
294 "Hook run after export on the exported buffer.
295 If functions in this hook modify the buffer, it will be saved."
296 :group 'org-publish
297 :type 'hook)
299 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
300 ;;; Timestamp-related functions
302 (defun org-publish-timestamp-filename (filename)
303 "Return path to timestamp file for filename FILENAME."
304 (concat (file-name-as-directory org-publish-timestamp-directory)
305 "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
307 (defun org-publish-needed-p (filename)
308 "Return `t' if FILENAME should be published."
309 (let ((rtn
310 (if org-publish-use-timestamps-flag
311 (if (file-exists-p org-publish-timestamp-directory)
312 ;; first handle possible wrong timestamp directory
313 (if (not (file-directory-p org-publish-timestamp-directory))
314 (error "Org publish timestamp: %s is not a directory"
315 org-publish-timestamp-directory)
316 ;; there is a timestamp, check if FILENAME is newer
317 (file-newer-than-file-p
318 filename (org-publish-timestamp-filename filename)))
319 (make-directory org-publish-timestamp-directory)
321 ;; don't use timestamps, always return t
322 t)))
323 (if rtn
324 (message "Publishing file %s" filename)
325 (message "Skipping unmodified file %s" filename))
326 rtn))
328 (defun org-publish-update-timestamp (filename)
329 "Update publishing timestamp for file FILENAME.
330 If there is no timestamp, create one."
331 (let ((timestamp-file (org-publish-timestamp-filename filename))
332 newly-created-timestamp)
333 (if (not (file-exists-p timestamp-file))
334 ;; create timestamp file if needed
335 (with-temp-buffer
336 (make-directory (file-name-directory timestamp-file) t)
337 (write-file timestamp-file)
338 (setq newly-created-timestamp t)))
339 ;; Emacs 21 doesn't have `set-file-times'
340 (if (and (fboundp 'set-file-times)
341 (not newly-created-timestamp))
342 (set-file-times timestamp-file)
343 (call-process "touch" nil 0 nil timestamp-file))))
345 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
346 ;;; Mapping files to project names
348 (defvar org-publish-files-alist nil
349 "Alist of files and their parent projects.
350 Each element of this alist is of the form:
352 (file-name . project-name)")
354 (defvar org-publish-initial-buffer nil
355 "The buffer `org-publish' has been called from.")
356 (defvar org-publish-temp-files nil
357 "Temporary list of files to be published.")
359 (defun org-publish-initialize-files-alist (&optional refresh)
360 "Set `org-publish-files-alist' if it is not set.
361 Also set it if the optional argument REFRESH is non-nil."
362 (interactive "P")
363 (when (or refresh (not org-publish-files-alist))
364 (setq org-publish-files-alist
365 (org-publish-get-files org-publish-project-alist))))
367 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
368 ;;; Compatibility aliases
370 ;; Delete-dups is not in Emacs <22
371 (if (fboundp 'delete-dups)
372 (defalias 'org-publish-delete-dups 'delete-dups)
373 (defun org-publish-delete-dups (list)
374 "Destructively remove `equal' duplicates from LIST.
375 Store the result in LIST and return it. LIST must be a proper list.
376 Of several `equal' occurrences of an element in LIST, the first
377 one is kept.
379 This is a compatibility function for Emacsen without `delete-dups'."
380 ;; Code from `subr.el' in Emacs 22:
381 (let ((tail list))
382 (while tail
383 (setcdr tail (delete (car tail) (cdr tail)))
384 (setq tail (cdr tail))))
385 list))
387 (declare-function org-publish-delete-dups "org-publish" (list))
389 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
390 ;;; Getting project information out of org-publish-project-alist
392 (defun org-publish-get-files (projects-alist &optional no-exclusion)
393 "Return the list of all publishable files for PROJECTS-ALIST.
394 If NO-EXCLUSION is non-nil, don't exclude files."
395 (let (all-files)
396 ;; add all projects
397 (mapc
398 (lambda(p)
399 (let* ((exclude (plist-get (cdr p) :exclude))
400 (files (and p (org-publish-get-base-files p exclude))))
401 ;; add all files from this project
402 (mapc (lambda(f)
403 (add-to-list 'all-files
404 (cons (expand-file-name f) (car p))))
405 files)))
406 (org-publish-expand-projects projects-alist))
407 all-files))
409 (defun org-publish-expand-projects (projects-alist)
410 "Expand projects in PROJECTS-ALIST.
411 This splices all the components into the list."
412 (let ((rest projects-alist) rtn p components)
413 (while (setq p (pop rest))
414 (if (setq components (plist-get (cdr p) :components))
415 (setq rest (append
416 (mapcar (lambda (x) (assoc x org-publish-project-alist))
417 components)
418 rest))
419 (push p rtn)))
420 (nreverse (org-publish-delete-dups (delq nil rtn)))))
422 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
423 "Set `org-publish-temp-files' with files from BASE-DIR directory.
424 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
425 non-nil, restrict this list to the files matching the regexp
426 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
427 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
428 matching the regexp SKIP-DIR when recursiing through BASE-DIR."
429 (mapc (lambda (f)
430 (let ((fd-p (car (file-attributes f)))
431 (fnd (file-name-nondirectory f)))
432 (if (and fd-p recurse
433 (not (string-match "^\\.+$" fnd))
434 (if skip-dir (not (string-match skip-dir fnd)) t))
435 (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
436 (unless (or fd-p ;; this is a directory
437 (and skip-file (string-match skip-file fnd))
438 (not (string-match match fnd)))
439 (pushnew f org-publish-temp-files)))))
440 (directory-files base-dir t (unless recurse match))))
442 (defun org-publish-get-base-files (project &optional exclude-regexp)
443 "Return a list of all files in PROJECT.
444 If EXCLUDE-REGEXP is set, this will be used to filter out
445 matching filenames."
446 (let* ((project-plist (cdr project))
447 (base-dir (file-name-as-directory
448 (plist-get project-plist :base-directory)))
449 (include-list (plist-get project-plist :include))
450 (recurse (plist-get project-plist :recursive))
451 (extension (or (plist-get project-plist :base-extension) "org"))
452 (match (concat "^[^\\.].*\\.\\(" extension "\\)$")))
453 (setq org-publish-temp-files nil)
454 (org-publish-get-base-files-1 base-dir recurse match
455 ;; FIXME distinguish exclude regexp
456 ;; for skip-file and skip-dir?
457 exclude-regexp exclude-regexp)
458 (mapc (lambda (f)
459 (pushnew
460 (expand-file-name (concat base-dir f))
461 org-publish-temp-files))
462 include-list)
463 org-publish-temp-files))
465 (defun org-publish-get-project-from-filename (filename)
466 "Return the project FILENAME belongs."
467 (let* ((project-name (cdr (assoc (expand-file-name filename)
468 org-publish-files-alist))))
469 (assoc project-name org-publish-project-alist)))
471 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
472 ;;; Pluggable publishing back-end functions
474 (defun org-publish-org-to (format plist filename pub-dir)
475 "Publish an org file to FORMAT.
476 PLIST is the property list for the given project.
477 FILENAME is the filename of the org file to be published.
478 PUB-DIR is the publishing directory."
479 (require 'org)
480 (unless (file-exists-p pub-dir)
481 (make-directory pub-dir t))
482 (let ((visiting (find-buffer-visiting filename)))
483 (save-excursion
484 (switch-to-buffer (or visiting (find-file filename)))
485 (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
486 (init-buf (current-buffer))
487 (init-point (point))
488 (init-buf-string (buffer-string))
489 export-buf-or-file)
490 ;; run hooks before exporting
491 (run-hooks 'org-publish-before-export-hook)
492 ;; export the possibly modified buffer
493 (setq export-buf-or-file
494 (funcall (intern (concat "org-export-as-" format))
495 (plist-get plist :headline-levels)
496 nil plist nil nil pub-dir))
497 (when (and (bufferp export-buf-or-file)
498 (buffer-live-p export-buf-or-file))
499 (set-buffer export-buf-or-file)
500 ;; run hooks after export and save export
501 (and (run-hooks 'org-publish-after-export-hook)
502 (if (buffer-modified-p) (save-buffer)))
503 (kill-buffer export-buf-or-file))
504 ;; maybe restore buffer's content
505 (set-buffer init-buf)
506 (when (buffer-modified-p init-buf)
507 (erase-buffer)
508 (insert init-buf-string)
509 (save-buffer)
510 (goto-char init-point))
511 (unless visiting
512 (kill-buffer init-buf))))))
514 (defun org-publish-org-to-latex (plist filename pub-dir)
515 "Publish an org file to LaTeX.
516 See `org-publish-org-to' to the list of arguments."
517 (org-publish-org-to "latex" plist filename pub-dir))
519 (defun org-publish-org-to-pdf (plist filename pub-dir)
520 "Publish an org file to PDF (via LaTeX).
521 See `org-publish-org-to' to the list of arguments."
522 (org-publish-org-to "pdf" plist filename pub-dir))
524 (defun org-publish-org-to-html (plist filename pub-dir)
525 "Publish an org file to HTML.
526 See `org-publish-org-to' to the list of arguments."
527 (org-publish-org-to "html" plist filename pub-dir))
529 (defun org-publish-attachment (plist filename pub-dir)
530 "Publish a file with no transformation of any kind.
531 See `org-publish-org-to' to the list of arguments."
532 ;; make sure eshell/cp code is loaded
533 (eval-and-compile
534 (require 'eshell)
535 (require 'esh-maint)
536 (require 'em-unix))
537 (unless (file-directory-p pub-dir)
538 (make-directory pub-dir t))
539 (eshell/cp filename pub-dir))
541 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
542 ;;; Publishing files, sets of files, and indices
544 (defun org-publish-file (filename &optional project)
545 "Publish file FILENAME from PROJECT."
546 (when (org-publish-needed-p filename)
547 (let* ((project
548 (or project
549 (or (org-publish-get-project-from-filename filename)
550 (if (y-or-n-p
551 (format "%s is not in a project. Re-read the list of projects files? "
552 (abbreviate-file-name filename)))
553 ;; If requested, re-initialize the list of projects files
554 (progn (org-publish-initialize-files-alist t)
555 (or (org-publish-get-project-from-filename filename)
556 (error "File %s not part of any known project"
557 (abbreviate-file-name filename))))
558 (error "Can't publish file outside of a project")))))
559 (project-plist (cdr project))
560 (ftname (file-truename filename))
561 (publishing-function
562 (or (plist-get project-plist :publishing-function)
563 'org-publish-org-to-html))
564 (base-dir (file-name-as-directory
565 (file-truename (plist-get project-plist :base-directory))))
566 (pub-dir (file-name-as-directory
567 (file-truename (plist-get project-plist :publishing-directory))))
568 tmp-pub-dir)
569 (setq tmp-pub-dir
570 (file-name-directory
571 (concat pub-dir
572 (and (string-match (regexp-quote base-dir) ftname)
573 (substring ftname (match-end 0))))))
574 (if (listp publishing-function)
575 ;; allow chain of publishing functions
576 (mapc (lambda (f)
577 (funcall f project-plist filename tmp-pub-dir))
578 publishing-function)
579 (funcall publishing-function project-plist filename tmp-pub-dir)))
580 (org-publish-update-timestamp filename)))
582 (defun org-publish-projects (projects)
583 "Publish all files belonging to the PROJECTS alist.
584 If :auto-index is set, publish the index too."
585 (mapc
586 (lambda (project)
587 (let*
588 ((project-plist (cdr project))
589 (exclude-regexp (plist-get project-plist :exclude))
590 (index-p (plist-get project-plist :auto-index))
591 (index-filename (or (plist-get project-plist :index-filename)
592 "index.org"))
593 (index-function (or (plist-get project-plist :index-function)
594 'org-publish-org-index))
595 (preparation-function (plist-get project-plist :preparation-function))
596 (completion-function (plist-get project-plist :completion-function))
597 (files (org-publish-get-base-files project exclude-regexp)) file)
598 (when preparation-function (funcall preparation-function))
599 (if index-p (funcall index-function project index-filename))
600 (while (setq file (pop files))
601 (org-publish-file file project))
602 (when completion-function (funcall completion-function))))
603 (org-publish-expand-projects projects)))
605 (defun org-publish-org-index (project &optional index-filename)
606 "Create an index of pages in set defined by PROJECT.
607 Optionally set the filename of the index with INDEX-FILENAME.
608 Default for INDEX-FILENAME is 'index.org'."
609 (let* ((project-plist (cdr project))
610 (dir (file-name-as-directory
611 (plist-get project-plist :base-directory)))
612 (localdir (file-name-directory dir))
613 (indent-str (make-string 2 ?\ ))
614 (exclude-regexp (plist-get project-plist :exclude))
615 (files (nreverse (org-publish-get-base-files project exclude-regexp)))
616 (index-filename (concat dir (or index-filename "index.org")))
617 (index-title (or (plist-get project-plist :index-title)
618 (concat "Index for project " (car project))))
619 (index-style (or (plist-get project-plist :index-style)
620 'tree))
621 (index-buffer (find-buffer-visiting index-filename))
622 (ifn (file-name-nondirectory index-filename))
623 file)
624 ;; if buffer is already open, kill it to prevent error message
625 (if index-buffer
626 (kill-buffer index-buffer))
627 (with-temp-buffer
628 (insert (concat index-title "\n\n"))
629 (while (setq file (pop files))
630 (let ((fn (file-name-nondirectory file))
631 (link (file-relative-name file dir))
632 (oldlocal localdir))
633 ;; index shouldn't index itself
634 (unless (string= fn ifn)
635 (if (eq index-style 'list)
636 (message "Generating list-style index for %s" index-title)
637 (message "Generating tree-style index for %s" index-title)
638 (setq localdir (concat (file-name-as-directory dir)
639 (file-name-directory link)))
640 (unless (string= localdir oldlocal)
641 (if (string= localdir dir)
642 (setq indent-str (make-string 2 ?\ ))
643 (let ((subdirs
644 (split-string
645 (directory-file-name
646 (file-name-directory
647 (file-relative-name localdir dir))) "/"))
648 (subdir ""))
649 (setq indent-str (make-string 2 ?\ ))
650 (dolist (d subdirs)
651 (setq subdir (concat subdir d "/"))
652 (insert (concat indent-str " + [[file:"
653 subdir "][" d "/]]\n"))
654 (setq indent-str (make-string
655 (+ (length indent-str) 2) ?\ )))))))
656 ;; This is common to 'flat and 'tree
657 (insert (concat indent-str " + [[file:" link "]["
658 (org-publish-find-title file)
659 "]]\n"))
661 (write-file index-filename)
662 (kill-buffer (current-buffer)))))
664 (defun org-publish-find-title (file)
665 "Find the title of file in project."
666 (save-excursion
667 (set-buffer (find-file-noselect file))
668 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
669 (org-infile-export-plist))))
670 (or (plist-get opt-plist :title)
671 (and (not
672 (plist-get opt-plist :skip-before-1st-heading))
673 (org-export-grab-title-from-buffer))
674 (file-name-nondirectory (file-name-sans-extension file))))))
677 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
678 ;;; Interactive publishing functions
680 ;;;###autoload
681 (defalias 'org-publish-project 'org-publish)
683 ;;;###autoload
684 (defun org-publish (project &optional force)
685 "Publish PROJECT."
686 (interactive "P")
687 (setq org-publish-initial-buffer (current-buffer))
688 (save-window-excursion
689 (let* ((force current-prefix-arg)
690 (org-publish-use-timestamps-flag
691 (if force nil org-publish-use-timestamps-flag)))
692 (org-publish-projects
693 (list (or project
694 (assoc (completing-read
695 "Publish project: "
696 org-publish-project-alist nil t)
697 org-publish-project-alist)))))))
699 ;;;###autoload
700 (defun org-publish-all (&optional force)
701 "Publish all projects.
702 With prefix argument, force publish all files."
703 (interactive "P")
704 (org-publish-initialize-files-alist)
705 (save-window-excursion
706 (let ((org-publish-use-timestamps-flag
707 (if force nil org-publish-use-timestamps-flag)))
708 (org-publish-projects org-publish-project-alist))))
710 ;;;###autoload
711 (defun org-publish-current-file (&optional force)
712 "Publish the current file.
713 With prefix argument, force publish the file."
714 (interactive "P")
715 (org-publish-initialize-files-alist)
716 (save-window-excursion
717 (let ((org-publish-use-timestamps-flag
718 (if force nil org-publish-use-timestamps-flag)))
719 (org-publish-file (buffer-file-name)))))
721 ;;;###autoload
722 (defun org-publish-current-project (&optional force)
723 "Publish the project associated with the current file.
724 With a prefix argument, force publishing of all files in
725 the project."
726 (interactive "P")
727 (org-publish-initialize-files-alist)
728 (save-window-excursion
729 (let ((project (org-publish-get-project-from-filename (buffer-file-name)))
730 (org-publish-use-timestamps-flag
731 (if force nil org-publish-use-timestamps-flag)))
732 (if (not project)
733 (error "File %s is not part of any known project" (buffer-file-name)))
734 (org-publish project))))
736 (provide 'org-publish)
739 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
741 ;;; org-publish.el ends here