1 ;;; org-publish.el --- publish related org-mode files as a website
2 ;; Copyright (C) 2006-2011 Free Software Foundation, Inc.
4 ;; Author: David O'Toole <dto@gnu.org>
5 ;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
6 ;; Keywords: hypermedia, outlines, wp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This program allow configurable publishing of related sets of
27 ;; Org-mode files as a complete website.
29 ;; org-publish.el can do the following:
31 ;; + Publish all one's org-files to HTML or PDF
32 ;; + Upload HTML, images, attachments and other files to a web server
33 ;; + Exclude selected private pages from publishing
34 ;; + Publish a clickable sitemap of pages
35 ;; + Manage local timestamps for publishing only changed files
36 ;; + Accept plugin functions to extend range of publishable content
38 ;; Documentation for publishing is in the manual.
43 (defun org-publish-sanitize-plist (plist)
45 (or (cdr (assq x
'((:index-filename .
:sitemap-filename
)
46 (:index-title .
:sitemap-title
)
47 (:index-function .
:sitemap-function
)
48 (:index-style .
:sitemap-style
)
49 (:auto-index .
:auto-sitemap
))))
59 (unless (fboundp 'declare-function
)
60 (defmacro declare-function
(fn file
&optional arglist fileonly
))))
62 (defgroup org-publish nil
63 "Options for publishing a set of Org-mode and related files."
67 (defcustom org-publish-project-alist nil
68 "Association list to control publishing behavior.
69 Each element of the alist is a publishing 'project.' The CAR of
70 each element is a string, uniquely identifying the project. The
71 CDR of each element is in one of the following forms:
73 1. A well-formed property list with an even number of elements, alternating
74 keys and values, specifying parameters for the publishing process.
76 (:property value :property value ... )
78 2. A meta-project definition, specifying of a list of sub-projects:
80 (:components (\"project-1\" \"project-2\" ...))
82 When the CDR of an element of org-publish-project-alist is in
83 this second form, the elements of the list after :components are
84 taken to be components of the project, which group together files
85 requiring different publishing options. When you publish such a
86 project with \\[org-publish], the components all publish.
88 When a property is given a value in org-publish-project-alist, its
89 setting overrides the value of the corresponding user variable
90 \(if any) during publishing. However, options set within a file
93 Most properties are optional, but some should always be set:
95 :base-directory Directory containing publishing source files
96 :base-extension Extension (without the dot!) of source files.
97 This can be a regular expression. If not given,
98 \"org\" will be used as default extension.
99 :publishing-directory Directory (possibly remote) where output
100 files will be published
102 The :exclude property may be used to prevent certain files from
103 being published. Its value may be a string or regexp matching
104 file names you don't want to be published.
106 The :include property may be used to include extra files. Its
107 value may be a list of filenames to include. The filenames are
108 considered relative to the base directory.
110 When both :include and :exclude properties are given values, the
111 exclusion step happens first.
113 One special property controls which back-end function to use for
114 publishing files in the project. This can be used to extend the
115 set of file types publishable by org-publish, as well as the set
118 :publishing-function Function to publish file. The default is
119 `org-publish-org-to-html', but other
120 values are possible. May also be a
121 list of functions, in which case
122 each function in the list is invoked
125 Another property allows you to insert code that prepares a
126 project for publishing. For example, you could call GNU Make on a
127 certain makefile, to ensure published files are built up to date.
129 :preparation-function Function to be called before publishing
130 this project. This may also be a list
132 :completion-function Function to be called after publishing
133 this project. This may also be a list
136 Some properties control details of the Org publishing process,
137 and are equivalent to the corresponding user variables listed in
138 the right column. See the documentation for those variables to
139 learn more about their use and default values.
141 :language `org-export-default-language'
142 :headline-levels `org-export-headline-levels'
143 :section-numbers `org-export-with-section-numbers'
144 :table-of-contents `org-export-with-toc'
145 :emphasize `org-export-with-emphasize'
146 :sub-superscript `org-export-with-sub-superscripts'
147 :TeX-macros `org-export-with-TeX-macros'
148 :fixed-width `org-export-with-fixed-width'
149 :tables `org-export-with-tables'
150 :table-auto-headline `org-export-highlight-first-table-line'
151 :style `org-export-html-style'
152 :convert-org-links `org-export-html-link-org-files-as-html'
153 :inline-images `org-export-html-inline-images'
154 :expand-quoted-html `org-export-html-expand'
155 :timestamp `org-export-html-with-timestamp'
156 :publishing-directory `org-export-publishing-directory'
157 :preamble `org-export-html-preamble'
158 :postamble `org-export-html-postamble'
159 :auto-preamble `org-export-html-auto-preamble'
160 :auto-postamble `org-export-html-auto-postamble'
161 :author `user-full-name'
162 :email `user-mail-address'
164 The following properties may be used to control publishing of a
165 sitemap of files or summary page for a given project.
167 :auto-sitemap Whether to publish a sitemap during
168 `org-publish-current-project' or `org-publish-all'.
169 :sitemap-filename Filename for output of sitemap. Defaults
170 to 'sitemap.org' (which becomes 'sitemap.html').
171 :sitemap-title Title of sitemap page. Defaults to name of file.
172 :sitemap-function Plugin function to use for generation of sitemap.
173 Defaults to `org-publish-org-sitemap', which
174 generates a plain list of links to all files
176 :sitemap-style Can be `list' (sitemap is just an itemized list
177 of the titles of the files involved) or
178 `tree' (the directory structure of the source
179 files is reflected in the sitemap). Defaults to
182 If you create a sitemap file, adjust the sorting like this:
184 :sitemap-sort-folders Where folders should appear in the sitemap.
185 Set this to `first' (default) or `last' to
186 display folders first or last, respectively.
187 Any other value will mix files and folders.
188 :sitemap-alphabetically The site map is normally sorted alphabetically.
189 Set this explicitly to nil to turn off sorting.
190 :sitemap-ignore-case Should sorting be case-sensitive? Default nil.
192 The following properties control the creation of a concept index.
194 :makeindex Create a concept index.
196 Other properties affecting publication.
198 :body-only Set this to 't' to publish only the body of the
199 documents, excluding everything outside and
200 including the <body> tags in HTML, or
201 \begin{document}..\end{document} in LaTeX."
205 (defcustom org-publish-use-timestamps-flag t
206 "Non-nil means use timestamp checking to publish only changed files.
207 When nil, do no timestamp checking and always publish all files."
211 (defcustom org-publish-timestamp-directory
(convert-standard-filename
212 "~/.org-timestamps/")
213 "Name of directory in which to store publishing timestamps."
217 (defcustom org-publish-list-skipped-files t
218 "Non-nil means show message about files *not* published."
222 (defcustom org-publish-before-export-hook nil
223 "Hook run before export on the Org file.
224 The hook may modify the file in arbitrary ways before publishing happens.
225 The original version of the buffer will be restored after publishing."
229 (defcustom org-publish-after-export-hook nil
230 "Hook run after export on the exported buffer.
231 Any changes made by this hook will be saved."
235 (defcustom org-publish-sitemap-sort-alphabetically t
236 "Should sitemaps be sorted alphabetically by default?
238 You can overwrite this default per project in your
239 `org-publish-project-alist', using `:sitemap-alphabetically'."
243 (defcustom org-publish-sitemap-sort-folders
'first
244 "A symbol, denoting if folders are sorted first in sitemaps.
245 Possible values are `first', `last', and nil.
246 If `first', folders will be sorted before files.
247 If `last', folders are sorted to the end after the files.
248 Any other value will not mix files and folders.
250 You can overwrite this default per project in your
251 `org-publish-project-alist', using `:sitemap-sort-folders'."
255 (defcustom org-publish-sitemap-sort-ignore-case nil
256 "Sort sitemaps case insensitively by default?
258 You can overwrite this default per project in your
259 `org-publish-project-alist', using `:sitemap-ignore-case'."
263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
264 ;;; Timestamp-related functions
266 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func
)
267 "Return path to timestamp file for filename FILENAME."
268 (setq filename
(concat filename
"::" (or pub-dir
"") "::"
269 (format "%s" (or pub-func
""))))
270 (concat "X" (if (fboundp 'sha1
) (sha1 filename
) (md5 filename
))))
272 (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir
)
273 "Return t if FILENAME should be published in PUB-DIR using PUB-FUNC.
274 TRUE-PUB-DIR is where the file will truly end up. Currently we are not using
275 this - maybe it can eventually be used to check if the file is present at
276 the target location, and how old it is. Right ow we cannot do this, because
277 we do not know under what file name the file will be stored - the publishing
278 function can still decide about that independently."
280 (if org-publish-use-timestamps-flag
281 (org-publish-cache-file-needs-publishing
282 filename pub-dir pub-func
)
283 ;; don't use timestamps, always return t
286 (message "Publishing file %s using `%s'" filename pub-func
)
287 (when org-publish-list-skipped-files
288 (message "Skipping unmodified file %s" filename
)))
291 (defun org-publish-update-timestamp (filename &optional pub-dir pub-func
)
292 "Update publishing timestamp for file FILENAME.
293 If there is no timestamp, create one."
294 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func
))
295 (stamp (org-publish-cache-ctime-of-src filename
)))
296 (org-publish-cache-set key stamp
)))
298 (defun org-publish-remove-all-timestamps ()
299 "Remove all files in the timestamp directory."
300 (let ((dir org-publish-timestamp-directory
)
302 (when (and (file-exists-p dir
)
303 (file-directory-p dir
))
304 (mapc 'delete-file
(directory-files dir
'full
"[^.]\\'"))
305 (org-publish-reset-cache))))
308 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
311 (defvar org-publish-initial-buffer nil
312 "The buffer `org-publish' has been called from.")
313 (defvar org-publish-temp-files nil
314 "Temporary list of files to be published.")
316 ;; Here, so you find the variable right before it's used the first time:
317 (defvar org-publish-cache nil
318 "This will cache timestamps and titles for files in publishing projects.
319 Blocks could hash sha1 values here.")
322 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
323 ;;; Compatibility aliases
325 ;; Delete-dups is not in Emacs <22
326 (if (fboundp 'delete-dups
)
327 (defalias 'org-publish-delete-dups
'delete-dups
)
328 (defun org-publish-delete-dups (list)
329 "Destructively remove `equal' duplicates from LIST.
330 Store the result in LIST and return it. LIST must be a proper list.
331 Of several `equal' occurrences of an element in LIST, the first
334 This is a compatibility function for Emacsen without `delete-dups'."
335 ;; Code from `subr.el' in Emacs 22:
338 (setcdr tail
(delete (car tail
) (cdr tail
)))
339 (setq tail
(cdr tail
))))
342 (declare-function org-publish-delete-dups
"org-publish" (list))
343 (declare-function find-lisp-find-files
"find-lisp" (directory regexp
))
345 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
346 ;;; Getting project information out of org-publish-project-alist
348 (defun org-publish-expand-projects (projects-alist)
349 "Expand projects in PROJECTS-ALIST.
350 This splices all the components into the list."
351 (let ((rest projects-alist
) rtn p components
)
352 (while (setq p
(pop rest
))
353 (if (setq components
(plist-get (cdr p
) :components
))
355 (mapcar (lambda (x) (assoc x org-publish-project-alist
))
359 (nreverse (org-publish-delete-dups (delq nil rtn
)))))
362 (defvar sitemap-alphabetically
)
363 (defvar sitemap-sort-folders
)
364 (defvar sitemap-ignore-case
)
365 (defvar sitemap-requested
)
366 (defun org-publish-compare-directory-files (a b
)
367 "Predicate for `sort', that sorts folders-first/last and alphabetically."
369 (when (or sitemap-alphabetically sitemap-sort-folders
)
370 ;; First we sort alphabetically:
371 (when sitemap-alphabetically
372 (let* ((adir (file-directory-p a
))
373 (aorg (and (string-match "\\.org$" a
) (not adir
)))
374 (bdir (file-directory-p b
))
375 (borg (and (string-match "\\.org$" b
) (not bdir
)))
377 (concat (file-name-directory a
)
378 (org-publish-find-title a
)) a
))
380 (concat (file-name-directory b
)
381 (org-publish-find-title b
)) b
)))
382 (setq retval
(if sitemap-ignore-case
383 (not (string-lessp (upcase B
) (upcase A
)))
384 (not (string-lessp B A
))))))
386 ;; Directory-wise wins:
387 (when sitemap-sort-folders
388 ;; a is directory, b not:
390 ((and (file-directory-p a
) (not (file-directory-p b
)))
391 (setq retval
(equal sitemap-sort-folders
'first
)))
392 ;; a is not a directory, but b is:
393 ((and (not (file-directory-p a
)) (file-directory-p b
))
394 (setq retval
(equal sitemap-sort-folders
'last
))))))
397 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir
)
398 "Set `org-publish-temp-files' with files from BASE-DIR directory.
399 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
400 non-nil, restrict this list to the files matching the regexp
401 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
402 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
403 matching the regexp SKIP-DIR when recursing through BASE-DIR."
405 (let ((fd-p (file-directory-p f
))
406 (fnd (file-name-nondirectory f
)))
407 (if (and fd-p recurse
408 (not (string-match "^\\.+$" fnd
))
409 (if skip-dir
(not (string-match skip-dir fnd
)) t
))
410 (org-publish-get-base-files-1 f recurse match skip-file skip-dir
)
411 (unless (or fd-p
;; this is a directory
412 (and skip-file
(string-match skip-file fnd
))
413 (not (file-exists-p (file-truename f
)))
414 (not (string-match match fnd
)))
416 (pushnew f org-publish-temp-files
)))))
417 (if sitemap-requested
418 (sort (directory-files base-dir t
(unless recurse match
))
419 'org-publish-compare-directory-files
)
420 (directory-files base-dir t
(unless recurse match
)))))
422 (defun org-publish-get-base-files (project &optional exclude-regexp
)
423 "Return a list of all files in PROJECT.
424 If EXCLUDE-REGEXP is set, this will be used to filter out
426 (let* ((project-plist (cdr project
))
427 (base-dir (file-name-as-directory
428 (plist-get project-plist
:base-directory
)))
429 (include-list (plist-get project-plist
:include
))
430 (recurse (plist-get project-plist
:recursive
))
431 (extension (or (plist-get project-plist
:base-extension
) "org"))
432 ;; sitemap-... variables are dynamically scoped for
433 ;; org-publish-compare-directory-files:
435 (plist-get project-plist
:auto-sitemap
))
436 (sitemap-sort-folders
437 (if (plist-member project-plist
:sitemap-sort-folders
)
438 (plist-get project-plist
:sitemap-sort-folders
)
439 org-publish-sitemap-sort-folders
))
440 (sitemap-alphabetically
441 (if (plist-member project-plist
:sitemap-alphabetically
)
442 (plist-get project-plist
:sitemap-alphabetically
)
443 org-publish-sitemap-sort-alphabetically
))
445 (if (plist-member project-plist
:sitemap-ignore-case
)
446 (plist-get project-plist
:sitemap-ignore-case
)
447 org-publish-sitemap-sort-ignore-case
))
448 (match (if (eq extension
'any
)
450 (concat "^[^\\.].*\\.\\(" extension
"\\)$"))))
451 ;; Make sure sitemap-sort-folders' has an accepted value
452 (unless (memq sitemap-sort-folders
'(first last
))
453 (setq sitemap-sort-folders nil
))
455 (setq org-publish-temp-files nil
)
456 (org-publish-get-base-files-1 base-dir recurse match
457 ;; FIXME distinguish exclude regexp
458 ;; for skip-file and skip-dir?
459 exclude-regexp exclude-regexp
)
462 (expand-file-name (concat base-dir f
))
463 org-publish-temp-files
))
465 org-publish-temp-files
))
467 (defun org-publish-get-project-from-filename (filename &optional up
)
468 "Return the project that FILENAME belongs to."
469 (let* ((filename (expand-file-name filename
))
473 (dolist (prj org-publish-project-alist
)
474 (unless (plist-get (cdr prj
) :components
)
475 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
476 (let* ((r (plist-get (cdr prj
) :recursive
))
477 (b (expand-file-name (file-name-as-directory
478 (plist-get (cdr prj
) :base-directory
))))
479 (x (or (plist-get (cdr prj
) :base-extension
) "org"))
480 (e (plist-get (cdr prj
) :exclude
))
481 (i (plist-get (cdr prj
) :include
))
482 (xm (concat "^" b
(if r
".+" "[^/]+") "\\.\\(" x
"\\)$")))
488 (lambda (file) (expand-file-name file b
))
491 (not (and e
(string-match e filename
)))
492 (string-match xm filename
)))
493 (setq project-name
(car prj
))
494 (throw 'p-found project-name
))))))
496 (dolist (prj org-publish-project-alist
)
497 (if (member project-name
(plist-get (cdr prj
) :components
))
498 (setq project-name
(car prj
)))))
499 (assoc project-name org-publish-project-alist
)))
501 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
502 ;;; Pluggable publishing back-end functions
504 (defun org-publish-org-to (format plist filename pub-dir
)
505 "Publish an org file to FORMAT.
506 PLIST is the property list for the given project.
507 FILENAME is the filename of the org file to be published.
508 PUB-DIR is the publishing directory."
510 (unless (file-exists-p pub-dir
)
511 (make-directory pub-dir t
))
512 (let ((visiting (find-buffer-visiting filename
)))
514 (switch-to-buffer (or visiting
(find-file filename
)))
515 (let* ((plist (cons :buffer-will-be-killed
(cons t plist
)))
516 (init-buf (current-buffer))
518 (init-buf-string (buffer-string))
520 ;; run hooks before exporting
521 (run-hooks 'org-publish-before-export-hook
)
522 ;; export the possibly modified buffer
523 (setq export-buf-or-file
524 (funcall (intern (concat "org-export-as-" format
))
525 (plist-get plist
:headline-levels
)
527 (plist-get plist
:body-only
)
529 (when (and (bufferp export-buf-or-file
)
530 (buffer-live-p export-buf-or-file
))
531 (set-buffer export-buf-or-file
)
532 ;; run hooks after export and save export
533 (progn (run-hooks 'org-publish-after-export-hook
)
534 (if (buffer-modified-p) (save-buffer)))
535 (kill-buffer export-buf-or-file
))
536 ;; maybe restore buffer's content
537 (set-buffer init-buf
)
538 (when (buffer-modified-p init-buf
)
540 (insert init-buf-string
)
542 (goto-char init-point
))
544 (kill-buffer init-buf
))))))
546 (defmacro org-publish-with-aux-preprocess-maybe
(&rest body
)
547 "Execute BODY with a modified hook to preprocess for index."
548 `(let ((org-export-preprocess-after-headline-targets-hook
549 (if (plist-get project-plist
:makeindex
)
550 (cons 'org-publish-aux-preprocess
551 org-export-preprocess-after-headline-targets-hook
)
552 org-export-preprocess-after-headline-targets-hook
)))
555 (defvar project-plist
)
556 (defun org-publish-org-to-latex (plist filename pub-dir
)
557 "Publish an org file to LaTeX.
558 See `org-publish-org-to' to the list of arguments."
559 (org-publish-with-aux-preprocess-maybe
560 (org-publish-org-to "latex" plist filename pub-dir
)))
562 (defun org-publish-org-to-pdf (plist filename pub-dir
)
563 "Publish an org file to PDF (via LaTeX).
564 See `org-publish-org-to' to the list of arguments."
565 (org-publish-with-aux-preprocess-maybe
566 (org-publish-org-to "pdf" plist filename pub-dir
)))
568 (defun org-publish-org-to-html (plist filename pub-dir
)
569 "Publish an org file to HTML.
570 See `org-publish-org-to' to the list of arguments."
571 (org-publish-with-aux-preprocess-maybe
572 (org-publish-org-to "html" plist filename pub-dir
)))
574 (defun org-publish-org-to-org (plist filename pub-dir
)
575 "Publish an org file to HTML.
576 See `org-publish-org-to' to the list of arguments."
577 (org-publish-org-to "org" plist filename pub-dir
))
579 (defun org-publish-org-to-ascii (plist filename pub-dir
)
580 "Publish an org file to ASCII.
581 See `org-publish-org-to' to the list of arguments."
582 (org-publish-with-aux-preprocess-maybe
583 (org-publish-org-to "ascii" plist filename pub-dir
)))
585 (defun org-publish-org-to-latin1 (plist filename pub-dir
)
586 "Publish an org file to Latin-1.
587 See `org-publish-org-to' to the list of arguments."
588 (org-publish-with-aux-preprocess-maybe
589 (org-publish-org-to "latin1" plist filename pub-dir
)))
591 (defun org-publish-org-to-utf8 (plist filename pub-dir
)
592 "Publish an org file to UTF-8.
593 See `org-publish-org-to' to the list of arguments."
594 (org-publish-with-aux-preprocess-maybe
595 (org-publish-org-to "utf8" plist filename pub-dir
)))
597 (defun org-publish-attachment (plist filename pub-dir
)
598 "Publish a file with no transformation of any kind.
599 See `org-publish-org-to' to the list of arguments."
600 ;; make sure eshell/cp code is loaded
601 (unless (file-directory-p pub-dir
)
602 (make-directory pub-dir t
))
603 (or (equal (expand-file-name (file-name-directory filename
))
604 (file-name-as-directory (expand-file-name pub-dir
)))
606 (expand-file-name (file-name-nondirectory filename
) pub-dir
)
609 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
610 ;;; Publishing files, sets of files, and indices
612 (defun org-publish-file (filename &optional project no-cache
)
613 "Publish file FILENAME from PROJECT.
614 If NO-CACHE is not nil, do not initialize org-publish-cache and
615 write it to disk. This is needed, since this function is used to
616 publish single files, when entire projects are published.
617 See `org-publish-projects'."
620 (or (org-publish-get-project-from-filename filename
)
621 (error "File %s not part of any known project"
622 (abbreviate-file-name filename
)))))
623 (project-plist (cdr project
))
624 (ftname (expand-file-name filename
))
626 (or (plist-get project-plist
:publishing-function
)
627 'org-publish-org-to-html
))
629 (file-name-as-directory
631 (or (plist-get project-plist
:base-directory
)
632 (error "Project %s does not have :base-directory defined"
635 (file-name-as-directory
637 (or (plist-get project-plist
:publishing-directory
)
638 (error "Project %s does not have :publishing-directory defined"
643 (org-publish-initialize-cache (car project
)))
648 (and (string-match (regexp-quote base-dir
) ftname
)
649 (substring ftname
(match-end 0))))))
650 (if (listp publishing-function
)
651 ;; allow chain of publishing functions
653 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir
)
654 (funcall f project-plist filename tmp-pub-dir
)
655 (org-publish-update-timestamp filename pub-dir f
)))
657 (when (org-publish-needed-p filename pub-dir publishing-function
659 (funcall publishing-function project-plist filename tmp-pub-dir
)
660 (org-publish-update-timestamp
661 filename pub-dir publishing-function
)))
662 (unless no-cache
(org-publish-write-cache-file))))
664 (defun org-publish-projects (projects)
665 "Publish all files belonging to the PROJECTS alist.
666 If :auto-sitemap is set, publish the sitemap too.
667 If :makeindex is set, also produce a file theindex.org."
670 ;; Each project uses it's own cache file:
671 (org-publish-initialize-cache (car project
))
673 ((project-plist (cdr project
))
674 (exclude-regexp (plist-get project-plist
:exclude
))
675 (sitemap-p (plist-get project-plist
:auto-sitemap
))
676 (sitemap-filename (or (plist-get project-plist
:sitemap-filename
)
678 (sitemap-function (or (plist-get project-plist
:sitemap-function
)
679 'org-publish-org-sitemap
))
680 (preparation-function (plist-get project-plist
:preparation-function
))
681 (completion-function (plist-get project-plist
:completion-function
))
682 (files (org-publish-get-base-files project exclude-regexp
)) file
)
683 (when preparation-function
(run-hooks 'preparation-function
))
684 (if sitemap-p
(funcall sitemap-function project sitemap-filename
))
685 (while (setq file
(pop files
))
686 (org-publish-file file project t
))
687 (when (plist-get project-plist
:makeindex
)
688 (org-publish-index-generate-theindex.inc
689 (plist-get project-plist
:base-directory
))
690 (org-publish-file (expand-file-name
692 (plist-get project-plist
:base-directory
))
694 (when completion-function
(run-hooks 'completion-function
))
695 (org-publish-write-cache-file)))
696 (org-publish-expand-projects projects
)))
698 (defun org-publish-org-sitemap (project &optional sitemap-filename
)
699 "Create a sitemap of pages in set defined by PROJECT.
700 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
701 Default for SITEMAP-FILENAME is 'sitemap.org'."
702 (let* ((project-plist (cdr project
))
703 (dir (file-name-as-directory
704 (plist-get project-plist
:base-directory
)))
705 (localdir (file-name-directory dir
))
706 (indent-str (make-string 2 ?\
))
707 (exclude-regexp (plist-get project-plist
:exclude
))
708 (files (nreverse (org-publish-get-base-files project exclude-regexp
)))
709 (sitemap-filename (concat dir
(or sitemap-filename
"sitemap.org")))
710 (sitemap-title (or (plist-get project-plist
:sitemap-title
)
711 (concat "Sitemap for project " (car project
))))
712 (sitemap-style (or (plist-get project-plist
:sitemap-style
)
714 (visiting (find-buffer-visiting sitemap-filename
))
715 (ifn (file-name-nondirectory sitemap-filename
))
717 (with-current-buffer (setq sitemap-buffer
718 (or visiting
(find-file sitemap-filename
)))
720 (insert (concat "#+TITLE: " sitemap-title
"\n\n"))
721 (while (setq file
(pop files
))
722 (let ((fn (file-name-nondirectory file
))
723 (link (file-relative-name file dir
))
725 ;; sitemap shouldn't list itself
726 (unless (equal (file-truename sitemap-filename
)
727 (file-truename file
))
728 (if (eq sitemap-style
'list
)
729 (message "Generating list-style sitemap for %s" sitemap-title
)
730 (message "Generating tree-style sitemap for %s" sitemap-title
)
731 (setq localdir
(concat (file-name-as-directory dir
)
732 (file-name-directory link
)))
733 (unless (string= localdir oldlocal
)
734 (if (string= localdir dir
)
735 (setq indent-str
(make-string 2 ?\
))
740 (file-relative-name localdir dir
))) "/"))
742 (old-subdirs (split-string
743 (file-relative-name oldlocal dir
) "/")))
744 (setq indent-str
(make-string 2 ?\
))
745 (while (string= (car old-subdirs
) (car subdirs
))
746 (setq indent-str
(concat indent-str
(make-string 2 ?\
)))
750 (setq subdir
(concat subdir d
"/"))
751 (insert (concat indent-str
" + " d
"\n"))
752 (setq indent-str
(make-string
753 (+ (length indent-str
) 2) ?\
)))))))
754 ;; This is common to 'flat and 'tree
755 (insert (concat indent-str
" + [[file:" link
"]["
756 (org-publish-find-title file
)
759 (or visiting
(kill-buffer sitemap-buffer
))))
761 (defun org-publish-find-title (file)
762 "Find the title of FILE in project."
764 (org-publish-cache-get-file-property file
:title nil t
)
765 (let* ((visiting (find-buffer-visiting file
))
766 (buffer (or visiting
(find-file-noselect file
)))
768 (with-current-buffer buffer
769 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
770 (org-infile-export-plist))))
772 (or (plist-get opt-plist
:title
)
774 (plist-get opt-plist
:skip-before-1st-heading
))
775 (org-export-grab-title-from-buffer))
776 (file-name-nondirectory (file-name-sans-extension file
))))))
778 (kill-buffer buffer
))
779 (org-publish-cache-set-file-property file
:title title
)
782 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
783 ;;; Interactive publishing functions
786 (defalias 'org-publish-project
'org-publish
)
789 (defun org-publish (project &optional force
)
793 (assoc (org-icompleting-read
795 org-publish-project-alist nil t
)
796 org-publish-project-alist
)
798 (setq org-publish-initial-buffer
(current-buffer))
799 (save-window-excursion
800 (let* ((org-publish-use-timestamps-flag
801 (if force nil org-publish-use-timestamps-flag
)))
802 (org-publish-projects
803 (if (stringp project
)
804 ;; If this function is called in batch mode,
805 ;; project is still a string here.
806 (list (assoc project org-publish-project-alist
))
810 (defun org-publish-all (&optional force
)
811 "Publish all projects.
812 With prefix argument, remove all files in the timestamp
813 directory and force publishing all files."
816 (org-publish-remove-all-timestamps))
817 (save-window-excursion
818 (let ((org-publish-use-timestamps-flag
819 (if force nil org-publish-use-timestamps-flag
)))
820 (org-publish-projects org-publish-project-alist
))))
824 (defun org-publish-current-file (&optional force
)
825 "Publish the current file.
826 With prefix argument, force publish the file."
828 (save-window-excursion
829 (let ((org-publish-use-timestamps-flag
830 (if force nil org-publish-use-timestamps-flag
)))
831 (org-publish-file (buffer-file-name)))))
834 (defun org-publish-current-project (&optional force
)
835 "Publish the project associated with the current file.
836 With a prefix argument, force publishing of all files in
839 (save-window-excursion
840 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up
))
841 (org-publish-use-timestamps-flag
842 (if force nil org-publish-use-timestamps-flag
)))
844 (error "File %s is not part of any known project" (buffer-file-name)))
845 ;; FIXME: force is not used here?
846 (org-publish project
))))
851 (defvar backend
) ; dynamically scoped
852 (defun org-publish-aux-preprocess ()
853 "Find index entries and write them to an .orgx file."
854 (let ((case-fold-search t
)
856 (goto-char (point-min))
859 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t
)
860 (> (match-end 1) (match-beginning 1)))
861 (setq entry
(match-string 1))
862 (when (eq backend
'latex
)
863 (replace-match (format "\\index{%s}" entry
) t t
))
865 (ignore-errors (org-back-to-heading t
))
866 (setq target
(get-text-property (point) 'target
))
867 (setq target
(or (cdr (assoc target org-export-preferred-target-alist
))
868 (cdr (assoc target org-export-id-target-alist
))
870 (push (cons entry target
) index
)))
872 (concat (file-name-sans-extension org-current-export-file
) ".orgx")
873 (dolist (entry (nreverse index
))
874 (insert (format "INDEX: (%s) %s\n" (cdr entry
) (car entry
)))))))
876 (defun org-publish-index-generate-theindex.inc
(directory)
877 "Generate the index from all .orgx files in the current directory and below."
879 (let* ((fulldir (file-name-as-directory
880 (expand-file-name directory
)))
881 (full-files (find-lisp-find-files directory
"\\.orgx\\'"))
882 (re (concat "\\`" fulldir
))
883 (files (mapcar (lambda (f) (if (string-match re f
)
884 (substring f
(match-end 0))
887 (default-directory directory
)
888 index origfile buf target entry ibuffer
889 main last-main letter last-letter file sub link tgext
)
890 ;; `files' contains the list of relative file names
892 (setq origfile
(substring file
0 -
1))
893 (setq buf
(find-file-noselect file
))
894 (with-current-buffer buf
895 (goto-char (point-min))
896 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t
)
897 (setq target
(match-string 1)
898 entry
(match-string 2))
899 (push (list entry origfile target
) index
)))
901 (setq index
(sort index
(lambda (a b
) (string< (downcase (car a
))
902 (downcase (car b
))))))
903 (setq ibuffer
(find-file-noselect (expand-file-name "theindex.inc" directory
)))
904 (with-current-buffer ibuffer
907 (setq last-letter nil
)
909 (setq entry
(car idx
) file
(nth 1 idx
) target
(nth 2 idx
))
910 (if (and (stringp target
) (string-match "\\S-" target
))
911 (setq tgext
(concat "::#" target
))
913 (setq letter
(upcase (substring entry
0 1)))
914 (when (not (equal letter last-letter
))
915 (insert "** " letter
"\n")
916 (setq last-letter letter
))
917 (if (string-match "!" entry
)
918 (setq main
(substring entry
0 (match-beginning 0))
919 sub
(substring entry
(match-end 0)))
920 (setq main nil sub nil last-main nil
))
921 (when (and main
(not (equal main last-main
)))
922 (insert " - " main
"\n")
923 (setq last-main main
))
924 (setq link
(concat "[[file:" file tgext
"]"
925 "[" (or sub entry
) "]]"))
927 (insert " - " link
"\n")
928 (insert " - " link
"\n")))
930 (kill-buffer ibuffer
)
932 (let ((index-file (expand-file-name "theindex.org" directory
)))
933 (unless (file-exists-p index-file
)
934 (setq ibuffer
(find-file-noselect index-file
))
935 (with-current-buffer ibuffer
937 (insert "\n\n#+include: \"theindex.inc\"\n\n")
939 (kill-buffer ibuffer
)))))
942 ;; Caching functions:
944 (defun org-publish-write-cache-file (&optional free-cache
)
945 "Write `org-publish-cache' to file.
946 If FREE-CACHE, empty the cache."
947 (unless org-publish-cache
948 (error "%s" "`org-publish-write-cache-file' called, but no cache present"))
950 (let ((cache-file (org-publish-cache-get ":cache-file:")))
953 "%s" "Cannot find cache-file name in `org-publish-write-cache-file'"))
954 (with-temp-file cache-file
955 (let ((print-level nil
)
957 (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
958 (maphash (lambda (k v
)
960 (format (concat "(puthash %S "
961 (if (or (listp v
) (symbolp v
))
963 "%S org-publish-cache)\n") k v
)))
965 (when free-cache
(org-publish-reset-cache))))
967 (defun org-publish-initialize-cache (project-name)
968 "Initialize the projects cache if not initialized yet and return it."
971 (error "%s%s" "Cannot initialize `org-publish-cache' without projects name"
972 " in `org-publish-initialize-cache'"))
974 (unless (file-exists-p org-publish-timestamp-directory
)
975 (make-directory org-publish-timestamp-directory t
))
976 (if (not (file-directory-p org-publish-timestamp-directory
))
977 (error "Org publish timestamp: %s is not a directory"
978 org-publish-timestamp-directory
))
980 (unless (and org-publish-cache
981 (string= (org-publish-cache-get ":project:") project-name
))
982 (let* ((cache-file (concat
983 (expand-file-name org-publish-timestamp-directory
)
986 (cexists (file-exists-p cache-file
)))
988 (when org-publish-cache
989 (org-publish-reset-cache))
992 (load-file cache-file
)
993 (setq org-publish-cache
994 (make-hash-table :test
'equal
:weakness nil
:size
100))
995 (org-publish-cache-set ":project:" project-name
)
996 (org-publish-cache-set ":cache-file:" cache-file
))
997 (unless cexists
(org-publish-write-cache-file nil
))))
1000 (defun org-publish-reset-cache ()
1001 "Empty org-publish-cache and reset it nil."
1002 (message "%s" "Resetting org-publish-cache")
1003 (if (hash-table-p org-publish-cache
)
1004 (clrhash org-publish-cache
))
1005 (setq org-publish-cache nil
))
1007 (defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func
)
1008 "Check the timestamp of the last publishing of FILENAME.
1009 Return `t', if the file needs publishing"
1010 (unless org-publish-cache
1011 (error "%s" "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1012 (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func
))
1013 (pstamp (org-publish-cache-get key
)))
1016 (let ((ctime (org-publish-cache-ctime-of-src filename
)))
1017 (< pstamp ctime
)))))
1019 (defun org-publish-cache-set-file-property (filename property value
&optional project-name
)
1020 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1021 Use cache file of PROJECT-NAME. If the entry does not exist, it will be
1022 created. Return VALUE."
1023 ;; Evtl. load the requested cache file:
1024 (if project-name
(org-publish-initialize-cache project-name
))
1025 (let ((pl (org-publish-cache-get filename
)))
1028 (plist-put pl property value
)
1030 (org-publish-cache-get-file-property
1031 filename property value nil project-name
))))
1033 (defun org-publish-cache-get-file-property
1034 (filename property
&optional default no-create project-name
)
1035 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1036 Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
1037 DEFAULT, if the value does not yet exist.
1038 If the entry will be created, unless NO-CREATE is not nil."
1039 ;; Evtl. load the requested cache file:
1040 (if project-name
(org-publish-initialize-cache project-name
))
1041 (let ((pl (org-publish-cache-get filename
))
1044 (if (plist-member pl property
)
1045 (setq retval
(plist-get pl property
))
1046 (setq retval default
))
1049 (org-publish-cache-set filename
(list property default
)))
1050 (setq retval default
))
1053 (defun org-publish-cache-get (key)
1054 "Return the value stored in `org-publish-cache' for key KEY.
1055 Returns nil, if no value or nil is found, or the cache does not
1057 (unless org-publish-cache
1058 (error "%s" "`org-publish-cache-get' called, but no cache present"))
1059 (gethash key org-publish-cache
))
1061 (defun org-publish-cache-set (key value
)
1062 "Store KEY VALUE pair in `org-publish-cache'.
1063 Returns value on success, else nil."
1064 (unless org-publish-cache
1065 (error "%s" "`org-publish-cache-set' called, but no cache present"))
1066 (puthash key value org-publish-cache
))
1068 (defun org-publish-cache-ctime-of-src (filename)
1069 "Get the files ctime as integer."
1070 (let ((src-attr (file-attributes filename
)))
1072 (lsh (car (nth 5 src-attr
)) 16)
1073 (cadr (nth 5 src-attr
)))))
1077 (provide 'org-publish
)
1080 ;;; org-publish.el ends here