Test for the existence of narrow-map.
[org-mode.git] / lisp / org-publish.el
blob4cb86df6fa221762f6043df96fc62ae39d3ab049
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.06pre01
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 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))))))
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)))))
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))))
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))
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)))
479 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
480 ;;; Pluggable publishing back-end functions
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))))
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))
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))
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))
538 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
539 ;;; Publishing files, sets of files, and indices
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 (publishing-function
558 (or (plist-get project-plist :publishing-function)
559 'org-publish-org-to-html))
560 (base-dir (file-name-as-directory
561 (file-truename (plist-get project-plist :base-directory))))
562 (pub-dir (file-name-as-directory
563 (file-truename (plist-get project-plist :publishing-directory))))
564 tmp-pub-dir)
565 (setq tmp-pub-dir
566 (file-name-directory
567 (concat pub-dir
568 (and (string-match (regexp-quote base-dir) filename)
569 (substring filename (match-end 0))))))
570 (if (listp publishing-function)
571 ;; allow chain of publishing functions
572 (mapc (lambda (f)
573 (funcall f project-plist filename tmp-pub-dir))
574 publishing-function)
575 (funcall publishing-function project-plist filename tmp-pub-dir)))
576 (org-publish-update-timestamp filename)))
578 (defun org-publish-projects (projects)
579 "Publish all files belonging to the PROJECTS alist.
580 If :auto-index is set, publish the index too."
581 (mapc
582 (lambda (project)
583 (let*
584 ((project-plist (cdr project))
585 (exclude-regexp (plist-get project-plist :exclude))
586 (index-p (plist-get project-plist :auto-index))
587 (index-filename (or (plist-get project-plist :index-filename)
588 "index.org"))
589 (index-function (or (plist-get project-plist :index-function)
590 'org-publish-org-index))
591 (preparation-function (plist-get project-plist :preparation-function))
592 (completion-function (plist-get project-plist :completion-function))
593 (files (org-publish-get-base-files project exclude-regexp)) file)
594 (when preparation-function (funcall preparation-function))
595 (if index-p (funcall index-function project index-filename))
596 (while (setq file (pop files))
597 (org-publish-file file project))
598 (when completion-function (funcall completion-function))))
599 (org-publish-expand-projects projects)))
601 (defun org-publish-org-index (project &optional index-filename)
602 "Create an index of pages in set defined by PROJECT.
603 Optionally set the filename of the index with INDEX-FILENAME.
604 Default for INDEX-FILENAME is 'index.org'."
605 (let* ((project-plist (cdr project))
606 (dir (file-name-as-directory
607 (plist-get project-plist :base-directory)))
608 (localdir (file-name-directory dir))
609 (indent-str (make-string 2 ?\ ))
610 (exclude-regexp (plist-get project-plist :exclude))
611 (files (nreverse (org-publish-get-base-files project exclude-regexp)))
612 (index-filename (concat dir (or index-filename "index.org")))
613 (index-title (or (plist-get project-plist :index-title)
614 (concat "Index for project " (car project))))
615 (index-style (or (plist-get project-plist :index-style)
616 'tree))
617 (index-buffer (find-buffer-visiting index-filename))
618 (ifn (file-name-nondirectory index-filename))
619 file)
620 ;; if buffer is already open, kill it to prevent error message
621 (if index-buffer
622 (kill-buffer index-buffer))
623 (with-temp-buffer
624 (insert (concat index-title "\n\n"))
625 (while (setq file (pop files))
626 (let ((fn (file-name-nondirectory file))
627 (link (file-relative-name file dir))
628 (oldlocal localdir))
629 ;; index shouldn't index itself
630 (unless (string= fn ifn)
631 (if (eq index-style 'list)
632 (message "Generating list-style index for %s" index-title)
633 (message "Generating tree-style index for %s" index-title)
634 (setq localdir (concat (file-name-as-directory dir)
635 (file-name-directory link)))
636 (unless (string= localdir oldlocal)
637 (if (string= localdir dir)
638 (setq indent-str (make-string 2 ?\ ))
639 (let ((subdirs
640 (split-string
641 (directory-file-name
642 (file-name-directory
643 (file-relative-name localdir dir))) "/"))
644 (subdir ""))
645 (setq indent-str (make-string 2 ?\ ))
646 (dolist (d subdirs)
647 (setq subdir (concat subdir d "/"))
648 (insert (concat indent-str " + [[file:"
649 subdir "][" d "/]]\n"))
650 (setq indent-str (make-string
651 (+ (length indent-str) 2) ?\ )))))))
652 ;; This is common to 'flat and 'tree
653 (insert (concat indent-str " + [[file:" link "]["
654 (org-publish-find-title file)
655 "]]\n"))
657 (write-file index-filename)
658 (kill-buffer (current-buffer)))))
660 (defun org-publish-find-title (file)
661 "Find the title of file in project."
662 (save-excursion
663 (set-buffer (find-file-noselect file))
664 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
665 (org-infile-export-plist))))
666 (or (plist-get opt-plist :title)
667 (and (not
668 (plist-get opt-plist :skip-before-1st-heading))
669 (org-export-grab-title-from-buffer))
670 (file-name-nondirectory (file-name-sans-extension file))))))
673 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
674 ;;; Interactive publishing functions
676 (defalias 'org-publish-project 'org-publish)
678 ;;;###autoload
679 (defun org-publish (project &optional force)
680 "Publish PROJECT."
681 (interactive "P")
682 (setq org-publish-initial-buffer (current-buffer))
683 (save-window-excursion
684 (let* ((force current-prefix-arg)
685 (org-publish-use-timestamps-flag
686 (if force nil org-publish-use-timestamps-flag)))
687 (org-publish-projects
688 (list (or project
689 (assoc (completing-read
690 "Publish project: "
691 org-publish-project-alist nil t)
692 org-publish-project-alist)))))))
694 ;;;###autoload
695 (defun org-publish-all (&optional force)
696 "Publish all projects.
697 With prefix argument, force publish all files."
698 (interactive "P")
699 (org-publish-initialize-files-alist)
700 (save-window-excursion
701 (let ((org-publish-use-timestamps-flag
702 (if force nil org-publish-use-timestamps-flag)))
703 (org-publish-projects org-publish-project-alist))))
705 ;;;###autoload
706 (defun org-publish-current-file (&optional force)
707 "Publish the current file.
708 With prefix argument, force publish the file."
709 (interactive "P")
710 (org-publish-initialize-files-alist)
711 (save-window-excursion
712 (let ((org-publish-use-timestamps-flag
713 (if force nil org-publish-use-timestamps-flag)))
714 (org-publish-file (buffer-file-name)))))
716 ;;;###autoload
717 (defun org-publish-current-project (&optional force)
718 "Publish the project associated with the current file.
719 With a prefix argument, force publishing of all files in
720 the project."
721 (interactive "P")
722 (org-publish-initialize-files-alist)
723 (save-window-excursion
724 (let ((project (org-publish-get-project-from-filename (buffer-file-name)))
725 (org-publish-use-timestamps-flag
726 (if force nil org-publish-use-timestamps-flag)))
727 (if (not project)
728 (error "File %s is not part of any known project" (buffer-file-name)))
729 (org-publish project))))
731 (provide 'org-publish)
734 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
736 ;;; org-publish.el ends here