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
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/>.
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.
44 (defun org-publish-sanitize-plist (plist)
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
))))
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."
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 ... )
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
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
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
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
129 :completion-function Function to be called after publishing
130 this project. This may also be a list
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
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
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."
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."
201 (defcustom org-publish-timestamp-directory
(convert-standard-filename
202 "~/.org-timestamps/")
203 "Name of directory in which to store publishing timestamps."
207 (defcustom org-publish-list-skipped-files t
208 "Non-nil means show message about files *not* published."
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."
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."
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."
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
258 (message "Publishing file %s using `%s'" filename pub-func
)
259 (when org-publish-list-skipped-files
260 (message "Skipping unmodified file %s" filename
)))
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
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
)
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."
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
333 This is a compatibility function for Emacsen without `delete-dups'."
334 ;; Code from `subr.el' in Emacs 22:
337 (setcdr tail
(delete (car tail
) (cdr tail
)))
338 (setq tail
(cdr tail
))))
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."
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
358 (add-to-list 'all-files
359 (cons (expand-file-name f
) (car p
))))
361 (org-publish-expand-projects projects-alist
))
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
))
371 (mapcar (lambda (x) (assoc x org-publish-project-alist
))
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."
384 (when (or sitemap-alphabetically sitemap-sort-folders
)
385 ;; First we sort alphabetically:
386 (when sitemap-alphabetically
387 (let ((aorg (and (string-match "\\.org$" a
) (not (file-directory-p a
))))
388 (borg (and (string-match "\\.org$" b
) (not (file-directory-p b
)))))
390 (if sitemap-ignore-case
391 (string-lessp (if borg
(upcase (org-publish-find-title a
)) (upcase a
))
392 (if aorg
(upcase (org-publish-find-title b
)) (upcase b
)))
393 (string-lessp (if borg
(org-publish-find-title a
) a
)
394 (if aorg
(org-publish-find-title b
) b
))))))
395 ;; Directory-wise wins:
396 (when sitemap-sort-folders
397 ;; a is directory, b not:
399 ((and (file-directory-p a
) (not (file-directory-p b
)))
400 (setq retval
(eq sitemap-sort-folders
'first
)))
401 ;; a is not a directory, but b is:
402 ((and (not (file-directory-p a
)) (file-directory-p b
))
403 (setq retval
(eq sitemap-sort-folders
'last
))))))
406 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir
)
407 "Set `org-publish-temp-files' with files from BASE-DIR directory.
408 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
409 non-nil, restrict this list to the files matching the regexp
410 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
411 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
412 matching the regexp SKIP-DIR when recursing through BASE-DIR."
414 (let ((fd-p (file-directory-p f
))
415 (fnd (file-name-nondirectory f
)))
416 (if (and fd-p recurse
417 (not (string-match "^\\.+$" fnd
))
418 (if skip-dir
(not (string-match skip-dir fnd
)) t
))
419 (org-publish-get-base-files-1 f recurse match skip-file skip-dir
)
420 (unless (or fd-p
;; this is a directory
421 (and skip-file
(string-match skip-file fnd
))
422 (not (file-exists-p (file-truename f
)))
423 (not (string-match match fnd
)))
424 (pushnew f org-publish-temp-files
)))))
425 (sort (directory-files base-dir t
(unless recurse match
))
426 'org-publish-compare-directory-files
)))
428 (defun org-publish-get-base-files (project &optional exclude-regexp
)
429 "Return a list of all files in PROJECT.
430 If EXCLUDE-REGEXP is set, this will be used to filter out
432 (let* ((project-plist (cdr project
))
433 (base-dir (file-name-as-directory
434 (plist-get project-plist
:base-directory
)))
435 (include-list (plist-get project-plist
:include
))
436 (recurse (plist-get project-plist
:recursive
))
437 (extension (or (plist-get project-plist
:base-extension
) "org"))
438 (match (if (eq extension
'any
)
440 (concat "^[^\\.].*\\.\\(" extension
"\\)$"))))
441 (setq org-publish-temp-files nil
)
442 (org-publish-get-base-files-1 base-dir recurse match
443 ;; FIXME distinguish exclude regexp
444 ;; for skip-file and skip-dir?
445 exclude-regexp exclude-regexp
)
448 (expand-file-name (concat base-dir f
))
449 org-publish-temp-files
))
451 org-publish-temp-files
))
453 (defun org-publish-get-project-from-filename (filename &optional up
)
454 "Return the project FILENAME belongs."
455 (let* ((project-name (cdr (assoc (expand-file-name filename
)
456 org-publish-files-alist
))))
458 (dolist (prj org-publish-project-alist
)
459 (if (member project-name
(plist-get (cdr prj
) :components
))
460 (setq project-name
(car prj
)))))
461 (assoc project-name org-publish-project-alist
)))
463 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
464 ;;; Pluggable publishing back-end functions
466 (defun org-publish-org-to (format plist filename pub-dir
)
467 "Publish an org file to FORMAT.
468 PLIST is the property list for the given project.
469 FILENAME is the filename of the org file to be published.
470 PUB-DIR is the publishing directory."
472 (unless (file-exists-p pub-dir
)
473 (make-directory pub-dir t
))
474 (let ((visiting (find-buffer-visiting filename
)))
476 (switch-to-buffer (or visiting
(find-file filename
)))
477 (let* ((plist (cons :buffer-will-be-killed
(cons t plist
)))
478 (init-buf (current-buffer))
480 (init-buf-string (buffer-string))
482 ;; run hooks before exporting
483 (run-hooks 'org-publish-before-export-hook
)
484 ;; export the possibly modified buffer
485 (setq export-buf-or-file
486 (funcall (intern (concat "org-export-as-" format
))
487 (plist-get plist
:headline-levels
)
488 nil plist nil nil pub-dir
))
489 (when (and (bufferp export-buf-or-file
)
490 (buffer-live-p export-buf-or-file
))
491 (set-buffer export-buf-or-file
)
492 ;; run hooks after export and save export
493 (progn (run-hooks 'org-publish-after-export-hook
)
494 (if (buffer-modified-p) (save-buffer)))
495 (kill-buffer export-buf-or-file
))
496 ;; maybe restore buffer's content
497 (set-buffer init-buf
)
498 (when (buffer-modified-p init-buf
)
500 (insert init-buf-string
)
502 (goto-char init-point
))
504 (kill-buffer init-buf
))))))
506 (defmacro org-publish-with-aux-preprocess-maybe
(&rest body
)
507 "Execute BODY with a modified hook to preprocess for index."
508 `(let ((org-export-preprocess-after-headline-targets-hook
509 (if (plist-get project-plist
:makeindex
)
510 (cons 'org-publish-aux-preprocess
511 org-export-preprocess-after-headline-targets-hook
)
512 org-export-preprocess-after-headline-targets-hook
)))
515 (defvar project-plist
)
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-with-aux-preprocess-maybe
520 (org-publish-org-to "latex" plist filename pub-dir
)))
522 (defun org-publish-org-to-pdf (plist filename pub-dir
)
523 "Publish an org file to PDF (via LaTeX).
524 See `org-publish-org-to' to the list of arguments."
525 (org-publish-with-aux-preprocess-maybe
526 (org-publish-org-to "pdf" plist filename pub-dir
)))
528 (defun org-publish-org-to-html (plist filename pub-dir
)
529 "Publish an org file to HTML.
530 See `org-publish-org-to' to the list of arguments."
531 (org-publish-with-aux-preprocess-maybe
532 (org-publish-org-to "html" plist filename pub-dir
)))
534 (defun org-publish-org-to-org (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-org-to "org" plist filename pub-dir
))
539 (defun org-publish-attachment (plist filename pub-dir
)
540 "Publish a file with no transformation of any kind.
541 See `org-publish-org-to' to the list of arguments."
542 ;; make sure eshell/cp code is loaded
543 (unless (file-directory-p pub-dir
)
544 (make-directory pub-dir t
))
545 (or (equal (expand-file-name (file-name-directory filename
))
546 (file-name-as-directory (expand-file-name pub-dir
)))
548 (expand-file-name (file-name-nondirectory filename
) pub-dir
)
551 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
552 ;;; Publishing files, sets of files, and indices
554 (defun org-publish-file (filename &optional project
)
555 "Publish file FILENAME from PROJECT."
558 (or (org-publish-get-project-from-filename filename
)
560 (format "%s is not in a project. Re-read the list of projects files? "
561 (abbreviate-file-name filename
)))
562 ;; If requested, re-initialize the list of projects files
563 (progn (org-publish-initialize-files-alist t
)
564 (or (org-publish-get-project-from-filename filename
)
565 (error "File %s not part of any known project"
566 (abbreviate-file-name filename
))))
567 (error "Can't publish file outside of a project")))))
568 (project-plist (cdr project
))
569 (ftname (file-truename filename
))
571 (or (plist-get project-plist
:publishing-function
)
572 'org-publish-org-to-html
))
573 (base-dir (file-name-as-directory
574 (file-truename (plist-get project-plist
:base-directory
))))
575 (pub-dir (file-name-as-directory
576 (file-truename (plist-get project-plist
:publishing-directory
))))
581 (and (string-match (regexp-quote base-dir
) ftname
)
582 (substring ftname
(match-end 0))))))
583 (if (listp publishing-function
)
584 ;; allow chain of publishing functions
586 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir
)
587 (funcall f project-plist filename tmp-pub-dir
)
588 (org-publish-update-timestamp filename pub-dir f
)))
590 (when (org-publish-needed-p filename pub-dir publishing-function
592 (funcall publishing-function project-plist filename tmp-pub-dir
)
593 (org-publish-update-timestamp
594 filename pub-dir publishing-function
)))))
596 (defun org-publish-projects (projects)
597 "Publish all files belonging to the PROJECTS alist.
598 If :auto-sitemap is set, publish the sitemap too.
599 If :makeindex is set, also produce a file theindex.org."
603 ((project-plist (cdr project
))
604 (exclude-regexp (plist-get project-plist
:exclude
))
605 (sitemap-p (plist-get project-plist
:auto-sitemap
))
606 (sitemap-filename (or (plist-get project-plist
:sitemap-filename
)
608 (sitemap-function (or (plist-get project-plist
:sitemap-function
)
609 'org-publish-org-sitemap
))
610 (sitemap-sort-folders
611 (if (plist-member project-plist
:sitemap-sort-folders
)
612 (plist-get project-plist
:sitemap-sort-folders
)
614 (sitemap-alphabetically
615 (if (plist-member project-plist
:sitemap-alphabetically
)
616 (plist-get project-plist
:sitemap-alphabetically
) t
))
617 (sitemap-ignore-case (plist-get project-plist
:sitemap-ignore-case
))
618 (preparation-function (plist-get project-plist
:preparation-function
))
619 (completion-function (plist-get project-plist
:completion-function
))
620 (files (org-publish-get-base-files project exclude-regexp
)) file
)
621 (when (and (not (stringp sitemap-sort-folders
))
622 (not (string= sitemap-sort-folders
"first"))
623 (not (string= sitemap-sort-folders
"last")))
624 (setq sitemap-sort-folders nil
))
625 (when preparation-function
(run-hooks 'preparation-function
))
626 (if sitemap-p
(funcall sitemap-function project sitemap-filename
))
627 (while (setq file
(pop files
))
628 (org-publish-file file project
))
629 (when (plist-get project-plist
:makeindex
)
630 (org-publish-index-generate-theindex.inc
631 (plist-get project-plist
:base-directory
))
632 (org-publish-file (expand-file-name
634 (plist-get project-plist
:base-directory
))
636 (when completion-function
(run-hooks 'completion-function
))))
637 (org-publish-expand-projects projects
)))
639 (defun org-publish-org-sitemap (project &optional sitemap-filename
)
640 "Create an sitemap of pages in set defined by PROJECT.
641 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
642 Default for SITEMAP-FILENAME is 'sitemap.org'."
643 (let* ((project-plist (cdr project
))
644 (dir (file-name-as-directory
645 (plist-get project-plist
:base-directory
)))
646 (localdir (file-name-directory dir
))
647 (indent-str (make-string 2 ?\
))
648 (exclude-regexp (plist-get project-plist
:exclude
))
649 (files (nreverse (org-publish-get-base-files project exclude-regexp
)))
650 (sitemap-filename (concat dir
(or sitemap-filename
"sitemap.org")))
651 (sitemap-title (or (plist-get project-plist
:sitemap-title
)
652 (concat "Sitemap for project " (car project
))))
653 (sitemap-style (or (plist-get project-plist
:sitemap-style
)
655 (visiting (find-buffer-visiting sitemap-filename
))
656 (ifn (file-name-nondirectory sitemap-filename
))
658 (with-current-buffer (setq sitemap-buffer
659 (or visiting
(find-file sitemap-filename
)))
661 (insert (concat "#+TITLE: " sitemap-title
"\n\n"))
662 (while (setq file
(pop files
))
663 (let ((fn (file-name-nondirectory file
))
664 (link (file-relative-name file dir
))
666 ;; sitemap shouldn't list itself
667 (unless (equal (file-truename sitemap-filename
)
668 (file-truename file
))
669 (if (eq sitemap-style
'list
)
670 (message "Generating list-style sitemap for %s" sitemap-title
)
671 (message "Generating tree-style sitemap for %s" sitemap-title
)
672 (setq localdir
(concat (file-name-as-directory dir
)
673 (file-name-directory link
)))
674 (unless (string= localdir oldlocal
)
675 (if (string= localdir dir
)
676 (setq indent-str
(make-string 2 ?\
))
681 (file-relative-name localdir dir
))) "/"))
683 (old-subdirs (split-string
684 (file-relative-name oldlocal dir
) "/")))
685 (setq indent-str
(make-string 2 ?\
))
686 (while (string= (car old-subdirs
) (car subdirs
))
687 (setq indent-str
(concat indent-str
(make-string 2 ?\
)))
691 (setq subdir
(concat subdir d
"/"))
692 (insert (concat indent-str
" + " d
"\n"))
693 (setq indent-str
(make-string
694 (+ (length indent-str
) 2) ?\
)))))))
695 ;; This is common to 'flat and 'tree
696 (insert (concat indent-str
" + [[file:" link
"]["
697 (org-publish-find-title file
)
700 (or visiting
(kill-buffer sitemap-buffer
))))
702 (defun org-publish-find-title (file)
703 "Find the title of file in project."
704 (if (member file org-publish-file-title-cache
)
705 (cadr (member file org-publish-file-title-cache
))
706 (let* ((visiting (find-buffer-visiting file
))
707 (buffer (or visiting
(find-file-noselect file
)))
709 (with-current-buffer buffer
710 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
711 (org-infile-export-plist))))
713 (or (plist-get opt-plist
:title
)
715 (plist-get opt-plist
:skip-before-1st-heading
))
716 (org-export-grab-title-from-buffer))
717 (file-name-nondirectory (file-name-sans-extension file
))))))
719 (kill-buffer buffer
))
720 (setq org-publish-file-title-cache
721 (append org-publish-file-title-cache
(list file title
)))
724 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
725 ;;; Interactive publishing functions
728 (defalias 'org-publish-project
'org-publish
)
731 (defun org-publish (project &optional force
)
735 (assoc (org-icompleting-read
737 org-publish-project-alist nil t
)
738 org-publish-project-alist
)
740 (setq org-publish-initial-buffer
(current-buffer))
741 (save-window-excursion
742 (let* ((org-publish-use-timestamps-flag
743 (if force nil org-publish-use-timestamps-flag
)))
744 (org-publish-projects (list project
)))))
747 (defun org-publish-all (&optional force
)
748 "Publish all projects.
749 With prefix argument, remove all files in the timestamp
750 directory and force publishing all files."
753 (org-publish-remove-all-timestamps))
754 (org-publish-initialize-files-alist force
)
755 (save-window-excursion
756 (let ((org-publish-use-timestamps-flag
757 (if force nil org-publish-use-timestamps-flag
)))
758 (org-publish-projects org-publish-project-alist
))))
762 (defun org-publish-current-file (&optional force
)
763 "Publish the current file.
764 With prefix argument, force publish the file."
766 (org-publish-initialize-files-alist force
)
767 (save-window-excursion
768 (let ((org-publish-use-timestamps-flag
769 (if force nil org-publish-use-timestamps-flag
)))
770 (org-publish-file (buffer-file-name)))))
773 (defun org-publish-current-project (&optional force
)
774 "Publish the project associated with the current file.
775 With a prefix argument, force publishing of all files in
778 (org-publish-initialize-files-alist force
)
779 (save-window-excursion
780 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up
))
781 (org-publish-use-timestamps-flag
782 (if force nil org-publish-use-timestamps-flag
)))
784 (error "File %s is not part of any known project" (buffer-file-name)))
785 (org-publish project
))))
790 (defvar backend
) ; dynamically scoped
791 (defun org-publish-aux-preprocess ()
792 "Find index entries and write them to an .orgx file."
793 (let ((case-fold-search t
)
795 (goto-char (point-min))
798 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t
)
799 (> (match-end 1) (match-beginning 1)))
800 (setq entry
(match-string 1))
801 (when (eq backend
'latex
)
802 (replace-match (format "\\index{%s}" entry
) t t
))
804 (ignore-errors (org-back-to-heading t
))
805 (setq target
(get-text-property (point) 'target
))
806 (setq target
(or (cdr (assoc target org-export-preferred-target-alist
))
807 (cdr (assoc target org-export-id-target-alist
))
809 (push (cons entry target
) index
)))
811 (concat (file-name-sans-extension org-current-export-file
) ".orgx")
812 (dolist (entry (nreverse index
))
813 (insert (format "INDEX: (%s) %s\n" (cdr entry
) (car entry
)))))))
815 (defun org-publish-index-generate-theindex.inc
(directory)
816 "Generate the index from all .orgx files in the current directory and below."
818 (let* ((fulldir (file-name-as-directory
819 (expand-file-name directory
)))
820 (full-files (find-lisp-find-files directory
"\\.orgx\\'"))
821 (re (concat "\\`" fulldir
))
822 (files (mapcar (lambda (f) (if (string-match re f
)
823 (substring f
(match-end 0))
826 (default-directory directory
)
827 index origfile buf target entry ibuffer
828 main last-main letter last-letter file sub link tgext
)
829 ;; `files' contains the list of relative file names
831 (setq origfile
(substring file
0 -
1))
832 (setq buf
(find-file-noselect file
))
833 (with-current-buffer buf
834 (goto-char (point-min))
835 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t
)
836 (setq target
(match-string 1)
837 entry
(match-string 2))
838 (push (list entry origfile target
) index
)))
840 (setq index
(sort index
(lambda (a b
) (string< (downcase (car a
))
841 (downcase (car b
))))))
842 (setq ibuffer
(find-file-noselect (expand-file-name "theindex.inc" directory
)))
843 (with-current-buffer ibuffer
846 (setq last-letter nil
)
848 (setq entry
(car idx
) file
(nth 1 idx
) target
(nth 2 idx
))
849 (if (and (stringp target
) (string-match "\\S-" target
))
850 (setq tgext
(concat "::#" target
))
852 (setq letter
(upcase (substring entry
0 1)))
853 (when (not (equal letter last-letter
))
854 (insert "** " letter
"\n")
855 (setq last-letter letter
))
856 (if (string-match "!" entry
)
857 (setq main
(substring entry
0 (match-beginning 0))
858 sub
(substring entry
(match-end 0)))
859 (setq main nil sub nil last-main nil
))
860 (when (and main
(not (equal main last-main
)))
861 (insert " - " main
"\n")
862 (setq last-main main
))
863 (setq link
(concat "[[file:" file tgext
"]"
864 "[" (or sub entry
) "]]"))
866 (insert " - " link
"\n")
867 (insert " - " link
"\n")))
869 (kill-buffer ibuffer
)
871 (let ((index-file (expand-file-name "theindex.org" directory
)))
872 (unless (file-exists-p index-file
)
873 (setq ibuffer
(find-file-noselect index-file
))
874 (with-current-buffer ibuffer
876 (insert "\n\n#+include: \"theindex.inc\"\n\n")
878 (kill-buffer ibuffer
)))))
880 (provide 'org-publish
)
883 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
885 ;;; org-publish.el ends here