1 ;;; ox-deck.el --- deck.js Presentation Back-End for Org Export Engine
3 ;; Copyright (C) 2013, 2014 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/>.
25 ;; This library implements a deck.js presentation back-end for the Org
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.)
43 ;; The title page is formatted using format-spec. This is error prone
44 ;; when details are missing and may insert empty tags, like <h2></h2>,
45 ;; for missing values.
48 (eval-when-compile (require 'cl
))
50 (org-export-define-derived-backend 'deck
'html
52 '(?d
"Export to deck.js HTML Presentation"
53 ((?H
"To temporary buffer" org-deck-export-as-html
)
54 (?h
"To file" org-deck-export-to-html
)
55 (?o
"To file and open"
57 (if a
(org-deck-export-to-html t s v b
)
58 (org-open-file (org-deck-export-to-html nil s v b
)))))))
60 '((:description
"DESCRIPTION" nil nil newline
)
61 (:keywords
"KEYWORDS" nil nil space
)
62 (:html-link-home
"HTML_LINK_HOME" nil nil
)
63 (:html-link-up
"HTML_LINK_UP" nil nil
)
64 (:deck-postamble
"DECK_POSTAMBLE" nil org-deck-postamble newline
)
65 (:deck-preamble
"DECK_PREAMBLE" nil org-deck-preamble newline
)
66 (:html-head-include-default-style
"HTML_INCLUDE_DEFAULT_STYLE" "html-style" nil
)
67 (:html-head-include-scripts
"HTML_INCLUDE_SCRIPTS" nil nil
)
68 (:deck-base-url
"DECK_BASE_URL" nil org-deck-base-url
)
69 (:deck-theme
"DECK_THEME" nil org-deck-theme
)
70 (:deck-transition
"DECK_TRANSITION" nil org-deck-transition
)
71 (:deck-include-extensions
"DECK_INCLUDE_EXTENSIONS" nil
72 org-deck-include-extensions split
)
73 (:deck-exclude-extensions
"DECK_EXCLUDE_EXTENSIONS" nil
74 org-deck-exclude-extensions split
))
76 '((headline . org-deck-headline
)
77 (inner-template . org-deck-inner-template
)
78 (item . org-deck-item
)
79 (link . org-deck-link
)
80 (template . org-deck-template
)))
82 (defgroup org-export-deck nil
83 "Options for exporting Org mode files to deck.js HTML Presentations."
84 :tag
"Org Export DECK"
85 :group
'org-export-html
)
87 (defcustom org-deck-directories
'("./deck.js")
88 "Directories to search for deck.js components (jquery,
89 modernizr; core, extensions and themes directories.)"
90 :group
'org-export-deck
91 :type
'(repeat (string :tag
"Directory")))
93 (defun org-deck--cleanup-components (components)
95 (car (remove 'nil components
))
97 (string= (file-name-nondirectory x
)
98 (file-name-nondirectory y
)))))
100 (defun org-deck--find-extensions ()
101 "Returns a unique list of all extensions found in
102 in the extensions directories under `org-deck-directories'"
103 (org-deck--cleanup-components
104 (mapcar ; extensions under existing dirs
106 (when (file-directory-p dir
) (directory-files dir t
"^[^.]")))
107 (mapcar ; possible extension directories
108 (lambda (x) (expand-file-name "extensions" x
))
109 org-deck-directories
))))
111 (defun org-deck--find-css (type)
112 "Return a unique list of all the css stylesheets in the themes/TYPE
113 directories under `org-deck-directories'."
114 (org-deck--cleanup-components
117 (let ((css-dir (expand-file-name
118 (concat (file-name-as-directory "themes") type
) dir
)))
119 (when (file-directory-p css-dir
)
120 (directory-files css-dir t
"\\.css$"))))
121 org-deck-directories
)))
123 (defun org-deck-list-components ()
124 "List all available deck extensions, styles and
125 transitions (with full paths) to a temporary buffer."
127 (let ((outbuf (get-buffer-create "*deck.js Extensions*")))
128 (with-current-buffer outbuf
130 (insert "Extensions\n----------\n")
131 (insert (mapconcat 'identity
(org-deck--find-extensions) "\n"))
132 (insert "\n\nStyles\n------\n")
133 (insert (mapconcat 'identity
(org-deck--find-css "style") "\n"))
134 (insert "\n\nTransitions\n----------\n")
135 (insert (mapconcat 'identity
(org-deck--find-css "transition") "\n")))
136 (switch-to-buffer-other-window outbuf
)))
138 (defcustom org-deck-include-extensions nil
139 "If non-nil, list of extensions to include instead of all available.
140 Can be overriden or set with the DECK_INCLUDE_EXTENSIONS property.
141 During output generation, the extensions found by
142 `org-deck--find-extensions' are searched for the appropriate
143 files (scripts and/or stylesheets) to include in the generated
144 html. The href/src attributes are created relative to `org-deck-base-url'."
145 :group
'org-export-deck
146 :type
'(repeat (string :tag
"Extension")))
148 (defcustom org-deck-exclude-extensions nil
149 "If non-nil, list of extensions to exclude.
150 Can be overriden or set with the DECK_EXCLUDE_EXTENSIONS property."
151 :group
'org-export-deck
152 :type
'(repeat (string :tag
"Extension")))
154 (defcustom org-deck-theme
"swiss.css"
155 "deck.js theme. Can be overriden with the DECK_THEME 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/style/."
159 :group
'org-export-deck
162 (defcustom org-deck-transition
"fade.css"
163 "deck.js transition theme. Can be overriden with the
164 DECK_TRANSITION property.
165 If this value contains a path component (\"/\"), it is used as a
166 literal path (url). Otherwise it is prepended with
167 `org-deck-base-url'/themes/transition/."
168 :group
'org-export-deck
171 (defcustom org-deck-base-url
"deck.js"
172 "Url prefix to deck.js base directory containing the core, extensions
173 and themes directories.
174 Can be overriden with the DECK_BASE_URL property."
175 :group
'org-export-deck
178 (defvar org-deck-pre
/postamble-styles
179 `((both "left: 5px; width: 100%;")
180 (preamble "position: absolute; top: 10px;")
182 "Alist of css styles for the preamble, postamble and both respectively.
183 Can be overriden in `org-deck-styles'. See also `org-html-divs'.")
185 (defcustom org-deck-postamble
"<h1>%a - %t</h1>"
186 "Non-nil means insert a postamble in HTML export.
188 When set to a string, use this string
189 as the postamble. When t, insert a string as defined by the
190 formatting string in `org-html-postamble-format'.
192 When set to a function, apply this function and insert the
193 returned string. The function takes the property list of export
194 options as its only argument.
196 This is included in the document at the bottom of the content
197 section, and uses the postamble element and id from
198 `org-html-divs'. The default places the author and presentation
199 title at the bottom of each slide.
201 The css styling is controlled by `org-deck-pre/postamble-styles'.
203 Setting :deck-postamble in publishing projects will take
204 precedence over this variable."
205 :group
'org-export-deck
206 :type
'(choice (const :tag
"No postamble" nil
)
207 (const :tag
"Default formatting string" t
)
208 (string :tag
"Custom formatting string")
209 (function :tag
"Function (must return a string)")))
211 (defcustom org-deck-preamble nil
212 "Non-nil means insert a preamble in HTML export.
214 When set to a string, use this string
215 as the preamble. When t, insert a string as defined by the
216 formatting string in `org-html-preamble-format'.
218 When set to a function, apply this function and insert the
219 returned string. The function takes the property list of export
220 options as its only argument.
222 This is included in the document at the top of content section, and
223 uses the preamble element and id from `org-html-divs'. The css
224 styling is controlled by `org-deck-pre/postamble-styles'.
226 Setting :deck-preamble in publishing projects will take
227 precedence over this variable."
228 :group
'org-export-deck
229 :type
'(choice (const :tag
"No preamble" nil
)
230 (const :tag
"Default formatting string" t
)
231 (string :tag
"Custom formatting string")
232 (function :tag
"Function (must return a string)")))
234 (defvar org-deck-toc-styles
238 "#table-of-contents a {color: inherit;}"
239 "#table-of-contents ul {margin-bottom: 0;}"
240 "#table-of-contents li {padding: 0;}") "\n")
241 "Default css styles used for formatting a table of contents slide.
242 Can be overriden in `org-deck-styles'.
243 Note that when the headline numbering option is true, a \"list-style: none\"
244 is automatically added to avoid both numbers and bullets on the toc entries.")
246 (defcustom org-deck-styles
249 position: static; padding: 0;
251 -webkit-transform: none;
252 -moz-transform: none;
263 "Deck specific CSS styles to include in exported html.
264 Defaults to styles for the title page."
265 :group
'org-export-deck
268 (defcustom org-deck-title-slide-template
274 "Format template to specify title page section.
275 See `org-html-postamble-format' for the valid elements which
278 It will be wrapped in the element defined in the :html-container
279 property, and defaults to the value of `org-html-container-element',
280 and have the id \"title-slide\"."
281 :group
'org-export-deck
284 (defun org-deck-toc (depth info
)
286 (format "<%s id='table-of-contents' class='slide'>\n"
287 (plist-get info
:html-container
))
288 (format "<h2>%s</h2>\n" (org-html--translate "Table of Contents" info
))
292 (let* ((class (org-element-property :HTML_CONTAINER_CLASS headline
))
295 (and (not (org-export-low-level-p headline info
))
296 (org-export-numbered-headline-p headline info
))
300 (org-export-get-headline-number headline info
) ".") ". ")))
304 (replace-regexp-in-string ; remove any links in headline...
307 (org-element-property :title headline
) info
)))))
309 (if (and class
(string-match-p "\\<slide\\>" class
))
311 "<a href='#outline-container-%s'>%s</a>"
312 (or (org-element-property :CUSTOM_ID headline
)
317 (org-export-get-headline-number headline info
) "-")))
320 (org-export-get-relative-level headline info
))))
321 (org-export-collect-headlines info depth
)))
322 (format "</%s>\n" (plist-get info
:html-container
))))
324 (defun org-deck--get-packages (info)
325 (let ((prefix (concat (plist-get info
:deck-base-url
) "/"))
326 (theme (plist-get info
:deck-theme
))
327 (transition (plist-get info
:deck-transition
))
328 (include (plist-get info
:deck-include-extensions
))
329 (exclude (plist-get info
:deck-exclude-extensions
))
330 (scripts '()) (sheets '()) (snippets '()))
331 (add-to-list 'scripts
(concat prefix
"jquery.min.js"))
332 (add-to-list 'scripts
(concat prefix
"core/deck.core.js"))
333 (add-to-list 'scripts
(concat prefix
"modernizr.custom.js"))
334 (add-to-list 'sheets
(concat prefix
"core/deck.core.css"))
337 (let* ((name (file-name-nondirectory extdir
))
338 (dir (file-name-as-directory extdir
))
339 (path (concat prefix
"extensions/" name
"/"))
340 (base (format "deck.%s." name
)))
341 (when (and (or (eq nil include
) (member name include
))
342 (not (member name exclude
)))
343 (when (file-exists-p (concat dir base
"js"))
344 (add-to-list 'scripts
(concat path base
"js")))
345 (when (file-exists-p (concat dir base
"css"))
346 (add-to-list 'sheets
(concat path base
"css")))
347 (when (file-exists-p (concat dir base
"html"))
348 (add-to-list 'snippets
(concat dir base
"html"))))))
349 (org-deck--find-extensions))
350 (if (not (string-match-p "^[[:space:]]*$" theme
))
352 (if (file-name-directory theme
) theme
353 (format "%sthemes/style/%s" prefix theme
))))
354 (if (not (string-match-p "^[[:space:]]*$" transition
))
357 (if (file-name-directory transition
) transition
358 (format "%sthemes/transition/%s" prefix transition
))))
359 (list :scripts
(nreverse scripts
) :sheets
(nreverse sheets
)
360 :snippets snippets
)))
362 (defun org-deck-inner-template (contents info
)
363 "Return body of document string after HTML conversion.
364 CONTENTS is the transcoded contents string. INFO is a plist
365 holding export options."
366 (concat contents
"\n"))
368 (defun org-deck-headline (headline contents info
)
369 (let ((org-html-toplevel-hlevel 2)
370 (class (or (org-element-property :HTML_CONTAINER_CLASS headline
) ""))
371 (level (org-export-get-relative-level headline info
)))
372 (when (and (= 1 level
) (not (string-match-p "\\<slide\\>" class
)))
373 (org-element-put-property headline
:HTML_CONTAINER_CLASS
(concat class
" slide")))
374 (org-html-headline headline contents info
)))
376 (defun org-deck-item (item contents info
)
377 "Transcode an ITEM element from Org to HTML.
378 CONTENTS holds the contents of the item. INFO is a plist holding
379 contextual information.
380 If the containing headline has the property :STEP, then
381 the \"slide\" class will be added to the to the list element,
382 which will make the list into a \"build\"."
383 (let ((text (org-html-item item contents info
)))
384 (if (org-export-get-node-property :STEP item t
)
385 (replace-regexp-in-string "^<li>" "<li class='slide'>" text
)
388 (defun org-deck-link (link desc info
)
389 (replace-regexp-in-string "href=\"#" "href=\"#outline-container-"
390 (org-export-with-backend 'html link desc info
)))
392 (defun org-deck-template (contents info
)
393 "Return complete document string after HTML conversion.
394 CONTENTS is the transcoded contents string. INFO is a plist
395 holding export options."
396 (let ((pkg-info (org-deck--get-packages info
))
397 (org-html--pre/postamble-class
"deck-status")
399 (plist-put info
:html-preamble
(plist-get info
:deck-preamble
))
400 :html-postamble
(plist-get info
:deck-postamble
))))
404 (org-html-doctype info
)
405 (let ((lang (plist-get info
:language
)))
410 "<!--%s <html %s lang='%s' xmlns='http://www.w3.org/1999/xhtml'> %s<![endif]-->"
412 (list `("[if lt IE 7]>" "class='no-js ie6'" ,lang
"")
413 `("[if IE 7]>" "class='no-js ie7'" ,lang
"")
414 `("[if IE 8]>" "class='no-js ie8'" ,lang
"")
415 `("[if gt IE 8]><!-->" "" ,lang
"<!--")) "\n"))
417 (org-deck--build-meta-info info
)
421 "<link rel='stylesheet' href='%s' type='text/css' />" sheet
))
422 (plist-get pkg-info
:sheets
) "\n")
426 "<script src='%s' type='text/javascript'></script>" script
))
427 (plist-get pkg-info
:scripts
) "\n")
428 (org-html--build-mathjax-config info
)
429 "<script type='text/javascript'>"
430 " $(document).ready(function () { $.deck('.slide'); });"
432 (org-html--build-head info
)
433 "<style type='text/css'>"
435 (when (plist-get info
:section-numbers
)
436 "#table-of-contents ul li {list-style-type: none;}")
437 (format "#%s, #%s {%s}"
438 (nth 2 (assq 'preamble org-html-divs
))
439 (nth 2 (assq 'postamble org-html-divs
))
440 (nth 1 (assq 'both org-deck-pre
/postamble-styles
)))
442 (nth 2 (assq 'preamble org-html-divs
))
443 (nth 1 (assq 'preamble org-deck-pre
/postamble-styles
)))
445 (nth 2 (assq 'postamble org-html-divs
))
446 (nth 1 (assq 'postamble org-deck-pre
/postamble-styles
)))
451 (format "<%s id='%s' class='deck-container'>"
452 (nth 1 (assq 'content org-html-divs
))
453 (nth 2 (assq 'content org-html-divs
)))
454 (org-html--build-pre/postamble
'preamble info
)
456 (format "<%s id='title-slide' class='slide'>"
457 (plist-get info
:html-container
))
458 (format-spec org-deck-title-slide-template
(org-html-format-spec info
))
459 (format "</%s>" (plist-get info
:html-container
))
461 (let ((depth (plist-get info
:with-toc
)))
462 (when depth
(org-deck-toc depth info
)))
466 (with-temp-buffer (insert-file-contents snippet
)
468 (plist-get pkg-info
:snippets
) "\n")
469 (org-html--build-pre/postamble
'postamble info
)
470 (format "</%s>" (nth 1 (assq 'content org-html-divs
)))
474 (defun org-deck--build-meta-info (info)
475 "Return meta tags for exported document.
476 INFO is a plist used as a communication channel."
477 (let* ((title (org-export-data (plist-get info
:title
) info
))
478 (author (and (plist-get info
:with-author
)
479 (let ((auth (plist-get info
:author
)))
480 (and auth
(org-export-data auth info
)))))
481 (date (and (plist-get info
:with-date
)
482 (let ((date (org-export-get-date info
)))
483 (and date
(org-export-data date info
)))))
484 (description (plist-get info
:description
))
485 (keywords (plist-get info
:keywords
)))
489 (format "<title>%s</title>" title
)
490 (format "<meta http-equiv='Content-Type' content='text/html; charset=%s'/>"
491 (or (and org-html-coding-system
492 (fboundp 'coding-system-get
)
494 org-html-coding-system
'mime-charset
))
498 (when (< 0 (length (car attr
)))
499 (format "<meta name='%s' content='%s'/>\n"
500 (nth 1 attr
) (car attr
))))
501 (list '("Org-mode" "generator")
503 `(,description
"description")
504 `(,keywords
"keywords")) "")) "\n")))
505 (defun org-deck-export-as-html
506 (&optional async subtreep visible-only body-only ext-plist
)
507 "Export current buffer to an HTML buffer.
509 If narrowing is active in the current buffer, only export its
512 If a region is active, export that region.
514 A non-nil optional argument ASYNC means the process should happen
515 asynchronously. The resulting buffer should be accessible
516 through the `org-export-stack' interface.
518 When optional argument SUBTREEP is non-nil, export the sub-tree
519 at point, extracting information from the headline properties
522 When optional argument VISIBLE-ONLY is non-nil, don't export
523 contents of hidden elements.
525 When optional argument BODY-ONLY is non-nil, only write code
526 between \"<body>\" and \"</body>\" tags.
528 EXT-PLIST, when provided, is a property list with external
529 parameters overriding Org default settings, but still inferior to
532 Export is done in a buffer named \"*Org deck.js Export*\", which
533 will be displayed when `org-export-show-temporary-export-buffer'
536 (org-export-to-buffer 'deck
"*Org deck.js Export*"
537 async subtreep visible-only body-only ext-plist
(lambda () (nxml-mode))))
539 (defun org-deck-export-to-html
540 (&optional async subtreep visible-only body-only ext-plist
)
541 "Export current buffer to a deck.js HTML file.
543 If narrowing is active in the current buffer, only export its
546 If a region is active, export that region.
548 A non-nil optional argument ASYNC means the process should happen
549 asynchronously. The resulting file should be accessible through
550 the `org-export-stack' interface.
552 When optional argument SUBTREEP is non-nil, export the sub-tree
553 at point, extracting information from the headline properties
556 When optional argument VISIBLE-ONLY is non-nil, don't export
557 contents of hidden elements.
559 When optional argument BODY-ONLY is non-nil, only write code
560 between \"<body>\" and \"</body>\" tags.
562 EXT-PLIST, when provided, is a property list with external
563 parameters overriding Org default settings, but still inferior to
566 Return output file's name."
568 (let* ((extension (concat "." org-html-extension
))
569 (file (org-export-output-file-name extension subtreep
))
570 (org-export-coding-system org-html-coding-system
))
571 (org-export-to-file 'deck file
572 async subtreep visible-only body-only ext-plist
)))
574 (defun org-deck-publish-to-html (plist filename pub-dir
)
575 "Publish an org file to deck.js HTML Presentation.
576 FILENAME is the filename of the Org file to be published. PLIST
577 is the property list for the given project. PUB-DIR is the
578 publishing directory. Returns output file name."
579 (org-publish-org-to 'deck filename
".html" plist pub-dir
))
583 ;;; ox-deck.el ends here