Merge branch 'master' of orgmode.org:org-mode
[org-mode.git] / contrib / lisp / org-e-publish.el
blob8eb8a7e446759ff4b9d0272e77d85d62b35b5f6c
1 ;;; org-e-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.
9 ;;
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/>.
23 ;;; Commentary:
25 ;; This program allow configurable publishing of related sets of
26 ;; Org mode files as a complete website.
28 ;; org-e-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.
39 ;;; Code:
41 (eval-when-compile (require 'cl))
42 (require 'format-spec)
43 (require 'org-export)
45 (declare-function org-e-latex-compile "org-e-latex" (texfile))
49 ;;; Variables
50 (defvar org-e-publish-initial-buffer nil
51 "The buffer `org-e-publish' has been called from.")
53 (defvar org-e-publish-temp-files nil
54 "Temporary list of files to be published.")
56 ;; Here, so you find the variable right before it's used the first time:
57 (defvar org-e-publish-cache nil
58 "This will cache timestamps and titles for files in publishing projects.
59 Blocks could hash sha1 values here.")
61 (defgroup org-e-publish nil
62 "Options for publishing a set of Org-mode and related files."
63 :tag "Org Publishing"
64 :group 'org)
66 (defcustom org-e-publish-project-alist nil
67 "Association list to control publishing behavior.
68 Each element of the alist is a publishing 'project.' The CAR of
69 each element is a string, uniquely identifying the project. The
70 CDR of each element is in one of the following forms:
72 1. A well-formed property list with an even number of elements,
73 alternating keys and values, specifying parameters for the
74 publishing process.
76 \(:property value :property value ... )
78 2. A meta-project definition, specifying of a list of
79 sub-projects:
81 \(:components \(\"project-1\" \"project-2\" ...))
83 When the CDR of an element of org-e-publish-project-alist is in
84 this second form, the elements of the list after `:components'
85 are taken to be components of the project, which group together
86 files requiring different publishing options. When you publish
87 such a project with \\[org-e-publish], the components all
88 publish.
90 When a property is given a value in
91 `org-e-publish-project-alist', its setting overrides the value of
92 the corresponding user variable \(if any) during publishing.
93 However, options set within a file override everything.
95 Most properties are optional, but some should always be set:
97 `:base-directory'
99 Directory containing publishing source files.
101 `:base-extension'
103 Extension \(without the dot!) of source files. This can be
104 a regular expression. If not given, \"org\" will be used as
105 default extension.
107 `:publishing-directory'
109 Directory \(possibly remote) where output files will be
110 published.
112 The `:exclude' property may be used to prevent certain files from
113 being published. Its value may be a string or regexp matching
114 file names you don't want to be published.
116 The `:include' property may be used to include extra files. Its
117 value may be a list of filenames to include. The filenames are
118 considered relative to the base directory.
120 When both `:include' and `:exclude' properties are given values,
121 the exclusion step happens first.
123 One special property controls which back-end function to use for
124 publishing files in the project. This can be used to extend the
125 set of file types publishable by `org-e-publish', as well as the
126 set of output formats.
128 `:publishing-function'
130 Function to publish file. The default is
131 `org-e-publish-org-to-ascii', but other values are possible.
132 May also be a list of functions, in which case each function
133 in the list is invoked in turn.
135 Another property allows you to insert code that prepares
136 a project for publishing. For example, you could call GNU Make
137 on a certain makefile, to ensure published files are built up to
138 date.
140 `:preparation-function'
142 Function to be called before publishing this project. This
143 may also be a list of functions.
145 `:completion-function'
147 Function to be called after publishing this project. This
148 may also be a list of functions.
150 Some properties control details of the Org publishing process,
151 and are equivalent to the corresponding user variables listed in
152 the right column. Back-end specific properties may also be
153 included. See the back-end documentation for more information.
155 :author `user-full-name'
156 :creator `org-export-creator-string'
157 :email `user-mail-address'
158 :exclude-tags `org-export-exclude-tags'
159 :headline-levels `org-export-headline-levels'
160 :language `org-export-default-language'
161 :preserve-breaks `org-export-preserve-breaks'
162 :section-numbers `org-export-with-section-numbers'
163 :select-tags `org-export-select-tags'
164 :time-stamp-file `org-export-time-stamp-file'
165 :with-archived-trees `org-export-with-archived-trees'
166 :with-author `org-export-with-author'
167 :with-creator `org-export-with-creator'
168 :with-drawers `org-export-with-drawers'
169 :with-email `org-export-with-email'
170 :with-emphasize `org-export-with-emphasize'
171 :with-entities `org-export-with-entities'
172 :with-fixed-width `org-export-with-fixed-width'
173 :with-footnotes `org-export-with-footnotes'
174 :with-priority `org-export-with-priority'
175 :with-special-strings `org-export-with-special-strings'
176 :with-sub-superscript `org-export-with-sub-superscripts'
177 :with-toc `org-export-with-toc'
178 :with-tables `org-export-with-tables'
179 :with-tags `org-export-with-tags'
180 :with-tasks `org-export-with-tasks'
181 :with-timestamps `org-export-with-timestamps'
182 :with-todo-keywords `org-export-with-todo-keywords'
184 The following properties may be used to control publishing of
185 a site-map of files or summary page for a given project.
187 `:auto-sitemap'
189 Whether to publish a site-map during
190 `org-e-publish-current-project' or `org-e-publish-all'.
192 `:sitemap-filename'
194 Filename for output of sitemap. Defaults to \"sitemap.org\".
196 `:sitemap-title'
198 Title of site-map page. Defaults to name of file.
200 `:sitemap-function'
202 Plugin function to use for generation of site-map. Defaults to
203 `org-e-publish-org-sitemap', which generates a plain list of
204 links to all files in the project.
206 `:sitemap-style'
208 Can be `list' \(site-map is just an itemized list of the
209 titles of the files involved) or `tree' \(the directory
210 structure of the source files is reflected in the site-map).
211 Defaults to `tree'.
213 `:sitemap-sans-extension'
215 Remove extension from site-map's file-names. Useful to have
216 cool URIs \(see http://www.w3.org/Provider/Style/URI).
217 Defaults to nil.
219 If you create a site-map file, adjust the sorting like this:
221 `:sitemap-sort-folders'
223 Where folders should appear in the site-map. Set this to
224 `first' \(default) or `last' to display folders first or
225 last, respectively. Any other value will mix files and
226 folders.
228 `:sitemap-sort-files'
230 The site map is normally sorted alphabetically. You can
231 change this behaviour setting this to `anti-chronologically',
232 `chronologically', or nil.
234 `:sitemap-ignore-case'
236 Should sorting be case-sensitive? Default nil.
238 The following properties control the creation of a concept index.
240 `:makeindex'
242 Create a concept index.
244 Other properties affecting publication.
246 `:body-only'
248 Set this to t to publish only the body of the documents."
249 :group 'org-e-publish
250 :type 'alist)
252 (defcustom org-e-publish-use-timestamps-flag t
253 "Non-nil means use timestamp checking to publish only changed files.
254 When nil, do no timestamp checking and always publish all files."
255 :group 'org-e-publish
256 :type 'boolean)
258 (defcustom org-e-publish-timestamp-directory
259 (convert-standard-filename "~/.org-timestamps/")
260 "Name of directory in which to store publishing timestamps."
261 :group 'org-e-publish
262 :type 'directory)
264 (defcustom org-e-publish-list-skipped-files t
265 "Non-nil means show message about files *not* published."
266 :group 'org-e-publish
267 :type 'boolean)
269 (defcustom org-e-publish-sitemap-sort-files 'alphabetically
270 "Method to sort files in site-maps.
271 Possible values are `alphabetically', `chronologically',
272 `anti-chronologically' and nil.
274 If `alphabetically', files will be sorted alphabetically. If
275 `chronologically', files will be sorted with older modification
276 time first. If `anti-chronologically', files will be sorted with
277 newer modification time first. nil won't sort files.
279 You can overwrite this default per project in your
280 `org-e-publish-project-alist', using `:sitemap-sort-files'."
281 :group 'org-e-publish
282 :type 'symbol)
284 (defcustom org-e-publish-sitemap-sort-folders 'first
285 "A symbol, denoting if folders are sorted first in sitemaps.
286 Possible values are `first', `last', and nil.
287 If `first', folders will be sorted before files.
288 If `last', folders are sorted to the end after the files.
289 Any other value will not mix files and folders.
291 You can overwrite this default per project in your
292 `org-e-publish-project-alist', using `:sitemap-sort-folders'."
293 :group 'org-e-publish
294 :type 'symbol)
296 (defcustom org-e-publish-sitemap-sort-ignore-case nil
297 "Non-nil when site-map sorting should ignore case.
299 You can overwrite this default per project in your
300 `org-e-publish-project-alist', using `:sitemap-ignore-case'."
301 :group 'org-e-publish
302 :type 'boolean)
304 (defcustom org-e-publish-sitemap-date-format "%Y-%m-%d"
305 "Format for `format-time-string' which is used to print a date
306 in the sitemap."
307 :group 'org-e-publish
308 :type 'string)
310 (defcustom org-e-publish-sitemap-file-entry-format "%t"
311 "Format string for site-map file entry.
312 You could use brackets to delimit on what part the link will be.
314 %t is the title.
315 %a is the author.
316 %d is the date formatted using `org-e-publish-sitemap-date-format'."
317 :group 'org-e-publish
318 :type 'string)
322 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
323 ;;; Timestamp-related functions
325 (defun org-e-publish-timestamp-filename (filename &optional pub-dir pub-func)
326 "Return path to timestamp file for filename FILENAME."
327 (setq filename (concat filename "::" (or pub-dir "") "::"
328 (format "%s" (or pub-func ""))))
329 (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
331 (defun org-e-publish-needed-p
332 (filename &optional pub-dir pub-func true-pub-dir base-dir)
333 "Non-nil if FILENAME should be published in PUB-DIR using PUB-FUNC.
334 TRUE-PUB-DIR is where the file will truly end up. Currently we
335 are not using this - maybe it can eventually be used to check if
336 the file is present at the target location, and how old it is.
337 Right now we cannot do this, because we do not know under what
338 file name the file will be stored - the publishing function can
339 still decide about that independently."
340 (let ((rtn (if (not org-e-publish-use-timestamps-flag) t
341 (org-e-publish-cache-file-needs-publishing
342 filename pub-dir pub-func base-dir))))
343 (if rtn (message "Publishing file %s using `%s'" filename pub-func)
344 (when org-e-publish-list-skipped-files
345 (message "Skipping unmodified file %s" filename)))
346 rtn))
348 (defun org-e-publish-update-timestamp
349 (filename &optional pub-dir pub-func base-dir)
350 "Update publishing timestamp for file FILENAME.
351 If there is no timestamp, create one."
352 (let ((key (org-e-publish-timestamp-filename filename pub-dir pub-func))
353 (stamp (org-e-publish-cache-ctime-of-src filename base-dir)))
354 (org-e-publish-cache-set key stamp)))
356 (defun org-e-publish-remove-all-timestamps ()
357 "Remove all files in the timestamp directory."
358 (let ((dir org-e-publish-timestamp-directory)
359 files)
360 (when (and (file-exists-p dir) (file-directory-p dir))
361 (mapc 'delete-file (directory-files dir 'full "[^.]\\'"))
362 (org-e-publish-reset-cache))))
366 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
367 ;;; Getting project information out of `org-e-publish-project-alist'
369 (defun org-e-publish-expand-projects (projects-alist)
370 "Expand projects in PROJECTS-ALIST.
371 This splices all the components into the list."
372 (let ((rest projects-alist) rtn p components)
373 (while (setq p (pop rest))
374 (if (setq components (plist-get (cdr p) :components))
375 (setq rest (append
376 (mapcar (lambda (x) (assoc x org-e-publish-project-alist))
377 components)
378 rest))
379 (push p rtn)))
380 (nreverse (delete-dups (delq nil rtn)))))
382 (defvar org-sitemap-sort-files)
383 (defvar org-sitemap-sort-folders)
384 (defvar org-sitemap-ignore-case)
385 (defvar org-sitemap-requested)
386 (defvar org-sitemap-date-format)
387 (defvar org-sitemap-file-entry-format)
388 (defun org-e-publish-compare-directory-files (a b)
389 "Predicate for `sort', that sorts folders and files for sitemap."
390 (let ((retval t))
391 (when (or org-sitemap-sort-files org-sitemap-sort-folders)
392 ;; First we sort files:
393 (when org-sitemap-sort-files
394 (case org-sitemap-sort-files
395 (alphabetically
396 (let* ((adir (file-directory-p a))
397 (aorg (and (string-match "\\.org$" a) (not adir)))
398 (bdir (file-directory-p b))
399 (borg (and (string-match "\\.org$" b) (not bdir)))
400 (A (if aorg (concat (file-name-directory a)
401 (org-e-publish-find-title a)) a))
402 (B (if borg (concat (file-name-directory b)
403 (org-e-publish-find-title b)) b)))
404 (setq retval (if org-sitemap-ignore-case
405 (not (string-lessp (upcase B) (upcase A)))
406 (not (string-lessp B A))))))
407 ((anti-chronologically chronologically)
408 (let* ((adate (org-e-publish-find-date a))
409 (bdate (org-e-publish-find-date b))
410 (A (+ (lsh (car adate) 16) (cadr adate)))
411 (B (+ (lsh (car bdate) 16) (cadr bdate))))
412 (setq retval
413 (if (eq org-sitemap-sort-files 'chronologically) (<= A B)
414 (>= A B)))))))
415 ;; Directory-wise wins:
416 (when org-sitemap-sort-folders
417 ;; a is directory, b not:
418 (cond
419 ((and (file-directory-p a) (not (file-directory-p b)))
420 (setq retval (equal org-sitemap-sort-folders 'first)))
421 ;; a is not a directory, but b is:
422 ((and (not (file-directory-p a)) (file-directory-p b))
423 (setq retval (equal org-sitemap-sort-folders 'last))))))
424 retval))
426 (defun org-e-publish-get-base-files-1
427 (base-dir &optional recurse match skip-file skip-dir)
428 "Set `org-e-publish-temp-files' with files from BASE-DIR directory.
429 If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
430 non-nil, restrict this list to the files matching the regexp
431 MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
432 SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
433 matching the regexp SKIP-DIR when recursing through BASE-DIR."
434 (mapc (lambda (f)
435 (let ((fd-p (file-directory-p f))
436 (fnd (file-name-nondirectory f)))
437 (if (and fd-p recurse
438 (not (string-match "^\\.+$" fnd))
439 (if skip-dir (not (string-match skip-dir fnd)) t))
440 (org-e-publish-get-base-files-1
441 f recurse match skip-file skip-dir)
442 (unless (or fd-p ;; this is a directory
443 (and skip-file (string-match skip-file fnd))
444 (not (file-exists-p (file-truename f)))
445 (not (string-match match fnd)))
447 (pushnew f org-e-publish-temp-files)))))
448 (if org-sitemap-requested
449 (sort (directory-files base-dir t (unless recurse match))
450 'org-e-publish-compare-directory-files)
451 (directory-files base-dir t (unless recurse match)))))
453 (defun org-e-publish-get-base-files (project &optional exclude-regexp)
454 "Return a list of all files in PROJECT.
455 If EXCLUDE-REGEXP is set, this will be used to filter out
456 matching filenames."
457 (let* ((project-plist (cdr project))
458 (base-dir (file-name-as-directory
459 (plist-get project-plist :base-directory)))
460 (include-list (plist-get project-plist :include))
461 (recurse (plist-get project-plist :recursive))
462 (extension (or (plist-get project-plist :base-extension) "org"))
463 ;; sitemap-... variables are dynamically scoped for
464 ;; org-e-publish-compare-directory-files:
465 (org-sitemap-requested
466 (plist-get project-plist :auto-sitemap))
467 (sitemap-filename
468 (or (plist-get project-plist :sitemap-filename) "sitemap.org"))
469 (org-sitemap-sort-folders
470 (if (plist-member project-plist :sitemap-sort-folders)
471 (plist-get project-plist :sitemap-sort-folders)
472 org-e-publish-sitemap-sort-folders))
473 (org-sitemap-sort-files
474 (cond ((plist-member project-plist :sitemap-sort-files)
475 (plist-get project-plist :sitemap-sort-files))
476 ;; For backward compatibility:
477 ((plist-member project-plist :sitemap-alphabetically)
478 (if (plist-get project-plist :sitemap-alphabetically)
479 'alphabetically nil))
480 (t org-e-publish-sitemap-sort-files)))
481 (org-sitemap-ignore-case
482 (if (plist-member project-plist :sitemap-ignore-case)
483 (plist-get project-plist :sitemap-ignore-case)
484 org-e-publish-sitemap-sort-ignore-case))
485 (match (if (eq extension 'any) "^[^\\.]"
486 (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
487 ;; Make sure `org-sitemap-sort-folders' has an accepted value
488 (unless (memq org-sitemap-sort-folders '(first last))
489 (setq org-sitemap-sort-folders nil))
491 (setq org-e-publish-temp-files nil)
492 (if org-sitemap-requested
493 (pushnew (expand-file-name (concat base-dir sitemap-filename))
494 org-e-publish-temp-files))
495 (org-e-publish-get-base-files-1 base-dir recurse match
496 ;; FIXME distinguish exclude regexp
497 ;; for skip-file and skip-dir?
498 exclude-regexp exclude-regexp)
499 (mapc (lambda (f)
500 (pushnew
501 (expand-file-name (concat base-dir f))
502 org-e-publish-temp-files))
503 include-list)
504 org-e-publish-temp-files))
506 (defun org-e-publish-get-project-from-filename (filename &optional up)
507 "Return the project that FILENAME belongs to."
508 (let* ((filename (expand-file-name filename))
509 project-name)
511 (catch 'p-found
512 (dolist (prj org-e-publish-project-alist)
513 (unless (plist-get (cdr prj) :components)
514 ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
515 (let* ((r (plist-get (cdr prj) :recursive))
516 (b (expand-file-name (file-name-as-directory
517 (plist-get (cdr prj) :base-directory))))
518 (x (or (plist-get (cdr prj) :base-extension) "org"))
519 (e (plist-get (cdr prj) :exclude))
520 (i (plist-get (cdr prj) :include))
521 (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
522 (when
523 (or (and i
524 (member filename
525 (mapcar (lambda (file)
526 (expand-file-name file b))
527 i)))
528 (and (not (and e (string-match e filename)))
529 (string-match xm filename)))
530 (setq project-name (car prj))
531 (throw 'p-found project-name))))))
532 (when up
533 (dolist (prj org-e-publish-project-alist)
534 (if (member project-name (plist-get (cdr prj) :components))
535 (setq project-name (car prj)))))
536 (assoc project-name org-e-publish-project-alist)))
540 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
541 ;;; Pluggable publishing back-end functions
543 (defun org-e-publish-org-to (backend filename extension plist pub-dir)
544 "Publish an Org file to a specified back-end.
546 BACKEND is a symbol representing the back-end used for
547 transcoding. FILENAME is the filename of the Org file to be
548 published. EXTENSION is the extension used for the output
549 string, with the leading dot. PLIST is the property list for the
550 given project. PUB-DIR is the publishing directory.
552 Return output file name."
553 (unless (file-exists-p pub-dir) (make-directory pub-dir t))
554 ;; Check if a buffer visiting FILENAME is already open.
555 (let* ((visitingp (find-buffer-visiting filename))
556 (work-buffer (or visitingp (find-file-noselect filename))))
557 (prog1 (with-current-buffer work-buffer
558 (let ((output-file
559 (org-export-output-file-name extension nil pub-dir))
560 (body-p (plist-get plist :body-only)))
561 (org-export-to-file
562 backend output-file nil nil body-p
563 ;; Install `org-e-publish-collect-index' in parse tree
564 ;; filters. It isn't dependent on `:makeindex', since
565 ;; we want to keep it up-to-date in cache anyway.
566 (org-combine-plists
567 plist `(:filter-parse-tree
568 (org-e-publish-collect-index
569 ,@(plist-get plist :filter-parse-tree)))))))
570 ;; Remove opened buffer in the process.
571 (unless visitingp (kill-buffer work-buffer)))))
573 (defvar project-plist)
574 (defun org-e-publish-org-to-latex (plist filename pub-dir)
575 "Publish an Org file to LaTeX.
577 FILENAME is the filename of the Org file to be published. PLIST
578 is the property list for the given project. PUB-DIR is the
579 publishing directory.
581 Return output file name."
582 (org-e-publish-org-to 'e-latex filename ".tex" plist pub-dir))
584 (defun org-e-publish-org-to-pdf (plist filename pub-dir)
585 "Publish an Org file to PDF \(via LaTeX).
587 FILENAME is the filename of the Org file to be published. PLIST
588 is the property list for the given project. PUB-DIR is the
589 publishing directory.
591 Return output file name."
592 (org-e-latex-compile
593 (org-e-publish-org-to 'e-latex filename ".tex" plist pub-dir)))
595 (defun org-e-publish-org-to-html (plist filename pub-dir)
596 "Publish an org file to HTML.
598 FILENAME is the filename of the Org file to be published. PLIST
599 is the property list for the given project. PUB-DIR is the
600 publishing directory.
602 Return output file name."
603 (org-e-publish-org-to 'e-html filename "html" plist pub-dir))
605 ;; TODO: Not implemented yet.
606 ;; (defun org-e-publish-org-to-org (plist filename pub-dir)
607 ;; "Publish an org file to HTML.
609 ;; FILENAME is the filename of the Org file to be published. PLIST
610 ;; is the property list for the given project. PUB-DIR is the
611 ;; publishing directory.
613 ;; Return output file name."
614 ;; (org-e-publish-org-to "org" plist filename pub-dir))
616 (defun org-e-publish-org-to-ascii (plist filename pub-dir)
617 "Publish an Org file to ASCII.
619 FILENAME is the filename of the Org file to be published. PLIST
620 is the property list for the given project. PUB-DIR is the
621 publishing directory.
623 Return output file name."
624 (org-e-publish-org-to
625 'e-ascii filename ".txt" `(:ascii-charset ascii ,@plist) pub-dir))
627 (defun org-e-publish-org-to-latin1 (plist filename pub-dir)
628 "Publish an Org file to Latin-1.
630 FILENAME is the filename of the Org file to be published. PLIST
631 is the property list for the given project. PUB-DIR is the
632 publishing directory.
634 Return output file name."
635 (org-e-publish-org-to
636 'e-ascii filename ".txt" `(:ascii-charset latin1 ,@plist) pub-dir))
638 (defun org-e-publish-org-to-utf8 (plist filename pub-dir)
639 "Publish an org file to UTF-8.
641 FILENAME is the filename of the Org file to be published. PLIST
642 is the property list for the given project. PUB-DIR is the
643 publishing directory.
645 Return output file name."
646 (org-e-publish-org-to
647 'e-ascii filename ".txt" `(:ascii-charset utf-8 ,@plist) pub-dir))
649 (defun org-e-publish-attachment (plist filename pub-dir)
650 "Publish a file with no transformation of any kind.
652 FILENAME is the filename of the Org file to be published. PLIST
653 is the property list for the given project. PUB-DIR is the
654 publishing directory.
656 Return output file name."
657 (unless (file-directory-p pub-dir)
658 (make-directory pub-dir t))
659 (or (equal (expand-file-name (file-name-directory filename))
660 (file-name-as-directory (expand-file-name pub-dir)))
661 (copy-file filename
662 (expand-file-name (file-name-nondirectory filename) pub-dir)
663 t)))
667 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
668 ;;; Publishing files, sets of files, and indices
670 (defun org-e-publish-file (filename &optional project no-cache)
671 "Publish file FILENAME from PROJECT.
672 If NO-CACHE is not nil, do not initialize org-e-publish-cache and
673 write it to disk. This is needed, since this function is used to
674 publish single files, when entire projects are published.
675 See `org-e-publish-projects'."
676 (let* ((project
677 (or project
678 (or (org-e-publish-get-project-from-filename filename)
679 (error "File %s not part of any known project"
680 (abbreviate-file-name filename)))))
681 (project-plist (cdr project))
682 (ftname (expand-file-name filename))
683 (publishing-function
684 (or (plist-get project-plist :publishing-function)
685 'org-e-publish-org-to-ascii))
686 (base-dir
687 (file-name-as-directory
688 (expand-file-name
689 (or (plist-get project-plist :base-directory)
690 (error "Project %s does not have :base-directory defined"
691 (car project))))))
692 (pub-dir
693 (file-name-as-directory
694 (file-truename
695 (or (eval (plist-get project-plist :publishing-directory))
696 (error "Project %s does not have :publishing-directory defined"
697 (car project))))))
698 tmp-pub-dir)
700 (unless no-cache (org-e-publish-initialize-cache (car project)))
702 (setq tmp-pub-dir
703 (file-name-directory
704 (concat pub-dir
705 (and (string-match (regexp-quote base-dir) ftname)
706 (substring ftname (match-end 0))))))
707 (if (listp publishing-function)
708 ;; allow chain of publishing functions
709 (mapc (lambda (f)
710 (when (org-e-publish-needed-p
711 filename pub-dir f tmp-pub-dir base-dir)
712 (funcall f project-plist filename tmp-pub-dir)
713 (org-e-publish-update-timestamp filename pub-dir f base-dir)))
714 publishing-function)
715 (when (org-e-publish-needed-p
716 filename pub-dir publishing-function tmp-pub-dir base-dir)
717 (funcall publishing-function project-plist filename tmp-pub-dir)
718 (org-e-publish-update-timestamp
719 filename pub-dir publishing-function base-dir)))
720 (unless no-cache (org-e-publish-write-cache-file))))
722 (defun org-e-publish-projects (projects)
723 "Publish all files belonging to the PROJECTS alist.
724 If `:auto-sitemap' is set, publish the sitemap too. If
725 `:makeindex' is set, also produce a file theindex.org."
726 (mapc
727 (lambda (project)
728 ;; Each project uses its own cache file:
729 (org-e-publish-initialize-cache (car project))
730 (let* ((project-plist (cdr project))
731 (exclude-regexp (plist-get project-plist :exclude))
732 (sitemap-p (plist-get project-plist :auto-sitemap))
733 (sitemap-filename (or (plist-get project-plist :sitemap-filename)
734 "sitemap.org"))
735 (sitemap-function (or (plist-get project-plist :sitemap-function)
736 'org-e-publish-org-sitemap))
737 (org-sitemap-date-format
738 (or (plist-get project-plist :sitemap-date-format)
739 org-e-publish-sitemap-date-format))
740 (org-sitemap-file-entry-format
741 (or (plist-get project-plist :sitemap-file-entry-format)
742 org-e-publish-sitemap-file-entry-format))
743 (preparation-function
744 (plist-get project-plist :preparation-function))
745 (completion-function (plist-get project-plist :completion-function))
746 (files (org-e-publish-get-base-files project exclude-regexp)) file)
747 (when preparation-function (run-hooks 'preparation-function))
748 (if sitemap-p (funcall sitemap-function project sitemap-filename))
749 (dolist (file files) (org-e-publish-file file project t))
750 (when (plist-get project-plist :makeindex)
751 (org-e-publish-index-generate-theindex
752 project (plist-get project-plist :base-directory))
753 (org-e-publish-file
754 (expand-file-name
755 "theindex.org" (plist-get project-plist :base-directory))
756 project t))
757 (when completion-function (run-hooks 'completion-function))
758 (org-e-publish-write-cache-file)))
759 (org-e-publish-expand-projects projects)))
761 (defun org-e-publish-org-sitemap (project &optional sitemap-filename)
762 "Create a sitemap of pages in set defined by PROJECT.
763 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
764 Default for SITEMAP-FILENAME is 'sitemap.org'."
765 (let* ((project-plist (cdr project))
766 (dir (file-name-as-directory
767 (plist-get project-plist :base-directory)))
768 (localdir (file-name-directory dir))
769 (indent-str (make-string 2 ?\ ))
770 (exclude-regexp (plist-get project-plist :exclude))
771 (files (nreverse
772 (org-e-publish-get-base-files project exclude-regexp)))
773 (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
774 (sitemap-title (or (plist-get project-plist :sitemap-title)
775 (concat "Sitemap for project " (car project))))
776 (sitemap-style (or (plist-get project-plist :sitemap-style)
777 'tree))
778 (sitemap-sans-extension
779 (plist-get project-plist :sitemap-sans-extension))
780 (visiting (find-buffer-visiting sitemap-filename))
781 (ifn (file-name-nondirectory sitemap-filename))
782 file sitemap-buffer)
783 (with-current-buffer (setq sitemap-buffer
784 (or visiting (find-file sitemap-filename)))
785 (erase-buffer)
786 (insert (concat "#+TITLE: " sitemap-title "\n\n"))
787 (while (setq file (pop files))
788 (let ((fn (file-name-nondirectory file))
789 (link (file-relative-name file dir))
790 (oldlocal localdir))
791 (when sitemap-sans-extension
792 (setq link (file-name-sans-extension link)))
793 ;; sitemap shouldn't list itself
794 (unless (equal (file-truename sitemap-filename)
795 (file-truename file))
796 (if (eq sitemap-style 'list)
797 (message "Generating list-style sitemap for %s" sitemap-title)
798 (message "Generating tree-style sitemap for %s" sitemap-title)
799 (setq localdir (concat (file-name-as-directory dir)
800 (file-name-directory link)))
801 (unless (string= localdir oldlocal)
802 (if (string= localdir dir)
803 (setq indent-str (make-string 2 ?\ ))
804 (let ((subdirs
805 (split-string
806 (directory-file-name
807 (file-name-directory
808 (file-relative-name localdir dir))) "/"))
809 (subdir "")
810 (old-subdirs (split-string
811 (file-relative-name oldlocal dir) "/")))
812 (setq indent-str (make-string 2 ?\ ))
813 (while (string= (car old-subdirs) (car subdirs))
814 (setq indent-str (concat indent-str (make-string 2 ?\ )))
815 (pop old-subdirs)
816 (pop subdirs))
817 (dolist (d subdirs)
818 (setq subdir (concat subdir d "/"))
819 (insert (concat indent-str " + " d "\n"))
820 (setq indent-str (make-string
821 (+ (length indent-str) 2) ?\ )))))))
822 ;; This is common to 'flat and 'tree
823 (let ((entry
824 (org-e-publish-format-file-entry
825 org-sitemap-file-entry-format file project-plist))
826 (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
827 (cond ((string-match-p regexp entry)
828 (string-match regexp entry)
829 (insert (concat indent-str " + " (match-string 1 entry)
830 "[[file:" link "]["
831 (match-string 2 entry)
832 "]]" (match-string 3 entry) "\n")))
834 (insert (concat indent-str " + [[file:" link "]["
835 entry
836 "]]\n"))))))))
837 (save-buffer))
838 (or visiting (kill-buffer sitemap-buffer))))
840 (defun org-e-publish-format-file-entry (fmt file project-plist)
841 (format-spec fmt
842 `((?t . ,(org-e-publish-find-title file t))
843 (?d . ,(format-time-string org-sitemap-date-format
844 (org-e-publish-find-date file)))
845 (?a . ,(or (plist-get project-plist :author) user-full-name)))))
847 (defun org-e-publish-find-title (file &optional reset)
848 "Find the title of FILE in project."
850 (and (not reset) (org-e-publish-cache-get-file-property file :title nil t))
851 (let* ((visiting (find-buffer-visiting file))
852 (buffer (or visiting (find-file-noselect file)))
853 title)
854 (with-current-buffer buffer
855 (org-mode)
856 (setq title
857 (or (plist-get (org-export-get-environment) :title)
858 (file-name-nondirectory (file-name-sans-extension file)))))
859 (unless visiting (kill-buffer buffer))
860 (org-e-publish-cache-set-file-property file :title title)
861 title)))
863 (defun org-e-publish-find-date (file)
864 "Find the date of FILE in project.
865 If FILE provides a #+date keyword use it else use the file
866 system's modification time.
868 It returns time in `current-time' format."
869 (let* ((visiting (find-buffer-visiting file))
870 (file-buf (or visiting (find-file-noselect file nil)))
871 (date (plist-get
872 (with-current-buffer file-buf
873 (org-mode)
874 (org-export--get-inbuffer-options))
875 :date)))
876 (unless visiting (kill-buffer file-buf))
877 (if date (org-time-string-to-time date)
878 (when (file-exists-p file)
879 (nth 5 (file-attributes file))))))
883 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
884 ;;; Interactive publishing functions
886 ;;;###autoload
887 (defalias 'org-e-publish-project 'org-e-publish)
889 ;;;###autoload
890 (defun org-e-publish (project &optional force)
891 "Publish PROJECT."
892 (interactive
893 (list
894 (assoc (org-icompleting-read
895 "Publish project: "
896 org-e-publish-project-alist nil t)
897 org-e-publish-project-alist)
898 current-prefix-arg))
899 (setq org-e-publish-initial-buffer (current-buffer))
900 (save-window-excursion
901 (let* ((org-e-publish-use-timestamps-flag
902 (if force nil org-e-publish-use-timestamps-flag)))
903 (org-e-publish-projects
904 (if (stringp project)
905 ;; If this function is called in batch mode, project is
906 ;; still a string here.
907 (list (assoc project org-e-publish-project-alist))
908 (list project))))))
910 ;;;###autoload
911 (defun org-e-publish-all (&optional force)
912 "Publish all projects.
913 With prefix argument, remove all files in the timestamp
914 directory and force publishing all files."
915 (interactive "P")
916 (when force (org-e-publish-remove-all-timestamps))
917 (save-window-excursion
918 (let ((org-e-publish-use-timestamps-flag
919 (if force nil org-e-publish-use-timestamps-flag)))
920 (org-e-publish-projects org-e-publish-project-alist))))
923 ;;;###autoload
924 (defun org-e-publish-current-file (&optional force)
925 "Publish the current file.
926 With prefix argument, force publish the file."
927 (interactive "P")
928 (save-window-excursion
929 (let ((org-e-publish-use-timestamps-flag
930 (if force nil org-e-publish-use-timestamps-flag)))
931 (org-e-publish-file (buffer-file-name (buffer-base-buffer))))))
933 ;;;###autoload
934 (defun org-e-publish-current-project (&optional force)
935 "Publish the project associated with the current file.
936 With a prefix argument, force publishing of all files in
937 the project."
938 (interactive "P")
939 (save-window-excursion
940 (let ((project (org-e-publish-get-project-from-filename
941 (buffer-file-name (buffer-base-buffer)) 'up))
942 (org-e-publish-use-timestamps-flag
943 (if force nil org-e-publish-use-timestamps-flag)))
944 (if project (org-e-publish project)
945 (error "File %s is not part of any known project"
946 (buffer-file-name (buffer-base-buffer)))))))
950 ;;; Index generation
952 (defun org-e-publish-collect-index (tree backend info)
953 "Update index for a file with TREE in cache.
955 BACKEND is the back-end being used for transcoding. INFO is
956 a plist containing publishing options.
958 The index relative to current file is stored as an alist. An
959 association has the following shape: \(TERM FILE-NAME PARENT),
960 where TERM is the indexed term, as a string, FILE-NAME is the
961 original full path of the file where the term in encountered, and
962 PARENT is the headline element containing the original index
963 keyword."
964 (org-e-publish-cache-set-file-property
965 (plist-get info :input-file) :index
966 (delete-dups
967 (org-element-map
968 tree 'keyword
969 (lambda (k)
970 (when (string= (downcase (org-element-property :key k))
971 "index")
972 (let ((index (org-element-property :value k))
973 (parent (org-export-get-parent-headline k)))
974 (list index (plist-get info :input-file) parent))))
975 info)))
976 ;; Return parse-tree to avoid altering output.
977 tree)
979 (defun org-e-publish-index-generate-theindex (project directory)
980 "Retrieve full index from cache and build \"theindex.org\".
981 PROJECT is the project the index relates to. DIRECTORY is the
982 publishing directory."
983 (let ((all-files (org-e-publish-get-base-files
984 project (plist-get (cdr project) :exclude)))
985 full-index)
986 ;; Compile full index.
987 (mapc
988 (lambda (file)
989 (let ((index (org-e-publish-cache-get-file-property file :index)))
990 (dolist (term index)
991 (unless (member term full-index) (push term full-index)))))
992 all-files)
993 ;; Sort it alphabetically.
994 (setq full-index
995 (sort full-index (lambda (a b) (string< (downcase (car a))
996 (downcase (car b))))))
997 ;; Fill "theindex.org".
998 (with-temp-buffer
999 (insert "#+TITLE: Index\n#+OPTIONS: num:nil author:nil\n")
1000 (let ((current-letter nil) (last-entry nil))
1001 (dolist (idx full-index)
1002 (let* ((entry (org-split-string (car idx) "!"))
1003 (letter (upcase (substring (car entry) 0 1)))
1004 ;; Transform file into a path relative to publishing
1005 ;; directory.
1006 (file (file-relative-name
1007 (nth 1 idx)
1008 (plist-get (cdr project) :base-directory))))
1009 ;; Check if another letter has to be inserted.
1010 (unless (string= letter current-letter)
1011 (insert (format "* %s\n" letter)))
1012 ;; Compute the first difference between last entry and
1013 ;; current one: it tells the level at which new items
1014 ;; should be added.
1015 (let* ((rank (loop for n from 0 to (length entry)
1016 unless (equal (nth n entry) (nth n last-entry))
1017 return n))
1018 (len (length (nthcdr rank entry))))
1019 ;; For each term after the first difference, create
1020 ;; a new sub-list with the term as body. Moreover,
1021 ;; linkify the last term.
1022 (dotimes (n len)
1023 (insert
1024 (concat
1025 (make-string (* (+ rank n) 2) ? ) " - "
1026 (if (not (= (1- len) n)) (nth (+ rank n) entry)
1027 ;; Last term: Link it to TARGET, if possible.
1028 (let ((target (nth 2 idx)))
1029 (format
1030 "[[%s][%s]]"
1031 ;; Destination.
1032 (cond
1033 ((not target) (format "file:%s" file))
1034 ((let ((id (org-element-property :id target)))
1035 (and id (format "id:%s" id))))
1036 ((let ((id (org-element-property :custom-id target)))
1037 (and id (format "file:%s::#%s" file id))))
1038 (t (format "file:%s::*%s" file
1039 (org-element-property :raw-value target))))
1040 ;; Description.
1041 (car (last entry)))))
1042 "\n"))))
1043 (setq current-letter letter last-entry entry))))
1044 ;; Write index.
1045 (write-file (expand-file-name "theindex.org" directory)))))
1049 ;;; Caching functions
1051 (defun org-e-publish-write-cache-file (&optional free-cache)
1052 "Write `org-e-publish-cache' to file.
1053 If FREE-CACHE, empty the cache."
1054 (unless org-e-publish-cache
1055 (error "`org-e-publish-write-cache-file' called, but no cache present"))
1057 (let ((cache-file (org-e-publish-cache-get ":cache-file:")))
1058 (unless cache-file
1059 (error "Cannot find cache-file name in `org-e-publish-write-cache-file'"))
1060 (with-temp-file cache-file
1061 (let (print-level print-length)
1062 (insert "(setq org-e-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
1063 (maphash (lambda (k v)
1064 (insert
1065 (format (concat "(puthash %S "
1066 (if (or (listp v) (symbolp v))
1067 "'" "")
1068 "%S org-e-publish-cache)\n") k v)))
1069 org-e-publish-cache)))
1070 (when free-cache (org-e-publish-reset-cache))))
1072 (defun org-e-publish-initialize-cache (project-name)
1073 "Initialize the projects cache if not initialized yet and return it."
1075 (unless project-name
1076 (error "Cannot initialize `org-e-publish-cache' without projects name in `org-e-publish-initialize-cache'"))
1078 (unless (file-exists-p org-e-publish-timestamp-directory)
1079 (make-directory org-e-publish-timestamp-directory t))
1080 (unless (file-directory-p org-e-publish-timestamp-directory)
1081 (error "Org publish timestamp: %s is not a directory"
1082 org-e-publish-timestamp-directory))
1084 (unless (and org-e-publish-cache
1085 (string= (org-e-publish-cache-get ":project:") project-name))
1086 (let* ((cache-file
1087 (concat
1088 (expand-file-name org-e-publish-timestamp-directory)
1089 project-name ".cache"))
1090 (cexists (file-exists-p cache-file)))
1092 (when org-e-publish-cache (org-e-publish-reset-cache))
1094 (if cexists (load-file cache-file)
1095 (setq org-e-publish-cache
1096 (make-hash-table :test 'equal :weakness nil :size 100))
1097 (org-e-publish-cache-set ":project:" project-name)
1098 (org-e-publish-cache-set ":cache-file:" cache-file))
1099 (unless cexists (org-e-publish-write-cache-file nil))))
1100 org-e-publish-cache)
1102 (defun org-e-publish-reset-cache ()
1103 "Empty org-e-publish-cache and reset it nil."
1104 (message "%s" "Resetting org-e-publish-cache")
1105 (when (hash-table-p org-e-publish-cache)
1106 (clrhash org-e-publish-cache))
1107 (setq org-e-publish-cache nil))
1109 (defun org-e-publish-cache-file-needs-publishing
1110 (filename &optional pub-dir pub-func base-dir)
1111 "Check the timestamp of the last publishing of FILENAME.
1112 Non-nil if the file needs publishing. The function also checks
1113 if any included files have been more recently published, so that
1114 the file including them will be republished as well."
1115 (unless org-e-publish-cache
1116 (error
1117 "`org-e-publish-cache-file-needs-publishing' called, but no cache present"))
1118 (let* ((case-fold-search t)
1119 (key (org-e-publish-timestamp-filename filename pub-dir pub-func))
1120 (pstamp (org-e-publish-cache-get key))
1121 (visiting (find-buffer-visiting filename))
1122 included-files-ctime buf)
1124 (when (equal (file-name-extension filename) "org")
1125 (setq buf (find-file (expand-file-name filename)))
1126 (with-current-buffer buf
1127 (goto-char (point-min))
1128 (while (re-search-forward
1129 "^#\\+INCLUDE:[ \t]+\"\\([^\t\n\r\"]*\\)\"[ \t]*.*$" nil t)
1130 (let* ((included-file (expand-file-name (match-string 1))))
1131 (add-to-list
1132 'included-files-ctime
1133 (org-e-publish-cache-ctime-of-src included-file base-dir)
1134 t))))
1135 ;; FIXME: don't kill current buffer.
1136 (unless visiting (kill-buffer buf)))
1137 (if (null pstamp) t
1138 (let ((ctime (org-e-publish-cache-ctime-of-src filename base-dir)))
1139 (or (< pstamp ctime)
1140 (when included-files-ctime
1141 (not (null (delq nil (mapcar (lambda(ct) (< ctime ct))
1142 included-files-ctime))))))))))
1144 (defun org-e-publish-cache-set-file-property
1145 (filename property value &optional project-name)
1146 "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
1147 Use cache file of PROJECT-NAME. If the entry does not exist, it
1148 will be created. Return VALUE."
1149 ;; Evtl. load the requested cache file:
1150 (if project-name (org-e-publish-initialize-cache project-name))
1151 (let ((pl (org-e-publish-cache-get filename)))
1152 (if pl (progn (plist-put pl property value) value)
1153 (org-e-publish-cache-get-file-property
1154 filename property value nil project-name))))
1156 (defun org-e-publish-cache-get-file-property
1157 (filename property &optional default no-create project-name)
1158 "Return the value for a PROPERTY of file FILENAME in publishing cache.
1159 Use cache file of PROJECT-NAME. Return the value of that PROPERTY
1160 or DEFAULT, if the value does not yet exist. If the entry will
1161 be created, unless NO-CREATE is not nil."
1162 ;; Evtl. load the requested cache file:
1163 (if project-name (org-e-publish-initialize-cache project-name))
1164 (let ((pl (org-e-publish-cache-get filename)) retval)
1165 (if pl
1166 (if (plist-member pl property)
1167 (setq retval (plist-get pl property))
1168 (setq retval default))
1169 ;; no pl yet:
1170 (unless no-create
1171 (org-e-publish-cache-set filename (list property default)))
1172 (setq retval default))
1173 retval))
1175 (defun org-e-publish-cache-get (key)
1176 "Return the value stored in `org-e-publish-cache' for key KEY.
1177 Returns nil, if no value or nil is found, or the cache does not
1178 exist."
1179 (unless org-e-publish-cache
1180 (error "`org-e-publish-cache-get' called, but no cache present"))
1181 (gethash key org-e-publish-cache))
1183 (defun org-e-publish-cache-set (key value)
1184 "Store KEY VALUE pair in `org-e-publish-cache'.
1185 Returns value on success, else nil."
1186 (unless org-e-publish-cache
1187 (error "`org-e-publish-cache-set' called, but no cache present"))
1188 (puthash key value org-e-publish-cache))
1190 (defun org-e-publish-cache-ctime-of-src (f base-dir)
1191 "Get the FILENAME ctime as an integer."
1192 (let ((attr (file-attributes
1193 (expand-file-name (or (file-symlink-p f) f) base-dir))))
1194 (+ (lsh (car (nth 5 attr)) 16)
1195 (cadr (nth 5 attr)))))
1198 (provide 'org-e-publish)
1200 ;;; org-e-publish.el ends here