Another fix for sitemap sorting
[org-mode/org-tableheadings.git] / lisp / org-publish.el
blobac22603a09dca0da9db36c15f98ee30de4fc5f20
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.35trans
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'.
179 If you create a sitemap file, adjust the sorting like this:
181 :sitemap-sort-folders Where folders should appear in the sitemap.
182 Set this to `first' (default) or `last' to
183 display folders first or last, respectively.
184 Any other value will mix files and folders.
185 :sitemap-alphabetically The site map is normally sorted alphabetically.
186 Set this explicitly to nil to turn off sorting.
187 :sitemap-ignore-case Should sorting be case-sensitively? Default nil.
189 The following properties control the creation of a concept index.
191 :makeindex Create a concept index."
192 :group 'org-publish
193 :type 'alist)
195 (defcustom org-publish-use-timestamps-flag t
196 "When non-nil, use timestamp checking to publish only changed files.
197 When nil, do no timestamp checking and always publish all files."
198 :group 'org-publish
199 :type 'boolean)
201 (defcustom org-publish-timestamp-directory (convert-standard-filename
202 "~/.org-timestamps/")
203 "Name of directory in which to store publishing timestamps."
204 :group 'org-publish
205 :type 'directory)
207 (defcustom org-publish-list-skipped-files t
208 "Non-nil means show message about files *not* published."
209 :group 'org-publish
210 :type 'boolean)
212 (defcustom org-publish-before-export-hook nil
213 "Hook run before export on the Org file.
214 The hook may modify the file in arbitrary ways before publishing happens.
215 The original version of the buffer will be restored after publishing."
216 :group 'org-publish
217 :type 'hook)
219 (defcustom org-publish-after-export-hook nil
220 "Hook run after export on the exported buffer.
221 Any changes made by this hook will be saved."
222 :group 'org-publish
223 :type 'hook)
225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
226 ;;; Timestamp-related functions
228 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
229 "Return path to timestamp file for filename FILENAME."
230 (setq filename (concat filename "::" (or pub-dir "") "::"
231 (format "%s" (or pub-func ""))))
232 (concat (file-name-as-directory org-publish-timestamp-directory)
233 "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
235 (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir)
236 "Return `t' if FILENAME should be published in PUB-DIR using PUB-FUNC.
237 TRUE-PUB-DIR is there the file will truely end up. Currently we are not using
238 this - maybe it can eventually be used to check if the file is present at
239 the target location, and how old it is. Right ow we cannot do this, because
240 we do not know under what file name the file will be stored - the publishing
241 function can still decide about that independently."
242 (let ((rtn
243 (if org-publish-use-timestamps-flag
244 (if (file-exists-p org-publish-timestamp-directory)
245 ;; first handle possible wrong timestamp directory
246 (if (not (file-directory-p org-publish-timestamp-directory))
247 (error "Org publish timestamp: %s is not a directory"
248 org-publish-timestamp-directory)
249 ;; there is a timestamp, check if FILENAME is newer
250 (file-newer-than-file-p
251 filename (org-publish-timestamp-filename
252 filename pub-dir pub-func)))
253 (make-directory org-publish-timestamp-directory)
255 ;; don't use timestamps, always return t
256 t)))
257 (if rtn
258 (message "Publishing file %s using `%s'" filename pub-func)
259 (when org-publish-list-skipped-files
260 (message "Skipping unmodified file %s" filename)))
261 rtn))
263 (defun org-publish-update-timestamp (filename &optional pub-dir pub-func)
264 "Update publishing timestamp for file FILENAME.
265 If there is no timestamp, create one."
266 (let ((timestamp-file (org-publish-timestamp-filename
267 filename pub-dir pub-func))
268 newly-created-timestamp)
269 (if (not (file-exists-p timestamp-file))
270 ;; create timestamp file if needed
271 (with-temp-buffer
272 (make-directory (file-name-directory timestamp-file) t)
273 (write-file timestamp-file)
274 (setq newly-created-timestamp t)))
275 ;; Emacs 21 doesn't have `set-file-times'
276 (if (and (fboundp 'set-file-times)
277 (not newly-created-timestamp))
278 (set-file-times timestamp-file)
279 (call-process "touch" nil 0 nil (expand-file-name timestamp-file)))))
281 (defun org-publish-remove-all-timestamps ()
282 "Remove all files in the timstamp directory."
283 (let ((dir org-publish-timestamp-directory)
284 files)
285 (when (and (file-exists-p dir)
286 (file-directory-p dir))
287 (mapc 'delete-file (directory-files dir 'full "[^.]\\'")))))
290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
291 ;;; Mapping files to project names
293 (defvar org-publish-files-alist nil
294 "Alist of files and their parent projects.
295 Each element of this alist is of the form:
297 (file-name . project-name)")
299 (defvar org-publish-initial-buffer nil
300 "The buffer `org-publish' has been called from.")
301 (defvar org-publish-temp-files nil
302 "Temporary list of files to be published.")
304 ;; Here, so you find the variable right before it's used the first time:
305 (defvar org-publish-file-title-cache nil
306 "List of absolute filenames and titles.")
308 (defun org-publish-initialize-files-alist (&optional refresh)
309 "Set `org-publish-files-alist' if it is not set.
310 Also set it if the optional argument REFRESH is non-nil."
311 (interactive "P")
312 (when (or refresh (not org-publish-files-alist))
313 (setq org-publish-file-title-cache nil)
314 (setq org-publish-files-alist
315 (org-publish-get-files org-publish-project-alist))))
317 (defun org-publish-validate-link (link &optional directory)
318 "Check if LINK points to a file in the current project."
319 (assoc (expand-file-name link directory) org-publish-files-alist))
321 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
322 ;;; Compatibility aliases
324 ;; Delete-dups is not in Emacs <22
325 (if (fboundp 'delete-dups)
326 (defalias 'org-publish-delete-dups 'delete-dups)
327 (defun org-publish-delete-dups (list)
328 "Destructively remove `equal' duplicates from LIST.
329 Store the result in LIST and return it. LIST must be a proper list.
330 Of several `equal' occurrences of an element in LIST, the first
331 one is kept.
333 This is a compatibility function for Emacsen without `delete-dups'."
334 ;; Code from `subr.el' in Emacs 22:
335 (let ((tail list))
336 (while tail
337 (setcdr tail (delete (car tail) (cdr tail)))
338 (setq tail (cdr tail))))
339 list))
341 (declare-function org-publish-delete-dups "org-publish" (list))
342 (declare-function find-lisp-find-files "find-lisp" (directory regexp))
344 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
345 ;;; Getting project information out of org-publish-project-alist
347 (defun org-publish-get-files (projects-alist &optional no-exclusion)
348 "Return the list of all publishable files for PROJECTS-ALIST.
349 If NO-EXCLUSION is non-nil, don't exclude files."
350 (let (all-files)
351 ;; add all projects
352 (mapc
353 (lambda(p)
354 (let* ((exclude (plist-get (cdr p) :exclude))
355 (files (and p (org-publish-get-base-files p exclude))))
356 ;; add all files from this project
357 (mapc (lambda(f)
358 (add-to-list 'all-files
359 (cons (expand-file-name f) (car p))))
360 files)))
361 (org-publish-expand-projects projects-alist))
362 all-files))
364 (defun org-publish-expand-projects (projects-alist)
365 "Expand projects in PROJECTS-ALIST.
366 This splices all the components into the list."
367 (let ((rest projects-alist) rtn p components)
368 (while (setq p (pop rest))
369 (if (setq components (plist-get (cdr p) :components))
370 (setq rest (append
371 (mapcar (lambda (x) (assoc x org-publish-project-alist))
372 components)
373 rest))
374 (push p rtn)))
375 (nreverse (org-publish-delete-dups (delq nil rtn)))))
377 (defvar sitemap-alphabetically)
378 (defvar sitemap-sort-folders)
379 (defvar sitemap-ignore-case)
380 (defun org-publish-compare-directory-files (a b)
381 "Predicate for `sort', that sorts folders-first/last and
382 eventually alphabetically."
383 (let ((retval t))
384 (when (or sitemap-alphabetically sitemap-sort-folders)
385 ;; First we sort alphabetically:
386 (when sitemap-alphabetically
387 (let* ((adir (file-directory-p a))
388 (aorg (and (string-match "\\.org$" a) (not adir)))
389 (bdir (file-directory-p b))
390 (borg (and (string-match "\\.org$" b) (not bdir)))
391 (A (if aorg
392 (concat (file-name-directory a)
393 (org-publish-find-title a)) a))
394 (B (if borg
395 (concat (file-name-directory b)
396 (org-publish-find-title b)) b)))
397 (setq retval (if sitemap-ignore-case
398 (not (string-lessp (upcase B) (upcase A)))
399 (not (string-lessp B A))))))
401 ;; Directory-wise wins:
402 (when sitemap-sort-folders
403 ;; a is directory, b not:
404 (cond
405 ((and (file-directory-p a) (not (file-directory-p b)))
406 (setq retval (equal sitemap-sort-folders 'first)))
407 ;; a is not a directory, but b is:
408 ((and (not (file-directory-p a)) (file-directory-p b))
409 (setq retval (equal sitemap-sort-folders 'last))))))
410 retval))
412 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir)
413 "Set `org-publish-temp-files' with files from BASE-DIR directory.
414 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
415 non-nil, restrict this list to the files matching the regexp
416 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
417 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
418 matching the regexp SKIP-DIR when recursing through BASE-DIR."
419 (mapc (lambda (f)
420 (let ((fd-p (file-directory-p f))
421 (fnd (file-name-nondirectory f)))
422 (if (and fd-p recurse
423 (not (string-match "^\\.+$" fnd))
424 (if skip-dir (not (string-match skip-dir fnd)) t))
425 (org-publish-get-base-files-1 f recurse match skip-file skip-dir)
426 (unless (or fd-p ;; this is a directory
427 (and skip-file (string-match skip-file fnd))
428 (not (file-exists-p (file-truename f)))
429 (not (string-match match fnd)))
430 (pushnew f org-publish-temp-files)))))
431 (sort (directory-files base-dir t (unless recurse match))
432 'org-publish-compare-directory-files)))
434 (defun org-publish-get-base-files (project &optional exclude-regexp)
435 "Return a list of all files in PROJECT.
436 If EXCLUDE-REGEXP is set, this will be used to filter out
437 matching filenames."
438 (let* ((project-plist (cdr project))
439 (base-dir (file-name-as-directory
440 (plist-get project-plist :base-directory)))
441 (include-list (plist-get project-plist :include))
442 (recurse (plist-get project-plist :recursive))
443 (extension (or (plist-get project-plist :base-extension) "org"))
444 (match (if (eq extension 'any)
445 "^[^\\.]"
446 (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
447 (setq org-publish-temp-files nil)
448 (org-publish-get-base-files-1 base-dir recurse match
449 ;; FIXME distinguish exclude regexp
450 ;; for skip-file and skip-dir?
451 exclude-regexp exclude-regexp)
452 (mapc (lambda (f)
453 (pushnew
454 (expand-file-name (concat base-dir f))
455 org-publish-temp-files))
456 include-list)
457 org-publish-temp-files))
459 (defun org-publish-get-project-from-filename (filename &optional up)
460 "Return the project FILENAME belongs."
461 (let* ((project-name (cdr (assoc (expand-file-name filename)
462 org-publish-files-alist))))
463 (when up
464 (dolist (prj org-publish-project-alist)
465 (if (member project-name (plist-get (cdr prj) :components))
466 (setq project-name (car prj)))))
467 (assoc project-name org-publish-project-alist)))
469 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
470 ;;; Pluggable publishing back-end functions
472 (defun org-publish-org-to (format plist filename pub-dir)
473 "Publish an org file to FORMAT.
474 PLIST is the property list for the given project.
475 FILENAME is the filename of the org file to be published.
476 PUB-DIR is the publishing directory."
477 (require 'org)
478 (unless (file-exists-p pub-dir)
479 (make-directory pub-dir t))
480 (let ((visiting (find-buffer-visiting filename)))
481 (save-excursion
482 (switch-to-buffer (or visiting (find-file filename)))
483 (let* ((plist (cons :buffer-will-be-killed (cons t plist)))
484 (init-buf (current-buffer))
485 (init-point (point))
486 (init-buf-string (buffer-string))
487 export-buf-or-file)
488 ;; run hooks before exporting
489 (run-hooks 'org-publish-before-export-hook)
490 ;; export the possibly modified buffer
491 (setq export-buf-or-file
492 (funcall (intern (concat "org-export-as-" format))
493 (plist-get plist :headline-levels)
494 nil plist nil nil pub-dir))
495 (when (and (bufferp export-buf-or-file)
496 (buffer-live-p export-buf-or-file))
497 (set-buffer export-buf-or-file)
498 ;; run hooks after export and save export
499 (progn (run-hooks 'org-publish-after-export-hook)
500 (if (buffer-modified-p) (save-buffer)))
501 (kill-buffer export-buf-or-file))
502 ;; maybe restore buffer's content
503 (set-buffer init-buf)
504 (when (buffer-modified-p init-buf)
505 (erase-buffer)
506 (insert init-buf-string)
507 (save-buffer)
508 (goto-char init-point))
509 (unless visiting
510 (kill-buffer init-buf))))))
512 (defmacro org-publish-with-aux-preprocess-maybe (&rest body)
513 "Execute BODY with a modified hook to preprocess for index."
514 `(let ((org-export-preprocess-after-headline-targets-hook
515 (if (plist-get project-plist :makeindex)
516 (cons 'org-publish-aux-preprocess
517 org-export-preprocess-after-headline-targets-hook)
518 org-export-preprocess-after-headline-targets-hook)))
519 ,@body))
521 (defvar project-plist)
522 (defun org-publish-org-to-latex (plist filename pub-dir)
523 "Publish an org file to LaTeX.
524 See `org-publish-org-to' to the list of arguments."
525 (org-publish-with-aux-preprocess-maybe
526 (org-publish-org-to "latex" plist filename pub-dir)))
528 (defun org-publish-org-to-pdf (plist filename pub-dir)
529 "Publish an org file to PDF (via LaTeX).
530 See `org-publish-org-to' to the list of arguments."
531 (org-publish-with-aux-preprocess-maybe
532 (org-publish-org-to "pdf" plist filename pub-dir)))
534 (defun org-publish-org-to-html (plist filename pub-dir)
535 "Publish an org file to HTML.
536 See `org-publish-org-to' to the list of arguments."
537 (org-publish-with-aux-preprocess-maybe
538 (org-publish-org-to "html" plist filename pub-dir)))
540 (defun org-publish-org-to-org (plist filename pub-dir)
541 "Publish an org file to HTML.
542 See `org-publish-org-to' to the list of arguments."
543 (org-publish-org-to "org" plist filename pub-dir))
545 (defun org-publish-attachment (plist filename pub-dir)
546 "Publish a file with no transformation of any kind.
547 See `org-publish-org-to' to the list of arguments."
548 ;; make sure eshell/cp code is loaded
549 (unless (file-directory-p pub-dir)
550 (make-directory pub-dir t))
551 (or (equal (expand-file-name (file-name-directory filename))
552 (file-name-as-directory (expand-file-name pub-dir)))
553 (copy-file filename
554 (expand-file-name (file-name-nondirectory filename) pub-dir)
555 t)))
557 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
558 ;;; Publishing files, sets of files, and indices
560 (defun org-publish-file (filename &optional project)
561 "Publish file FILENAME from PROJECT."
562 (let* ((project
563 (or project
564 (or (org-publish-get-project-from-filename filename)
565 (if (y-or-n-p
566 (format "%s is not in a project. Re-read the list of projects files? "
567 (abbreviate-file-name filename)))
568 ;; If requested, re-initialize the list of projects files
569 (progn (org-publish-initialize-files-alist t)
570 (or (org-publish-get-project-from-filename filename)
571 (error "File %s not part of any known project"
572 (abbreviate-file-name filename))))
573 (error "Can't publish file outside of a project")))))
574 (project-plist (cdr project))
575 (ftname (file-truename filename))
576 (publishing-function
577 (or (plist-get project-plist :publishing-function)
578 'org-publish-org-to-html))
579 (base-dir (file-name-as-directory
580 (file-truename (plist-get project-plist :base-directory))))
581 (pub-dir (file-name-as-directory
582 (file-truename (plist-get project-plist :publishing-directory))))
583 tmp-pub-dir)
584 (setq tmp-pub-dir
585 (file-name-directory
586 (concat pub-dir
587 (and (string-match (regexp-quote base-dir) ftname)
588 (substring ftname (match-end 0))))))
589 (if (listp publishing-function)
590 ;; allow chain of publishing functions
591 (mapc (lambda (f)
592 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir)
593 (funcall f project-plist filename tmp-pub-dir)
594 (org-publish-update-timestamp filename pub-dir f)))
595 publishing-function)
596 (when (org-publish-needed-p filename pub-dir publishing-function
597 tmp-pub-dir)
598 (funcall publishing-function project-plist filename tmp-pub-dir)
599 (org-publish-update-timestamp
600 filename pub-dir publishing-function)))))
602 (defun org-publish-projects (projects)
603 "Publish all files belonging to the PROJECTS alist.
604 If :auto-sitemap is set, publish the sitemap too.
605 If :makeindex is set, also produce a file theindex.org."
606 (mapc
607 (lambda (project)
608 (let*
609 ((project-plist (cdr project))
610 (exclude-regexp (plist-get project-plist :exclude))
611 (sitemap-p (plist-get project-plist :auto-sitemap))
612 (sitemap-filename (or (plist-get project-plist :sitemap-filename)
613 "sitemap.org"))
614 (sitemap-function (or (plist-get project-plist :sitemap-function)
615 'org-publish-org-sitemap))
616 (sitemap-sort-folders
617 (if (plist-member project-plist :sitemap-sort-folders)
618 (plist-get project-plist :sitemap-sort-folders)
619 'first))
620 (sitemap-alphabetically
621 (if (plist-member project-plist :sitemap-alphabetically)
622 (plist-get project-plist :sitemap-alphabetically) t))
623 (sitemap-ignore-case (plist-get project-plist :sitemap-ignore-case))
624 (preparation-function (plist-get project-plist :preparation-function))
625 (completion-function (plist-get project-plist :completion-function))
626 (files (org-publish-get-base-files project exclude-regexp)) file)
627 ;; Make sure sitemap-sort-folders' has an accepted value
628 (unless (memq sitemap-sort-folders '(first last))
629 (setq sitemap-sort-folders nil))
630 (when preparation-function (run-hooks 'preparation-function))
631 (if sitemap-p (funcall sitemap-function project sitemap-filename))
632 (while (setq file (pop files))
633 (org-publish-file file project))
634 (when (plist-get project-plist :makeindex)
635 (org-publish-index-generate-theindex.inc
636 (plist-get project-plist :base-directory))
637 (org-publish-file (expand-file-name
638 "theindex.org"
639 (plist-get project-plist :base-directory))
640 project))
641 (when completion-function (run-hooks 'completion-function))))
642 (org-publish-expand-projects projects)))
644 (defun org-publish-org-sitemap (project &optional sitemap-filename)
645 "Create an sitemap of pages in set defined by PROJECT.
646 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
647 Default for SITEMAP-FILENAME is 'sitemap.org'."
648 (let* ((project-plist (cdr project))
649 (dir (file-name-as-directory
650 (plist-get project-plist :base-directory)))
651 (localdir (file-name-directory dir))
652 (indent-str (make-string 2 ?\ ))
653 (exclude-regexp (plist-get project-plist :exclude))
654 (files (nreverse (org-publish-get-base-files project exclude-regexp)))
655 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
656 (sitemap-title (or (plist-get project-plist :sitemap-title)
657 (concat "Sitemap for project " (car project))))
658 (sitemap-style (or (plist-get project-plist :sitemap-style)
659 'tree))
660 (visiting (find-buffer-visiting sitemap-filename))
661 (ifn (file-name-nondirectory sitemap-filename))
662 file sitemap-buffer)
663 (with-current-buffer (setq sitemap-buffer
664 (or visiting (find-file sitemap-filename)))
665 (erase-buffer)
666 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
667 (while (setq file (pop files))
668 (let ((fn (file-name-nondirectory file))
669 (link (file-relative-name file dir))
670 (oldlocal localdir))
671 ;; sitemap shouldn't list itself
672 (unless (equal (file-truename sitemap-filename)
673 (file-truename file))
674 (if (eq sitemap-style 'list)
675 (message "Generating list-style sitemap for %s" sitemap-title)
676 (message "Generating tree-style sitemap for %s" sitemap-title)
677 (setq localdir (concat (file-name-as-directory dir)
678 (file-name-directory link)))
679 (unless (string= localdir oldlocal)
680 (if (string= localdir dir)
681 (setq indent-str (make-string 2 ?\ ))
682 (let ((subdirs
683 (split-string
684 (directory-file-name
685 (file-name-directory
686 (file-relative-name localdir dir))) "/"))
687 (subdir "")
688 (old-subdirs (split-string
689 (file-relative-name oldlocal dir) "/")))
690 (setq indent-str (make-string 2 ?\ ))
691 (while (string= (car old-subdirs) (car subdirs))
692 (setq indent-str (concat indent-str (make-string 2 ?\ )))
693 (pop old-subdirs)
694 (pop subdirs))
695 (dolist (d subdirs)
696 (setq subdir (concat subdir d "/"))
697 (insert (concat indent-str " + " d "\n"))
698 (setq indent-str (make-string
699 (+ (length indent-str) 2) ?\ )))))))
700 ;; This is common to 'flat and 'tree
701 (insert (concat indent-str " + [[file:" link "]["
702 (org-publish-find-title file)
703 "]]\n")))))
704 (save-buffer))
705 (or visiting (kill-buffer sitemap-buffer))))
707 (defun org-publish-find-title (file)
708 "Find the title of file in project."
709 (if (member file org-publish-file-title-cache)
710 (cadr (member file org-publish-file-title-cache))
711 (let* ((visiting (find-buffer-visiting file))
712 (buffer (or visiting (find-file-noselect file)))
713 title)
714 (with-current-buffer buffer
715 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
716 (org-infile-export-plist))))
717 (setq title
718 (or (plist-get opt-plist :title)
719 (and (not
720 (plist-get opt-plist :skip-before-1st-heading))
721 (org-export-grab-title-from-buffer))
722 (file-name-nondirectory (file-name-sans-extension file))))))
723 (unless visiting
724 (kill-buffer buffer))
725 (setq org-publish-file-title-cache
726 (append org-publish-file-title-cache (list file title)))
727 title)))
729 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
730 ;;; Interactive publishing functions
732 ;;;###autoload
733 (defalias 'org-publish-project 'org-publish)
735 ;;;###autoload
736 (defun org-publish (project &optional force)
737 "Publish PROJECT."
738 (interactive
739 (list
740 (assoc (org-icompleting-read
741 "Publish project: "
742 org-publish-project-alist nil t)
743 org-publish-project-alist)
744 current-prefix-arg))
745 (setq org-publish-initial-buffer (current-buffer))
746 (save-window-excursion
747 (let* ((org-publish-use-timestamps-flag
748 (if force nil org-publish-use-timestamps-flag)))
749 (org-publish-projects (list project)))))
751 ;;;###autoload
752 (defun org-publish-all (&optional force)
753 "Publish all projects.
754 With prefix argument, remove all files in the timestamp
755 directory and force publishing all files."
756 (interactive "P")
757 (when force
758 (org-publish-remove-all-timestamps))
759 (org-publish-initialize-files-alist force)
760 (save-window-excursion
761 (let ((org-publish-use-timestamps-flag
762 (if force nil org-publish-use-timestamps-flag)))
763 (org-publish-projects org-publish-project-alist))))
766 ;;;###autoload
767 (defun org-publish-current-file (&optional force)
768 "Publish the current file.
769 With prefix argument, force publish the file."
770 (interactive "P")
771 (org-publish-initialize-files-alist force)
772 (save-window-excursion
773 (let ((org-publish-use-timestamps-flag
774 (if force nil org-publish-use-timestamps-flag)))
775 (org-publish-file (buffer-file-name)))))
777 ;;;###autoload
778 (defun org-publish-current-project (&optional force)
779 "Publish the project associated with the current file.
780 With a prefix argument, force publishing of all files in
781 the project."
782 (interactive "P")
783 (org-publish-initialize-files-alist force)
784 (save-window-excursion
785 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up))
786 (org-publish-use-timestamps-flag
787 (if force nil org-publish-use-timestamps-flag)))
788 (if (not project)
789 (error "File %s is not part of any known project" (buffer-file-name)))
790 (org-publish project))))
793 ;;; Index generation
795 (defvar backend) ; dynamically scoped
796 (defun org-publish-aux-preprocess ()
797 "Find index entries and write them to an .orgx file."
798 (let ((case-fold-search t)
799 entry index target)
800 (goto-char (point-min))
801 (while
802 (and
803 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t)
804 (> (match-end 1) (match-beginning 1)))
805 (setq entry (match-string 1))
806 (when (eq backend 'latex)
807 (replace-match (format "\\index{%s}" entry) t t))
808 (save-excursion
809 (ignore-errors (org-back-to-heading t))
810 (setq target (get-text-property (point) 'target))
811 (setq target (or (cdr (assoc target org-export-preferred-target-alist))
812 (cdr (assoc target org-export-id-target-alist))
813 target ""))
814 (push (cons entry target) index)))
815 (with-temp-file
816 (concat (file-name-sans-extension org-current-export-file) ".orgx")
817 (dolist (entry (nreverse index))
818 (insert (format "INDEX: (%s) %s\n" (cdr entry) (car entry)))))))
820 (defun org-publish-index-generate-theindex.inc (directory)
821 "Generate the index from all .orgx files in the current directory and below."
822 (require 'find-lisp)
823 (let* ((fulldir (file-name-as-directory
824 (expand-file-name directory)))
825 (full-files (find-lisp-find-files directory "\\.orgx\\'"))
826 (re (concat "\\`" fulldir))
827 (files (mapcar (lambda (f) (if (string-match re f)
828 (substring f (match-end 0))
830 full-files))
831 (default-directory directory)
832 index origfile buf target entry ibuffer
833 main last-main letter last-letter file sub link tgext)
834 ;; `files' contains the list of relative file names
835 (dolist (file files)
836 (setq origfile (substring file 0 -1))
837 (setq buf (find-file-noselect file))
838 (with-current-buffer buf
839 (goto-char (point-min))
840 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t)
841 (setq target (match-string 1)
842 entry (match-string 2))
843 (push (list entry origfile target) index)))
844 (kill-buffer buf))
845 (setq index (sort index (lambda (a b) (string< (downcase (car a))
846 (downcase (car b))))))
847 (setq ibuffer (find-file-noselect (expand-file-name "theindex.inc" directory)))
848 (with-current-buffer ibuffer
849 (erase-buffer)
850 (insert "* Index\n")
851 (setq last-letter nil)
852 (dolist (idx index)
853 (setq entry (car idx) file (nth 1 idx) target (nth 2 idx))
854 (if (and (stringp target) (string-match "\\S-" target))
855 (setq tgext (concat "::#" target))
856 (setq tgext ""))
857 (setq letter (upcase (substring entry 0 1)))
858 (when (not (equal letter last-letter))
859 (insert "** " letter "\n")
860 (setq last-letter letter))
861 (if (string-match "!" entry)
862 (setq main (substring entry 0 (match-beginning 0))
863 sub (substring entry (match-end 0)))
864 (setq main nil sub nil last-main nil))
865 (when (and main (not (equal main last-main)))
866 (insert " - " main "\n")
867 (setq last-main main))
868 (setq link (concat "[[file:" file tgext "]"
869 "[" (or sub entry) "]]"))
870 (if (and main sub)
871 (insert " - " link "\n")
872 (insert " - " link "\n")))
873 (save-buffer))
874 (kill-buffer ibuffer)
876 (let ((index-file (expand-file-name "theindex.org" directory)))
877 (unless (file-exists-p index-file)
878 (setq ibuffer (find-file-noselect index-file))
879 (with-current-buffer ibuffer
880 (erase-buffer)
881 (insert "\n\n#+include: \"theindex.inc\"\n\n")
882 (save-buffer))
883 (kill-buffer ibuffer)))))
885 (provide 'org-publish)
888 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
890 ;;; org-publish.el ends here