1 ;;; org-publish.el --- publish related org-mode files as a website
2 ;; Copyright (C) 2006-2012 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
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This program allow configurable publishing of related sets of
26 ;; Org-mode files as a complete website.
28 ;; org-publish.el can do the following:
30 ;; + Publish all one's org-files to HTML or PDF
31 ;; + Upload HTML, images, attachments and other files to a web server
32 ;; + Exclude selected private pages from publishing
33 ;; + Publish a clickable sitemap of pages
34 ;; + Manage local timestamps for publishing only changed files
35 ;; + Accept plugin functions to extend range of publishable content
37 ;; Documentation for publishing is in the manual.
46 (require 'format-spec
)
49 (unless (fboundp 'declare-function
)
50 (defmacro declare-function
(fn file
&optional arglist fileonly
))))
52 (defvar org-publish-initial-buffer nil
53 "The buffer `org-publish' has been called from.")
55 (defvar org-publish-temp-files nil
56 "Temporary list of files to be published.")
58 ;; Here, so you find the variable right before it's used the first time:
59 (defvar org-publish-cache nil
60 "This will cache timestamps and titles for files in publishing projects.
61 Blocks could hash sha1 values here.")
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 :html-preamble `org-export-html-preamble'
159 :html-postamble `org-export-html-postamble'
160 :author `user-full-name'
161 :email `user-mail-address'
163 The following properties may be used to control publishing of a
164 sitemap of files or summary page for a given project.
166 :auto-sitemap Whether to publish a sitemap during
167 `org-publish-current-project' or `org-publish-all'.
168 :sitemap-filename Filename for output of sitemap. Defaults
169 to 'sitemap.org' (which becomes 'sitemap.html').
170 :sitemap-title Title of sitemap page. Defaults to name of file.
171 :sitemap-function Plugin function to use for generation of sitemap.
172 Defaults to `org-publish-org-sitemap', which
173 generates a plain list of links to all files
175 :sitemap-style Can be `list' (sitemap is just an itemized list
176 of the titles of the files involved) or
177 `tree' (the directory structure of the source
178 files is reflected in the sitemap). Defaults to
180 :sitemap-sans-extension Remove extension from sitemap's
181 filenames. Useful to have cool
183 http://www.w3.org/Provider/Style/URI).
186 If you create a sitemap file, adjust the sorting like this:
188 :sitemap-sort-folders Where folders should appear in the sitemap.
189 Set this to `first' (default) or `last' to
190 display folders first or last, respectively.
191 Any other value will mix files and folders.
192 :sitemap-sort-files The site map is normally sorted alphabetically.
193 You can change this behaviour setting this to
194 `chronologically', `anti-chronologically' or nil.
195 :sitemap-ignore-case Should sorting be case-sensitive? Default nil.
197 The following properties control the creation of a concept index.
199 :makeindex Create a concept index.
201 Other properties affecting publication.
203 :body-only Set this to 't' to publish only the body of the
204 documents, excluding everything outside and
205 including the <body> tags in HTML, or
206 \begin{document}..\end{document} in LaTeX."
210 (defcustom org-publish-use-timestamps-flag t
211 "Non-nil means use timestamp checking to publish only changed files.
212 When nil, do no timestamp checking and always publish all files."
216 (defcustom org-publish-timestamp-directory
(convert-standard-filename
217 "~/.org-timestamps/")
218 "Name of directory in which to store publishing timestamps."
222 (defcustom org-publish-list-skipped-files t
223 "Non-nil means show message about files *not* published."
227 (defcustom org-publish-before-export-hook nil
228 "Hook run before export on the Org file.
229 The hook may modify the file in arbitrary ways before publishing happens.
230 The original version of the buffer will be restored after publishing."
234 (defcustom org-publish-after-export-hook nil
235 "Hook run after export on the exported buffer.
236 Any changes made by this hook will be saved."
240 (defcustom org-publish-sitemap-sort-files
'alphabetically
241 "How sitemaps files should be sorted by default?
242 Possible values are `alphabetically', `chronologically', `anti-chronologically' and nil.
243 If `alphabetically', files will be sorted alphabetically.
244 If `chronologically', files will be sorted with older modification time first.
245 If `anti-chronologically', files will be sorted with newer modification time first.
246 nil won't sort files.
248 You can overwrite this default per project in your
249 `org-publish-project-alist', using `:sitemap-sort-files'."
254 (defcustom org-publish-sitemap-sort-folders
'first
255 "A symbol, denoting if folders are sorted first in sitemaps.
256 Possible values are `first', `last', and nil.
257 If `first', folders will be sorted before files.
258 If `last', folders are sorted to the end after the files.
259 Any other value will not mix files and folders.
261 You can overwrite this default per project in your
262 `org-publish-project-alist', using `:sitemap-sort-folders'."
267 (defcustom org-publish-sitemap-sort-ignore-case nil
268 "Sort sitemaps case insensitively by default?
270 You can overwrite this default per project in your
271 `org-publish-project-alist', using `:sitemap-ignore-case'."
276 (defcustom org-publish-sitemap-date-format
"%Y-%m-%d"
277 "Format for `format-time-string' which is used to print a date
283 (defcustom org-publish-sitemap-file-entry-format
"%t"
284 "How a sitemap file entry is formatted.
285 You could use brackets to delimit on what part the link will be.
289 %d is the date formatted using `org-publish-sitemap-date-format'."
295 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
296 ;;; Sanitize-plist (FIXME why?)
298 (defun org-publish-sanitize-plist (plist)
301 (or (cdr (assq x
'((:index-filename .
:sitemap-filename
)
302 (:index-title .
:sitemap-title
)
303 (:index-function .
:sitemap-function
)
304 (:index-style .
:sitemap-style
)
305 (:auto-index .
:auto-sitemap
))))
309 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
310 ;;; Timestamp-related functions
312 (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func
)
313 "Return path to timestamp file for filename FILENAME."
314 (setq filename
(concat filename
"::" (or pub-dir
"") "::"
315 (format "%s" (or pub-func
""))))
316 (concat "X" (if (fboundp 'sha1
) (sha1 filename
) (md5 filename
))))
318 (defun org-publish-needed-p (filename &optional pub-dir pub-func true-pub-dir
)
319 "Return t if FILENAME should be published in PUB-DIR using PUB-FUNC.
320 TRUE-PUB-DIR is where the file will truly end up. Currently we are not using
321 this - maybe it can eventually be used to check if the file is present at
322 the target location, and how old it is. Right now we cannot do this, because
323 we do not know under what file name the file will be stored - the publishing
324 function can still decide about that independently."
326 (if org-publish-use-timestamps-flag
327 (org-publish-cache-file-needs-publishing
328 filename pub-dir pub-func
)
329 ;; don't use timestamps, always return t
332 (message "Publishing file %s using `%s'" filename pub-func
)
333 (when org-publish-list-skipped-files
334 (message "Skipping unmodified file %s" filename
)))
337 (defun org-publish-update-timestamp (filename &optional pub-dir pub-func
)
338 "Update publishing timestamp for file FILENAME.
339 If there is no timestamp, create one."
340 (let ((key (org-publish-timestamp-filename filename pub-dir pub-func
))
341 (stamp (org-publish-cache-ctime-of-src filename
)))
342 (org-publish-cache-set key stamp
)))
344 (defun org-publish-remove-all-timestamps ()
345 "Remove all files in the timestamp directory."
346 (let ((dir org-publish-timestamp-directory
)
348 (when (and (file-exists-p dir
)
349 (file-directory-p dir
))
350 (mapc 'delete-file
(directory-files dir
'full
"[^.]\\'"))
351 (org-publish-reset-cache))))
354 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
355 ;;; Compatibility aliases
357 ;; Delete-dups is not in Emacs <22
358 (if (fboundp 'delete-dups
)
359 (defalias 'org-publish-delete-dups
'delete-dups
)
360 (defun org-publish-delete-dups (list)
361 "Destructively remove `equal' duplicates from LIST.
362 Store the result in LIST and return it. LIST must be a proper list.
363 Of several `equal' occurrences of an element in LIST, the first
366 This is a compatibility function for Emacsen without `delete-dups'."
367 ;; Code from `subr.el' in Emacs 22:
370 (setcdr tail
(delete (car tail
) (cdr tail
)))
371 (setq tail
(cdr tail
))))
374 (declare-function org-publish-delete-dups
"org-publish" (list))
375 (declare-function find-lisp-find-files
"find-lisp" (directory regexp
))
376 (declare-function org-pop-to-buffer-same-window
377 "org-compat" (&optional buffer-or-name norecord label
))
379 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
380 ;;; Getting project information out of org-publish-project-alist
382 (defun org-publish-expand-projects (projects-alist)
383 "Expand projects in PROJECTS-ALIST.
384 This splices all the components into the list."
385 (let ((rest projects-alist
) rtn p components
)
386 (while (setq p
(pop rest
))
387 (if (setq components
(plist-get (cdr p
) :components
))
389 (mapcar (lambda (x) (assoc x org-publish-project-alist
))
393 (nreverse (org-publish-delete-dups (delq nil rtn
)))))
395 (defvar org-sitemap-sort-files
)
396 (defvar org-sitemap-sort-folders
)
397 (defvar org-sitemap-ignore-case
)
398 (defvar org-sitemap-requested
)
399 (defvar org-sitemap-date-format
)
400 (defvar org-sitemap-file-entry-format
)
401 (defun org-publish-compare-directory-files (a b
)
402 "Predicate for `sort', that sorts folders and files for sitemap."
404 (when (or org-sitemap-sort-files org-sitemap-sort-folders
)
405 ;; First we sort files:
406 (when org-sitemap-sort-files
407 (cond ((equal org-sitemap-sort-files
'alphabetically
)
408 (let* ((adir (file-directory-p a
))
409 (aorg (and (string-match "\\.org$" a
) (not adir
)))
410 (bdir (file-directory-p b
))
411 (borg (and (string-match "\\.org$" b
) (not bdir
)))
413 (concat (file-name-directory a
)
414 (org-publish-find-title a
)) a
))
416 (concat (file-name-directory b
)
417 (org-publish-find-title b
)) b
)))
418 (setq retval
(if org-sitemap-ignore-case
419 (not (string-lessp (upcase B
) (upcase A
)))
420 (not (string-lessp B A
))))))
421 ((or (equal org-sitemap-sort-files
'chronologically
)
422 (equal org-sitemap-sort-files
'anti-chronologically
))
423 (let* ((adate (org-publish-find-date a
))
424 (bdate (org-publish-find-date b
))
425 (A (+ (lsh (car adate
) 16) (cadr adate
)))
426 (B (+ (lsh (car bdate
) 16) (cadr bdate
))))
427 (setq retval
(if (equal org-sitemap-sort-files
'chronologically
)
430 ;; Directory-wise wins:
431 (when org-sitemap-sort-folders
432 ;; a is directory, b not:
434 ((and (file-directory-p a
) (not (file-directory-p b
)))
435 (setq retval
(equal org-sitemap-sort-folders
'first
)))
436 ;; a is not a directory, but b is:
437 ((and (not (file-directory-p a
)) (file-directory-p b
))
438 (setq retval
(equal org-sitemap-sort-folders
'last
))))))
441 (defun org-publish-get-base-files-1 (base-dir &optional recurse match skip-file skip-dir
)
442 "Set `org-publish-temp-files' with files from BASE-DIR directory.
443 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
444 non-nil, restrict this list to the files matching the regexp
445 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
446 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
447 matching the regexp SKIP-DIR when recursing through BASE-DIR."
449 (let ((fd-p (file-directory-p f
))
450 (fnd (file-name-nondirectory f
)))
451 (if (and fd-p recurse
452 (not (string-match "^\\.+$" fnd
))
453 (if skip-dir
(not (string-match skip-dir fnd
)) t
))
454 (org-publish-get-base-files-1 f recurse match skip-file skip-dir
)
455 (unless (or fd-p
;; this is a directory
456 (and skip-file
(string-match skip-file fnd
))
457 (not (file-exists-p (file-truename f
)))
458 (not (string-match match fnd
)))
460 (pushnew f org-publish-temp-files
)))))
461 (if org-sitemap-requested
462 (sort (directory-files base-dir t
(unless recurse match
))
463 'org-publish-compare-directory-files
)
464 (directory-files base-dir t
(unless recurse match
)))))
466 (defun org-publish-get-base-files (project &optional exclude-regexp
)
467 "Return a list of all files in PROJECT.
468 If EXCLUDE-REGEXP is set, this will be used to filter out
470 (let* ((project-plist (cdr project
))
471 (base-dir (file-name-as-directory
472 (plist-get project-plist
:base-directory
)))
473 (include-list (plist-get project-plist
:include
))
474 (recurse (plist-get project-plist
:recursive
))
475 (extension (or (plist-get project-plist
:base-extension
) "org"))
476 ;; sitemap-... variables are dynamically scoped for
477 ;; org-publish-compare-directory-files:
478 (org-sitemap-requested
479 (plist-get project-plist
:auto-sitemap
))
481 (or (plist-get project-plist
:sitemap-filename
)
483 (org-sitemap-sort-folders
484 (if (plist-member project-plist
:sitemap-sort-folders
)
485 (plist-get project-plist
:sitemap-sort-folders
)
486 org-publish-sitemap-sort-folders
))
487 (org-sitemap-sort-files
488 (cond ((plist-member project-plist
:sitemap-sort-files
)
489 (plist-get project-plist
:sitemap-sort-files
))
490 ;; For backward compatibility:
491 ((plist-member project-plist
:sitemap-alphabetically
)
492 (if (plist-get project-plist
:sitemap-alphabetically
)
493 'alphabetically nil
))
494 (t org-publish-sitemap-sort-files
)))
495 (org-sitemap-ignore-case
496 (if (plist-member project-plist
:sitemap-ignore-case
)
497 (plist-get project-plist
:sitemap-ignore-case
)
498 org-publish-sitemap-sort-ignore-case
))
499 (match (if (eq extension
'any
)
501 (concat "^[^\\.].*\\.\\(" extension
"\\)$"))))
502 ;; Make sure `org-sitemap-sort-folders' has an accepted value
503 (unless (memq org-sitemap-sort-folders
'(first last
))
504 (setq org-sitemap-sort-folders nil
))
506 (setq org-publish-temp-files nil
)
507 (if org-sitemap-requested
508 (pushnew (expand-file-name (concat base-dir sitemap-filename
))
509 org-publish-temp-files
))
510 (org-publish-get-base-files-1 base-dir recurse match
511 ;; FIXME distinguish exclude regexp
512 ;; for skip-file and skip-dir?
513 exclude-regexp exclude-regexp
)
516 (expand-file-name (concat base-dir f
))
517 org-publish-temp-files
))
519 org-publish-temp-files
))
521 (defun org-publish-get-project-from-filename (filename &optional up
)
522 "Return the project that FILENAME belongs to."
523 (let* ((filename (expand-file-name filename
))
527 (dolist (prj org-publish-project-alist
)
528 (unless (plist-get (cdr prj
) :components
)
529 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
530 (let* ((r (plist-get (cdr prj
) :recursive
))
531 (b (expand-file-name (file-name-as-directory
532 (plist-get (cdr prj
) :base-directory
))))
533 (x (or (plist-get (cdr prj
) :base-extension
) "org"))
534 (e (plist-get (cdr prj
) :exclude
))
535 (i (plist-get (cdr prj
) :include
))
536 (xm (concat "^" b
(if r
".+" "[^/]+") "\\.\\(" x
"\\)$")))
542 (lambda (file) (expand-file-name file b
))
545 (not (and e
(string-match e filename
)))
546 (string-match xm filename
)))
547 (setq project-name
(car prj
))
548 (throw 'p-found project-name
))))))
550 (dolist (prj org-publish-project-alist
)
551 (if (member project-name
(plist-get (cdr prj
) :components
))
552 (setq project-name
(car prj
)))))
553 (assoc project-name org-publish-project-alist
)))
555 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
556 ;;; Pluggable publishing back-end functions
558 (defun org-publish-org-to (format plist filename pub-dir
)
559 "Publish an org file to FORMAT.
560 PLIST is the property list for the given project.
561 FILENAME is the filename of the org file to be published.
562 PUB-DIR is the publishing directory."
564 (unless (file-exists-p pub-dir
)
565 (make-directory pub-dir t
))
566 (let ((visiting (find-buffer-visiting filename
)))
568 (org-pop-to-buffer-same-window (or visiting
(find-file filename
)))
569 (let* ((plist (cons :buffer-will-be-killed
(cons t plist
)))
570 (init-buf (current-buffer))
572 (init-buf-string (buffer-string))
574 ;; run hooks before exporting
575 (run-hooks 'org-publish-before-export-hook
)
576 ;; export the possibly modified buffer
577 (setq export-buf-or-file
578 (funcall (intern (concat "org-export-as-" format
))
579 (plist-get plist
:headline-levels
)
581 (plist-get plist
:body-only
)
583 (when (and (bufferp export-buf-or-file
)
584 (buffer-live-p export-buf-or-file
))
585 (set-buffer export-buf-or-file
)
586 ;; run hooks after export and save export
587 (progn (run-hooks 'org-publish-after-export-hook
)
588 (if (buffer-modified-p) (save-buffer)))
589 (kill-buffer export-buf-or-file
))
590 ;; maybe restore buffer's content
591 (set-buffer init-buf
)
592 (when (buffer-modified-p init-buf
)
594 (insert init-buf-string
)
596 (goto-char init-point
))
598 (kill-buffer init-buf
))))))
600 (defmacro org-publish-with-aux-preprocess-maybe
(&rest body
)
601 "Execute BODY with a modified hook to preprocess for index."
602 `(let ((org-export-preprocess-after-headline-targets-hook
603 (if (plist-get project-plist
:makeindex
)
604 (cons 'org-publish-aux-preprocess
605 org-export-preprocess-after-headline-targets-hook
)
606 org-export-preprocess-after-headline-targets-hook
)))
608 (def-edebug-spec org-publish-with-aux-preprocess-maybe
(body))
610 (defvar project-plist
)
611 (defun org-publish-org-to-latex (plist filename pub-dir
)
612 "Publish an org file to LaTeX.
613 See `org-publish-org-to' to the list of arguments."
614 (org-publish-with-aux-preprocess-maybe
615 (org-publish-org-to "latex" plist filename pub-dir
)))
617 (defun org-publish-org-to-pdf (plist filename pub-dir
)
618 "Publish an org file to PDF (via LaTeX).
619 See `org-publish-org-to' to the list of arguments."
620 (org-publish-with-aux-preprocess-maybe
621 (org-publish-org-to "pdf" plist filename pub-dir
)))
623 (defun org-publish-org-to-html (plist filename pub-dir
)
624 "Publish an org file to HTML.
625 See `org-publish-org-to' to the list of arguments."
626 (org-publish-with-aux-preprocess-maybe
627 (org-publish-org-to "html" plist filename pub-dir
)))
629 (defun org-publish-org-to-org (plist filename pub-dir
)
630 "Publish an org file to HTML.
631 See `org-publish-org-to' to the list of arguments."
632 (org-publish-org-to "org" plist filename pub-dir
))
634 (defun org-publish-org-to-ascii (plist filename pub-dir
)
635 "Publish an org file to ASCII.
636 See `org-publish-org-to' to the list of arguments."
637 (org-publish-with-aux-preprocess-maybe
638 (org-publish-org-to "ascii" plist filename pub-dir
)))
640 (defun org-publish-org-to-latin1 (plist filename pub-dir
)
641 "Publish an org file to Latin-1.
642 See `org-publish-org-to' to the list of arguments."
643 (org-publish-with-aux-preprocess-maybe
644 (org-publish-org-to "latin1" plist filename pub-dir
)))
646 (defun org-publish-org-to-utf8 (plist filename pub-dir
)
647 "Publish an org file to UTF-8.
648 See `org-publish-org-to' to the list of arguments."
649 (org-publish-with-aux-preprocess-maybe
650 (org-publish-org-to "utf8" plist filename pub-dir
)))
652 (defun org-publish-attachment (plist filename pub-dir
)
653 "Publish a file with no transformation of any kind.
654 See `org-publish-org-to' to the list of arguments."
655 ;; make sure eshell/cp code is loaded
656 (unless (file-directory-p pub-dir
)
657 (make-directory pub-dir t
))
658 (or (equal (expand-file-name (file-name-directory filename
))
659 (file-name-as-directory (expand-file-name pub-dir
)))
661 (expand-file-name (file-name-nondirectory filename
) pub-dir
)
664 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
665 ;;; Publishing files, sets of files, and indices
667 (defun org-publish-file (filename &optional project no-cache
)
668 "Publish file FILENAME from PROJECT.
669 If NO-CACHE is not nil, do not initialize org-publish-cache and
670 write it to disk. This is needed, since this function is used to
671 publish single files, when entire projects are published.
672 See `org-publish-projects'."
675 (or (org-publish-get-project-from-filename filename
)
676 (error "File %s not part of any known project"
677 (abbreviate-file-name filename
)))))
678 (project-plist (cdr project
))
679 (ftname (expand-file-name filename
))
681 (or (plist-get project-plist
:publishing-function
)
682 'org-publish-org-to-html
))
684 (file-name-as-directory
686 (or (plist-get project-plist
:base-directory
)
687 (error "Project %s does not have :base-directory defined"
690 (file-name-as-directory
692 (or (eval (plist-get project-plist
:publishing-directory
))
693 (error "Project %s does not have :publishing-directory defined"
698 (org-publish-initialize-cache (car project
)))
703 (and (string-match (regexp-quote base-dir
) ftname
)
704 (substring ftname
(match-end 0))))))
705 (if (listp publishing-function
)
706 ;; allow chain of publishing functions
708 (when (org-publish-needed-p filename pub-dir f tmp-pub-dir
)
709 (funcall f project-plist filename tmp-pub-dir
)
710 (org-publish-update-timestamp filename pub-dir f
)))
712 (when (org-publish-needed-p filename pub-dir publishing-function
714 (funcall publishing-function project-plist filename tmp-pub-dir
)
715 (org-publish-update-timestamp
716 filename pub-dir publishing-function
)))
717 (unless no-cache
(org-publish-write-cache-file))))
719 (defun org-publish-projects (projects)
720 "Publish all files belonging to the PROJECTS alist.
721 If :auto-sitemap is set, publish the sitemap too.
722 If :makeindex is set, also produce a file theindex.org."
725 ;; Each project uses its own cache file:
726 (org-publish-initialize-cache (car project
))
728 ((project-plist (cdr project
))
729 (exclude-regexp (plist-get project-plist
:exclude
))
730 (sitemap-p (plist-get project-plist
:auto-sitemap
))
731 (sitemap-filename (or (plist-get project-plist
:sitemap-filename
)
733 (sitemap-function (or (plist-get project-plist
:sitemap-function
)
734 'org-publish-org-sitemap
))
735 (org-sitemap-date-format (or (plist-get project-plist
:sitemap-date-format
)
736 org-publish-sitemap-date-format
))
737 (org-sitemap-file-entry-format (or (plist-get project-plist
:sitemap-file-entry-format
)
738 org-publish-sitemap-file-entry-format
))
739 (preparation-function (plist-get project-plist
:preparation-function
))
740 (completion-function (plist-get project-plist
:completion-function
))
741 (files (org-publish-get-base-files project exclude-regexp
)) file
)
742 (when preparation-function
(run-hooks 'preparation-function
))
743 (if sitemap-p
(funcall sitemap-function project sitemap-filename
))
744 (while (setq file
(pop files
))
745 (org-publish-file file project t
))
746 (when (plist-get project-plist
:makeindex
)
747 (org-publish-index-generate-theindex
748 (plist-get project-plist
:base-directory
))
749 (org-publish-file (expand-file-name
751 (plist-get project-plist
:base-directory
))
753 (when completion-function
(run-hooks 'completion-function
))
754 (org-publish-write-cache-file)))
755 (org-publish-expand-projects projects
)))
757 (defun org-publish-org-sitemap (project &optional sitemap-filename
)
758 "Create a sitemap of pages in set defined by PROJECT.
759 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
760 Default for SITEMAP-FILENAME is 'sitemap.org'."
761 (let* ((project-plist (cdr project
))
762 (dir (file-name-as-directory
763 (plist-get project-plist
:base-directory
)))
764 (localdir (file-name-directory dir
))
765 (indent-str (make-string 2 ?\
))
766 (exclude-regexp (plist-get project-plist
:exclude
))
767 (files (nreverse (org-publish-get-base-files project exclude-regexp
)))
768 (sitemap-filename (concat dir
(or sitemap-filename
"sitemap.org")))
769 (sitemap-title (or (plist-get project-plist
:sitemap-title
)
770 (concat "Sitemap for project " (car project
))))
771 (sitemap-style (or (plist-get project-plist
:sitemap-style
)
773 (sitemap-sans-extension (plist-get project-plist
:sitemap-sans-extension
))
774 (visiting (find-buffer-visiting sitemap-filename
))
775 (ifn (file-name-nondirectory sitemap-filename
))
777 (with-current-buffer (setq sitemap-buffer
778 (or visiting
(find-file sitemap-filename
)))
780 (insert (concat "#+TITLE: " sitemap-title
"\n\n"))
781 (while (setq file
(pop files
))
782 (let ((fn (file-name-nondirectory file
))
783 (link (file-relative-name file dir
))
785 (when sitemap-sans-extension
786 (setq link
(file-name-sans-extension link
)))
787 ;; sitemap shouldn't list itself
788 (unless (equal (file-truename sitemap-filename
)
789 (file-truename file
))
790 (if (eq sitemap-style
'list
)
791 (message "Generating list-style sitemap for %s" sitemap-title
)
792 (message "Generating tree-style sitemap for %s" sitemap-title
)
793 (setq localdir
(concat (file-name-as-directory dir
)
794 (file-name-directory link
)))
795 (unless (string= localdir oldlocal
)
796 (if (string= localdir dir
)
797 (setq indent-str
(make-string 2 ?\
))
802 (file-relative-name localdir dir
))) "/"))
804 (old-subdirs (split-string
805 (file-relative-name oldlocal dir
) "/")))
806 (setq indent-str
(make-string 2 ?\
))
807 (while (string= (car old-subdirs
) (car subdirs
))
808 (setq indent-str
(concat indent-str
(make-string 2 ?\
)))
812 (setq subdir
(concat subdir d
"/"))
813 (insert (concat indent-str
" + " d
"\n"))
814 (setq indent-str
(make-string
815 (+ (length indent-str
) 2) ?\
)))))))
816 ;; This is common to 'flat and 'tree
818 (org-publish-format-file-entry org-sitemap-file-entry-format
820 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
821 (cond ((string-match-p regexp entry
)
822 (string-match regexp entry
)
823 (insert (concat indent-str
" + " (match-string 1 entry
)
825 (match-string 2 entry
)
826 "]]" (match-string 3 entry
) "\n")))
828 (insert (concat indent-str
" + [[file:" link
"]["
832 (or visiting
(kill-buffer sitemap-buffer
))))
834 (defun org-publish-format-file-entry (fmt file project-plist
)
836 `((?t .
,(org-publish-find-title file t
))
837 (?d .
,(format-time-string org-sitemap-date-format
838 (org-publish-find-date file
)))
839 (?a .
,(or (plist-get project-plist
:author
) user-full-name
)))))
841 (defun org-publish-find-title (file &optional reset
)
842 "Find the title of FILE in project."
844 (and (not reset
) (org-publish-cache-get-file-property file
:title nil t
))
845 (let* ((visiting (find-buffer-visiting file
))
846 (buffer (or visiting
(find-file-noselect file
)))
848 (with-current-buffer buffer
849 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
850 (org-infile-export-plist))))
852 (or (plist-get opt-plist
:title
)
854 (plist-get opt-plist
:skip-before-1st-heading
))
855 (org-export-grab-title-from-buffer))
856 (file-name-nondirectory (file-name-sans-extension file
))))))
858 (kill-buffer buffer
))
859 (org-publish-cache-set-file-property file
:title title
)
862 (defun org-publish-find-date (file)
863 "Find the date of FILE in project.
864 If FILE provides a #+date keyword use it else use the file
865 system's modification time.
867 It returns time in `current-time' format."
868 (let ((visiting (find-buffer-visiting file
)))
870 (org-pop-to-buffer-same-window (or visiting
(find-file-noselect file nil t
)))
871 (let* ((plist (org-infile-export-plist))
872 (date (plist-get plist
:date
)))
874 (kill-buffer (current-buffer)))
876 (org-time-string-to-time date
)
877 (when (file-exists-p file
)
878 (nth 5 (file-attributes file
))))))))
880 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
881 ;;; Interactive publishing functions
884 (defalias 'org-publish-project
'org-publish
)
887 (defun org-publish (project &optional force
)
891 (assoc (org-icompleting-read
893 org-publish-project-alist nil t
)
894 org-publish-project-alist
)
896 (setq org-publish-initial-buffer
(current-buffer))
897 (save-window-excursion
898 (let* ((org-publish-use-timestamps-flag
899 (if force nil org-publish-use-timestamps-flag
)))
900 (org-publish-projects
901 (if (stringp project
)
902 ;; If this function is called in batch mode,
903 ;; project is still a string here.
904 (list (assoc project org-publish-project-alist
))
908 (defun org-publish-all (&optional force
)
909 "Publish all projects.
910 With prefix argument, remove all files in the timestamp
911 directory and force publishing all files."
914 (org-publish-remove-all-timestamps))
915 (save-window-excursion
916 (let ((org-publish-use-timestamps-flag
917 (if force nil org-publish-use-timestamps-flag
)))
918 (org-publish-projects org-publish-project-alist
))))
922 (defun org-publish-current-file (&optional force
)
923 "Publish the current file.
924 With prefix argument, force publish the file."
926 (save-window-excursion
927 (let ((org-publish-use-timestamps-flag
928 (if force nil org-publish-use-timestamps-flag
)))
929 (org-publish-file (buffer-file-name)))))
932 (defun org-publish-current-project (&optional force
)
933 "Publish the project associated with the current file.
934 With a prefix argument, force publishing of all files in
937 (save-window-excursion
938 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up
))
939 (org-publish-use-timestamps-flag
940 (if force nil org-publish-use-timestamps-flag
)))
942 (error "File %s is not part of any known project" (buffer-file-name)))
943 ;; FIXME: force is not used here?
944 (org-publish project
))))
949 (defun org-publish-aux-preprocess ()
950 "Find index entries and write them to an .orgx file."
951 (let ((case-fold-search t
)
953 (goto-char (point-min))
956 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t
)
957 (> (match-end 1) (match-beginning 1)))
958 (setq entry
(match-string 1))
959 (when (eq org-export-current-backend
'latex
)
960 (replace-match (format "\\index{%s}" entry
) t t
))
962 (ignore-errors (org-back-to-heading t
))
963 (setq target
(get-text-property (point) 'target
))
964 (setq target
(or (cdr (assoc target org-export-preferred-target-alist
))
965 (cdr (assoc target org-export-id-target-alist
))
967 (push (cons entry target
) index
)))
970 (file-name-directory org-current-export-file
) "."
971 (file-name-sans-extension
972 (file-name-nondirectory org-current-export-file
)) ".orgx")
973 (dolist (entry (nreverse index
))
974 (insert (format "INDEX: (%s) %s\n" (cdr entry
) (car entry
)))))))
976 (defun org-publish-index-generate-theindex (directory)
977 "Generate the index from all .orgx files in DIRECTORY."
979 (let* ((fulldir (file-name-as-directory
980 (expand-file-name directory
)))
981 (full-files (find-lisp-find-files directory
"\\.orgx\\'"))
982 (re (concat "\\`" fulldir
))
983 (files (mapcar (lambda (f) (if (string-match re f
)
984 (substring f
(match-end 0))
987 (default-directory directory
)
988 index origfile buf target entry ibuffer
989 main last-main letter last-letter file sub link tgext
)
990 ;; `files' contains the list of relative file names
993 (concat (file-name-directory file
)
994 (substring (file-name-nondirectory file
) 1 -
1)))
995 (setq buf
(find-file-noselect file
))
996 (with-current-buffer buf
997 (goto-char (point-min))
998 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t
)
999 (setq target
(match-string 1)
1000 entry
(match-string 2))
1001 (push (list entry origfile target
) index
)))
1003 (setq index
(sort index
(lambda (a b
) (string< (downcase (car a
))
1004 (downcase (car b
))))))
1005 (setq ibuffer
(find-file-noselect (expand-file-name "theindex.inc" directory
)))
1006 (with-current-buffer ibuffer
1008 (insert "* Index\n")
1009 (setq last-letter nil
)
1011 (setq entry
(car idx
) file
(nth 1 idx
) target
(nth 2 idx
))
1012 (if (and (stringp target
) (string-match "\\S-" target
))
1013 (setq tgext
(concat "::#" target
))
1015 (setq letter
(upcase (substring entry
0 1)))
1016 (when (not (equal letter last-letter
))
1017 (insert "** " letter
"\n")
1018 (setq last-letter letter
))
1019 (if (string-match "!" entry
)
1020 (setq main
(substring entry
0 (match-beginning 0))
1021 sub
(substring entry
(match-end 0)))
1022 (setq main nil sub nil last-main nil
))
1023 (when (and main
(not (equal main last-main
)))
1024 (insert " - " main
"\n")
1025 (setq last-main main
))
1026 (setq link
(concat "[[file:" file tgext
"]"
1027 "[" (or sub entry
) "]]"))
1029 (insert " - " link
"\n")
1030 (insert " - " link
"\n")))
1032 (kill-buffer ibuffer
)
1033 ;; Create theindex.org if it doesn't exist already
1034 (let ((index-file (expand-file-name "theindex.org" directory
)))
1035 (unless (file-exists-p index-file
)
1036 (setq ibuffer
(find-file-noselect index-file
))
1037 (with-current-buffer ibuffer
1039 (insert "\n\n#+INCLUDE: \"theindex.inc\"\n\n")
1041 (kill-buffer ibuffer
)))))
1043 ;; Caching functions:
1045 (defun org-publish-write-cache-file (&optional free-cache
)
1046 "Write `org-publish-cache' to file.
1047 If FREE-CACHE, empty the cache."
1048 (unless org-publish-cache
1049 (error "%s" "`org-publish-write-cache-file' called, but no cache present"))
1051 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1054 "%s" "Cannot find cache-file name in `org-publish-write-cache-file'"))
1055 (with-temp-file cache-file
1056 (let ((print-level nil
)
1058 (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
1059 (maphash (lambda (k v
)
1061 (format (concat "(puthash %S "
1062 (if (or (listp v
) (symbolp v
))
1064 "%S org-publish-cache)\n") k v
)))
1065 org-publish-cache
)))
1066 (when free-cache
(org-publish-reset-cache))))
1068 (defun org-publish-initialize-cache (project-name)
1069 "Initialize the projects cache if not initialized yet and return it."
1071 (unless project-name
1072 (error "%s%s" "Cannot initialize `org-publish-cache' without projects name"
1073 " in `org-publish-initialize-cache'"))
1075 (unless (file-exists-p org-publish-timestamp-directory
)
1076 (make-directory org-publish-timestamp-directory t
))
1077 (if (not (file-directory-p org-publish-timestamp-directory
))
1078 (error "Org publish timestamp: %s is not a directory"
1079 org-publish-timestamp-directory
))
1081 (unless (and org-publish-cache
1082 (string= (org-publish-cache-get ":project:") project-name
))
1083 (let* ((cache-file (concat
1084 (expand-file-name org-publish-timestamp-directory
)
1087 (cexists (file-exists-p cache-file
)))
1089 (when org-publish-cache
1090 (org-publish-reset-cache))
1093 (load-file cache-file
)
1094 (setq org-publish-cache
1095 (make-hash-table :test
'equal
:weakness nil
:size
100))
1096 (org-publish-cache-set ":project:" project-name
)
1097 (org-publish-cache-set ":cache-file:" cache-file
))
1098 (unless cexists
(org-publish-write-cache-file nil
))))
1101 (defun org-publish-reset-cache ()
1102 "Empty org-publish-cache and reset it nil."
1103 (message "%s" "Resetting org-publish-cache")
1104 (if (hash-table-p org-publish-cache
)
1105 (clrhash org-publish-cache
))
1106 (setq org-publish-cache nil
))
1108 (defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func
)
1109 "Check the timestamp of the last publishing of FILENAME.
1110 Return `t', if the file needs publishing. The function also
1111 checks if any included files have been more recently published,
1112 so that the file including them will be republished as well."
1113 (unless org-publish-cache
1114 (error "%s" "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1115 (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func
))
1116 (pstamp (org-publish-cache-get key
))
1117 (visiting (find-buffer-visiting filename
))
1118 included-files-ctime buf
)
1120 (when (equal (file-name-extension filename
) "org")
1121 (setq buf
(find-file (expand-file-name filename
)))
1122 (with-current-buffer buf
1123 (goto-char (point-min))
1124 (while (re-search-forward "^#\\+INCLUDE:[ \t]+\"?\\([^ \t\n\r\"]*\\)\"?[ \t]*.*$" nil t
)
1125 (let* ((included-file (expand-file-name (match-string 1))))
1126 (add-to-list 'included-files-ctime
1127 (org-publish-cache-ctime-of-src included-file
) t
))))
1128 ;; FIXME don't kill current buffer
1129 (unless visiting
(kill-buffer buf
)))
1132 (let ((ctime (org-publish-cache-ctime-of-src filename
)))
1133 (or (< pstamp ctime
)
1134 (when included-files-ctime
1135 (not (null (delq nil
(mapcar (lambda(ct) (< ctime ct
))
1136 included-files-ctime
))))))))))
1138 (defun org-publish-cache-set-file-property (filename property value
&optional project-name
)
1139 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1140 Use cache file of PROJECT-NAME. If the entry does not exist, it will be
1141 created. Return VALUE."
1142 ;; Evtl. load the requested cache file:
1143 (if project-name
(org-publish-initialize-cache project-name
))
1144 (let ((pl (org-publish-cache-get filename
)))
1147 (plist-put pl property value
)
1149 (org-publish-cache-get-file-property
1150 filename property value nil project-name
))))
1152 (defun org-publish-cache-get-file-property
1153 (filename property
&optional default no-create project-name
)
1154 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1155 Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
1156 DEFAULT, if the value does not yet exist.
1157 If the entry will be created, unless NO-CREATE is not nil."
1158 ;; Evtl. load the requested cache file:
1159 (if project-name
(org-publish-initialize-cache project-name
))
1160 (let ((pl (org-publish-cache-get filename
))
1163 (if (plist-member pl property
)
1164 (setq retval
(plist-get pl property
))
1165 (setq retval default
))
1168 (org-publish-cache-set filename
(list property default
)))
1169 (setq retval default
))
1172 (defun org-publish-cache-get (key)
1173 "Return the value stored in `org-publish-cache' for key KEY.
1174 Returns nil, if no value or nil is found, or the cache does not
1176 (unless org-publish-cache
1177 (error "%s" "`org-publish-cache-get' called, but no cache present"))
1178 (gethash key org-publish-cache
))
1180 (defun org-publish-cache-set (key value
)
1181 "Store KEY VALUE pair in `org-publish-cache'.
1182 Returns value on success, else nil."
1183 (unless org-publish-cache
1184 (error "%s" "`org-publish-cache-set' called, but no cache present"))
1185 (puthash key value org-publish-cache
))
1187 (defun org-publish-cache-ctime-of-src (filename)
1188 "Get the FILENAME ctime as an integer."
1189 (let* ((symlink-maybe (or (file-symlink-p filename
) filename
))
1190 (src-attr (file-attributes (if (file-name-absolute-p symlink-maybe
)
1194 (file-name-directory filename
))))))
1196 (lsh (car (nth 5 src-attr
)) 16)
1197 (cadr (nth 5 src-attr
)))))
1199 (provide 'org-publish
)
1201 ;;; org-publish.el ends here