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