Release 6.35g
[org-mode.git] / lisp / org-publish.el
blob85f7b3ffc0393fc7f89456eaeea0fc11e0ffb452
1 ;;; org-publish.el --- publish related org-mode files as a website
2 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010
3 ;; Free Software Foundation, Inc.
5 ;; Author: David O'Toole <dto@gnu.org>
6 ;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
7 ;; Keywords: hypermedia, outlines, wp
8 ;; Version: 6.35g
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This program allow configurable publishing of related sets of
28 ;; Org-mode files as a complete website.
30 ;; org-publish.el can do the following:
32 ;; + Publish all one's org-files to HTML or PDF
33 ;; + Upload HTML, images, attachments and other files to a web server
34 ;; + Exclude selected private pages from publishing
35 ;; + Publish a clickable sitemap of pages
36 ;; + Manage local timestamps for publishing only changed files
37 ;; + Accept plugin functions to extend range of publishable content
39 ;; Documentation for publishing is in the manual.
41 ;;; Code:
44 (defun org-publish-sanitize-plist (plist)
45 (mapcar (lambda (x)
46 (or (cdr (assq x '((:index-filename . :sitemap-filename)
47 (:index-title . :sitemap-title)
48 (:index-function . :sitemap-function)
49 (:index-style . :sitemap-style)
50 (:auto-index . :auto-sitemap))))
51 x))
52 plist))
54 (eval-when-compile
55 (require 'cl))
56 (require 'org)
57 (require 'org-exp)
59 (eval-and-compile
60 (unless (fboundp 'declare-function)
61 (defmacro declare-function (fn file &optional arglist fileonly))))
63 (defgroup org-publish nil
64 "Options for publishing a set of Org-mode and related files."
65 :tag "Org Publishing"
66 :group 'org)
68 (defcustom org-publish-project-alist nil
69 "Association list to control publishing behavior.
70 Each element of the alist is a publishing 'project.' The CAR of
71 each element is a string, uniquely identifying the project. The
72 CDR of each element is in one of the following forms:
74 (:property value :property value ... )
76 OR,
78 (:components (\"project-1\" \"project-2\" ...))
80 When the CDR of an element of org-publish-project-alist is in
81 this second form, the elements of the list after :components are
82 taken to be components of the project, which group together files
83 requiring different publishing options. When you publish such a
84 project with \\[org-publish], the components all publish.
86 When a property is given a value in org-publish-project-alist, its
87 setting overrides the value of the corresponding user variable
88 \(if any) during publishing. However, options set within a file
89 override everything.
91 Most properties are optional, but some should always be set:
93 :base-directory Directory containing publishing source files
94 :base-extension Extension (without the dot!) of source files.
95 This can be a regular expression.
96 :publishing-directory Directory (possibly remote) where output
97 files will be published
99 The :exclude property may be used to prevent certain files from
100 being published. Its value may be a string or regexp matching
101 file names you don't want to be published.
103 The :include property may be used to include extra files. Its
104 value may be a list of filenames to include. The filenames are
105 considered relative to the base directory.
107 When both :include and :exclude properties are given values, the
108 exclusion step happens first.
110 One special property controls which back-end function to use for
111 publishing files in the project. This can be used to extend the
112 set of file types publishable by org-publish, as well as the set
113 of output formats.
115 :publishing-function Function to publish file. The default is
116 `org-publish-org-to-html', but other
117 values are possible. May also be a
118 list of functions, in which case
119 each function in the list is invoked
120 in turn.
122 Another property allows you to insert code that prepares a
123 project for publishing. For example, you could call GNU Make on a
124 certain makefile, to ensure published files are built up to date.
126 :preparation-function Function to be called before publishing
127 this project. This may also be a list
128 of functions.
129 :completion-function Function to be called after publishing
130 this project. This may also be a list
131 of functions.
133 Some properties control details of the Org publishing process,
134 and are equivalent to the corresponding user variables listed in
135 the right column. See the documentation for those variables to
136 learn more about their use and default values.
138 :language `org-export-default-language'
139 :headline-levels `org-export-headline-levels'
140 :section-numbers `org-export-with-section-numbers'
141 :table-of-contents `org-export-with-toc'
142 :emphasize `org-export-with-emphasize'
143 :sub-superscript `org-export-with-sub-superscripts'
144 :TeX-macros `org-export-with-TeX-macros'
145 :fixed-width `org-export-with-fixed-width'
146 :tables `org-export-with-tables'
147 :table-auto-headline `org-export-highlight-first-table-line'
148 :style `org-export-html-style'
149 :convert-org-links `org-export-html-link-org-files-as-html'
150 :inline-images `org-export-html-inline-images'
151 :expand-quoted-html `org-export-html-expand'
152 :timestamp `org-export-html-with-timestamp'
153 :publishing-directory `org-export-publishing-directory'
154 :preamble `org-export-html-preamble'
155 :postamble `org-export-html-postamble'
156 :auto-preamble `org-export-html-auto-preamble'
157 :auto-postamble `org-export-html-auto-postamble'
158 :author `user-full-name'
159 :email `user-mail-address'
161 The following properties may be used to control publishing of a
162 sitemap of files or summary page for a given project.
164 :auto-sitemap Whether to publish a sitemap during
165 `org-publish-current-project' or `org-publish-all'.
166 :sitemap-filename Filename for output of sitemap. Defaults
167 to 'sitemap.org' (which becomes 'sitemap.html').
168 :sitemap-title Title of sitemap page. Defaults to name of file.
169 :sitemap-function Plugin function to use for generation of sitemap.
170 Defaults to `org-publish-org-sitemap', which
171 generates a plain list of links to all files
172 in the project.
173 :sitemap-style Can be `list' (sitemap is just an itemized list
174 of the titles of the files involved) or
175 `tree' (the directory structure of the source
176 files is reflected in the sitemap). Defaults to
177 `tree'."
178 :group 'org-publish
179 :type 'alist)
181 (defcustom org-publish-use-timestamps-flag t
182 "When non-nil, use timestamp checking to publish only changed files.
183 When nil, do no timestamp checking and always publish all files."
184 :group 'org-publish
185 :type 'boolean)
187 (defcustom org-publish-timestamp-directory (convert-standard-filename
188 "~/.org-timestamps/")
189 "Name of directory in which to store publishing timestamps."
190 :group 'org-publish
191 :type 'directory)
193 (defcustom org-publish-list-skipped-files t
194 "Non-nil means show message about files *not* published."
195 :group 'org-publish
196 :type 'boolean)
198 (defcustom org-publish-before-export-hook nil
199 "Hook run before export on the Org file.
200 The hook may modify the file in arbitrary ways before publishing happens.
201 The original version of the buffer will be restored after publishing."
202 :group 'org-publish
203 :type 'hook)
205 (defcustom org-publish-after-export-hook nil
206 "Hook run after export on the exported buffer.
207 Any changes made by this hook will be saved."
208 :group 'org-publish
209 :type 'hook)
211 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
212 ;;; Timestamp-related functions
214 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
215 "Return path to timestamp file for filename FILENAME."
216 (setq filename (concat filename "::" (or pub-dir "") "::"
217 (format "%s" (or pub-func ""))))
218 (concat (file-name-as-directory org-publish-timestamp-directory)
219 "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
221 (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir)
222 "Return `t' if FILENAME should be published in PUB-DIR using PUB-FUNC.
223 TRUE-PUB-DIR is there the file will truely end up. Currently we are not using
224 this - maybe it can eventually be used to check if the file is present at
225 the target location, and how old it is. Right ow we cannot do this, because
226 we do not know under what file name the file will be stored - the publishing
227 function can still decide about that independently."
228 (let ((rtn
229 (if org-publish-use-timestamps-flag
230 (if (file-exists-p org-publish-timestamp-directory)
231 ;; first handle possible wrong timestamp directory
232 (if (not (file-directory-p org-publish-timestamp-directory))
233 (error "Org publish timestamp: %s is not a directory"
234 org-publish-timestamp-directory)
235 ;; there is a timestamp, check if FILENAME is newer
236 (file-newer-than-file-p
237 filename (org-publish-timestamp-filename
238 filename pub-dir pub-func)))
239 (make-directory org-publish-timestamp-directory)
241 ;; don't use timestamps, always return t
242 t)))
243 (if rtn
244 (message "Publishing file %s using `%s'" filename pub-func)
245 (when org-publish-list-skipped-files
246 (message "Skipping unmodified file %s" filename)))
247 rtn))
249 (defun org-publish-update-timestamp (filename &optional pub-dir pub-func)
250 "Update publishing timestamp for file FILENAME.
251 If there is no timestamp, create one."
252 (let ((timestamp-file (org-publish-timestamp-filename
253 filename pub-dir pub-func))
254 newly-created-timestamp)
255 (if (not (file-exists-p timestamp-file))
256 ;; create timestamp file if needed
257 (with-temp-buffer
258 (make-directory (file-name-directory timestamp-file) t)
259 (write-file timestamp-file)
260 (setq newly-created-timestamp t)))
261 ;; Emacs 21 doesn't have `set-file-times'
262 (if (and (fboundp 'set-file-times)
263 (not newly-created-timestamp))
264 (set-file-times timestamp-file)
265 (call-process "touch" nil 0 nil (expand-file-name timestamp-file)))))
267 (defun org-publish-remove-all-timestamps ()
268 "Remove all files in the timstamp directory."
269 (let ((dir org-publish-timestamp-directory)
270 files)
271 (when (and (file-exists-p dir)
272 (file-directory-p dir))
273 (mapc 'delete-file (directory-files dir 'full "[^.]\\'")))))
276 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
277 ;;; Mapping files to project names
279 (defvar org-publish-files-alist nil
280 "Alist of files and their parent projects.
281 Each element of this alist is of the form:
283 (file-name . project-name)")
285 (defvar org-publish-initial-buffer nil
286 "The buffer `org-publish' has been called from.")
287 (defvar org-publish-temp-files nil
288 "Temporary list of files to be published.")
290 (defun org-publish-initialize-files-alist (&optional refresh)
291 "Set `org-publish-files-alist' if it is not set.
292 Also set it if the optional argument REFRESH is non-nil."
293 (interactive "P")
294 (when (or refresh (not org-publish-files-alist))
295 (setq org-publish-files-alist
296 (org-publish-get-files org-publish-project-alist))))
298 (defun org-publish-validate-link (link &optional directory)
299 "Check if LINK points to a file in the current project."
300 (assoc (expand-file-name link directory) org-publish-files-alist))
302 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
303 ;;; Compatibility aliases
305 ;; Delete-dups is not in Emacs <22
306 (if (fboundp 'delete-dups)
307 (defalias 'org-publish-delete-dups 'delete-dups)
308 (defun org-publish-delete-dups (list)
309 "Destructively remove `equal' duplicates from LIST.
310 Store the result in LIST and return it. LIST must be a proper list.
311 Of several `equal' occurrences of an element in LIST, the first
312 one is kept.
314 This is a compatibility function for Emacsen without `delete-dups'."
315 ;; Code from `subr.el' in Emacs 22:
316 (let ((tail list))
317 (while tail
318 (setcdr tail (delete (car tail) (cdr tail)))
319 (setq tail (cdr tail))))
320 list))
322 (declare-function org-publish-delete-dups "org-publish" (list))
323 (declare-function find-lisp-find-files "find-lisp" (directory regexp))
325 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
326 ;;; Getting project information out of org-publish-project-alist
328 (defun org-publish-get-files (projects-alist &optional no-exclusion)
329 "Return the list of all publishable files for PROJECTS-ALIST.
330 If NO-EXCLUSION is non-nil, don't exclude files."
331 (let (all-files)
332 ;; add all projects
333 (mapc
334 (lambda(p)
335 (let* ((exclude (plist-get (cdr p) :exclude))
336 (files (and p (org-publish-get-base-files p exclude))))
337 ;; add all files from this project
338 (mapc (lambda(f)
339 (add-to-list 'all-files
340 (cons (expand-file-name f) (car p))))
341 files)))
342 (org-publish-expand-projects projects-alist))
343 all-files))
345 (defun org-publish-expand-projects (projects-alist)
346 "Expand projects in PROJECTS-ALIST.
347 This splices all the components into the list."
348 (let ((rest projects-alist) rtn p components)
349 (while (setq p (pop rest))
350 (if (setq components (plist-get (cdr p) :components))
351 (setq rest (append
352 (mapcar (lambda (x) (assoc x org-publish-project-alist))
353 components)
354 rest))
355 (push p rtn)))
356 (nreverse (org-publish-delete-dups (delq nil rtn)))))
358 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
359 "Set `org-publish-temp-files' with files from BASE-DIR directory.
360 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
361 non-nil, restrict this list to the files matching the regexp
362 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
363 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
364 matching the regexp SKIP-DIR when recursing through BASE-DIR."
365 (mapc (lambda (f)
366 (let ((fd-p (file-directory-p f))
367 (fnd (file-name-nondirectory f)))
368 (if (and fd-p recurse
369 (not (string-match "^\\.+$" fnd))
370 (if skip-dir (not (string-match skip-dir fnd)) t))
371 (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
372 (unless (or fd-p ;; this is a directory
373 (and skip-file (string-match skip-file fnd))
374 (not (file-exists-p (file-truename f)))
375 (not (string-match match fnd)))
376 (pushnew f org-publish-temp-files)))))
377 (directory-files base-dir t (unless recurse match))))
379 (defun org-publish-get-base-files (project &optional exclude-regexp)
380 "Return a list of all files in PROJECT.
381 If EXCLUDE-REGEXP is set, this will be used to filter out
382 matching filenames."
383 (let* ((project-plist (cdr project))
384 (base-dir (file-name-as-directory
385 (plist-get project-plist :base-directory)))
386 (include-list (plist-get project-plist :include))
387 (recurse (plist-get project-plist :recursive))
388 (extension (or (plist-get project-plist :base-extension) "org"))
389 (match (if (eq extension 'any)
390 "^[^\\.]"
391 (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
392 (setq org-publish-temp-files nil)
393 (org-publish-get-base-files-1 base-dir recurse match
394 ;; FIXME distinguish exclude regexp
395 ;; for skip-file and skip-dir?
396 exclude-regexp exclude-regexp)
397 (mapc (lambda (f)
398 (pushnew
399 (expand-file-name (concat base-dir f))
400 org-publish-temp-files))
401 include-list)
402 org-publish-temp-files))
404 (defun org-publish-get-project-from-filename (filename &optional up)
405 "Return the project FILENAME belongs."
406 (let* ((project-name (cdr (assoc (expand-file-name filename)
407 org-publish-files-alist))))
408 (when up
409 (dolist (prj org-publish-project-alist)
410 (if (member project-name (plist-get (cdr prj) :components))
411 (setq project-name (car prj)))))
412 (assoc project-name org-publish-project-alist)))
414 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
415 ;;; Pluggable publishing back-end functions
417 (defun org-publish-org-to (format plist filename pub-dir)
418 "Publish an org file to FORMAT.
419 PLIST is the property list for the given project.
420 FILENAME is the filename of the org file to be published.
421 PUB-DIR is the publishing directory."
422 (require 'org)
423 (unless (file-exists-p pub-dir)
424 (make-directory pub-dir t))
425 (let ((visiting (find-buffer-visiting filename)))
426 (save-excursion
427 (switch-to-buffer (or visiting (find-file filename)))
428 (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
429 (init-buf (current-buffer))
430 (init-point (point))
431 (init-buf-string (buffer-string))
432 export-buf-or-file)
433 ;; run hooks before exporting
434 (run-hooks 'org-publish-before-export-hook)
435 ;; export the possibly modified buffer
436 (setq export-buf-or-file
437 (funcall (intern (concat "org-export-as-" format))
438 (plist-get plist :headline-levels)
439 nil plist nil nil pub-dir))
440 (when (and (bufferp export-buf-or-file)
441 (buffer-live-p export-buf-or-file))
442 (set-buffer export-buf-or-file)
443 ;; run hooks after export and save export
444 (progn (run-hooks 'org-publish-after-export-hook)
445 (if (buffer-modified-p) (save-buffer)))
446 (kill-buffer export-buf-or-file))
447 ;; maybe restore buffer's content
448 (set-buffer init-buf)
449 (when (buffer-modified-p init-buf)
450 (erase-buffer)
451 (insert init-buf-string)
452 (save-buffer)
453 (goto-char init-point))
454 (unless visiting
455 (kill-buffer init-buf))))))
457 (defmacro org-publish-with-aux-preprocess-maybe (&rest body)
458 "Execute BODY with a modified hook to preprocess for index."
459 `(let ((org-export-preprocess-after-headline-targets-hook
460 (if (plist-get project-plist :makeindex)
461 (cons 'org-publish-aux-preprocess
462 org-export-preprocess-after-headline-targets-hook)
463 org-export-preprocess-after-headline-targets-hook)))
464 ,@body))
466 (defvar project-plist)
467 (defun org-publish-org-to-latex (plist filename pub-dir)
468 "Publish an org file to LaTeX.
469 See `org-publish-org-to' to the list of arguments."
470 (org-publish-with-aux-preprocess-maybe
471 (org-publish-org-to "latex" plist filename pub-dir)))
473 (defun org-publish-org-to-pdf (plist filename pub-dir)
474 "Publish an org file to PDF (via LaTeX).
475 See `org-publish-org-to' to the list of arguments."
476 (org-publish-with-aux-preprocess-maybe
477 (org-publish-org-to "pdf" plist filename pub-dir)))
479 (defun org-publish-org-to-html (plist filename pub-dir)
480 "Publish an org file to HTML.
481 See `org-publish-org-to' to the list of arguments."
482 (org-publish-with-aux-preprocess-maybe
483 (org-publish-org-to "html" plist filename pub-dir)))
485 (defun org-publish-org-to-org (plist filename pub-dir)
486 "Publish an org file to HTML.
487 See `org-publish-org-to' to the list of arguments."
488 (org-publish-org-to "org" plist filename pub-dir))
490 (defun org-publish-attachment (plist filename pub-dir)
491 "Publish a file with no transformation of any kind.
492 See `org-publish-org-to' to the list of arguments."
493 ;; make sure eshell/cp code is loaded
494 (unless (file-directory-p pub-dir)
495 (make-directory pub-dir t))
496 (or (equal (expand-file-name (file-name-directory filename))
497 (file-name-as-directory (expand-file-name pub-dir)))
498 (copy-file filename
499 (expand-file-name (file-name-nondirectory filename) pub-dir)
500 t)))
502 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
503 ;;; Publishing files, sets of files, and indices
505 (defun org-publish-file (filename &optional project)
506 "Publish file FILENAME from PROJECT."
507 (let* ((project
508 (or project
509 (or (org-publish-get-project-from-filename filename)
510 (if (y-or-n-p
511 (format "%s is not in a project. Re-read the list of projects files? "
512 (abbreviate-file-name filename)))
513 ;; If requested, re-initialize the list of projects files
514 (progn (org-publish-initialize-files-alist t)
515 (or (org-publish-get-project-from-filename filename)
516 (error "File %s not part of any known project"
517 (abbreviate-file-name filename))))
518 (error "Can't publish file outside of a project")))))
519 (project-plist (cdr project))
520 (ftname (file-truename filename))
521 (publishing-function
522 (or (plist-get project-plist :publishing-function)
523 'org-publish-org-to-html))
524 (base-dir (file-name-as-directory
525 (file-truename (plist-get project-plist :base-directory))))
526 (pub-dir (file-name-as-directory
527 (file-truename (plist-get project-plist :publishing-directory))))
528 tmp-pub-dir)
529 (setq tmp-pub-dir
530 (file-name-directory
531 (concat pub-dir
532 (and (string-match (regexp-quote base-dir) ftname)
533 (substring ftname (match-end 0))))))
534 (if (listp publishing-function)
535 ;; allow chain of publishing functions
536 (mapc (lambda (f)
537 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir)
538 (funcall f project-plist filename tmp-pub-dir)
539 (org-publish-update-timestamp filename pub-dir f)))
540 publishing-function)
541 (when (org-publish-needed-p filename pub-dir publishing-function
542 tmp-pub-dir)
543 (funcall publishing-function project-plist filename tmp-pub-dir)
544 (org-publish-update-timestamp
545 filename pub-dir publishing-function)))))
547 (defun org-publish-projects (projects)
548 "Publish all files belonging to the PROJECTS alist.
549 If :auto-sitemap is set, publish the sitemap too.
550 If :makeindex is set, also produce a file theindex.org."
551 (mapc
552 (lambda (project)
553 (let*
554 ((project-plist (cdr project))
555 (exclude-regexp (plist-get project-plist :exclude))
556 (sitemap-p (plist-get project-plist :auto-sitemap))
557 (sitemap-filename (or (plist-get project-plist :sitemap-filename)
558 "sitemap.org"))
559 (sitemap-function (or (plist-get project-plist :sitemap-function)
560 'org-publish-org-sitemap))
561 (preparation-function (plist-get project-plist :preparation-function))
562 (completion-function (plist-get project-plist :completion-function))
563 (files (org-publish-get-base-files project exclude-regexp)) file)
564 (when preparation-function (run-hooks 'preparation-function))
565 (if sitemap-p (funcall sitemap-function project sitemap-filename))
566 (while (setq file (pop files))
567 (org-publish-file file project))
568 (when (plist-get project-plist :makeindex)
569 (org-publish-index-generate-theindex.inc
570 (plist-get project-plist :base-directory))
571 (org-publish-file (expand-file-name
572 "theindex.org"
573 (plist-get project-plist :base-directory))
574 project))
575 (when completion-function (run-hooks 'completion-function))))
576 (org-publish-expand-projects projects)))
578 (defun org-publish-org-sitemap (project &optional sitemap-filename)
579 "Create an sitemap of pages in set defined by PROJECT.
580 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
581 Default for SITEMAP-FILENAME is 'sitemap.org'."
582 (let* ((project-plist (cdr project))
583 (dir (file-name-as-directory
584 (plist-get project-plist :base-directory)))
585 (localdir (file-name-directory dir))
586 (indent-str (make-string 2 ?\ ))
587 (exclude-regexp (plist-get project-plist :exclude))
588 (files (nreverse (org-publish-get-base-files project exclude-regexp)))
589 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
590 (sitemap-title (or (plist-get project-plist :sitemap-title)
591 (concat "Sitemap for project " (car project))))
592 (sitemap-style (or (plist-get project-plist :sitemap-style)
593 'tree))
594 (visiting (find-buffer-visiting sitemap-filename))
595 (ifn (file-name-nondirectory sitemap-filename))
596 file sitemap-buffer)
597 (with-current-buffer (setq sitemap-buffer
598 (or visiting (find-file sitemap-filename)))
599 (erase-buffer)
600 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
601 (while (setq file (pop files))
602 (let ((fn (file-name-nondirectory file))
603 (link (file-relative-name file dir))
604 (oldlocal localdir))
605 ;; sitemap shouldn't list itself
606 (unless (equal (file-truename sitemap-filename)
607 (file-truename file))
608 (if (eq sitemap-style 'list)
609 (message "Generating list-style sitemap for %s" sitemap-title)
610 (message "Generating tree-style sitemap for %s" sitemap-title)
611 (setq localdir (concat (file-name-as-directory dir)
612 (file-name-directory link)))
613 (unless (string= localdir oldlocal)
614 (if (string= localdir dir)
615 (setq indent-str (make-string 2 ?\ ))
616 (let ((subdirs
617 (split-string
618 (directory-file-name
619 (file-name-directory
620 (file-relative-name localdir dir))) "/"))
621 (subdir "")
622 (old-subdirs (split-string
623 (file-relative-name oldlocal dir) "/")))
624 (setq indent-str (make-string 2 ?\ ))
625 (while (string= (car old-subdirs) (car subdirs))
626 (setq indent-str (concat indent-str (make-string 2 ?\ )))
627 (pop old-subdirs)
628 (pop subdirs))
629 (dolist (d subdirs)
630 (setq subdir (concat subdir d "/"))
631 (insert (concat indent-str " + " d "\n"))
632 (setq indent-str (make-string
633 (+ (length indent-str) 2) ?\ )))))))
634 ;; This is common to 'flat and 'tree
635 (insert (concat indent-str " + [[file:" link "]["
636 (org-publish-find-title file)
637 "]]\n")))))
638 (save-buffer))
639 (or visiting (kill-buffer sitemap-buffer))))
641 (defun org-publish-find-title (file)
642 "Find the title of file in project."
643 (let* ((visiting (find-buffer-visiting file))
644 (buffer (or visiting (find-file-noselect file)))
645 title)
646 (with-current-buffer buffer
647 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
648 (org-infile-export-plist))))
649 (setq title
650 (or (plist-get opt-plist :title)
651 (and (not
652 (plist-get opt-plist :skip-before-1st-heading))
653 (org-export-grab-title-from-buffer))
654 (file-name-nondirectory (file-name-sans-extension file))))))
655 (unless visiting
656 (kill-buffer buffer))
657 title))
659 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
660 ;;; Interactive publishing functions
662 ;;;###autoload
663 (defalias 'org-publish-project 'org-publish)
665 ;;;###autoload
666 (defun org-publish (project &optional force)
667 "Publish PROJECT."
668 (interactive
669 (list
670 (assoc (org-icompleting-read
671 "Publish project: "
672 org-publish-project-alist nil t)
673 org-publish-project-alist)
674 current-prefix-arg))
675 (setq org-publish-initial-buffer (current-buffer))
676 (save-window-excursion
677 (let* ((org-publish-use-timestamps-flag
678 (if force nil org-publish-use-timestamps-flag)))
679 (org-publish-projects (list project)))))
681 ;;;###autoload
682 (defun org-publish-all (&optional force)
683 "Publish all projects.
684 With prefix argument, remove all files in the timestamp
685 directory and force publishing all files."
686 (interactive "P")
687 (when force
688 (org-publish-remove-all-timestamps))
689 (org-publish-initialize-files-alist force)
690 (save-window-excursion
691 (let ((org-publish-use-timestamps-flag
692 (if force nil org-publish-use-timestamps-flag)))
693 (org-publish-projects org-publish-project-alist))))
696 ;;;###autoload
697 (defun org-publish-current-file (&optional force)
698 "Publish the current file.
699 With prefix argument, force publish the file."
700 (interactive "P")
701 (org-publish-initialize-files-alist force)
702 (save-window-excursion
703 (let ((org-publish-use-timestamps-flag
704 (if force nil org-publish-use-timestamps-flag)))
705 (org-publish-file (buffer-file-name)))))
707 ;;;###autoload
708 (defun org-publish-current-project (&optional force)
709 "Publish the project associated with the current file.
710 With a prefix argument, force publishing of all files in
711 the project."
712 (interactive "P")
713 (org-publish-initialize-files-alist force)
714 (save-window-excursion
715 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up))
716 (org-publish-use-timestamps-flag
717 (if force nil org-publish-use-timestamps-flag)))
718 (if (not project)
719 (error "File %s is not part of any known project" (buffer-file-name)))
720 (org-publish project))))
723 ;;; Index generation
725 (defvar backend) ; dynamically scoped
726 (defun org-publish-aux-preprocess ()
727 "Find index entries and write them to an .orgx file."
728 (let ((case-fold-search t)
729 entry index target)
730 (goto-char (point-min))
731 (while
732 (and
733 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t)
734 (> (match-end 1) (match-beginning 1)))
735 (setq entry (match-string 1))
736 (when (eq backend 'latex)
737 (replace-match (format "\\index{%s}" entry) t t))
738 (save-excursion
739 (org-back-to-heading t)
740 (setq target (get-text-property (point) 'target))
741 (setq target (or (cdr (assoc target org-export-preferred-target-alist))
742 (cdr (assoc target org-export-id-target-alist))
743 target))
744 (push (cons entry target) index)))
745 (with-temp-file
746 (concat (file-name-sans-extension org-current-export-file) ".orgx")
747 (dolist (entry (nreverse index))
748 (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
750 (defun org-publish-index-generate-theindex.inc (directory)
751 "Generate the index from all .orgx files in the current directory and below."
752 (require 'find-lisp)
753 (let* ((fulldir (file-name-as-directory
754 (expand-file-name directory)))
755 (full-files (find-lisp-find-files directory "\\.orgx\\'"))
756 (re (concat "\\`" fulldir))
757 (files (mapcar (lambda (f) (if (string-match re f)
758 (substring f (match-end 0))
760 full-files))
761 (default-directory directory)
762 index origfile buf target entry ibuffer
763 main last-main letter last-letter file sub link)
764 ;; `files' contains the list of relative file names
765 (dolist (file files)
766 (setq origfile (substring file 0 -1))
767 (setq buf (find-file-noselect file))
768 (with-current-buffer buf
769 (goto-char (point-min))
770 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
771 (setq target (match-string 1)
772 entry (match-string 2))
773 (push (list entry origfile target) index)))
774 (kill-buffer buf))
775 (setq index (sort index (lambda (a b) (string< (downcase (car a))
776 (downcase (car b))))))
777 (setq ibuffer (find-file-noselect (expand-file-name "theindex.inc" directory)))
778 (with-current-buffer ibuffer
779 (erase-buffer)
780 (insert "* Index\n")
781 (setq last-letter nil)
782 (dolist (idx index)
783 (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
784 (setq letter (upcase (substring entry 0 1)))
785 (when (not (equal letter last-letter))
786 (insert "** " letter "\n")
787 (setq last-letter letter))
788 (if (string-match "!" entry)
789 (setq main (substring entry 0 (match-beginning 0))
790 sub (substring entry (match-end 0)))
791 (setq main nil sub nil last-main nil))
792 (when (and main (not (equal main last-main)))
793 (insert " - " main "\n")
794 (setq last-main main))
795 (setq link (concat "[[file:" file "::#" target "]"
796 "[" (or sub entry) "]]"))
797 (if (and main sub)
798 (insert " - " link "\n")
799 (insert " - " link "\n")))
800 (save-buffer))
801 (kill-buffer ibuffer)
803 (let ((index-file (expand-file-name "theindex.org" directory)))
804 (unless (file-exists-p index-file)
805 (setq ibuffer (find-file-noselect index-file))
806 (with-current-buffer ibuffer
807 (erase-buffer)
808 (insert "\n\n#+include: \"theindex.inc\"\n\n")
809 (save-buffer))
810 (kill-buffer ibuffer)))))
812 (provide 'org-publish)
815 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
817 ;;; org-publish.el ends here