1 ;;; org-publish.el --- publish related org-mode files as a website
2 ;; Copyright (C) 2006-2013 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 base-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 base-dir
)
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 base-dir
)
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 base-dir
)
709 (funcall f project-plist filename tmp-pub-dir
)
710 (org-publish-update-timestamp filename pub-dir f base-dir
)))
712 (when (org-publish-needed-p filename pub-dir publishing-function tmp-pub-dir base-dir
)
713 (funcall publishing-function project-plist filename tmp-pub-dir
)
714 (org-publish-update-timestamp
715 filename pub-dir publishing-function base-dir
)))
716 (unless no-cache
(org-publish-write-cache-file))))
718 (defun org-publish-projects (projects)
719 "Publish all files belonging to the PROJECTS alist.
720 If :auto-sitemap is set, publish the sitemap too.
721 If :makeindex is set, also produce a file theindex.org."
724 ;; Each project uses its own cache file:
725 (org-publish-initialize-cache (car project
))
727 ((project-plist (cdr project
))
728 (exclude-regexp (plist-get project-plist
:exclude
))
729 (sitemap-p (plist-get project-plist
:auto-sitemap
))
730 (sitemap-filename (or (plist-get project-plist
:sitemap-filename
)
732 (sitemap-function (or (plist-get project-plist
:sitemap-function
)
733 'org-publish-org-sitemap
))
734 (org-sitemap-date-format (or (plist-get project-plist
:sitemap-date-format
)
735 org-publish-sitemap-date-format
))
736 (org-sitemap-file-entry-format (or (plist-get project-plist
:sitemap-file-entry-format
)
737 org-publish-sitemap-file-entry-format
))
738 (preparation-function (plist-get project-plist
:preparation-function
))
739 (completion-function (plist-get project-plist
:completion-function
))
740 (files (org-publish-get-base-files project exclude-regexp
)) file
)
741 (when preparation-function
(run-hooks 'preparation-function
))
742 (if sitemap-p
(funcall sitemap-function project sitemap-filename
))
743 (while (setq file
(pop files
))
744 (org-publish-file file project t
))
745 (when (plist-get project-plist
:makeindex
)
746 (org-publish-index-generate-theindex
747 (plist-get project-plist
:base-directory
))
748 (org-publish-file (expand-file-name
750 (plist-get project-plist
:base-directory
))
752 (when completion-function
(run-hooks 'completion-function
))
753 (org-publish-write-cache-file)))
754 (org-publish-expand-projects projects
)))
756 (defun org-publish-org-sitemap (project &optional sitemap-filename
)
757 "Create a sitemap of pages in set defined by PROJECT.
758 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
759 Default for SITEMAP-FILENAME is 'sitemap.org'."
760 (let* ((project-plist (cdr project
))
761 (dir (file-name-as-directory
762 (plist-get project-plist
:base-directory
)))
763 (localdir (file-name-directory dir
))
764 (indent-str (make-string 2 ?\
))
765 (exclude-regexp (plist-get project-plist
:exclude
))
766 (files (nreverse (org-publish-get-base-files project exclude-regexp
)))
767 (sitemap-filename (concat dir
(or sitemap-filename
"sitemap.org")))
768 (sitemap-title (or (plist-get project-plist
:sitemap-title
)
769 (concat "Sitemap for project " (car project
))))
770 (sitemap-style (or (plist-get project-plist
:sitemap-style
)
772 (sitemap-sans-extension (plist-get project-plist
:sitemap-sans-extension
))
773 (visiting (find-buffer-visiting sitemap-filename
))
774 (ifn (file-name-nondirectory sitemap-filename
))
776 (with-current-buffer (setq sitemap-buffer
777 (or visiting
(find-file sitemap-filename
)))
779 (insert (concat "#+TITLE: " sitemap-title
"\n\n"))
780 (while (setq file
(pop files
))
781 (let ((fn (file-name-nondirectory file
))
782 (link (file-relative-name file dir
))
784 (when sitemap-sans-extension
785 (setq link
(file-name-sans-extension link
)))
786 ;; sitemap shouldn't list itself
787 (unless (equal (file-truename sitemap-filename
)
788 (file-truename file
))
789 (if (eq sitemap-style
'list
)
790 (message "Generating list-style sitemap for %s" sitemap-title
)
791 (message "Generating tree-style sitemap for %s" sitemap-title
)
792 (setq localdir
(concat (file-name-as-directory dir
)
793 (file-name-directory link
)))
794 (unless (string= localdir oldlocal
)
795 (if (string= localdir dir
)
796 (setq indent-str
(make-string 2 ?\
))
801 (file-relative-name localdir dir
))) "/"))
803 (old-subdirs (split-string
804 (file-relative-name oldlocal dir
) "/")))
805 (setq indent-str
(make-string 2 ?\
))
806 (while (string= (car old-subdirs
) (car subdirs
))
807 (setq indent-str
(concat indent-str
(make-string 2 ?\
)))
811 (setq subdir
(concat subdir d
"/"))
812 (insert (concat indent-str
" + " d
"\n"))
813 (setq indent-str
(make-string
814 (+ (length indent-str
) 2) ?\
)))))))
815 ;; This is common to 'flat and 'tree
817 (org-publish-format-file-entry org-sitemap-file-entry-format
819 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
820 (cond ((string-match-p regexp entry
)
821 (string-match regexp entry
)
822 (insert (concat indent-str
" + " (match-string 1 entry
)
824 (match-string 2 entry
)
825 "]]" (match-string 3 entry
) "\n")))
827 (insert (concat indent-str
" + [[file:" link
"]["
831 (or visiting
(kill-buffer sitemap-buffer
))))
833 (defun org-publish-format-file-entry (fmt file project-plist
)
835 `((?t .
,(org-publish-find-title file t
))
836 (?d .
,(format-time-string org-sitemap-date-format
837 (org-publish-find-date file
)))
838 (?a .
,(or (plist-get project-plist
:author
) user-full-name
)))))
840 (defun org-publish-find-title (file &optional reset
)
841 "Find the title of FILE in project."
843 (and (not reset
) (org-publish-cache-get-file-property file
:title nil t
))
844 (let* ((visiting (find-buffer-visiting file
))
845 (buffer (or visiting
(find-file-noselect file
)))
847 (with-current-buffer buffer
848 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
849 (org-infile-export-plist))))
851 (or (plist-get opt-plist
:title
)
853 (plist-get opt-plist
:skip-before-1st-heading
))
854 (org-export-grab-title-from-buffer))
855 (file-name-nondirectory (file-name-sans-extension file
))))))
857 (kill-buffer buffer
))
858 (org-publish-cache-set-file-property file
:title title
)
861 (defun org-publish-find-date (file)
862 "Find the date of FILE in project.
863 If FILE provides a #+date keyword use it else use the file
864 system's modification time.
866 It returns time in `current-time' format."
867 (let ((visiting (find-buffer-visiting file
)))
869 (org-pop-to-buffer-same-window (or visiting
(find-file-noselect file nil t
)))
870 (let* ((plist (org-infile-export-plist))
871 (date (plist-get plist
:date
)))
873 (kill-buffer (current-buffer)))
875 (org-time-string-to-time date
)
876 (when (file-exists-p file
)
877 (nth 5 (file-attributes file
))))))))
879 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
880 ;;; Interactive publishing functions
883 (defalias 'org-publish-project
'org-publish
)
886 (defun org-publish (project &optional force
)
890 (assoc (org-icompleting-read
892 org-publish-project-alist nil t
)
893 org-publish-project-alist
)
895 (setq org-publish-initial-buffer
(current-buffer))
896 (save-window-excursion
897 (let* ((org-publish-use-timestamps-flag
898 (if force nil org-publish-use-timestamps-flag
)))
899 (org-publish-projects
900 (if (stringp project
)
901 ;; If this function is called in batch mode,
902 ;; project is still a string here.
903 (list (assoc project org-publish-project-alist
))
907 (defun org-publish-all (&optional force
)
908 "Publish all projects.
909 With prefix argument, remove all files in the timestamp
910 directory and force publishing all files."
913 (org-publish-remove-all-timestamps))
914 (save-window-excursion
915 (let ((org-publish-use-timestamps-flag
916 (if force nil org-publish-use-timestamps-flag
)))
917 (org-publish-projects org-publish-project-alist
))))
920 (defun org-publish-current-file (&optional force
)
921 "Publish the current file.
922 With prefix argument, force publish the file."
924 (save-window-excursion
925 (let ((org-publish-use-timestamps-flag
926 (if force nil org-publish-use-timestamps-flag
)))
927 (org-publish-file (buffer-file-name)))))
930 (defun org-publish-current-project (&optional force
)
931 "Publish the project associated with the current file.
932 With a prefix argument, force publishing of all files in
935 (save-window-excursion
936 (let ((project (org-publish-get-project-from-filename (buffer-file-name) 'up
))
937 (org-publish-use-timestamps-flag
938 (if force nil org-publish-use-timestamps-flag
)))
940 (error "File %s is not part of any known project" (buffer-file-name)))
941 ;; FIXME: force is not used here?
942 (org-publish project
))))
947 (defun org-publish-aux-preprocess ()
948 "Find index entries and write them to an .orgx file."
949 (let ((case-fold-search t
)
951 (goto-char (point-min))
954 (re-search-forward "^[ \t]*#\\+index:[ \t]*\\(.*?\\)[ \t]*$" nil t
)
955 (> (match-end 1) (match-beginning 1)))
956 (setq entry
(match-string 1))
957 (when (eq org-export-current-backend
'latex
)
958 (replace-match (format "\\index{%s}" entry
) t t
))
960 (ignore-errors (org-back-to-heading t
))
961 (setq target
(get-text-property (point) 'target
))
962 (setq target
(or (cdr (assoc target org-export-preferred-target-alist
))
963 (cdr (assoc target org-export-id-target-alist
))
965 (push (cons entry target
) index
)))
968 (file-name-directory org-current-export-file
) "."
969 (file-name-sans-extension
970 (file-name-nondirectory org-current-export-file
)) ".orgx")
971 (dolist (entry (nreverse index
))
972 (insert (format "INDEX: (%s) %s\n" (cdr entry
) (car entry
)))))))
974 (defun org-publish-index-generate-theindex (directory)
975 "Generate the index from all .orgx files in DIRECTORY."
977 (let* ((fulldir (file-name-as-directory
978 (expand-file-name directory
)))
979 (full-files (find-lisp-find-files directory
"\\.orgx\\'"))
980 (re (concat "\\`" fulldir
))
981 (files (mapcar (lambda (f) (if (string-match re f
)
982 (substring f
(match-end 0))
985 (default-directory directory
)
986 index origfile buf target entry ibuffer
987 main last-main letter last-letter file sub link tgext
)
988 ;; `files' contains the list of relative file names
991 (concat (file-name-directory file
)
992 (substring (file-name-nondirectory file
) 1 -
1)))
993 (setq buf
(find-file-noselect file
))
994 (with-current-buffer buf
995 (goto-char (point-min))
996 (while (re-search-forward "^INDEX: (\\(.*?\\)) \\(.*\\)" nil t
)
997 (setq target
(match-string 1)
998 entry
(match-string 2))
999 (push (list entry origfile target
) index
)))
1001 (setq index
(sort index
(lambda (a b
) (string< (downcase (car a
))
1002 (downcase (car b
))))))
1003 (setq ibuffer
(find-file-noselect (expand-file-name "theindex.inc" directory
)))
1004 (with-current-buffer ibuffer
1006 (insert "* Index\n")
1007 (setq last-letter nil
)
1009 (setq entry
(car idx
) file
(nth 1 idx
) target
(nth 2 idx
))
1010 (if (and (stringp target
) (string-match "\\S-" target
))
1011 (setq tgext
(concat "::#" target
))
1013 (setq letter
(upcase (substring entry
0 1)))
1014 (when (not (equal letter last-letter
))
1015 (insert "** " letter
"\n")
1016 (setq last-letter letter
))
1017 (if (string-match "!" entry
)
1018 (setq main
(substring entry
0 (match-beginning 0))
1019 sub
(substring entry
(match-end 0)))
1020 (setq main nil sub nil last-main nil
))
1021 (when (and main
(not (equal main last-main
)))
1022 (insert " - " main
"\n")
1023 (setq last-main main
))
1024 (setq link
(concat "[[file:" file tgext
"]"
1025 "[" (or sub entry
) "]]"))
1027 (insert " - " link
"\n")
1028 (insert " - " link
"\n")))
1030 (kill-buffer ibuffer
)
1031 ;; Create theindex.org if it doesn't exist already
1032 (let ((index-file (expand-file-name "theindex.org" directory
)))
1033 (unless (file-exists-p index-file
)
1034 (setq ibuffer
(find-file-noselect index-file
))
1035 (with-current-buffer ibuffer
1037 (insert "\n\n#+INCLUDE: \"theindex.inc\"\n\n")
1039 (kill-buffer ibuffer
)))))
1041 ;; Caching functions:
1043 (defun org-publish-write-cache-file (&optional free-cache
)
1044 "Write `org-publish-cache' to file.
1045 If FREE-CACHE, empty the cache."
1046 (or org-publish-cache
1047 (error "`org-publish-write-cache-file' called, but no cache present"))
1049 (let ((cache-file (org-publish-cache-get ":cache-file:")))
1051 (error "Cannot find cache-file name in `org-publish-write-cache-file'"))
1052 (with-temp-file cache-file
1053 (let ((print-level nil
)
1055 (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
1056 (maphash (lambda (k v
)
1058 (format (concat "(puthash %S "
1059 (if (or (listp v
) (symbolp v
))
1061 "%S org-publish-cache)\n") k v
)))
1062 org-publish-cache
)))
1063 (when free-cache
(org-publish-reset-cache))))
1065 (defun org-publish-initialize-cache (project-name)
1066 "Initialize the projects cache if not initialized yet and return it."
1069 (error "Cannot initialize `org-publish-cache' without projects name in `org-publish-initialize-cache'"))
1071 (unless (file-exists-p org-publish-timestamp-directory
)
1072 (make-directory org-publish-timestamp-directory t
))
1073 (if (not (file-directory-p org-publish-timestamp-directory
))
1074 (error "Org publish timestamp: %s is not a directory"
1075 org-publish-timestamp-directory
))
1077 (unless (and org-publish-cache
1078 (string= (org-publish-cache-get ":project:") project-name
))
1079 (let* ((cache-file (concat
1080 (expand-file-name org-publish-timestamp-directory
)
1083 (cexists (file-exists-p cache-file
)))
1085 (when org-publish-cache
1086 (org-publish-reset-cache))
1089 (load-file cache-file
)
1090 (setq org-publish-cache
1091 (make-hash-table :test
'equal
:weakness nil
:size
100))
1092 (org-publish-cache-set ":project:" project-name
)
1093 (org-publish-cache-set ":cache-file:" cache-file
))
1094 (unless cexists
(org-publish-write-cache-file nil
))))
1097 (defun org-publish-reset-cache ()
1098 "Empty org-publish-cache and reset it nil."
1099 (message "%s" "Resetting org-publish-cache")
1100 (if (hash-table-p org-publish-cache
)
1101 (clrhash org-publish-cache
))
1102 (setq org-publish-cache nil
))
1104 (defun org-publish-cache-file-needs-publishing (filename &optional pub-dir pub-func base-dir
)
1105 "Check the timestamp of the last publishing of FILENAME.
1106 Return `t', if the file needs publishing. The function also
1107 checks if any included files have been more recently published,
1108 so that the file including them will be republished as well."
1109 (or org-publish-cache
1110 (error "`org-publish-cache-file-needs-publishing' called, but no cache present"))
1111 (let* ((key (org-publish-timestamp-filename filename pub-dir pub-func
))
1112 (pstamp (org-publish-cache-get key
))
1113 (visiting (find-buffer-visiting filename
))
1114 (case-fold-search t
)
1115 included-files-ctime buf
)
1117 (when (equal (file-name-extension filename
) "org")
1118 (setq buf
(find-file (expand-file-name filename
)))
1119 (with-current-buffer buf
1120 (goto-char (point-min))
1121 (while (re-search-forward "^#\\+include:[ \t]+\"\\([^\t\n\r\"]*\\)\"[ \t]*.*$" nil t
)
1122 (let* ((included-file (expand-file-name (match-string 1))))
1123 (add-to-list 'included-files-ctime
1124 (org-publish-cache-ctime-of-src included-file
) t
))))
1125 ;; FIXME don't kill current buffer
1126 (unless visiting
(kill-buffer buf
)))
1129 (let ((ctime (org-publish-cache-ctime-of-src filename
)))
1130 (or (< pstamp ctime
)
1131 (when included-files-ctime
1132 (not (null (delq nil
(mapcar (lambda(ct) (< ctime ct
))
1133 included-files-ctime
))))))))))
1135 (defun org-publish-cache-set-file-property (filename property value
&optional project-name
)
1136 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1137 Use cache file of PROJECT-NAME. If the entry does not exist, it will be
1138 created. Return VALUE."
1139 ;; Evtl. load the requested cache file:
1140 (if project-name
(org-publish-initialize-cache project-name
))
1141 (let ((pl (org-publish-cache-get filename
)))
1144 (plist-put pl property value
)
1146 (org-publish-cache-get-file-property
1147 filename property value nil project-name
))))
1149 (defun org-publish-cache-get-file-property
1150 (filename property
&optional default no-create project-name
)
1151 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1152 Use cache file of PROJECT-NAME. Return the value of that PROPERTY or
1153 DEFAULT, if the value does not yet exist.
1154 If the entry will be created, unless NO-CREATE is not nil."
1155 ;; Evtl. load the requested cache file:
1156 (if project-name
(org-publish-initialize-cache project-name
))
1157 (let ((pl (org-publish-cache-get filename
))
1160 (if (plist-member pl property
)
1161 (setq retval
(plist-get pl property
))
1162 (setq retval default
))
1165 (org-publish-cache-set filename
(list property default
)))
1166 (setq retval default
))
1169 (defun org-publish-cache-get (key)
1170 "Return the value stored in `org-publish-cache' for key KEY.
1171 Returns nil, if no value or nil is found, or the cache does not
1173 (or org-publish-cache
1174 (error "`org-publish-cache-get' called, but no cache present"))
1175 (gethash key org-publish-cache
))
1177 (defun org-publish-cache-set (key value
)
1178 "Store KEY VALUE pair in `org-publish-cache'.
1179 Returns value on success, else nil."
1180 (or org-publish-cache
1181 (error "`org-publish-cache-set' called, but no cache present"))
1182 (puthash key value org-publish-cache
))
1184 (defun org-publish-cache-ctime-of-src (file)
1185 "Get the ctime of filename F as an integer."
1186 (let ((attr (file-attributes
1187 (expand-file-name (or (file-symlink-p file
) file
)
1188 (file-name-directory file
)))))
1189 (+ (lsh (car (nth 5 attr
)) 16)
1190 (cadr (nth 5 attr
)))))
1192 (provide 'org-publish
)
1195 ;; generated-autoload-file: "org-loaddefs.el"
1198 ;;; org-publish.el ends here