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 1. A well-formed property list with an even number of elements, alternating
75 keys and values, specifying parameters for the publishing process.
77 (:property value :property value ... )
79 2. A meta-project definition, specifying of a list of sub-projects:
81 (:components (\"project-1\" \"project-2\" ...))
83 When the CDR of an element of org-publish-project-alist is in
84 this second form, the elements of the list after :components are
85 taken to be components of the project, which group together files
86 requiring different publishing options. When you publish such a
87 project with \\[org-publish], the components all publish.
89 When a property is given a value in org-publish-project-alist, its
90 setting overrides the value of the corresponding user variable
91 \(if any) during publishing. However, options set within a file
94 Most properties are optional, but some should always be set:
96 :base-directory Directory containing publishing source files
97 :base-extension Extension (without the dot!) of source files.
98 This can be a regular expression. If not given,
99 \"org\" will be used as default extension.
100 :publishing-directory Directory (possibly remote) where output
101 files will be published
103 The :exclude property may be used to prevent certain files from
104 being published. Its value may be a string or regexp matching
105 file names you don't want to be published.
107 The :include property may be used to include extra files. Its
108 value may be a list of filenames to include. The filenames are
109 considered relative to the base directory.
111 When both :include and :exclude properties are given values, the
112 exclusion step happens first.
114 One special property controls which back-end function to use for
115 publishing files in the project. This can be used to extend the
116 set of file types publishable by org-publish, as well as the set
119 :publishing-function Function to publish file. The default is
120 `org-publish-org-to-html', but other
121 values are possible. May also be a
122 list of functions, in which case
123 each function in the list is invoked
126 Another property allows you to insert code that prepares a
127 project for publishing. For example, you could call GNU Make on a
128 certain makefile, to ensure published files are built up to date.
130 :preparation-function Function to be called before publishing
131 this project. This may also be a list
133 :completion-function Function to be called after publishing
134 this project. This may also be a list
137 Some properties control details of the Org publishing process,
138 and are equivalent to the corresponding user variables listed in
139 the right column. See the documentation for those variables to
140 learn more about their use and default values.
142 :language `org-export-default-language'
143 :headline-levels `org-export-headline-levels'
144 :section-numbers `org-export-with-section-numbers'
145 :table-of-contents `org-export-with-toc'
146 :emphasize `org-export-with-emphasize'
147 :sub-superscript `org-export-with-sub-superscripts'
148 :TeX-macros `org-export-with-TeX-macros'
149 :fixed-width `org-export-with-fixed-width'
150 :tables `org-export-with-tables'
151 :table-auto-headline `org-export-highlight-first-table-line'
152 :style `org-export-html-style'
153 :convert-org-links `org-export-html-link-org-files-as-html'
154 :inline-images `org-export-html-inline-images'
155 :expand-quoted-html `org-export-html-expand'
156 :timestamp `org-export-html-with-timestamp'
157 :publishing-directory `org-export-publishing-directory'
158 :preamble `org-export-html-preamble'
159 :postamble `org-export-html-postamble'
160 :auto-preamble `org-export-html-auto-preamble'
161 :auto-postamble `org-export-html-auto-postamble'
162 :author `user-full-name'
163 :email `user-mail-address'
165 The following properties may be used to control publishing of a
166 sitemap of files or summary page for a given project.
168 :auto-sitemap Whether to publish a sitemap during
169 `org-publish-current-project' or `org-publish-all'.
170 :sitemap-filename Filename for output of sitemap. Defaults
171 to 'sitemap.org' (which becomes 'sitemap.html').
172 :sitemap-title Title of sitemap page. Defaults to name of file.
173 :sitemap-function Plugin function to use for generation of sitemap.
174 Defaults to `org-publish-org-sitemap', which
175 generates a plain list of links to all files
177 :sitemap-style Can be `list' (sitemap is just an itemized list
178 of the titles of the files involved) or
179 `tree' (the directory structure of the source
180 files is reflected in the sitemap). Defaults to
183 If you create a sitemap file, adjust the sorting like this:
185 :sitemap-sort-folders Where folders should appear in the sitemap.
186 Set this to `first' (default) or `last' to
187 display folders first or last, respectively.
188 Any other value will mix files and folders.
189 :sitemap-alphabetically The site map is normally sorted alphabetically.
190 Set this explicitly to nil to turn off sorting.
191 :sitemap-ignore-case Should sorting be case-sensitive? Default nil.
193 The following properties control the creation of a concept index.
195 :makeindex Create a concept index.
197 Other properties affecting publication.
199 :body-only Set this to 't' to publish only the body of the
200 documents, excluding everything outside and
201 including the <body> tags in HTML, or
202 \begin{document}..\end{document} in LaTeX."
206 (defcustom org-publish-use-timestamps-flag t
207 "Non-nil means use timestamp checking to publish only changed files.
208 When nil, do no timestamp checking and always publish all files."
212 (defcustom org-publish-timestamp-directory
(convert-standard-filename
213 "~/.org-timestamps/")
214 "Name of directory in which to store publishing timestamps."
218 (defcustom org-publish-list-skipped-files t
219 "Non-nil means show message about files *not* published."
223 (defcustom org-publish-before-export-hook nil
224 "Hook run before export on the Org file.
225 The hook may modify the file in arbitrary ways before publishing happens.
226 The original version of the buffer will be restored after publishing."
230 (defcustom org-publish-after-export-hook nil
231 "Hook run after export on the exported buffer.
232 Any changes made by this hook will be saved."
236 (defcustom org-publish-sitemap-sort-alphabetically t
237 "Should sitemaps be sorted alphabetically by default?
239 You can overwrite this default per project in your
240 `org-publish-project-alist', using `:sitemap-alphabetically'."
244 (defcustom org-publish-sitemap-sort-folders
'first
245 "A symbol, denoting if folders are sorted first in sitemaps.
246 Possible values are `first', `last', and nil.
247 If `first', folders will be sorted before files.
248 If `last', folders are sorted to the end after the files.
249 Any other value will not mix files and folders.
251 You can overwrite this default per project in your
252 `org-publish-project-alist', using `:sitemap-sort-folders'."
256 (defcustom org-publish-sitemap-sort-ignore-case nil
257 "Sort sitemaps case insensitively by default?
259 You can overwrite this default per project in your
260 `org-publish-project-alist', using `:sitemap-ignore-case'."
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265 ;;; Timestamp-related functions
267 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func
)
268 "Return path to timestamp file for filename FILENAME."
269 (setq filename
(concat filename
"::" (or pub-dir
"") "::"
270 (format "%s" (or pub-func
""))))
271 (concat "X" (if (fboundp 'sha1
) (sha1 filename
) (md5 filename
))))
273 (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir
)
274 "Return t if FILENAME should be published in PUB-DIR using PUB-FUNC.
275 TRUE-PUB-DIR is where the file will truly end up. Currently we are not using
276 this - maybe it can eventually be used to check if the file is present at
277 the target location, and how old it is. Right ow we cannot do this, because
278 we do not know under what file name the file will be stored - the publishing
279 function can still decide about that independently."
281 (if org-publish-use-timestamps-flag
282 (org-publish-cache-file-needs-publishing
283 filename pub-dir pub-func
)
284 ;; don't use timestamps, always return t
287 (message "Publishing file %s using `%s'" filename pub-func
)
288 (when org-publish-list-skipped-files
289 (message "Skipping unmodified file %s" filename
)))
292 (defun org-publish-update-timestamp (filename &optional pub-dir pub-func
)
293 "Update publishing timestamp for file FILENAME.
294 If there is no timestamp, create one."
295 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func
))
296 (stamp (org-publish-cache-ctime-of-src filename
)))
297 (org-publish-cache-set key stamp
)))
299 (defun org-publish-remove-all-timestamps ()
300 "Remove all files in the timestamp directory."
301 (let ((dir org-publish-timestamp-directory
)
303 (when (and (file-exists-p dir
)
304 (file-directory-p dir
))
305 (mapc 'delete-file
(directory-files dir
'full
"[^.]\\'"))
306 (org-publish-reset-cache))))
309 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
312 (defvar org-publish-initial-buffer nil
313 "The buffer `org-publish' has been called from.")
314 (defvar org-publish-temp-files nil
315 "Temporary list of files to be published.")
317 ;; Here, so you find the variable right before it's used the first time:
318 (defvar org-publish-cache nil
319 "This will cache timestamps and titles for files in publishing projects.
320 Blocks could hash sha1 values here.")
323 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
324 ;;; Compatibility aliases
326 ;; Delete-dups is not in Emacs <22
327 (if (fboundp 'delete-dups
)
328 (defalias 'org-publish-delete-dups
'delete-dups
)
329 (defun org-publish-delete-dups (list)
330 "Destructively remove `equal' duplicates from LIST.
331 Store the result in LIST and return it. LIST must be a proper list.
332 Of several `equal' occurrences of an element in LIST, the first
335 This is a compatibility function for Emacsen without `delete-dups'."
336 ;; Code from `subr.el' in Emacs 22:
339 (setcdr tail
(delete (car tail
) (cdr tail
)))
340 (setq tail
(cdr tail
))))
343 (declare-function org-publish-delete-dups
"org-publish" (list))
344 (declare-function find-lisp-find-files
"find-lisp" (directory regexp
))
346 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
347 ;;; Getting project information out of org-publish-project-alist
349 (defun org-publish-expand-projects (projects-alist)
350 "Expand projects in PROJECTS-ALIST.
351 This splices all the components into the list."
352 (let ((rest projects-alist
) rtn p components
)
353 (while (setq p
(pop rest
))
354 (if (setq components
(plist-get (cdr p
) :components
))
356 (mapcar (lambda (x) (assoc x org-publish-project-alist
))
360 (nreverse (org-publish-delete-dups (delq nil rtn
)))))
363 (defvar sitemap-alphabetically
)
364 (defvar sitemap-sort-folders
)
365 (defvar sitemap-ignore-case
)
366 (defvar sitemap-requested
)
367 (defun org-publish-compare-directory-files (a b
)
368 "Predicate for `sort', that sorts folders-first/last and alphabetically."
370 (when (or sitemap-alphabetically sitemap-sort-folders
)
371 ;; First we sort alphabetically:
372 (when sitemap-alphabetically
373 (let* ((adir (file-directory-p a
))
374 (aorg (and (string-match "\\.org$" a
) (not adir
)))
375 (bdir (file-directory-p b
))
376 (borg (and (string-match "\\.org$" b
) (not bdir
)))
378 (concat (file-name-directory a
)
379 (org-publish-find-title a
)) a
))
381 (concat (file-name-directory b
)
382 (org-publish-find-title b
)) b
)))
383 (setq retval
(if sitemap-ignore-case
384 (not (string-lessp (upcase B
) (upcase A
)))
385 (not (string-lessp B A
))))))
387 ;; Directory-wise wins:
388 (when sitemap-sort-folders
389 ;; a is directory, b not:
391 ((and (file-directory-p a
) (not (file-directory-p b
)))
392 (setq retval
(equal sitemap-sort-folders
'first
)))
393 ;; a is not a directory, but b is:
394 ((and (not (file-directory-p a
)) (file-directory-p b
))
395 (setq retval
(equal sitemap-sort-folders
'last
))))))
398 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir
)
399 "Set `org-publish-temp-files' with files from BASE-DIR directory.
400 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
401 non-nil, restrict this list to the files matching the regexp
402 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
403 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
404 matching the regexp SKIP-DIR when recursing through BASE-DIR."
406 (let ((fd-p (file-directory-p f
))
407 (fnd (file-name-nondirectory f
)))
408 (if (and fd-p recurse
409 (not (string-match "^\\.+$" fnd
))
410 (if skip-dir
(not (string-match skip-dir fnd
)) t
))
411 (org-publish-get-base-files-1 f recurse match skip-file skip-dir
)
412 (unless (or fd-p
;; this is a directory
413 (and skip-file
(string-match skip-file fnd
))
414 (not (file-exists-p (file-truename f
)))
415 (not (string-match match fnd
)))
417 (pushnew f org-publish-temp-files
)))))
418 (if sitemap-requested
419 (sort (directory-files base-dir t
(unless recurse match
))
420 'org-publish-compare-directory-files
)
421 (directory-files base-dir t
(unless recurse match
)))))
423 (defun org-publish-get-base-files (project &optional exclude-regexp
)
424 "Return a list of all files in PROJECT.
425 If EXCLUDE-REGEXP is set, this will be used to filter out
427 (let* ((project-plist (cdr project
))
428 (base-dir (file-name-as-directory
429 (plist-get project-plist
:base-directory
)))
430 (include-list (plist-get project-plist
:include
))
431 (recurse (plist-get project-plist
:recursive
))
432 (extension (or (plist-get project-plist
:base-extension
) "org"))
433 ;; sitemap-... variables are dynamically scoped for
434 ;; org-publish-compare-directory-files:
436 (plist-get project-plist
:auto-sitemap
))
437 (sitemap-sort-folders
438 (if (plist-member project-plist
:sitemap-sort-folders
)
439 (plist-get project-plist
:sitemap-sort-folders
)
440 org-publish-sitemap-sort-folders
))
441 (sitemap-alphabetically
442 (if (plist-member project-plist
:sitemap-alphabetically
)
443 (plist-get project-plist
:sitemap-alphabetically
)
444 org-publish-sitemap-sort-alphabetically
))
446 (if (plist-member project-plist
:sitemap-ignore-case
)
447 (plist-get project-plist
:sitemap-ignore-case
)
448 org-publish-sitemap-sort-ignore-case
))
449 (match (if (eq extension
'any
)
451 (concat "^[^\\.].*\\.\\(" extension
"\\)$"))))
452 ;; Make sure sitemap-sort-folders' has an accepted value
453 (unless (memq sitemap-sort-folders
'(first last
))
454 (setq sitemap-sort-folders nil
))
456 (setq org-publish-temp-files nil
)
457 (org-publish-get-base-files-1 base-dir recurse match
458 ;; FIXME distinguish exclude regexp
459 ;; for skip-file and skip-dir?
460 exclude-regexp exclude-regexp
)
463 (expand-file-name (concat base-dir f
))
464 org-publish-temp-files
))
466 org-publish-temp-files
))
468 (defun org-publish-get-project-from-filename (filename &optional up
)
469 "Return the project that FILENAME belongs to."
470 (let* ((filename (expand-file-name filename
))
474 (dolist (prj org-publish-project-alist
)
475 (unless (plist-get (cdr prj
) :components
)
476 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
477 (let* ((r (plist-get (cdr prj
) :recursive
))
478 (b (expand-file-name (file-name-as-directory
479 (plist-get (cdr prj
) :base-directory
))))
480 (x (or (plist-get (cdr prj
) :base-extension
) "org"))
481 (e (plist-get (cdr prj
) :exclude
))
482 (i (plist-get (cdr prj
) :include
))
483 (xm (concat "^" b
(if r
".+" "[^/]+") "\\.\\(" x
"\\)$")))
489 (lambda (file) (expand-file-name file b
))
492 (not (and e
(string-match e filename
)))
493 (string-match xm filename
)))
494 (setq project-name
(car prj
))
495 (throw 'p-found project-name
))))))
497 (dolist (prj org-publish-project-alist
)
498 (if (member project-name
(plist-get (cdr prj
) :components
))
499 (setq project-name
(car prj
)))))
500 (assoc project-name org-publish-project-alist
)))
502 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
503 ;;; Pluggable publishing back-end functions
505 (defun org-publish-org-to (format plist filename pub-dir
)
506 "Publish an org file to FORMAT.
507 PLIST is the property list for the given project.
508 FILENAME is the filename of the org file to be published.
509 PUB-DIR is the publishing directory."
511 (unless (file-exists-p pub-dir
)
512 (make-directory pub-dir t
))
513 (let ((visiting (find-buffer-visiting filename
)))
515 (switch-to-buffer (or visiting
(find-file filename
)))
516 (let* ((plist (cons :buffer-will-be-killed
(cons t plist
)))
517 (init-buf (current-buffer))
519 (init-buf-string (buffer-string))
521 ;; run hooks before exporting
522 (run-hooks 'org-publish-before-export-hook
)
523 ;; export the possibly modified buffer
524 (setq export-buf-or-file
525 (funcall (intern (concat "org-export-as-" format
))
526 (plist-get plist
:headline-levels
)
528 (plist-get plist
:body-only
)
530 (when (and (bufferp export-buf-or-file
)
531 (buffer-live-p export-buf-or-file
))
532 (set-buffer export-buf-or-file
)
533 ;; run hooks after export and save export
534 (progn (run-hooks 'org-publish-after-export-hook
)
535 (if (buffer-modified-p) (save-buffer)))
536 (kill-buffer export-buf-or-file
))
537 ;; maybe restore buffer's content
538 (set-buffer init-buf
)
539 (when (buffer-modified-p init-buf
)
541 (insert init-buf-string
)
543 (goto-char init-point
))
545 (kill-buffer init-buf
))))))
547 (defmacro org-publish-with-aux-preprocess-maybe
(&rest body
)
548 "Execute BODY with a modified hook to preprocess for index."
549 `(let ((org-export-preprocess-after-headline-targets-hook
550 (if (plist-get project-plist
:makeindex
)
551 (cons 'org-publish-aux-preprocess
552 org-export-preprocess-after-headline-targets-hook
)
553 org-export-preprocess-after-headline-targets-hook
)))
556 (defvar project-plist
)
557 (defun org-publish-org-to-latex (plist filename pub-dir
)
558 "Publish an org file to LaTeX.
559 See `org-publish-org-to' to the list of arguments."
560 (org-publish-with-aux-preprocess-maybe
561 (org-publish-org-to "latex" plist filename pub-dir
)))
563 (defun org-publish-org-to-pdf (plist filename pub-dir
)
564 "Publish an org file to PDF (via LaTeX).
565 See `org-publish-org-to' to the list of arguments."
566 (org-publish-with-aux-preprocess-maybe
567 (org-publish-org-to "pdf" plist filename pub-dir
)))
569 (defun org-publish-org-to-html (plist filename pub-dir
)
570 "Publish an org file to HTML.
571 See `org-publish-org-to' to the list of arguments."
572 (org-publish-with-aux-preprocess-maybe
573 (org-publish-org-to "html" plist filename pub-dir
)))
575 (defun org-publish-org-to-org (plist filename pub-dir
)
576 "Publish an org file to HTML.
577 See `org-publish-org-to' to the list of arguments."
578 (org-publish-org-to "org" plist filename pub-dir
))
580 (defun org-publish-org-to-ascii (plist filename pub-dir
)
581 "Publish an org file to ASCII.
582 See `org-publish-org-to' to the list of arguments."
583 (org-publish-with-aux-preprocess-maybe
584 (org-publish-org-to "ascii" plist filename pub-dir
)))
586 (defun org-publish-org-to-latin1 (plist filename pub-dir
)
587 "Publish an org file to Latin-1.
588 See `org-publish-org-to' to the list of arguments."
589 (org-publish-with-aux-preprocess-maybe
590 (org-publish-org-to "latin1" plist filename pub-dir
)))
592 (defun org-publish-org-to-utf8 (plist filename pub-dir
)
593 "Publish an org file to UTF-8.
594 See `org-publish-org-to' to the list of arguments."
595 (org-publish-with-aux-preprocess-maybe
596 (org-publish-org-to "utf8" plist filename pub-dir
)))
598 (defun org-publish-attachment (plist filename pub-dir
)
599 "Publish a file with no transformation of any kind.
600 See `org-publish-org-to' to the list of arguments."
601 ;; make sure eshell/cp code is loaded
602 (unless (file-directory-p pub-dir
)
603 (make-directory pub-dir t
))
604 (or (equal (expand-file-name (file-name-directory filename
))
605 (file-name-as-directory (expand-file-name pub-dir
)))
607 (expand-file-name (file-name-nondirectory filename
) pub-dir
)
610 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
611 ;;; Publishing files, sets of files, and indices
613 (defun org-publish-file (filename &optional project no-cache
)
614 "Publish file FILENAME from PROJECT.
615 If NO-CACHE is not nil, do not initialize org-publish-cache and
616 write it to disk. This is needed, since this function is used to
617 publish single files, when entire projects are published.
618 See `org-publish-projects'."
621 (or (org-publish-get-project-from-filename filename
)
622 (error "File %s not part of any known project"
623 (abbreviate-file-name filename
)))))
624 (project-plist (cdr project
))
625 (ftname (expand-file-name filename
))
627 (or (plist-get project-plist
:publishing-function
)
628 'org-publish-org-to-html
))
630 (file-name-as-directory
632 (or (plist-get project-plist
:base-directory
)
633 (error "Project %s does not have :base-directory defined"
636 (file-name-as-directory
638 (or (plist-get project-plist
:publishing-directory
)
639 (error "Project %s does not have :publishing-directory defined"
644 (org-publish-initialize-cache (car project
)))
649 (and (string-match (regexp-quote base-dir
) ftname
)
650 (substring ftname
(match-end 0))))))
651 (if (listp publishing-function
)
652 ;; allow chain of publishing functions
654 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir
)
655 (funcall f project-plist filename tmp-pub-dir
)
656 (org-publish-update-timestamp filename pub-dir f
)))
658 (when (org-publish-needed-p filename pub-dir publishing-function
660 (funcall publishing-function project-plist filename tmp-pub-dir
)
661 (org-publish-update-timestamp
662 filename pub-dir publishing-function
)))
663 (unless no-cache
(org-publish-write-cache-file))))
665 (defun org-publish-projects (projects)
666 "Publish all files belonging to the PROJECTS alist.
667 If :auto-sitemap is set, publish the sitemap too.
668 If :makeindex is set, also produce a file theindex.org."
671 ;; Each project uses it's own cache file:
672 (org-publish-initialize-cache (car project
))
674 ((project-plist (cdr project
))
675 (exclude-regexp (plist-get project-plist
:exclude
))
676 (sitemap-p (plist-get project-plist
:auto-sitemap
))
677 (sitemap-filename (or (plist-get project-plist
:sitemap-filename
)
679 (sitemap-function (or (plist-get project-plist
:sitemap-function
)
680 'org-publish-org-sitemap
))
681 (preparation-function (plist-get project-plist
:preparation-function
))
682 (completion-function (plist-get project-plist
:completion-function
))
683 (files (org-publish-get-base-files project exclude-regexp
)) file
)
684 (when preparation-function
(run-hooks 'preparation-function
))
685 (if sitemap-p
(funcall sitemap-function project sitemap-filename
))
686 (while (setq file
(pop files
))
687 (org-publish-file file project t
))
688 (when (plist-get project-plist
:makeindex
)
689 (org-publish-index-generate-theindex.inc
690 (plist-get project-plist
:base-directory
))
691 (org-publish-file (expand-file-name
693 (plist-get project-plist
:base-directory
))
695 (when completion-function
(run-hooks 'completion-function
))
696 (org-publish-write-cache-file)))
697 (org-publish-expand-projects projects
)))
699 (defun org-publish-org-sitemap (project &optional sitemap-filename
)
700 "Create a sitemap of pages in set defined by PROJECT.
701 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
702 Default for SITEMAP-FILENAME is 'sitemap.org'."
703 (let* ((project-plist (cdr project
))
704 (dir (file-name-as-directory
705 (plist-get project-plist
:base-directory
)))
706 (localdir (file-name-directory dir
))
707 (indent-str (make-string 2 ?\
))
708 (exclude-regexp (plist-get project-plist
:exclude
))
709 (files (nreverse (org-publish-get-base-files project exclude-regexp
)))
710 (sitemap-filename (concat dir
(or sitemap-filename
"sitemap.org")))
711 (sitemap-title (or (plist-get project-plist
:sitemap-title
)
712 (concat "Sitemap for project " (car project
))))
713 (sitemap-style (or (plist-get project-plist
:sitemap-style
)
715 (visiting (find-buffer-visiting sitemap-filename
))
716 (ifn (file-name-nondirectory sitemap-filename
))
718 (with-current-buffer (setq sitemap-buffer
719 (or visiting
(find-file sitemap-filename
)))
721 (insert (concat "#+TITLE: " sitemap-title
"\n\n"))
722 (while (setq file
(pop files
))
723 (let ((fn (file-name-nondirectory file
))
724 (link (file-relative-name file dir
))
726 ;; sitemap shouldn't list itself
727 (unless (equal (file-truename sitemap-filename
)
728 (file-truename file
))
729 (if (eq sitemap-style
'list
)
730 (message "Generating list-style sitemap for %s" sitemap-title
)
731 (message "Generating tree-style sitemap for %s" sitemap-title
)
732 (setq localdir
(concat (file-name-as-directory dir
)
733 (file-name-directory link
)))
734 (unless (string= localdir oldlocal
)
735 (if (string= localdir dir
)
736 (setq indent-str
(make-string 2 ?\
))
741 (file-relative-name localdir dir
))) "/"))
743 (old-subdirs (split-string
744 (file-relative-name oldlocal dir
) "/")))
745 (setq indent-str
(make-string 2 ?\
))
746 (while (string= (car old-subdirs
) (car subdirs
))
747 (setq indent-str
(concat indent-str
(make-string 2 ?\
)))
751 (setq subdir
(concat subdir d
"/"))
752 (insert (concat indent-str
" + " d
"\n"))
753 (setq indent-str
(make-string
754 (+ (length indent-str
) 2) ?\
)))))))
755 ;; This is common to 'flat and 'tree
756 (insert (concat indent-str
" + [[file:" link
"]["
757 (org-publish-find-title file
)
760 (or visiting
(kill-buffer sitemap-buffer
))))
762 (defun org-publish-find-title (file)
763 "Find the title of FILE in project."
765 (org-publish-cache-get-file-property file
:title nil t
)
766 (let* ((visiting (find-buffer-visiting file
))
767 (buffer (or visiting
(find-file-noselect file
)))
769 (with-current-buffer buffer
770 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
771 (org-infile-export-plist))))
773 (or (plist-get opt-plist
:title
)
775 (plist-get opt-plist
:skip-before-1st-heading
))
776 (org-export-grab-title-from-buffer))
777 (file-name-nondirectory (file-name-sans-extension file
))))))
779 (kill-buffer buffer
))
780 (org-publish-cache-set-file-property file
:title title
)
783 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
784 ;;; Interactive publishing functions
787 (defalias 'org-publish-project
'org-publish
)
790 (defun org-publish (project &optional force
)
794 (assoc (org-icompleting-read
796 org-publish-project-alist nil t
)
797 org-publish-project-alist
)
799 (setq org-publish-initial-buffer
(current-buffer))
800 (save-window-excursion
801 (let* ((org-publish-use-timestamps-flag
802 (if force nil org-publish-use-timestamps-flag
)))
803 (org-publish-projects
804 (if (stringp project
)
805 ;; If this function is called in batch mode,
806 ;; project is still a string here.
807 (list (assoc project org-publish-project-alist
))
811 (defun org-publish-all (&optional force
)
812 "Publish all projects.
813 With prefix argument, remove all files in the timestamp
814 directory and force publishing all files."
817 (org-publish-remove-all-timestamps))
818 (save-window-excursion
819 (let ((org-publish-use-timestamps-flag
820 (if force nil org-publish-use-timestamps-flag
)))
821 (org-publish-projects org-publish-project-alist
))))
825 (defun org-publish-current-file (&optional force
)
826 "Publish the current file.
827 With prefix argument, force publish the file."
829 (save-window-excursion
830 (let ((org-publish-use-timestamps-flag
831 (if force nil org-publish-use-timestamps-flag
)))
832 (org-publish-file (buffer-file-name)))))
835 (defun org-publish-current-project (&optional force
)
836 "Publish the project associated with the current file.
837 With a prefix argument, force publishing of all files in
840 (save-window-excursion
841 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up
))
842 (org-publish-use-timestamps-flag
843 (if force nil org-publish-use-timestamps-flag
)))
845 (error "File %s is not part of any known project" (buffer-file-name)))
846 ;; FIXME: force is not used here?
847 (org-publish project
))))
852 (defvar backend
) ; dynamically scoped
853 (defun org-publish-aux-preprocess ()
854 "Find index entries and write them to an .orgx file."
855 (let ((case-fold-search t
)
857 (goto-char (point-min))
860 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t
)
861 (> (match-end 1) (match-beginning 1)))
862 (setq entry
(match-string 1))
863 (when (eq backend
'latex
)
864 (replace-match (format "\\index{%s}" entry
) t t
))
866 (ignore-errors (org-back-to-heading t
))
867 (setq target
(get-text-property (point) 'target
))
868 (setq target
(or (cdr (assoc target org-export-preferred-target-alist
))
869 (cdr (assoc target org-export-id-target-alist
))
871 (push (cons entry target
) index
)))
873 (concat (file-name-sans-extension org-current-export-file
) ".orgx")
874 (dolist (entry (nreverse index
))
875 (insert (format "INDEX: (%s) %s\n" (cdr entry
) (car entry
)))))))
877 (defun org-publish-index-generate-theindex.inc
(directory)
878 "Generate the index from all .orgx files in the current directory and below."
880 (let* ((fulldir (file-name-as-directory
881 (expand-file-name directory
)))
882 (full-files (find-lisp-find-files directory
"\\.orgx\\'"))
883 (re (concat "\\`" fulldir
))
884 (files (mapcar (lambda (f) (if (string-match re f
)
885 (substring f
(match-end 0))
888 (default-directory directory
)
889 index origfile buf target entry ibuffer
890 main last-main letter last-letter file sub link tgext
)
891 ;; `files' contains the list of relative file names
893 (setq origfile
(substring file
0 -
1))
894 (setq buf
(find-file-noselect file
))
895 (with-current-buffer buf
896 (goto-char (point-min))
897 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t
)
898 (setq target
(match-string 1)
899 entry
(match-string 2))
900 (push (list entry origfile target
) index
)))
902 (setq index
(sort index
(lambda (a b
) (string< (downcase (car a
))
903 (downcase (car b
))))))
904 (setq ibuffer
(find-file-noselect (expand-file-name "theindex.inc" directory
)))
905 (with-current-buffer ibuffer
908 (setq last-letter nil
)
910 (setq entry
(car idx
) file
(nth 1 idx
) target
(nth 2 idx
))
911 (if (and (stringp target
) (string-match "\\S-" target
))
912 (setq tgext
(concat "::#" target
))
914 (setq letter
(upcase (substring entry
0 1)))
915 (when (not (equal letter last-letter
))
916 (insert "** " letter
"\n")
917 (setq last-letter letter
))
918 (if (string-match "!" entry
)
919 (setq main
(substring entry
0 (match-beginning 0))
920 sub
(substring entry
(match-end 0)))
921 (setq main nil sub nil last-main nil
))
922 (when (and main
(not (equal main last-main
)))
923 (insert " - " main
"\n")
924 (setq last-main main
))
925 (setq link
(concat "[[file:" file tgext
"]"
926 "[" (or sub entry
) "]]"))
928 (insert " - " link
"\n")
929 (insert " - " link
"\n")))
931 (kill-buffer ibuffer
)
933 (let ((index-file (expand-file-name "theindex.org" directory
)))
934 (unless (file-exists-p index-file
)
935 (setq ibuffer
(find-file-noselect index-file
))
936 (with-current-buffer ibuffer
938 (insert "\n\n#+include: \"theindex.inc\"\n\n")
940 (kill-buffer ibuffer
)))))
943 ;; Caching functions:
945 (defun org-publish-write-cache-file (&optional free-cache
)
946 "Write `org-publish-cache' to file.
947 If FREE-CACHE, empty the cache."
948 (unless org-publish-cache
949 (error "%s" "`org-publish-write-cache-file' called, but no cache present"))
951 (let ((cache-file (org-publish-cache-get ":cache-file:")))
954 "%s" "Cannot find cache-file name in `org-publish-write-cache-file'"))
955 (with-temp-file cache-file
956 (let ((print-level nil
)
958 (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
959 (maphash (lambda (k v
)
961 (format (concat "(puthash %S "
962 (if (or (listp v
) (symbolp v
))
964 "%S org-publish-cache)\n") k v
)))
966 (when free-cache
(org-publish-reset-cache))))
968 (defun org-publish-initialize-cache (project-name)
969 "Initialize the projects cache if not initialized yet and return it."
972 (error "%s%s" "Cannot initialize `org-publish-cache' without projects name"
973 " in `org-publish-initialize-cache'"))
975 (unless (file-exists-p org-publish-timestamp-directory
)
976 (make-directory org-publish-timestamp-directory t
))
977 (if (not (file-directory-p org-publish-timestamp-directory
))
978 (error "Org publish timestamp: %s is not a directory"
979 org-publish-timestamp-directory
))
981 (unless (and org-publish-cache
982 (string= (org-publish-cache-get ":project:") project-name
))
983 (let* ((cache-file (concat
984 (expand-file-name org-publish-timestamp-directory
)
987 (cexists (file-exists-p cache-file
)))
989 (when org-publish-cache
990 (org-publish-reset-cache))
993 (load-file cache-file
)
994 (setq org-publish-cache
995 (make-hash-table :test
'equal
:weakness nil
:size
100))
996 (org-publish-cache-set ":project:" project-name
)
997 (org-publish-cache-set ":cache-file:" cache-file
))
998 (unless cexists
(org-publish-write-cache-file nil
))))
1001 (defun org-publish-reset-cache ()
1002 "Empty org-publish-cache and reset it nil."
1003 (message "%s" "Resetting org-publish-cache")
1004 (if (hash-table-p org-publish-cache
)
1005 (clrhash org-publish-cache
))
1006 (setq org-publish-cache nil
))
1008 (defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func
)
1009 "Check the timestamp of the last publishing of FILENAME.
1010 Return `t', if the file needs publishing"
1011 (unless org-publish-cache
1012 (error "%s" "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1013 (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func
))
1014 (pstamp (org-publish-cache-get key
)))
1017 (let ((ctime (org-publish-cache-ctime-of-src filename
)))
1018 (< pstamp ctime
)))))
1020 (defun org-publish-cache-set-file-property (filename property value
&optional project-name
)
1021 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1022 Use cache file of PROJECT-NAME. If the entry does not exist, it will be
1023 created. Return VALUE."
1024 ;; Evtl. load the requested cache file:
1025 (if project-name
(org-publish-initialize-cache project-name
))
1026 (let ((pl (org-publish-cache-get filename
)))
1029 (plist-put pl property value
)
1031 (org-publish-cache-get-file-property
1032 filename property value nil project-name
))))
1034 (defun org-publish-cache-get-file-property
1035 (filename property
&optional default no-create project-name
)
1036 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1037 Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
1038 DEFAULT, if the value does not yet exist.
1039 If the entry will be created, unless NO-CREATE is not nil."
1040 ;; Evtl. load the requested cache file:
1041 (if project-name
(org-publish-initialize-cache project-name
))
1042 (let ((pl (org-publish-cache-get filename
))
1045 (if (plist-member pl property
)
1046 (setq retval
(plist-get pl property
))
1047 (setq retval default
))
1050 (org-publish-cache-set filename
(list property default
)))
1051 (setq retval default
))
1054 (defun org-publish-cache-get (key)
1055 "Return the value stored in `org-publish-cache' for key KEY.
1056 Returns nil, if no value or nil is found, or the cache does not
1058 (unless org-publish-cache
1059 (error "%s" "`org-publish-cache-get' called, but no cache present"))
1060 (gethash key org-publish-cache
))
1062 (defun org-publish-cache-set (key value
)
1063 "Store KEY VALUE pair in `org-publish-cache'.
1064 Returns value on success, else nil."
1065 (unless org-publish-cache
1066 (error "%s" "`org-publish-cache-set' called, but no cache present"))
1067 (puthash key value org-publish-cache
))
1069 (defun org-publish-cache-ctime-of-src (filename)
1070 "Get the files ctime as integer."
1071 (let ((src-attr (file-attributes (if (stringp (file-symlink-p filename
))
1072 (file-symlink-p filename
)
1075 (lsh (car (nth 5 src-attr
)) 16)
1076 (cadr (nth 5 src-attr
)))))
1080 (provide 'org-publish
)
1082 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
1084 ;;; org-publish.el ends here