Merge from emacs--rel--22
[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.06b
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 :index-style Can be `list' (index is just an itemized list
268 of the titles of the files involved) or
269 `tree' (the directory structure of the source
270 files is reflected in the index). Defaults to
271 `tree'."
272 :group 'org-publish
273 :type 'alist)
274
275 (defcustom org-publish-use-timestamps-flag t
276 "When non-nil, use timestamp checking to publish only changed files.
277 When nil, do no timestamp checking and always publish all files."
278 :group 'org-publish
279 :type 'boolean)
280
281 (defcustom org-publish-timestamp-directory "~/.org-timestamps/"
282 "Name of directory in which to store publishing timestamps."
283 :group 'org-publish
284 :type 'directory)
285
286 (defcustom org-publish-before-export-hook nil
287 "Hook run before export on the Org file.
288 If the functions in this hook modify the original Org buffer, the
289 modified buffer will be used for export, but the buffer will be
290 restored and saved back to its initial state after export."
291 :group 'org-publish
292 :type 'hook)
293
294 (defcustom org-publish-after-export-hook nil
295 "Hook run after export on the exported buffer.
296 If functions in this hook modify the buffer, it will be saved."
297 :group 'org-publish
298 :type 'hook)
299
300 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
301 ;;; Timestamp-related functions
302
303 (defun org-publish-timestamp-filename (filename)
304 "Return path to timestamp file for filename FILENAME."
305 (concat (file-name-as-directory org-publish-timestamp-directory)
306 "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
307
308 (defun org-publish-needed-p (filename)
309 "Return `t' if FILENAME should be published."
310 (let ((rtn
311 (if org-publish-use-timestamps-flag
312 (if (file-exists-p org-publish-timestamp-directory)
313 ;; first handle possible wrong timestamp directory
314 (if (not (file-directory-p org-publish-timestamp-directory))
315 (error "Org publish timestamp: %s is not a directory"
316 org-publish-timestamp-directory)
317 ;; there is a timestamp, check if FILENAME is newer
318 (file-newer-than-file-p
319 filename (org-publish-timestamp-filename filename)))
320 (make-directory org-publish-timestamp-directory)
321 t)
322 ;; don't use timestamps, always return t
323 t)))
324 (if rtn
325 (message "Publishing file %s" filename)
326 (message "Skipping unmodified file %s" filename))
327 rtn))
328
329 (defun org-publish-update-timestamp (filename)
330 "Update publishing timestamp for file FILENAME.
331 If there is no timestamp, create one."
332 (let ((timestamp-file (org-publish-timestamp-filename filename))
333 newly-created-timestamp)
334 (if (not (file-exists-p timestamp-file))
335 ;; create timestamp file if needed
336 (with-temp-buffer
337 (make-directory (file-name-directory timestamp-file) t)
338 (write-file timestamp-file)
339 (setq newly-created-timestamp t)))
340 ;; Emacs 21 doesn't have `set-file-times'
341 (if (and (fboundp 'set-file-times)
342 (not newly-created-timestamp))
343 (set-file-times timestamp-file)
344 (call-process "touch" nil 0 nil timestamp-file))))
345
346 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
347 ;;; Mapping files to project names
348
349 (defvar org-publish-files-alist nil
350 "Alist of files and their parent projects.
351 Each element of this alist is of the form:
352
353 (file-name . project-name)")
354
355 (defvar org-publish-initial-buffer nil
356 "The buffer `org-publish' has been called from.")
357 (defvar org-publish-temp-files nil
358 "Temporary list of files to be published.")
359
360 (defun org-publish-initialize-files-alist (&optional refresh)
361 "Set `org-publish-files-alist' if it is not set.
362 Also set it if the optional argument REFRESH is non-nil."
363 (interactive "P")
364 (when (or refresh (not org-publish-files-alist))
365 (setq org-publish-files-alist
366 (org-publish-get-files org-publish-project-alist))))
367
368 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
369 ;;; Compatibility aliases
370
371 ;; Delete-dups is not in Emacs <22
372 (if (fboundp 'delete-dups)
373 (defalias 'org-publish-delete-dups 'delete-dups)
374 (defun org-publish-delete-dups (list)
375 "Destructively remove `equal' duplicates from LIST.
376 Store the result in LIST and return it. LIST must be a proper list.
377 Of several `equal' occurrences of an element in LIST, the first
378 one is kept.
379
380 This is a compatibility function for Emacsen without `delete-dups'."
381 ;; Code from `subr.el' in Emacs 22:
382 (let ((tail list))
383 (while tail
384 (setcdr tail (delete (car tail) (cdr tail)))
385 (setq tail (cdr tail))))
386 list))
387
388 (declare-function org-publish-delete-dups "org-publish" (list))
389
390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
391 ;;; Getting project information out of org-publish-project-alist
392
393 (defun org-publish-get-files (projects-alist &optional no-exclusion)
394 "Return the list of all publishable files for PROJECTS-ALIST.
395 If NO-EXCLUSION is non-nil, don't exclude files."
396 (let (all-files)
397 ;; add all projects
398 (mapc
399 (lambda(p)
400 (let* ((exclude (plist-get (cdr p) :exclude))
401 (files (and p (org-publish-get-base-files p exclude))))
402 ;; add all files from this project
403 (mapc (lambda(f)
404 (add-to-list 'all-files
405 (cons (expand-file-name f) (car p))))
406 files)))
407 (org-publish-expand-projects projects-alist))
408 all-files))
409
410 (defun org-publish-expand-projects (projects-alist)
411 "Expand projects contained in PROJECTS-ALIST."
412 (let (without-component with-component)
413 (mapc (lambda(p)
414 (add-to-list
415 (if (plist-get (cdr p) :components)
416 'with-component 'without-component) p))
417 projects-alist)
418 (org-publish-delete-dups
419 (append without-component
420 (car (mapcar (lambda(p) (org-publish-expand-components p))
421 with-component))))))
422
423 (defun org-publish-expand-components (project)
424 "Expand PROJECT into an alist of its components."
425 (let* ((components (plist-get (cdr project) :components)))
426 (org-publish-delete-dups
427 (delq nil (mapcar (lambda(c) (assoc c org-publish-project-alist))
428 components)))))
429
430 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
431 "Set `org-publish-temp-files' with files from BASE-DIR directory.
432 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
433 non-nil, restrict this list to the files matching the regexp
434 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
435 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
436 matching the regexp SKIP-DIR when recursiing through BASE-DIR."
437 (mapc (lambda (f)
438 (let ((fd-p (car (file-attributes f)))
439 (fnd (file-name-nondirectory f)))
440 (if (and fd-p recurse
441 (not (string-match "^\\.+$" fnd))
442 (if skip-dir (not (string-match skip-dir fnd)) t))
443 (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
444 (unless (or fd-p ;; this is a directory
445 (and skip-file (string-match skip-file fnd))
446 (not (string-match match fnd)))
447 (pushnew f org-publish-temp-files)))))
448 (directory-files base-dir t (unless recurse match))))
449
450 (defun org-publish-get-base-files (project &optional exclude-regexp)
451 "Return a list of all files in PROJECT.
452 If EXCLUDE-REGEXP is set, this will be used to filter out
453 matching filenames."
454 (let* ((project-plist (cdr project))
455 (base-dir (file-name-as-directory
456 (plist-get project-plist :base-directory)))
457 (include-list (plist-get project-plist :include))
458 (recurse (plist-get project-plist :recursive))
459 (extension (or (plist-get project-plist :base-extension) "org"))
460 (match (concat "^[^\\.].*\\.\\(" extension "\\)$")))
461 (setq org-publish-temp-files nil)
462 (org-publish-get-base-files-1 base-dir recurse match
463 ;; FIXME distinguish exclude regexp
464 ;; for skip-file and skip-dir?
465 exclude-regexp exclude-regexp)
466 (mapc (lambda (f)
467 (pushnew
468 (expand-file-name (concat base-dir f))
469 org-publish-temp-files))
470 include-list)
471 org-publish-temp-files))
472
473 (defun org-publish-get-project-from-filename (filename)
474 "Return the project FILENAME belongs."
475 (let* ((project-name (cdr (assoc (expand-file-name filename)
476 org-publish-files-alist))))
477 (assoc project-name org-publish-project-alist)))
478
479 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
480 ;;; Pluggable publishing back-end functions
481
482 (defun org-publish-org-to (format plist filename pub-dir)
483 "Publish an org file to FORMAT.
484 PLIST is the property list for the given project.
485 FILENAME is the filename of the org file to be published.
486 PUB-DIR is the publishing directory."
487 (require 'org)
488 (unless (file-exists-p pub-dir)
489 (make-directory pub-dir t))
490 (find-file filename)
491 (let ((init-buf (current-buffer))
492 (init-point (point))
493 (init-buf-string (buffer-string)) export-buf)
494 ;; run hooks before exporting
495 (run-hooks 'org-publish-before-export-hook)
496 ;; export the possibly modified buffer
497 (setq export-buf
498 (funcall (intern (concat "org-export-as-" format))
499 (plist-get plist :headline-levels)
500 nil plist nil nil pub-dir))
501 (set-buffer export-buf)
502 ;; run hooks after export and save export
503 (and (run-hooks 'org-publish-after-export-hook)
504 (if (buffer-modified-p) (save-buffer)))
505 (kill-buffer export-buf)
506 ;; maybe restore buffer's content
507 (set-buffer init-buf)
508 (when (buffer-modified-p init-buf)
509 (erase-buffer)
510 (insert init-buf-string)
511 (save-buffer)
512 (goto-char init-point))
513 (unless (eq init-buf org-publish-initial-buffer)
514 (kill-buffer init-buf))))
515
516 (defun org-publish-org-to-latex (plist filename pub-dir)
517 "Publish an org file to LaTeX.
518 See `org-publish-org-to' to the list of arguments."
519 (org-publish-org-to "latex" plist filename pub-dir))
520
521 (defun org-publish-org-to-html (plist filename pub-dir)
522 "Publish an org file to HTML.
523 See `org-publish-org-to' to the list of arguments."
524 (org-publish-org-to "html" plist filename pub-dir))
525
526 (defun org-publish-attachment (plist filename pub-dir)
527 "Publish a file with no transformation of any kind.
528 See `org-publish-org-to' to the list of arguments."
529 ;; make sure eshell/cp code is loaded
530 (eval-and-compile
531 (require 'eshell)
532 (require 'esh-maint)
533 (require 'em-unix))
534 (unless (file-directory-p pub-dir)
535 (make-directory pub-dir t))
536 (eshell/cp filename pub-dir))
537
538 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
539 ;;; Publishing files, sets of files, and indices
540
541 (defun org-publish-file (filename &optional project)
542 "Publish file FILENAME from PROJECT."
543 (when (org-publish-needed-p filename)
544 (let* ((project
545 (or project
546 (or (org-publish-get-project-from-filename filename)
547 (if (y-or-n-p
548 (format "%s is not in a project. Re-read the list of projects files? "
549 (abbreviate-file-name filename)))
550 ;; If requested, re-initialize the list of projects files
551 (progn (org-publish-initialize-files-alist t)
552 (or (org-publish-get-project-from-filename filename)
553 (error "File %s not part of any known project"
554 (abbreviate-file-name filename))))
555 (error "Can't publish file outside of a project")))))
556 (project-plist (cdr project))
557 (ftname (file-truename filename))
558 (publishing-function
559 (or (plist-get project-plist :publishing-function)
560 'org-publish-org-to-html))
561 (base-dir (file-name-as-directory
562 (file-truename (plist-get project-plist :base-directory))))
563 (pub-dir (file-name-as-directory
564 (file-truename (plist-get project-plist :publishing-directory))))
565 tmp-pub-dir)
566 (setq tmp-pub-dir
567 (file-name-directory
568 (concat pub-dir
569 (and (string-match (regexp-quote base-dir) ftname)
570 (substring ftname (match-end 0))))))
571 (if (listp publishing-function)
572 ;; allow chain of publishing functions
573 (mapc (lambda (f)
574 (funcall f project-plist filename tmp-pub-dir))
575 publishing-function)
576 (funcall publishing-function project-plist filename tmp-pub-dir)))
577 (org-publish-update-timestamp filename)))
578
579 (defun org-publish-projects (projects)
580 "Publish all files belonging to the PROJECTS alist.
581 If :auto-index is set, publish the index too."
582 (mapc
583 (lambda (project)
584 (let*
585 ((project-plist (cdr project))
586 (exclude-regexp (plist-get project-plist :exclude))
587 (index-p (plist-get project-plist :auto-index))
588 (index-filename (or (plist-get project-plist :index-filename)
589 "index.org"))
590 (index-function (or (plist-get project-plist :index-function)
591 'org-publish-org-index))
592 (preparation-function (plist-get project-plist :preparation-function))
593 (completion-function (plist-get project-plist :completion-function))
594 (files (org-publish-get-base-files project exclude-regexp)) file)
595 (when preparation-function (funcall preparation-function))
596 (if index-p (funcall index-function project index-filename))
597 (while (setq file (pop files))
598 (org-publish-file file project))
599 (when completion-function (funcall completion-function))))
600 (org-publish-expand-projects projects)))
601
602 (defun org-publish-org-index (project &optional index-filename)
603 "Create an index of pages in set defined by PROJECT.
604 Optionally set the filename of the index with INDEX-FILENAME.
605 Default for INDEX-FILENAME is 'index.org'."
606 (let* ((project-plist (cdr project))
607 (dir (file-name-as-directory
608 (plist-get project-plist :base-directory)))
609 (localdir (file-name-directory dir))
610 (indent-str (make-string 2 ?\ ))
611 (exclude-regexp (plist-get project-plist :exclude))
612 (files (nreverse (org-publish-get-base-files project exclude-regexp)))
613 (index-filename (concat dir (or index-filename "index.org")))
614 (index-title (or (plist-get project-plist :index-title)
615 (concat "Index for project " (car project))))
616 (index-style (or (plist-get project-plist :index-style)
617 'tree))
618 (index-buffer (find-buffer-visiting index-filename))
619 (ifn (file-name-nondirectory index-filename))
620 file)
621 ;; if buffer is already open, kill it to prevent error message
622 (if index-buffer
623 (kill-buffer index-buffer))
624 (with-temp-buffer
625 (insert (concat index-title "\n\n"))
626 (while (setq file (pop files))
627 (let ((fn (file-name-nondirectory file))
628 (link (file-relative-name file dir))
629 (oldlocal localdir))
630 ;; index shouldn't index itself
631 (unless (string= fn ifn)
632 (if (eq index-style 'list)
633 (message "Generating list-style index for %s" index-title)
634 (message "Generating tree-style index for %s" index-title)
635 (setq localdir (concat (file-name-as-directory dir)
636 (file-name-directory link)))
637 (unless (string= localdir oldlocal)
638 (if (string= localdir dir)
639 (setq indent-str (make-string 2 ?\ ))
640 (let ((subdirs
641 (split-string
642 (directory-file-name
643 (file-name-directory
644 (file-relative-name localdir dir))) "/"))
645 (subdir ""))
646 (setq indent-str (make-string 2 ?\ ))
647 (dolist (d subdirs)
648 (setq subdir (concat subdir d "/"))
649 (insert (concat indent-str " + [[file:"
650 subdir "][" d "/]]\n"))
651 (setq indent-str (make-string
652 (+ (length indent-str) 2) ?\ )))))))
653 ;; This is common to 'flat and 'tree
654 (insert (concat indent-str " + [[file:" link "]["
655 (org-publish-find-title file)
656 "]]\n"))
657 )))
658 (write-file index-filename)
659 (kill-buffer (current-buffer)))))
660
661 (defun org-publish-find-title (file)
662 "Find the title of file in project."
663 (save-excursion
664 (set-buffer (find-file-noselect file))
665 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
666 (org-infile-export-plist))))
667 (or (plist-get opt-plist :title)
668 (and (not
669 (plist-get opt-plist :skip-before-1st-heading))
670 (org-export-grab-title-from-buffer))
671 (file-name-nondirectory (file-name-sans-extension file))))))
672
673
674 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
675 ;;; Interactive publishing functions
676
677 (defalias 'org-publish-project 'org-publish)
678
679 ;;;###autoload
680 (defun org-publish (project &optional force)
681 "Publish PROJECT."
682 (interactive "P")
683 (setq org-publish-initial-buffer (current-buffer))
684 (save-window-excursion
685 (let* ((force current-prefix-arg)
686 (org-publish-use-timestamps-flag
687 (if force nil org-publish-use-timestamps-flag)))
688 (org-publish-projects
689 (list (or project
690 (assoc (completing-read
691 "Publish project: "
692 org-publish-project-alist nil t)
693 org-publish-project-alist)))))))
694
695 ;;;###autoload
696 (defun org-publish-all (&optional force)
697 "Publish all projects.
698 With prefix argument, force publish all files."
699 (interactive "P")
700 (org-publish-initialize-files-alist)
701 (save-window-excursion
702 (let ((org-publish-use-timestamps-flag
703 (if force nil org-publish-use-timestamps-flag)))
704 (org-publish-projects org-publish-project-alist))))
705
706 ;;;###autoload
707 (defun org-publish-current-file (&optional force)
708 "Publish the current file.
709 With prefix argument, force publish the file."
710 (interactive "P")
711 (org-publish-initialize-files-alist)
712 (save-window-excursion
713 (let ((org-publish-use-timestamps-flag
714 (if force nil org-publish-use-timestamps-flag)))
715 (org-publish-file (buffer-file-name)))))
716
717 ;;;###autoload
718 (defun org-publish-current-project (&optional force)
719 "Publish the project associated with the current file.
720 With a prefix argument, force publishing of all files in
721 the project."
722 (interactive "P")
723 (org-publish-initialize-files-alist)
724 (save-window-excursion
725 (let ((project (org-publish-get-project-from-filename (buffer-file-name)))
726 (org-publish-use-timestamps-flag
727 (if force nil org-publish-use-timestamps-flag)))
728 (if (not project)
729 (error "File %s is not part of any known project" (buffer-file-name)))
730 (org-publish project))))
731
732 (provide 'org-publish)
733
734
735 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
736
737 ;;; org-publish.el ends here