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. If not given,
96 \"org\" will be used as default extension.
97 :publishing-directory Directory (possibly remote) where output
98 files will be published
100 The :exclude property may be used to prevent certain files from
101 being published. Its value may be a string or regexp matching
102 file names you don't want to be published.
104 The :include property may be used to include extra files. Its
105 value may be a list of filenames to include. The filenames are
106 considered relative to the base directory.
108 When both :include and :exclude properties are given values, the
109 exclusion step happens first.
111 One special property controls which back-end function to use for
112 publishing files in the project. This can be used to extend the
113 set of file types publishable by org-publish, as well as the set
116 :publishing-function Function to publish file. The default is
117 `org-publish-org-to-html', but other
118 values are possible. May also be a
119 list of functions, in which case
120 each function in the list is invoked
123 Another property allows you to insert code that prepares a
124 project for publishing. For example, you could call GNU Make on a
125 certain makefile, to ensure published files are built up to date.
127 :preparation-function Function to be called before publishing
128 this project. This may also be a list
130 :completion-function Function to be called after publishing
131 this project. This may also be a list
134 Some properties control details of the Org publishing process,
135 and are equivalent to the corresponding user variables listed in
136 the right column. See the documentation for those variables to
137 learn more about their use and default values.
139 :language `org-export-default-language'
140 :headline-levels `org-export-headline-levels'
141 :section-numbers `org-export-with-section-numbers'
142 :table-of-contents `org-export-with-toc'
143 :emphasize `org-export-with-emphasize'
144 :sub-superscript `org-export-with-sub-superscripts'
145 :TeX-macros `org-export-with-TeX-macros'
146 :fixed-width `org-export-with-fixed-width'
147 :tables `org-export-with-tables'
148 :table-auto-headline `org-export-highlight-first-table-line'
149 :style `org-export-html-style'
150 :convert-org-links `org-export-html-link-org-files-as-html'
151 :inline-images `org-export-html-inline-images'
152 :expand-quoted-html `org-export-html-expand'
153 :timestamp `org-export-html-with-timestamp'
154 :publishing-directory `org-export-publishing-directory'
155 :preamble `org-export-html-preamble'
156 :postamble `org-export-html-postamble'
157 :auto-preamble `org-export-html-auto-preamble'
158 :auto-postamble `org-export-html-auto-postamble'
159 :author `user-full-name'
160 :email `user-mail-address'
162 The following properties may be used to control publishing of a
163 sitemap of files or summary page for a given project.
165 :auto-sitemap Whether to publish a sitemap during
166 `org-publish-current-project' or `org-publish-all'.
167 :sitemap-filename Filename for output of sitemap. Defaults
168 to 'sitemap.org' (which becomes 'sitemap.html').
169 :sitemap-title Title of sitemap page. Defaults to name of file.
170 :sitemap-function Plugin function to use for generation of sitemap.
171 Defaults to `org-publish-org-sitemap', which
172 generates a plain list of links to all files
174 :sitemap-style Can be `list' (sitemap is just an itemized list
175 of the titles of the files involved) or
176 `tree' (the directory structure of the source
177 files is reflected in the sitemap). Defaults to
180 If you create a sitemap file, adjust the sorting like this:
182 :sitemap-sort-folders Where folders should appear in the sitemap.
183 Set this to `first' (default) or `last' to
184 display folders first or last, respectively.
185 Any other value will mix files and folders.
186 :sitemap-alphabetically The site map is normally sorted alphabetically.
187 Set this explicitly to nil to turn off sorting.
188 :sitemap-ignore-case Should sorting be case-sensitive? Default nil.
190 The following properties control the creation of a concept index.
192 :makeindex Create a concept index."
196 (defcustom org-publish-use-timestamps-flag t
197 "Non-nil means use timestamp checking to publish only changed files.
198 When nil, do no timestamp checking and always publish all files."
202 (defcustom org-publish-timestamp-directory
(convert-standard-filename
203 "~/.org-timestamps/")
204 "Name of directory in which to store publishing timestamps."
208 (defcustom org-publish-list-skipped-files t
209 "Non-nil means show message about files *not* published."
213 (defcustom org-publish-before-export-hook nil
214 "Hook run before export on the Org file.
215 The hook may modify the file in arbitrary ways before publishing happens.
216 The original version of the buffer will be restored after publishing."
220 (defcustom org-publish-after-export-hook nil
221 "Hook run after export on the exported buffer.
222 Any changes made by this hook will be saved."
226 (defcustom org-publish-sitemap-sort-alphabetically t
227 "Should sitemaps be sorted alphabetically by default?
229 You can overwrite this default per project in your
230 `org-publish-project-alist', using `:sitemap-alphabetically'."
234 (defcustom org-publish-sitemap-sort-folders
'first
235 "A symbol, denoting if folders are sorted first in sitemaps.
236 Possible values are `first', `last', and nil.
237 If `first', folders will be sorted before files.
238 If `last', folders are sorted to the end after the files.
239 Any other value will not mix files and folders.
241 You can overwrite this default per project in your
242 `org-publish-project-alist', using `:sitemap-sort-folders'."
246 (defcustom org-publish-sitemap-sort-ignore-case nil
247 "Sort sitemaps case insensitively by default?
249 You can overwrite this default per project in your
250 `org-publish-project-alist', using `:sitemap-ignore-case'."
254 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
255 ;;; Timestamp-related functions
257 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func
)
258 "Return path to timestamp file for filename FILENAME."
259 (setq filename
(concat filename
"::" (or pub-dir
"") "::"
260 (format "%s" (or pub-func
""))))
261 (concat "X" (if (fboundp 'sha1
) (sha1 filename
) (md5 filename
))))
263 (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir
)
264 "Return t if FILENAME should be published in PUB-DIR using PUB-FUNC.
265 TRUE-PUB-DIR is where the file will truly end up. Currently we are not using
266 this - maybe it can eventually be used to check if the file is present at
267 the target location, and how old it is. Right ow we cannot do this, because
268 we do not know under what file name the file will be stored - the publishing
269 function can still decide about that independently."
271 (if org-publish-use-timestamps-flag
272 (org-publish-cache-file-needs-publishing
273 filename pub-dir pub-func
)
274 ;; don't use timestamps, always return t
277 (message "Publishing file %s using `%s'" filename pub-func
)
278 (when org-publish-list-skipped-files
279 (message "Skipping unmodified file %s" filename
)))
282 (defun org-publish-update-timestamp (filename &optional pub-dir pub-func
)
283 "Update publishing timestamp for file FILENAME.
284 If there is no timestamp, create one."
285 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func
))
286 (stamp (org-publish-cache-ctime-of-src filename
)))
287 (org-publish-cache-set key stamp
)))
289 (defun org-publish-remove-all-timestamps ()
290 "Remove all files in the timestamp directory."
291 (let ((dir org-publish-timestamp-directory
)
293 (when (and (file-exists-p dir
)
294 (file-directory-p dir
))
295 (mapc 'delete-file
(directory-files dir
'full
"[^.]\\'"))
296 (org-publish-reset-cache))))
299 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
302 (defvar org-publish-initial-buffer nil
303 "The buffer `org-publish' has been called from.")
304 (defvar org-publish-temp-files nil
305 "Temporary list of files to be published.")
307 ;; Here, so you find the variable right before it's used the first time:
308 (defvar org-publish-cache nil
309 "This will cache timestamps and titles for files in publishing projects.
310 Blocks could hash sha1 values here.")
313 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
314 ;;; Compatibility aliases
316 ;; Delete-dups is not in Emacs <22
317 (if (fboundp 'delete-dups
)
318 (defalias 'org-publish-delete-dups
'delete-dups
)
319 (defun org-publish-delete-dups (list)
320 "Destructively remove `equal' duplicates from LIST.
321 Store the result in LIST and return it. LIST must be a proper list.
322 Of several `equal' occurrences of an element in LIST, the first
325 This is a compatibility function for Emacsen without `delete-dups'."
326 ;; Code from `subr.el' in Emacs 22:
329 (setcdr tail
(delete (car tail
) (cdr tail
)))
330 (setq tail
(cdr tail
))))
333 (declare-function org-publish-delete-dups
"org-publish" (list))
334 (declare-function find-lisp-find-files
"find-lisp" (directory regexp
))
336 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
337 ;;; Getting project information out of org-publish-project-alist
339 (defun org-publish-expand-projects (projects-alist)
340 "Expand projects in PROJECTS-ALIST.
341 This splices all the components into the list."
342 (let ((rest projects-alist
) rtn p components
)
343 (while (setq p
(pop rest
))
344 (if (setq components
(plist-get (cdr p
) :components
))
346 (mapcar (lambda (x) (assoc x org-publish-project-alist
))
350 (nreverse (org-publish-delete-dups (delq nil rtn
)))))
353 (defvar sitemap-alphabetically
)
354 (defvar sitemap-sort-folders
)
355 (defvar sitemap-ignore-case
)
356 (defvar sitemap-requested
)
357 (defun org-publish-compare-directory-files (a b
)
358 "Predicate for `sort', that sorts folders-first/last and alphabetically."
360 (when (or sitemap-alphabetically sitemap-sort-folders
)
361 ;; First we sort alphabetically:
362 (when sitemap-alphabetically
363 (let* ((adir (file-directory-p a
))
364 (aorg (and (string-match "\\.org$" a
) (not adir
)))
365 (bdir (file-directory-p b
))
366 (borg (and (string-match "\\.org$" b
) (not bdir
)))
368 (concat (file-name-directory a
)
369 (org-publish-find-title a
)) a
))
371 (concat (file-name-directory b
)
372 (org-publish-find-title b
)) b
)))
373 (setq retval
(if sitemap-ignore-case
374 (not (string-lessp (upcase B
) (upcase A
)))
375 (not (string-lessp B A
))))))
377 ;; Directory-wise wins:
378 (when sitemap-sort-folders
379 ;; a is directory, b not:
381 ((and (file-directory-p a
) (not (file-directory-p b
)))
382 (setq retval
(equal sitemap-sort-folders
'first
)))
383 ;; a is not a directory, but b is:
384 ((and (not (file-directory-p a
)) (file-directory-p b
))
385 (setq retval
(equal sitemap-sort-folders
'last
))))))
388 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir
)
389 "Set `org-publish-temp-files' with files from BASE-DIR directory.
390 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
391 non-nil, restrict this list to the files matching the regexp
392 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
393 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
394 matching the regexp SKIP-DIR when recursing through BASE-DIR."
396 (let ((fd-p (file-directory-p f
))
397 (fnd (file-name-nondirectory f
)))
398 (if (and fd-p recurse
399 (not (string-match "^\\.+$" fnd
))
400 (if skip-dir
(not (string-match skip-dir fnd
)) t
))
401 (org-publish-get-base-files-1 f recurse match skip-file skip-dir
)
402 (unless (or fd-p
;; this is a directory
403 (and skip-file
(string-match skip-file fnd
))
404 (not (file-exists-p (file-truename f
)))
405 (not (string-match match fnd
)))
407 (pushnew f org-publish-temp-files
)))))
408 (if sitemap-requested
409 (sort (directory-files base-dir t
(unless recurse match
))
410 'org-publish-compare-directory-files
)
411 (directory-files base-dir t
(unless recurse match
)))))
413 (defun org-publish-get-base-files (project &optional exclude-regexp
)
414 "Return a list of all files in PROJECT.
415 If EXCLUDE-REGEXP is set, this will be used to filter out
417 (let* ((project-plist (cdr project
))
418 (base-dir (file-name-as-directory
419 (plist-get project-plist
:base-directory
)))
420 (include-list (plist-get project-plist
:include
))
421 (recurse (plist-get project-plist
:recursive
))
422 (extension (or (plist-get project-plist
:base-extension
) "org"))
423 ;; sitemap-... variables are dynamically scoped for
424 ;; org-publish-compare-directory-files:
426 (plist-get project-plist
:auto-sitemap
))
427 (sitemap-sort-folders
428 (if (plist-member project-plist
:sitemap-sort-folders
)
429 (plist-get project-plist
:sitemap-sort-folders
)
430 org-publish-sitemap-sort-folders
))
431 (sitemap-alphabetically
432 (if (plist-member project-plist
:sitemap-alphabetically
)
433 (plist-get project-plist
:sitemap-alphabetically
)
434 org-publish-sitemap-sort-alphabetically
))
436 (if (plist-member project-plist
:sitemap-ignore-case
)
437 (plist-get project-plist
:sitemap-ignore-case
)
438 org-publish-sitemap-sort-ignore-case
))
439 (match (if (eq extension
'any
)
441 (concat "^[^\\.].*\\.\\(" extension
"\\)$"))))
442 ;; Make sure sitemap-sort-folders' has an accepted value
443 (unless (memq sitemap-sort-folders
'(first last
))
444 (setq sitemap-sort-folders nil
))
446 (setq org-publish-temp-files nil
)
447 (org-publish-get-base-files-1 base-dir recurse match
448 ;; FIXME distinguish exclude regexp
449 ;; for skip-file and skip-dir?
450 exclude-regexp exclude-regexp
)
453 (expand-file-name (concat base-dir f
))
454 org-publish-temp-files
))
456 org-publish-temp-files
))
458 (defun org-publish-get-project-from-filename (filename &optional up
)
459 "Return the project that FILENAME belongs to."
460 (let* ((filename (expand-file-name filename
))
464 (dolist (prj org-publish-project-alist
)
465 (unless (plist-get (cdr prj
) :components
)
466 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
467 (let* ((r (plist-get (cdr prj
) :recursive
))
468 (b (expand-file-name (plist-get (cdr prj
) :base-directory
)))
469 (x (or (plist-get (cdr prj
) :base-extension
) "org"))
470 (e (plist-get (cdr prj
) :exclude
))
471 (i (plist-get (cdr prj
) :include
))
472 (xm (concat "^" b
(if r
".+" "[^/]+") "\\.\\(" x
"\\)$")))
474 (and i
(string-match i filename
))
476 (not (and e
(string-match e filename
)))
477 (string-match xm filename
)))
478 (setq project-name
(car prj
))
479 (throw 'p-found project-name
))))))
481 (dolist (prj org-publish-project-alist
)
482 (if (member project-name
(plist-get (cdr prj
) :components
))
483 (setq project-name
(car prj
)))))
484 (assoc project-name org-publish-project-alist
)))
486 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
487 ;;; Pluggable publishing back-end functions
489 (defun org-publish-org-to (format plist filename pub-dir
)
490 "Publish an org file to FORMAT.
491 PLIST is the property list for the given project.
492 FILENAME is the filename of the org file to be published.
493 PUB-DIR is the publishing directory."
495 (unless (file-exists-p pub-dir
)
496 (make-directory pub-dir t
))
497 (let ((visiting (find-buffer-visiting filename
)))
499 (switch-to-buffer (or visiting
(find-file filename
)))
500 (let* ((plist (cons :buffer-will-be-killed
(cons t plist
)))
501 (init-buf (current-buffer))
503 (init-buf-string (buffer-string))
505 ;; run hooks before exporting
506 (run-hooks 'org-publish-before-export-hook
)
507 ;; export the possibly modified buffer
508 (setq export-buf-or-file
509 (funcall (intern (concat "org-export-as-" format
))
510 (plist-get plist
:headline-levels
)
511 nil plist nil nil pub-dir
))
512 (when (and (bufferp export-buf-or-file
)
513 (buffer-live-p export-buf-or-file
))
514 (set-buffer export-buf-or-file
)
515 ;; run hooks after export and save export
516 (progn (run-hooks 'org-publish-after-export-hook
)
517 (if (buffer-modified-p) (save-buffer)))
518 (kill-buffer export-buf-or-file
))
519 ;; maybe restore buffer's content
520 (set-buffer init-buf
)
521 (when (buffer-modified-p init-buf
)
523 (insert init-buf-string
)
525 (goto-char init-point
))
527 (kill-buffer init-buf
))))))
529 (defmacro org-publish-with-aux-preprocess-maybe
(&rest body
)
530 "Execute BODY with a modified hook to preprocess for index."
531 `(let ((org-export-preprocess-after-headline-targets-hook
532 (if (plist-get project-plist
:makeindex
)
533 (cons 'org-publish-aux-preprocess
534 org-export-preprocess-after-headline-targets-hook
)
535 org-export-preprocess-after-headline-targets-hook
)))
538 (defvar project-plist
)
539 (defun org-publish-org-to-latex (plist filename pub-dir
)
540 "Publish an org file to LaTeX.
541 See `org-publish-org-to' to the list of arguments."
542 (org-publish-with-aux-preprocess-maybe
543 (org-publish-org-to "latex" plist filename pub-dir
)))
545 (defun org-publish-org-to-pdf (plist filename pub-dir
)
546 "Publish an org file to PDF (via LaTeX).
547 See `org-publish-org-to' to the list of arguments."
548 (org-publish-with-aux-preprocess-maybe
549 (org-publish-org-to "pdf" plist filename pub-dir
)))
551 (defun org-publish-org-to-html (plist filename pub-dir
)
552 "Publish an org file to HTML.
553 See `org-publish-org-to' to the list of arguments."
554 (org-publish-with-aux-preprocess-maybe
555 (org-publish-org-to "html" plist filename pub-dir
)))
557 (defun org-publish-org-to-org (plist filename pub-dir
)
558 "Publish an org file to HTML.
559 See `org-publish-org-to' to the list of arguments."
560 (org-publish-org-to "org" plist filename pub-dir
))
562 (defun org-publish-attachment (plist filename pub-dir
)
563 "Publish a file with no transformation of any kind.
564 See `org-publish-org-to' to the list of arguments."
565 ;; make sure eshell/cp code is loaded
566 (unless (file-directory-p pub-dir
)
567 (make-directory pub-dir t
))
568 (or (equal (expand-file-name (file-name-directory filename
))
569 (file-name-as-directory (expand-file-name pub-dir
)))
571 (expand-file-name (file-name-nondirectory filename
) pub-dir
)
574 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
575 ;;; Publishing files, sets of files, and indices
577 (defun org-publish-file (filename &optional project no-cache
)
578 "Publish file FILENAME from PROJECT.
579 If NO-CACHE is not nil, do not initialize org-publish-cache and
580 write it to disk. This is needed, since this function is used to
581 publish single files, when entire projects are published.
582 See `org-publish-projects'."
585 (or (org-publish-get-project-from-filename filename
)
586 (error "File %s not part of any known project"
587 (abbreviate-file-name filename
)))))
588 (project-plist (cdr project
))
589 (ftname (file-truename filename
))
591 (or (plist-get project-plist
:publishing-function
)
592 'org-publish-org-to-html
))
594 (file-name-as-directory
596 (or (plist-get project-plist
:base-directory
)
597 (error "Project %s does not have :base-directory defined"
600 (file-name-as-directory
602 (or (plist-get project-plist
:publishing-directory
)
603 (error "Project %s does not have :publishing-directory defined"
608 (org-publish-initialize-cache (car project
)))
613 (and (string-match (regexp-quote base-dir
) ftname
)
614 (substring ftname
(match-end 0))))))
615 (if (listp publishing-function
)
616 ;; allow chain of publishing functions
618 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir
)
619 (funcall f project-plist filename tmp-pub-dir
)
620 (org-publish-update-timestamp filename pub-dir f
)))
622 (when (org-publish-needed-p filename pub-dir publishing-function
624 (funcall publishing-function project-plist filename tmp-pub-dir
)
625 (org-publish-update-timestamp
626 filename pub-dir publishing-function
)))
627 (unless no-cache
(org-publish-write-cache-file))))
629 (defun org-publish-projects (projects)
630 "Publish all files belonging to the PROJECTS alist.
631 If :auto-sitemap is set, publish the sitemap too.
632 If :makeindex is set, also produce a file theindex.org."
635 ;; Each project uses it's own cache file:
636 (org-publish-initialize-cache (car project
))
638 ((project-plist (cdr project
))
639 (exclude-regexp (plist-get project-plist
:exclude
))
640 (sitemap-p (plist-get project-plist
:auto-sitemap
))
641 (sitemap-filename (or (plist-get project-plist
:sitemap-filename
)
643 (sitemap-function (or (plist-get project-plist
:sitemap-function
)
644 'org-publish-org-sitemap
))
645 (preparation-function (plist-get project-plist
:preparation-function
))
646 (completion-function (plist-get project-plist
:completion-function
))
647 (files (org-publish-get-base-files project exclude-regexp
)) file
)
648 (when preparation-function
(run-hooks 'preparation-function
))
649 (if sitemap-p
(funcall sitemap-function project sitemap-filename
))
650 (while (setq file
(pop files
))
651 (org-publish-file file project t
))
652 (when (plist-get project-plist
:makeindex
)
653 (org-publish-index-generate-theindex.inc
654 (plist-get project-plist
:base-directory
))
655 (org-publish-file (expand-file-name
657 (plist-get project-plist
:base-directory
))
659 (when completion-function
(run-hooks 'completion-function
))
660 (org-publish-write-cache-file)))
661 (org-publish-expand-projects projects
)))
663 (defun org-publish-org-sitemap (project &optional sitemap-filename
)
664 "Create a sitemap of pages in set defined by PROJECT.
665 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
666 Default for SITEMAP-FILENAME is 'sitemap.org'."
667 (let* ((project-plist (cdr project
))
668 (dir (file-name-as-directory
669 (plist-get project-plist
:base-directory
)))
670 (localdir (file-name-directory dir
))
671 (indent-str (make-string 2 ?\
))
672 (exclude-regexp (plist-get project-plist
:exclude
))
673 (files (nreverse (org-publish-get-base-files project exclude-regexp
)))
674 (sitemap-filename (concat dir
(or sitemap-filename
"sitemap.org")))
675 (sitemap-title (or (plist-get project-plist
:sitemap-title
)
676 (concat "Sitemap for project " (car project
))))
677 (sitemap-style (or (plist-get project-plist
:sitemap-style
)
679 (visiting (find-buffer-visiting sitemap-filename
))
680 (ifn (file-name-nondirectory sitemap-filename
))
682 (with-current-buffer (setq sitemap-buffer
683 (or visiting
(find-file sitemap-filename
)))
685 (insert (concat "#+TITLE: " sitemap-title
"\n\n"))
686 (while (setq file
(pop files
))
687 (let ((fn (file-name-nondirectory file
))
688 (link (file-relative-name file dir
))
690 ;; sitemap shouldn't list itself
691 (unless (equal (file-truename sitemap-filename
)
692 (file-truename file
))
693 (if (eq sitemap-style
'list
)
694 (message "Generating list-style sitemap for %s" sitemap-title
)
695 (message "Generating tree-style sitemap for %s" sitemap-title
)
696 (setq localdir
(concat (file-name-as-directory dir
)
697 (file-name-directory link
)))
698 (unless (string= localdir oldlocal
)
699 (if (string= localdir dir
)
700 (setq indent-str
(make-string 2 ?\
))
705 (file-relative-name localdir dir
))) "/"))
707 (old-subdirs (split-string
708 (file-relative-name oldlocal dir
) "/")))
709 (setq indent-str
(make-string 2 ?\
))
710 (while (string= (car old-subdirs
) (car subdirs
))
711 (setq indent-str
(concat indent-str
(make-string 2 ?\
)))
715 (setq subdir
(concat subdir d
"/"))
716 (insert (concat indent-str
" + " d
"\n"))
717 (setq indent-str
(make-string
718 (+ (length indent-str
) 2) ?\
)))))))
719 ;; This is common to 'flat and 'tree
720 (insert (concat indent-str
" + [[file:" link
"]["
721 (org-publish-find-title file
)
724 (or visiting
(kill-buffer sitemap-buffer
))))
726 (defun org-publish-find-title (file)
727 "Find the title of FILE in project."
729 (org-publish-cache-get-file-property file
:title nil t
)
730 (let* ((visiting (find-buffer-visiting file
))
731 (buffer (or visiting
(find-file-noselect file
)))
733 (with-current-buffer buffer
734 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
735 (org-infile-export-plist))))
737 (or (plist-get opt-plist
:title
)
739 (plist-get opt-plist
:skip-before-1st-heading
))
740 (org-export-grab-title-from-buffer))
741 (file-name-nondirectory (file-name-sans-extension file
))))))
743 (kill-buffer buffer
))
744 (org-publish-cache-set-file-property file
:title title
)
747 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
748 ;;; Interactive publishing functions
751 (defalias 'org-publish-project
'org-publish
)
754 (defun org-publish (project &optional force
)
758 (assoc (org-icompleting-read
760 org-publish-project-alist nil t
)
761 org-publish-project-alist
)
763 (setq org-publish-initial-buffer
(current-buffer))
764 (save-window-excursion
765 (let* ((org-publish-use-timestamps-flag
766 (if force nil org-publish-use-timestamps-flag
)))
767 (org-publish-projects
768 (if (stringp project
)
769 ;; If this function is called in batch mode,
770 ;; project is still a string here.
771 (list (assoc project org-publish-project-alist
))
775 (defun org-publish-all (&optional force
)
776 "Publish all projects.
777 With prefix argument, remove all files in the timestamp
778 directory and force publishing all files."
781 (org-publish-remove-all-timestamps))
782 ;; (org-publish-initialize-files-alist force)
783 (save-window-excursion
784 (let ((org-publish-use-timestamps-flag
785 (if force nil org-publish-use-timestamps-flag
)))
786 (org-publish-projects org-publish-project-alist
))))
790 (defun org-publish-current-file (&optional force
)
791 "Publish the current file.
792 With prefix argument, force publish the file."
794 (save-window-excursion
795 (let ((org-publish-use-timestamps-flag
796 (if force nil org-publish-use-timestamps-flag
)))
797 (org-publish-file (buffer-file-name)))))
800 (defun org-publish-current-project (&optional force
)
801 "Publish the project associated with the current file.
802 With a prefix argument, force publishing of all files in
805 (save-window-excursion
806 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up
))
807 (org-publish-use-timestamps-flag
808 (if force nil org-publish-use-timestamps-flag
)))
810 (error "File %s is not part of any known project" (buffer-file-name)))
811 ;; FIXME: force is not used here?
812 (org-publish project
))))
817 (defvar backend
) ; dynamically scoped
818 (defun org-publish-aux-preprocess ()
819 "Find index entries and write them to an .orgx file."
820 (let ((case-fold-search t
)
822 (goto-char (point-min))
825 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t
)
826 (> (match-end 1) (match-beginning 1)))
827 (setq entry
(match-string 1))
828 (when (eq backend
'latex
)
829 (replace-match (format "\\index{%s}" entry
) t t
))
831 (ignore-errors (org-back-to-heading t
))
832 (setq target
(get-text-property (point) 'target
))
833 (setq target
(or (cdr (assoc target org-export-preferred-target-alist
))
834 (cdr (assoc target org-export-id-target-alist
))
836 (push (cons entry target
) index
)))
838 (concat (file-name-sans-extension org-current-export-file
) ".orgx")
839 (dolist (entry (nreverse index
))
840 (insert (format "INDEX: (%s) %s\n" (cdr entry
) (car entry
)))))))
842 (defun org-publish-index-generate-theindex.inc
(directory)
843 "Generate the index from all .orgx files in the current directory and below."
845 (let* ((fulldir (file-name-as-directory
846 (expand-file-name directory
)))
847 (full-files (find-lisp-find-files directory
"\\.orgx\\'"))
848 (re (concat "\\`" fulldir
))
849 (files (mapcar (lambda (f) (if (string-match re f
)
850 (substring f
(match-end 0))
853 (default-directory directory
)
854 index origfile buf target entry ibuffer
855 main last-main letter last-letter file sub link tgext
)
856 ;; `files' contains the list of relative file names
858 (setq origfile
(substring file
0 -
1))
859 (setq buf
(find-file-noselect file
))
860 (with-current-buffer buf
861 (goto-char (point-min))
862 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t
)
863 (setq target
(match-string 1)
864 entry
(match-string 2))
865 (push (list entry origfile target
) index
)))
867 (setq index
(sort index
(lambda (a b
) (string< (downcase (car a
))
868 (downcase (car b
))))))
869 (setq ibuffer
(find-file-noselect (expand-file-name "theindex.inc" directory
)))
870 (with-current-buffer ibuffer
873 (setq last-letter nil
)
875 (setq entry
(car idx
) file
(nth 1 idx
) target
(nth 2 idx
))
876 (if (and (stringp target
) (string-match "\\S-" target
))
877 (setq tgext
(concat "::#" target
))
879 (setq letter
(upcase (substring entry
0 1)))
880 (when (not (equal letter last-letter
))
881 (insert "** " letter
"\n")
882 (setq last-letter letter
))
883 (if (string-match "!" entry
)
884 (setq main
(substring entry
0 (match-beginning 0))
885 sub
(substring entry
(match-end 0)))
886 (setq main nil sub nil last-main nil
))
887 (when (and main
(not (equal main last-main
)))
888 (insert " - " main
"\n")
889 (setq last-main main
))
890 (setq link
(concat "[[file:" file tgext
"]"
891 "[" (or sub entry
) "]]"))
893 (insert " - " link
"\n")
894 (insert " - " link
"\n")))
896 (kill-buffer ibuffer
)
898 (let ((index-file (expand-file-name "theindex.org" directory
)))
899 (unless (file-exists-p index-file
)
900 (setq ibuffer
(find-file-noselect index-file
))
901 (with-current-buffer ibuffer
903 (insert "\n\n#+include: \"theindex.inc\"\n\n")
905 (kill-buffer ibuffer
)))))
908 ;; Caching functions:
910 (defun org-publish-write-cache-file (&optional free-cache
)
911 "Write `org-publish-cache' to file.
912 If FREE-CACHE, empty the cache."
913 (unless org-publish-cache
914 (error "%s" "`org-publish-write-cache-file' called, but no cache present"))
916 (let ((cache-file (org-publish-cache-get ":cache-file:")))
919 "%s" "Cannot find cache-file name in `org-publish-write-cache-file'"))
920 (with-temp-file cache-file
921 (let ((print-level nil
)
923 (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
924 (maphash (lambda (k v
)
926 (format (concat "(puthash %S "
927 (if (or (listp v
) (symbolp v
))
929 "%S org-publish-cache)\n") k v
)))
931 (when free-cache
(org-publish-reset-cache))))
933 (defun org-publish-initialize-cache (project-name)
934 "Initialize the projects cache if not initialized yet and return it."
937 (error "%s%s" "Cannot initialize `org-publish-cache' without projects name"
938 " in `org-publish-initialize-cache'"))
940 (unless (file-exists-p org-publish-timestamp-directory
)
941 (make-directory org-publish-timestamp-directory t
))
942 (if (not (file-directory-p org-publish-timestamp-directory
))
943 (error "Org publish timestamp: %s is not a directory"
944 org-publish-timestamp-directory
))
946 (unless (and org-publish-cache
947 (string= (org-publish-cache-get ":project:") project-name
))
948 (let* ((cache-file (concat
949 (expand-file-name org-publish-timestamp-directory
)
952 (cexists (file-exists-p cache-file
)))
954 (when org-publish-cache
955 (org-publish-reset-cache))
958 (load-file cache-file
)
959 (setq org-publish-cache
960 (make-hash-table :test
'equal
:weakness nil
:size
100))
961 (org-publish-cache-set ":project:" project-name
)
962 (org-publish-cache-set ":cache-file:" cache-file
))
963 (unless cexists
(org-publish-write-cache-file nil
))))
966 (defun org-publish-reset-cache ()
967 "Empty org-publish-cache and reset it nil."
968 (message "%s" "Resetting org-publish-cache")
969 (if (hash-table-p org-publish-cache
)
970 (clrhash org-publish-cache
))
971 (setq org-publish-cache nil
))
973 (defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func
)
974 "Check the timestamp of the last publishing of FILENAME.
975 Return `t', if the file needs publishing"
976 (unless org-publish-cache
977 (error "%s" "`org-publish-cache-file-needs-publishing' called, but no cache present"))
978 (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func
))
979 (pstamp (org-publish-cache-get key
)))
982 (let ((ctime (org-publish-cache-ctime-of-src filename
)))
985 (defun org-publish-cache-set-file-property (filename property value
&optional project-name
)
986 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
987 Use cache file of PROJECT-NAME. If the entry does not exist, it will be
988 created. Return VALUE."
989 ;; Evtl. load the requested cache file:
990 (if project-name
(org-publish-initialize-cache project-name
))
991 (let ((pl (org-publish-cache-get filename
)))
994 (plist-put pl property value
)
996 (org-publish-cache-get-file-property
997 filename property value nil project-name
))))
999 (defun org-publish-cache-get-file-property
1000 (filename property
&optional default no-create project-name
)
1001 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1002 Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
1003 DEFAULT, if the value does not yet exist.
1004 If the entry will be created, unless NO-CREATE is not nil."
1005 ;; Evtl. load the requested cache file:
1006 (if project-name
(org-publish-initialize-cache project-name
))
1007 (let ((pl (org-publish-cache-get filename
))
1010 (if (plist-member pl property
)
1011 (setq retval
(plist-get pl property
))
1012 (setq retval default
))
1015 (org-publish-cache-set filename
(list property default
)))
1016 (setq retval default
))
1019 (defun org-publish-cache-get (key)
1020 "Return the value stored in `org-publish-cache' for key KEY.
1021 Returns nil, if no value or nil is found, or the cache does not
1023 (unless org-publish-cache
1024 (error "%s" "`org-publish-cache-get' called, but no cache present"))
1025 (gethash key org-publish-cache
))
1027 (defun org-publish-cache-set (key value
)
1028 "Store KEY VALUE pair in `org-publish-cache'.
1029 Returns value on success, else nil."
1030 (unless org-publish-cache
1031 (error "%s" "`org-publish-cache-set' called, but no cache present"))
1032 (puthash key value org-publish-cache
))
1034 (defun org-publish-cache-ctime-of-src (filename)
1035 "Get the files ctime as integer."
1036 (let ((src-attr (file-attributes filename
)))
1038 (lsh (car (nth 5 src-attr
)) 16)
1039 (cadr (nth 5 src-attr
)))))
1043 (provide 'org-publish
)
1045 ;; arch-tag: 72807f3c-8af0-4a6b-8dca-c3376eb25adb
1047 ;;; org-publish.el ends here