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