Allow project components to have components.
[org-mode.git] / lisp / org-publish.el
blob98b6c6b4967e2c9a518fd3577fe4320790696825
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.06b
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 ;; Requires at least version 4.27 of org.el
28 ;; This program allow configurable publishing of related sets of
29 ;; Org-mode files as a complete website.
31 ;; org-publish.el can do the following:
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
40 ;; Special thanks to the org-mode maintainer Carsten Dominik for his
41 ;; ideas, enthusiasm, and cooperation.
43 ;;; Installation:
45 ;; Put org-publish.el in your load path, byte-compile it, and then add
46 ;; the following lines to your emacs initialization file:
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)
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.
57 ;;; Usage:
59 ;; The program's main configuration variable is
60 ;; `org-publish-project-alist'. See below for example configurations
61 ;; with commentary.
63 ;; The main interactive functions are:
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
70 ;;;; Simple example configuration:
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\">")))
82 ;;;; More complex example configuration:
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)
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.
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"))))
121 ;; For more information, see the documentation for the variable
122 ;; `org-publish-project-alist'.
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.
128 ;;; List of user-visible changes since version 1.27
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)
148 ;;; Code:
150 (eval-when-compile
151 (require 'cl))
152 (require 'org)
153 (require 'org-exp)
155 (eval-and-compile
156 (unless (fboundp 'declare-function)
157 (defmacro declare-function (fn file &optional arglist fileonly))))
159 (defgroup org-publish nil
160 "Options for publishing a set of Org-mode and related files."
161 :tag "Org Publishing"
162 :group 'org)
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:
170 (:property value :property value ... )
174 (:components (\"project-1\" \"project-2\" ...))
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.
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.
187 Most properties are optional, but some should always be set:
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
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.
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.
203 When both :include and :exclude properties are given values, the
204 exclusion step happens first.
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.
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.
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.
222 :preparation-function Function to be called before publishing
223 this project.
224 :completion-function Function to be called after publishing
225 this project.
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.
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'
255 The following properties may be used to control publishing of an
256 index of files or summary page for a given project.
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)
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)
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)
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)
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)
300 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
301 ;;; Timestamp-related functions
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))))
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)
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))
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))))
346 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
347 ;;; Mapping files to project names
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:
353 (file-name . project-name)")
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.")
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))))
368 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
369 ;;; Compatibility aliases
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.
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))
388 (declare-function org-publish-delete-dups "org-publish" (list))
390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
391 ;;; Getting project information out of org-publish-project-alist
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))
410 (defun org-publish-expand-projects (projects-alist)
411 "Expand projects in PROJECTS-ALIST.
412 This splices all the components into the list."
413 (let ((rest projects-alist) rtn p c)
414 (while (setq p (pop rest))
415 (if (setq components (plist-get (cdr p) :components))
416 (setq rest (append
417 (mapcar (lambda (x) (assoc x org-publish-project-alist))
418 components)
419 rest))
420 (push p rtn)))
421 (nreverse (org-publish-delete-dups (delq nil rtn)))))
423 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
424 "Set `org-publish-temp-files' with files from BASE-DIR directory.
425 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
426 non-nil, restrict this list to the files matching the regexp
427 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
428 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
429 matching the regexp SKIP-DIR when recursiing through BASE-DIR."
430 (mapc (lambda (f)
431 (let ((fd-p (car (file-attributes f)))
432 (fnd (file-name-nondirectory f)))
433 (if (and fd-p recurse
434 (not (string-match "^\\.+$" fnd))
435 (if skip-dir (not (string-match skip-dir fnd)) t))
436 (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
437 (unless (or fd-p ;; this is a directory
438 (and skip-file (string-match skip-file fnd))
439 (not (string-match match fnd)))
440 (pushnew f org-publish-temp-files)))))
441 (directory-files base-dir t (unless recurse match))))
443 (defun org-publish-get-base-files (project &optional exclude-regexp)
444 "Return a list of all files in PROJECT.
445 If EXCLUDE-REGEXP is set, this will be used to filter out
446 matching filenames."
447 (let* ((project-plist (cdr project))
448 (base-dir (file-name-as-directory
449 (plist-get project-plist :base-directory)))
450 (include-list (plist-get project-plist :include))
451 (recurse (plist-get project-plist :recursive))
452 (extension (or (plist-get project-plist :base-extension) "org"))
453 (match (concat "^[^\\.].*\\.\\(" extension "\\)$")))
454 (setq org-publish-temp-files nil)
455 (org-publish-get-base-files-1 base-dir recurse match
456 ;; FIXME distinguish exclude regexp
457 ;; for skip-file and skip-dir?
458 exclude-regexp exclude-regexp)
459 (mapc (lambda (f)
460 (pushnew
461 (expand-file-name (concat base-dir f))
462 org-publish-temp-files))
463 include-list)
464 org-publish-temp-files))
466 (defun org-publish-get-project-from-filename (filename)
467 "Return the project FILENAME belongs."
468 (let* ((project-name (cdr (assoc (expand-file-name filename)
469 org-publish-files-alist))))
470 (assoc project-name org-publish-project-alist)))
472 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
473 ;;; Pluggable publishing back-end functions
475 (defun org-publish-org-to (format plist filename pub-dir)
476 "Publish an org file to FORMAT.
477 PLIST is the property list for the given project.
478 FILENAME is the filename of the org file to be published.
479 PUB-DIR is the publishing directory."
480 (require 'org)
481 (unless (file-exists-p pub-dir)
482 (make-directory pub-dir t))
483 (let* ((visiting (find-buffer-visiting filename))
484 (plist (cons :buffer-will-be-killed (cons t plist)))
485 (init-buf (or visiting (find-file filename)))
486 (init-point (point))
487 (init-buf-string (buffer-string)) export-buf)
488 ;; run hooks before exporting
489 (run-hooks 'org-publish-before-export-hook)
490 ;; export the possibly modified buffer
491 (setq export-buf
492 (funcall (intern (concat "org-export-as-" format))
493 (plist-get plist :headline-levels)
494 nil plist nil nil pub-dir))
495 (set-buffer export-buf)
496 ;; run hooks after export and save export
497 (and (run-hooks 'org-publish-after-export-hook)
498 (if (buffer-modified-p) (save-buffer)))
499 (kill-buffer export-buf)
500 ;; maybe restore buffer's content
501 (set-buffer init-buf)
502 (when (buffer-modified-p init-buf)
503 (erase-buffer)
504 (insert init-buf-string)
505 (save-buffer)
506 (goto-char init-point))
507 (unless visiting
508 (kill-buffer init-buf))))
510 (defun org-publish-org-to-latex (plist filename pub-dir)
511 "Publish an org file to LaTeX.
512 See `org-publish-org-to' to the list of arguments."
513 (org-publish-org-to "latex" plist filename pub-dir))
515 (defun org-publish-org-to-html (plist filename pub-dir)
516 "Publish an org file to HTML.
517 See `org-publish-org-to' to the list of arguments."
518 (org-publish-org-to "html" plist filename pub-dir))
520 (defun org-publish-attachment (plist filename pub-dir)
521 "Publish a file with no transformation of any kind.
522 See `org-publish-org-to' to the list of arguments."
523 ;; make sure eshell/cp code is loaded
524 (eval-and-compile
525 (require 'eshell)
526 (require 'esh-maint)
527 (require 'em-unix))
528 (unless (file-directory-p pub-dir)
529 (make-directory pub-dir t))
530 (eshell/cp filename pub-dir))
532 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
533 ;;; Publishing files, sets of files, and indices
535 (defun org-publish-file (filename &optional project)
536 "Publish file FILENAME from PROJECT."
537 (when (org-publish-needed-p filename)
538 (let* ((project
539 (or project
540 (or (org-publish-get-project-from-filename filename)
541 (if (y-or-n-p
542 (format "%s is not in a project. Re-read the list of projects files? "
543 (abbreviate-file-name filename)))
544 ;; If requested, re-initialize the list of projects files
545 (progn (org-publish-initialize-files-alist t)
546 (or (org-publish-get-project-from-filename filename)
547 (error "File %s not part of any known project"
548 (abbreviate-file-name filename))))
549 (error "Can't publish file outside of a project")))))
550 (project-plist (cdr project))
551 (ftname (file-truename filename))
552 (publishing-function
553 (or (plist-get project-plist :publishing-function)
554 'org-publish-org-to-html))
555 (base-dir (file-name-as-directory
556 (file-truename (plist-get project-plist :base-directory))))
557 (pub-dir (file-name-as-directory
558 (file-truename (plist-get project-plist :publishing-directory))))
559 tmp-pub-dir)
560 (setq tmp-pub-dir
561 (file-name-directory
562 (concat pub-dir
563 (and (string-match (regexp-quote base-dir) ftname)
564 (substring ftname (match-end 0))))))
565 (if (listp publishing-function)
566 ;; allow chain of publishing functions
567 (mapc (lambda (f)
568 (funcall f project-plist filename tmp-pub-dir))
569 publishing-function)
570 (funcall publishing-function project-plist filename tmp-pub-dir)))
571 (org-publish-update-timestamp filename)))
573 (defun org-publish-projects (projects)
574 "Publish all files belonging to the PROJECTS alist.
575 If :auto-index is set, publish the index too."
576 (mapc
577 (lambda (project)
578 (let*
579 ((project-plist (cdr project))
580 (exclude-regexp (plist-get project-plist :exclude))
581 (index-p (plist-get project-plist :auto-index))
582 (index-filename (or (plist-get project-plist :index-filename)
583 "index.org"))
584 (index-function (or (plist-get project-plist :index-function)
585 'org-publish-org-index))
586 (preparation-function (plist-get project-plist :preparation-function))
587 (completion-function (plist-get project-plist :completion-function))
588 (files (org-publish-get-base-files project exclude-regexp)) file)
589 (when preparation-function (funcall preparation-function))
590 (if index-p (funcall index-function project index-filename))
591 (while (setq file (pop files))
592 (org-publish-file file project))
593 (when completion-function (funcall completion-function))))
594 (org-publish-expand-projects projects)))
596 (defun org-publish-org-index (project &optional index-filename)
597 "Create an index of pages in set defined by PROJECT.
598 Optionally set the filename of the index with INDEX-FILENAME.
599 Default for INDEX-FILENAME is 'index.org'."
600 (let* ((project-plist (cdr project))
601 (dir (file-name-as-directory
602 (plist-get project-plist :base-directory)))
603 (localdir (file-name-directory dir))
604 (indent-str (make-string 2 ?\ ))
605 (exclude-regexp (plist-get project-plist :exclude))
606 (files (nreverse (org-publish-get-base-files project exclude-regexp)))
607 (index-filename (concat dir (or index-filename "index.org")))
608 (index-title (or (plist-get project-plist :index-title)
609 (concat "Index for project " (car project))))
610 (index-style (or (plist-get project-plist :index-style)
611 'tree))
612 (index-buffer (find-buffer-visiting index-filename))
613 (ifn (file-name-nondirectory index-filename))
614 file)
615 ;; if buffer is already open, kill it to prevent error message
616 (if index-buffer
617 (kill-buffer index-buffer))
618 (with-temp-buffer
619 (insert (concat index-title "\n\n"))
620 (while (setq file (pop files))
621 (let ((fn (file-name-nondirectory file))
622 (link (file-relative-name file dir))
623 (oldlocal localdir))
624 ;; index shouldn't index itself
625 (unless (string= fn ifn)
626 (if (eq index-style 'list)
627 (message "Generating list-style index for %s" index-title)
628 (message "Generating tree-style index for %s" index-title)
629 (setq localdir (concat (file-name-as-directory dir)
630 (file-name-directory link)))
631 (unless (string= localdir oldlocal)
632 (if (string= localdir dir)
633 (setq indent-str (make-string 2 ?\ ))
634 (let ((subdirs
635 (split-string
636 (directory-file-name
637 (file-name-directory
638 (file-relative-name localdir dir))) "/"))
639 (subdir ""))
640 (setq indent-str (make-string 2 ?\ ))
641 (dolist (d subdirs)
642 (setq subdir (concat subdir d "/"))
643 (insert (concat indent-str " + [[file:"
644 subdir "][" d "/]]\n"))
645 (setq indent-str (make-string
646 (+ (length indent-str) 2) ?\ )))))))
647 ;; This is common to 'flat and 'tree
648 (insert (concat indent-str " + [[file:" link "]["
649 (org-publish-find-title file)
650 "]]\n"))
652 (write-file index-filename)
653 (kill-buffer (current-buffer)))))
655 (defun org-publish-find-title (file)
656 "Find the title of file in project."
657 (save-excursion
658 (set-buffer (find-file-noselect file))
659 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
660 (org-infile-export-plist))))
661 (or (plist-get opt-plist :title)
662 (and (not
663 (plist-get opt-plist :skip-before-1st-heading))
664 (org-export-grab-title-from-buffer))
665 (file-name-nondirectory (file-name-sans-extension file))))))
668 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
669 ;;; Interactive publishing functions
671 (defalias 'org-publish-project 'org-publish)
673 ;;;###autoload
674 (defun org-publish (project &optional force)
675 "Publish PROJECT."
676 (interactive "P")
677 (setq org-publish-initial-buffer (current-buffer))
678 (save-window-excursion
679 (let* ((force current-prefix-arg)
680 (org-publish-use-timestamps-flag
681 (if force nil org-publish-use-timestamps-flag)))
682 (org-publish-projects
683 (list (or project
684 (assoc (completing-read
685 "Publish project: "
686 org-publish-project-alist nil t)
687 org-publish-project-alist)))))))
689 ;;;###autoload
690 (defun org-publish-all (&optional force)
691 "Publish all projects.
692 With prefix argument, force publish all files."
693 (interactive "P")
694 (org-publish-initialize-files-alist)
695 (save-window-excursion
696 (let ((org-publish-use-timestamps-flag
697 (if force nil org-publish-use-timestamps-flag)))
698 (org-publish-projects org-publish-project-alist))))
700 ;;;###autoload
701 (defun org-publish-current-file (&optional force)
702 "Publish the current file.
703 With prefix argument, force publish the file."
704 (interactive "P")
705 (org-publish-initialize-files-alist)
706 (save-window-excursion
707 (let ((org-publish-use-timestamps-flag
708 (if force nil org-publish-use-timestamps-flag)))
709 (org-publish-file (buffer-file-name)))))
711 ;;;###autoload
712 (defun org-publish-current-project (&optional force)
713 "Publish the project associated with the current file.
714 With a prefix argument, force publishing of all files in
715 the project."
716 (interactive "P")
717 (org-publish-initialize-files-alist)
718 (save-window-excursion
719 (let ((project (org-publish-get-project-from-filename (buffer-file-name)))
720 (org-publish-use-timestamps-flag
721 (if force nil org-publish-use-timestamps-flag)))
722 (if (not project)
723 (error "File %s is not part of any known project" (buffer-file-name)))
724 (org-publish project))))
726 (provide 'org-publish)
729 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
731 ;;; org-publish.el ends here