org.texi: Update Beamer documentation
[org-mode.git] / contrib / lisp / ox-deck.el
blob6338643b48be6bb40ea1179e3a008ed593ba25c5
1 ;;; ox-deck.el --- deck.js Presentation Back-End for Org Export Engine
3 ;; Copyright (C) 2013 Rick Frankel
5 ;; Author: Rick Frankel <emacs at rickster dot com>
6 ;; Keywords: outlines, hypermedia, slideshow
8 ;; This file is not part of GNU Emacs.
10 ;; This program 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 ;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This library implements a deck.js presentation back-end for the Org
26 ;; generic exporter.
28 ;; Installation
29 ;; -------------
30 ;; Get a copy of deck.js from http://imakewebthings.com/deck.js/ or
31 ;; the gitub repository at https://github.com/imakewebthings/deck.js.
33 ;; Add the path to the extracted code to the variable
34 ;; `org-deck-directories' There are a number of customization in the
35 ;; org-export-deck group, most of which can be overrriden with buffer
36 ;; local customization (starting with DECK_.)
38 ;; See ox.el and ox-html.el for more details on how this exporter
39 ;; works (it is derived from ox-html.)
41 (require 'ox-html)
42 (eval-when-compile (require 'cl))
44 (org-export-define-derived-backend 'deck 'html
45 :menu-entry
46 '(?d "Export to deck.js HTML Presentation"
47 ((?H "To temporary buffer" org-deck-export-as-html)
48 (?h "To file" org-deck-export-to-html)
49 (?o "To file and open"
50 (lambda (a s v b)
51 (if a (org-deck-export-to-html t s v b)
52 (org-open-file (org-deck-export-to-html nil s v b)))))))
53 :options-alist
54 '((:html-link-home "HTML_LINK_HOME" nil nil)
55 (:html-link-up "HTML_LINK_UP" nil nil)
56 (:deck-postamble "DECK_POSTAMBLE" nil org-deck-postamble newline)
57 (:deck-preamble "DECK_PREAMBLE" nil org-deck-preamble newline)
58 (:html-head-include-default-style "HTML_INCLUDE_DEFAULT_STYLE" nil nil)
59 (:html-head-include-scripts "HTML_INCLUDE_SCRIPTS" nil nil)
60 (:deck-base-url "DECK_BASE_URL" nil org-deck-base-url)
61 (:deck-theme "DECK_THEME" nil org-deck-theme)
62 (:deck-transition "DECK_TRANSITION" nil org-deck-transition)
63 (:deck-include-extensions "DECK_INCLUDE_EXTENSIONS" nil
64 org-deck-include-extensions split)
65 (:deck-exclude-extensions "DECK_EXCLUDE_EXTENSIONS" nil
66 org-deck-exclude-extensions split))
67 :translate-alist
68 '((headline . org-deck-headline)
69 (inner-template . org-deck-inner-template)
70 (item . org-deck-item)
71 (template . org-deck-template)))
73 (defgroup org-export-deck nil
74 "Options for exporting Org mode files to deck.js HTML Presentations."
75 :tag "Org Export DECK"
76 :group 'org-export-html)
78 (defcustom org-deck-directories '("./deck.js")
79 "Directories to search for deck.js components (jquery,
80 modernizr; core, extensions and themes directories.)"
81 :group 'org-export-deck
82 :type '(repeat (string :tag "Directory")))
84 (defun org-deck--cleanup-components (components)
85 (remove-duplicates
86 (car (remove 'nil components))
87 :test (lambda (x y)
88 (string= (file-name-nondirectory x)
89 (file-name-nondirectory y)))))
91 (defun org-deck--find-extensions ()
92 "Returns a unique list of all extensions found in
93 in the extensions directories under `org-deck-directories'"
94 (org-deck--cleanup-components
95 (mapcar ; extensions under existing dirs
96 (lambda (dir)
97 (when (file-directory-p dir) (directory-files dir t "^[^.]")))
98 (mapcar ; possible extension directories
99 (lambda (x) (expand-file-name "extensions" x))
100 org-deck-directories))))
102 (defun org-deck--find-css (type)
103 "Return a unique list of all the css stylesheets in the themes/TYPE
104 directories under `org-deck-directories'."
105 (org-deck--cleanup-components
106 (mapcar
107 (lambda (dir)
108 (let ((css-dir (expand-file-name
109 (concat (file-name-as-directory "themes") type) dir)))
110 (when (file-directory-p css-dir)
111 (directory-files css-dir t "\\.css$"))))
112 org-deck-directories)))
114 (defun org-deck-list-components ()
115 "List all available deck extensions, styles and
116 transitions (with full paths) to a temporary buffer."
117 (interactive)
118 (let ((outbuf (get-buffer-create "*deck.js Extensions*")))
119 (with-current-buffer outbuf
120 (erase-buffer)
121 (insert "Extensions\n----------\n")
122 (insert (mapconcat 'identity (org-deck--find-extensions) "\n"))
123 (insert "\n\nStyles\n------\n")
124 (insert (mapconcat 'identity (org-deck--find-css "style") "\n"))
125 (insert "\n\nTransitions\n----------\n")
126 (insert (mapconcat 'identity (org-deck--find-css "transition") "\n")))
127 (switch-to-buffer-other-window outbuf)))
129 (defcustom org-deck-include-extensions nil
130 "If non-nil, list of extensions to include instead of all available.
131 Can be overriden or set with the DECK_INCLUDE_EXTENSIONS property.
132 During output generation, the extensions found by
133 `org-deck--find-extensions' are searched for the appropriate
134 files (scripts and/or stylesheets) to include in the generated
135 html. The href/src attributes are created relative to `org-deck-base-url'."
136 :group 'org-export-deck
137 :type '(repeat (string :tag "Extension")))
139 (defcustom org-deck-exclude-extensions nil
140 "If non-nil, list of extensions to exclude.
141 Can be overriden or set with the DECK_EXCLUDE_EXTENSIONS property."
142 :group 'org-export-deck
143 :type '(repeat (string :tag "Extension")))
145 (defcustom org-deck-theme "swiss.css"
146 "deck.js theme. Can be overriden with the DECK_THEME property.
147 If this value contains a path component (\"/\"), it is used as a
148 literal path (url). Otherwise it is prepended with
149 `org-deck-base-url'/themes/style/."
150 :group 'org-export-deck
151 :type 'string)
153 (defcustom org-deck-transition "fade.css"
154 "deck.js transition theme. Can be overriden with the
155 DECK_TRANSITION property.
156 If this value contains a path component (\"/\"), it is used as a
157 literal path (url). Otherwise it is prepended with
158 `org-deck-base-url'/themes/transition/."
159 :group 'org-export-deck
160 :type 'string)
162 (defcustom org-deck-base-url "deck.js"
163 "Url prefix to deck.js base directory containing the core, extensions
164 and themes directories.
165 Can be overriden with the DECK_BASE_URL property."
166 :group 'org-export-deck
167 :type 'string)
169 (defvar org-deck-pre/postamble-styles
170 `((both "left: 5px; width: 100%;")
171 (preamble "position: absolute; top: 10px;")
172 (postamble ""))
173 "Alist of css styles for the preamble, postamble and both respectively.
174 Can be overriden in `org-deck-styles'. See also `org-html-divs'.")
176 (defcustom org-deck-postamble "<h1>%a - %t</h1>"
177 "Non-nil means insert a postamble in HTML export.
179 When set to a string, use this string
180 as the postamble. When t, insert a string as defined by the
181 formatting string in `org-html-postamble-format'.
183 When set to a function, apply this function and insert the
184 returned string. The function takes the property list of export
185 options as its only argument.
187 This is included in the document at the bottom of the content
188 section, and uses the postamble element and id from
189 `org-html-divs'. The default places the author and presentation
190 title at the bottom of each slide.
192 The css styling is controlled by `org-deck-pre/postamble-styles'.
194 Setting :deck-postamble in publishing projects will take
195 precedence over this variable."
196 :group 'org-export-deck
197 :type '(choice (const :tag "No postamble" nil)
198 (const :tag "Default formatting string" t)
199 (string :tag "Custom formatting string")
200 (function :tag "Function (must return a string)")))
202 (defcustom org-deck-preamble nil
203 "Non-nil means insert a preamble in HTML export.
205 When set to a string, use this string
206 as the preamble. When t, insert a string as defined by the
207 formatting string in `org-html-preamble-format'.
209 When set to a function, apply this function and insert the
210 returned string. The function takes the property list of export
211 options as its only argument.
213 This is included in the document at the top of content section, and
214 uses the preamble element and id from `org-html-divs'. The css
215 styling is controlled by `org-deck-pre/postamble-styles'.
217 Setting :deck-preamble in publishing projects will take
218 precedence over this variable."
219 :group 'org-export-deck
220 :type '(choice (const :tag "No preamble" nil)
221 (const :tag "Default formatting string" t)
222 (string :tag "Custom formatting string")
223 (function :tag "Function (must return a string)")))
225 (defvar org-deck-toc-styles
226 (mapconcat
227 'identity
228 (list
229 "#table-of-contents a {color: inherit;}"
230 "#table-of-contents ul {margin-bottom: 0;}"
231 "#table-of-contents li {padding: 0;}") "\n")
232 "Default css styles used for formatting a table of contents slide.
233 Can be overriden in `org-deck-styles'.
234 Note that when the headline numbering option is true, a \"list-style: none\"
235 is automatically added to avoid both numbers and bullets on the toc entries.")
237 (defcustom org-deck-styles
239 #title-slide h1 {
240 position: static; padding: 0;
241 margin-top: 10%;
242 -webkit-transform: none;
243 -moz-transform: none;
244 -ms-transform: none;
245 -o-transform: none;
246 transform: none;
248 #title-slide h2 {
249 text-align: center;
250 border:none;
251 padding: 0;
252 margin: 0;
254 "Deck specific CSS styles to include in exported html.
255 Defaults to styles for the title page."
256 :group 'org-export-deck
257 :type 'string)
259 (defcustom org-deck-title-slide-template
260 "<h1>%t</h1>
261 <h2>%a</h2>
262 <h2>%e</h2>
263 <h2>%d</h2>"
264 "Format template to specify title page section.
265 See `org-html-postamble-format' for the valid elements which
266 can be included.
268 It will be wrapped in the element defined in the :html-container
269 property, and defaults to the value of `org-html-container-element',
270 and have the id \"title-slide\"."
271 :group 'org-export-deck
272 :type 'string)
274 (defun org-deck-toc (depth info)
275 (concat
276 (format "<%s id='table-of-contents' class='slide'>\n"
277 (plist-get info :html-container))
278 (format "<h2>%s</h2>\n" (org-html--translate "Table of Contents" info))
279 (org-html--toc-text
280 (mapcar
281 (lambda (headline)
282 (let* ((class (org-element-property :HTML_CONTAINER_CLASS headline))
283 (section-number
284 (when
285 (and (not (org-export-low-level-p headline info))
286 (org-export-numbered-headline-p headline info))
287 (concat
288 (mapconcat
289 'number-to-string
290 (org-export-get-headline-number headline info) ".") ". ")))
291 (title
292 (concat
293 section-number
294 (replace-regexp-in-string ; remove any links in headline...
295 "</?a[^>]*>" ""
296 (org-export-data
297 (org-element-property :title headline) info)))))
298 (cons
299 (if (and class (string-match-p "\\<slide\\>" class))
300 (format
301 "<a href='#outline-container-%s'>%s</a>"
302 (or (org-element-property :CUSTOM_ID headline)
303 (mapconcat
304 'number-to-string
305 (org-export-get-headline-number headline info) "-"))
306 title)
307 title)
308 (org-export-get-relative-level headline info))))
309 (org-export-collect-headlines info depth)))
310 (format "</%s>\n" (plist-get info :html-container))))
312 (defun org-deck--get-packages (info)
313 (let ((prefix (concat (plist-get info :deck-base-url) "/"))
314 (theme (plist-get info :deck-theme))
315 (transition (plist-get info :deck-transition))
316 (include (plist-get info :deck-include-extensions))
317 (exclude (plist-get info :deck-exclude-extensions))
318 (scripts '()) (sheets '()) (snippets '()))
319 (add-to-list 'scripts (concat prefix "jquery-1.7.2.min.js"))
320 (add-to-list 'scripts (concat prefix "core/deck.core.js"))
321 (add-to-list 'scripts (concat prefix "modernizr.custom.js"))
322 (add-to-list 'sheets (concat prefix "core/deck.core.css"))
323 (mapc
324 (lambda (extdir)
325 (let* ((name (file-name-nondirectory extdir))
326 (dir (file-name-as-directory extdir))
327 (path (concat prefix "extensions/" name "/"))
328 (base (format "deck.%s." name)))
329 (when (and (or (eq nil include) (member name include))
330 (not (member name exclude)))
331 (when (file-exists-p (concat dir base "js"))
332 (add-to-list 'scripts (concat path base "js")))
333 (when (file-exists-p (concat dir base "css"))
334 (add-to-list 'sheets (concat path base "css")))
335 (when (file-exists-p (concat dir base "html"))
336 (add-to-list 'snippets (concat dir base "html"))))))
337 (org-deck--find-extensions))
338 (if (not (string-match-p "^[[:space:]]*$" theme))
339 (add-to-list 'sheets
340 (if (file-name-directory theme) theme
341 (format "%sthemes/style/%s" prefix theme))))
342 (if (not (string-match-p "^[[:space:]]*$" transition))
343 (add-to-list
344 'sheets
345 (if (file-name-directory transition) transition
346 (format "%sthemes/transition/%s" prefix transition))))
347 (list :scripts (nreverse scripts) :sheets (nreverse sheets)
348 :snippets snippets)))
350 (defun org-deck-inner-template (contents info)
351 "Return body of document string after HTML conversion.
352 CONTENTS is the transcoded contents string. INFO is a plist
353 holding export options."
354 (concat contents "\n"))
356 (defun org-deck-headline (headline contents info)
357 (let ((org-html-toplevel-hlevel 2)
358 (class (or (org-element-property :HTML_CONTAINER_CLASS headline) ""))
359 (level (org-export-get-relative-level headline info)))
360 (when (and (= 1 level) (not (string-match-p "\\<slide\\>" class)))
361 (org-element-put-property headline :HTML_CONTAINER_CLASS (concat class " slide")))
362 (org-html-headline headline contents info)))
364 (defun org-deck-item (item contents info)
365 "Transcode an ITEM element from Org to HTML.
366 CONTENTS holds the contents of the item. INFO is a plist holding
367 contextual information.
368 If the containing headline has the property :slide, then
369 the \"slide\" class will be added to the to the list element,
370 which will make the list into a \"build\"."
371 (let ((text (org-html-item item contents info)))
372 (if (org-export-get-node-property :STEP item t)
373 (replace-regexp-in-string "^<li>" "<li class='slide'>" text)
374 text)))
376 (defun org-deck-template (contents info)
377 "Return complete document string after HTML conversion.
378 CONTENTS is the transcoded contents string. INFO is a plist
379 holding export options."
380 (let ((pkg-info (org-deck--get-packages info))
381 (org-html--pre/postamble-class "deck-status")
382 (info (plist-put
383 (plist-put info :html-preamble (plist-get info :deck-preamble))
384 :html-postamble (plist-get info :deck-postamble))))
385 (mapconcat
386 'identity
387 (list
388 (plist-get info :html-doctype)
389 (let ((lang (plist-get info :language)))
390 (mapconcat
391 (lambda (x)
392 (apply
393 'format
394 "<!--%s <html %s lang='%s' xmlns='http://www.w3.org/1999/xhtml'> %s<![endif]-->"
396 (list `("[if lt IE 7]>" "class='no-js ie6'" ,lang "")
397 `("[if IE 7]>" "class='no-js ie7'" ,lang "")
398 `("[if IE 8]>" "class='no-js ie8'" ,lang "")
399 `("[if gt IE 8]><!-->" "" ,lang "<!--")) "\n"))
400 "<head>"
401 (org-deck--build-meta-info info)
402 (mapconcat
403 (lambda (sheet)
404 (format
405 "<link rel='stylesheet' href='%s' type='text/css' />" sheet))
406 (plist-get pkg-info :sheets) "\n")
407 (mapconcat
408 (lambda (script)
409 (format
410 "<script src='%s' type='text/javascript'></script>" script))
411 (plist-get pkg-info :scripts) "\n")
412 (org-html--build-mathjax-config info)
413 "<script type='text/javascript'>"
414 " $(document).ready(function () { $.deck('.slide'); });"
415 "</script>"
416 (org-html--build-head info)
417 "<style type='text/css'>"
418 org-deck-toc-styles
419 (when (plist-get info :section-numbers)
420 "#table-of-contents ul li {list-style-type: none;}")
421 (format "#%s, #%s {%s}"
422 (nth 2 (assq 'preamble org-html-divs))
423 (nth 2 (assq 'postamble org-html-divs))
424 (nth 1 (assq 'both org-deck-pre/postamble-styles)))
425 (format "#%s {%s}"
426 (nth 2 (assq 'preamble org-html-divs))
427 (nth 1 (assq 'preamble org-deck-pre/postamble-styles)))
428 (format "#%s {%s}"
429 (nth 2 (assq 'postamble org-html-divs))
430 (nth 1 (assq 'postamble org-deck-pre/postamble-styles)))
431 org-deck-styles
432 "</style>"
433 "</head>"
434 "<body>"
435 (format "<%s id='%s' class='deck-container'>"
436 (nth 1 (assq 'content org-html-divs))
437 (nth 2 (assq 'content org-html-divs)))
438 (org-html--build-pre/postamble 'preamble info)
439 ;; title page
440 (format "<%s id='title-slide' class='slide'>"
441 (plist-get info :html-container))
442 (format-spec org-deck-title-slide-template (org-html-format-spec info))
443 (format "</%s>" (plist-get info :html-container))
444 ;; toc page
445 (let ((depth (plist-get info :with-toc)))
446 (when depth (org-deck-toc depth info)))
447 contents
448 (mapconcat
449 (lambda (snippet)
450 (with-temp-buffer (insert-file-contents snippet)
451 (buffer-string)))
452 (plist-get pkg-info :snippets) "\n")
453 (org-html--build-pre/postamble 'postamble info)
454 (format "</%s>" (nth 1 (assq 'content org-html-divs)))
455 "</body>"
456 "</html>\n") "\n")))
458 (defun org-deck--build-meta-info (info)
459 "Return meta tags for exported document.
460 INFO is a plist used as a communication channel."
461 (let* ((title (org-export-data (plist-get info :title) info))
462 (author (and (plist-get info :with-author)
463 (let ((auth (plist-get info :author)))
464 (and auth (org-export-data auth info)))))
465 (date (and (plist-get info :with-date)
466 (let ((date (org-export-get-date info)))
467 (and date (org-export-data date info)))))
468 (description (plist-get info :description))
469 (keywords (plist-get info :keywords)))
470 (mapconcat
471 'identity
472 (list
473 (format "<title>%s</title>" title)
474 (format "<meta http-equiv='Content-Type' content='text/html; charset=%s'/>"
475 (or (and org-html-coding-system
476 (fboundp 'coding-system-get)
477 (coding-system-get
478 org-html-coding-system 'mime-charset))
479 "iso-8859-1"))
480 (mapconcat
481 (lambda (attr)
482 (when (< 0 (length (car attr)))
483 (format "<meta name='%s' content='%s'/>\n"
484 (nth 1 attr) (car attr))))
485 (list '("Org-mode" "generator")
486 `(,author "author")
487 `(,description "description")
488 `(,keywords "keywords")) "")) "\n")))
489 (defun org-deck-export-as-html
490 (&optional async subtreep visible-only body-only ext-plist)
491 "Export current buffer to an HTML buffer.
493 If narrowing is active in the current buffer, only export its
494 narrowed part.
496 If a region is active, export that region.
498 A non-nil optional argument ASYNC means the process should happen
499 asynchronously. The resulting buffer should be accessible
500 through the `org-export-stack' interface.
502 When optional argument SUBTREEP is non-nil, export the sub-tree
503 at point, extracting information from the headline properties
504 first.
506 When optional argument VISIBLE-ONLY is non-nil, don't export
507 contents of hidden elements.
509 When optional argument BODY-ONLY is non-nil, only write code
510 between \"<body>\" and \"</body>\" tags.
512 EXT-PLIST, when provided, is a property list with external
513 parameters overriding Org default settings, but still inferior to
514 file-local settings.
516 Export is done in a buffer named \"*Org deck.js Export*\", which
517 will be displayed when `org-export-show-temporary-export-buffer'
518 is non-nil."
519 (interactive)
520 (if async
521 (org-export-async-start
522 (lambda (output)
523 (with-current-buffer (get-buffer-create "*Org deck.js Export*")
524 (erase-buffer)
525 (insert output)
526 (goto-char (point-min))
527 (nxml-mode)
528 (org-export-add-to-stack (current-buffer) 'deck)))
529 `(org-export-as 'deck ,subtreep ,visible-only ,body-only ',ext-plist))
530 (let ((outbuf (org-export-to-buffer
531 'deck "*Org deck.js Export*"
532 subtreep visible-only body-only ext-plist)))
533 ;; Set major mode.
534 (with-current-buffer outbuf (nxml-mode))
535 (when org-export-show-temporary-export-buffer
536 (switch-to-buffer-other-window outbuf)))))
538 (defun org-deck-export-to-html
539 (&optional async subtreep visible-only body-only ext-plist)
540 "Export current buffer to a deck.js HTML file.
542 If narrowing is active in the current buffer, only export its
543 narrowed part.
545 If a region is active, export that region.
547 A non-nil optional argument ASYNC means the process should happen
548 asynchronously. The resulting file should be accessible through
549 the `org-export-stack' interface.
551 When optional argument SUBTREEP is non-nil, export the sub-tree
552 at point, extracting information from the headline properties
553 first.
555 When optional argument VISIBLE-ONLY is non-nil, don't export
556 contents of hidden elements.
558 When optional argument BODY-ONLY is non-nil, only write code
559 between \"<body>\" and \"</body>\" tags.
561 EXT-PLIST, when provided, is a property list with external
562 parameters overriding Org default settings, but still inferior to
563 file-local settings.
565 Return output file's name."
566 (interactive)
567 (let* ((extension (concat "." org-html-extension))
568 (file (org-export-output-file-name extension subtreep))
569 (org-export-coding-system org-html-coding-system))
570 (if async
571 (org-export-async-start
572 (lambda (f) (org-export-add-to-stack f 'deck))
573 (let ((org-export-coding-system org-html-coding-system))
574 `(expand-file-name
575 (org-export-to-file
576 'deck ,file ,subtreep ,visible-only ,body-only ',ext-plist))))
577 (let ((org-export-coding-system org-html-coding-system))
578 (org-export-to-file
579 'deck file subtreep visible-only body-only ext-plist)))))
581 (defun org-deck-publish-to-html (plist filename pub-dir)
582 "Publish an org file to deck.js HTML Presentation.
583 FILENAME is the filename of the Org file to be published. PLIST
584 is the property list for the given project. PUB-DIR is the
585 publishing directory. Returns output file name."
586 (org-publish-org-to 'deck filename ".html" plist pub-dir))
588 (provide 'ox-deck)
590 ;;; ox-deck.el ends here