ox-deck.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. ;;; ox-deck.el --- deck.js Presentation Back-End for Org Export Engine
  2. ;; Copyright (C) 2013 Rick Frankel
  3. ;; Author: Rick Frankel <emacs at rickster dot com>
  4. ;; Keywords: outlines, hypermedia, slideshow
  5. ;; This file is not part of GNU Emacs.
  6. ;; This program is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; This program is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This library implements a deck.js presentation back-end for the Org
  18. ;; generic exporter.
  19. ;; Installation
  20. ;; -------------
  21. ;; Get a copy of deck.js from http://imakewebthings.com/deck.js/ or
  22. ;; the gitub repository at https://github.com/imakewebthings/deck.js.
  23. ;;
  24. ;; Add the path to the extracted code to the variable
  25. ;; `org-deck-directories' There are a number of customization in the
  26. ;; org-export-deck group, most of which can be overrriden with buffer
  27. ;; local customization (starting with DECK_.)
  28. ;; See ox.el and ox-html.el for more details on how this exporter
  29. ;; works (it is derived from ox-html.)
  30. (require 'ox-html)
  31. (eval-when-compile (require 'cl))
  32. (org-export-define-derived-backend deck html
  33. :menu-entry
  34. (?d "Export to deck.js HTML Presentation"
  35. ((?H "To temporary buffer" org-deck-export-as-html)
  36. (?h "To file" org-deck-export-to-html)
  37. (?o "To file and open"
  38. (lambda (a s v b)
  39. (if a (org-deck-export-to-html t s v b)
  40. (org-open-file (org-deck-export-to-html nil s v b)))))))
  41. :options-alist
  42. ((:html-link-home "HTML_LINK_HOME" nil nil)
  43. (:html-link-up "HTML_LINK_UP" nil nil)
  44. (:html-postamble nil "html-postamble" nil t)
  45. (:html-preamble nil "html-preamble" nil t)
  46. (:html-head-include-default-style "HTML_INCLUDE_DEFAULT_STYLE" nil nil)
  47. (:html-head-include-scripts "HTML_INCLUDE_SCRIPTS" nil nil)
  48. (:deck-base-url "DECK_BASE_URL" nil org-deck-base-url)
  49. (:deck-theme "DECK_THEME" nil org-deck-theme)
  50. (:deck-transition "DECK_TRANSITION" nil org-deck-transition)
  51. (:deck-include-extensions "DECK_INCLUDE_EXTENSIONS" nil
  52. org-deck-include-extensions split)
  53. (:deck-exclude-extensions "DECK_EXCLUDE_EXTENSIONS" nil
  54. org-deck-exclude-extensions split))
  55. :translate-alist
  56. ((headline . org-deck-headline)
  57. (inner-template . org-deck-inner-template)
  58. (item . org-deck-item)
  59. (template . org-deck-template)))
  60. (defgroup org-export-deck nil
  61. "Options for exporting Org mode files to deck.js HTML Presentations."
  62. :tag "Org Export DECK"
  63. :group 'org-export-html)
  64. (defcustom org-deck-directories '("./deck.js")
  65. "Directories to search for deck.js components (jquery,
  66. modernizr; core, extensions and themes directories.)"
  67. :group 'org-export-deck
  68. :type '(repeat (string :tag "Directory")))
  69. (defun org-deck--cleanup-components (components)
  70. (remove-duplicates
  71. (car (remove 'nil components))
  72. :test (lambda (x y)
  73. (string= (file-name-nondirectory x)
  74. (file-name-nondirectory y)))))
  75. (defun org-deck--find-extensions ()
  76. "Returns a unique list of all extensions found in
  77. in the extensions directories under `org-deck-directories'"
  78. (org-deck--cleanup-components
  79. (mapcar ; extensions under existing dirs
  80. (lambda (dir)
  81. (when (file-directory-p dir) (directory-files dir t "^[^.]")))
  82. (mapcar ; possible extension directories
  83. (lambda (x) (expand-file-name "extensions" x))
  84. org-deck-directories))))
  85. (defun org-deck--find-css (type)
  86. "Return a unique list of all the css stylesheets in the themes/TYPE
  87. directories under `org-deck-directories'."
  88. (org-deck--cleanup-components
  89. (mapcar
  90. (lambda (dir)
  91. (let ((css-dir (expand-file-name
  92. (concat (file-name-as-directory "themes") type) dir)))
  93. (when (file-directory-p css-dir)
  94. (directory-files css-dir t "\\.css$"))))
  95. org-deck-directories)))
  96. (defun org-deck-list-components ()
  97. "List all available deck extensions, styles and
  98. transitions (with full paths) to a temporary buffer."
  99. (interactive)
  100. (let ((outbuf (get-buffer-create "*deck.js Extensions*")))
  101. (with-current-buffer outbuf
  102. (erase-buffer)
  103. (insert "Extensions\n----------\n")
  104. (insert (mapconcat 'identity (org-deck--find-extensions) "\n"))
  105. (insert "\n\nStyles\n------\n")
  106. (insert (mapconcat 'identity (org-deck--find-css "style") "\n"))
  107. (insert "\n\nTransitions\n----------\n")
  108. (insert (mapconcat 'identity (org-deck--find-css "transition") "\n")))
  109. (switch-to-buffer-other-window outbuf)))
  110. (defcustom org-deck-include-extensions nil
  111. "If non-nil, list of extensions to include instead of all available.
  112. Can be overriden or set with the DECK_INCLUDE_EXTENSIONS property.
  113. During output generation, the extensions found by
  114. `org-deck--find-extensions' are searched for the appropriate
  115. files (scripts and/or stylesheets) to include in the generated
  116. html. The href/src attributes are created relative to `org-deck-base-url'."
  117. :group 'org-export-deck
  118. :type '(repeat (string :tag "Extension")))
  119. (defcustom org-deck-exclude-extensions nil
  120. "If non-nil, list of extensions to exclude.
  121. Can be overriden or set with the DECK_EXCLUDE_EXTENSIONS property."
  122. :group 'org-export-deck
  123. :type '(repeat (string :tag "Extension")))
  124. (defcustom org-deck-theme "swiss.css"
  125. "deck.js theme. Can be overriden with the DECK_THEME property.
  126. If this value contains a path component (\"/\"), it is used as a
  127. literal path (url). Otherwise it is prepended with
  128. `org-deck-base-url'/themes/style/."
  129. :group 'org-export-deck
  130. :type 'string)
  131. (defcustom org-deck-transition "fade.css"
  132. "deck.js transition theme. Can be overriden with the
  133. DECK_TRANSITION property.
  134. If this value contains a path component (\"/\"), it is used as a
  135. literal path (url). Otherwise it is prepended with
  136. `org-deck-base-url'/themes/transition/."
  137. :group 'org-export-deck
  138. :type 'string)
  139. (defcustom org-deck-base-url "deck.js"
  140. "Url prefix to deck.js base directory containing the core, extensions
  141. and themes directories.
  142. Can be overriden with the DECK_BASE_URL property."
  143. :group 'org-export-deck
  144. :type 'string)
  145. (defcustom org-deck-footer-template
  146. "<h1>%author - %title</h1>"
  147. "Format template to specify footer div.
  148. Completed using `org-fill-template'.
  149. Optional keys include %author, %email, %file, %title and %date.
  150. This is included in a <footer> section."
  151. :group 'org-export-deck
  152. :type 'string)
  153. (defcustom org-deck-header-template ""
  154. "Format template to specify page. Completed using `org-fill-template'.
  155. Optional keys include %author, %email, %file, %title and %date.
  156. This is included in a <header> section."
  157. :group 'org-export-deck
  158. :type 'string)
  159. (defcustom org-deck-title-page-style
  160. "<style type='text/css'>
  161. header, footer { left: 5px; width: 100% }
  162. header { position: absolute; top: 10px; }
  163. #title-slide h1 {
  164. position: static; padding: 0;
  165. margin-top: 10%;
  166. -webkit-transform: none;
  167. -moz-transform: none;
  168. -ms-transform: none;
  169. -o-transform: none;
  170. transform: none;
  171. }
  172. #title-slide h2 {
  173. text-align: center;
  174. border:none;
  175. padding: 0;
  176. margin: 0;
  177. }
  178. </style>"
  179. "CSS styles to use for title page"
  180. :group 'org-export-deck
  181. :type 'string)
  182. (defcustom org-deck-title-page-template
  183. "<div class='slide' id='title-slide'>
  184. <h1>%title</h1>
  185. <h2>%author</h2>
  186. <h2>%email</h2>
  187. <h2>%date</h2>
  188. </div>"
  189. "Format template to specify title page div.
  190. Completed using `org-fill-template'.
  191. Optional keys include %author, %email, %file, %title and %date.
  192. Note that the wrapper div must include the class \"slide\"."
  193. :group 'org-export-deck
  194. :type 'string)
  195. (defcustom org-deck-toc-style
  196. "<style type='text/css'>
  197. header, footer { left: 5px; width: 100% }
  198. header { position: absolute; top: 10px; }
  199. #table-of-contents h1 {
  200. position: static; padding: 0;
  201. margin-top: 10%;
  202. -webkit-transform: none;
  203. -moz-transform: none;
  204. -ms-transform: none;
  205. -o-transform: none;
  206. Transform: none;
  207. }
  208. #title-slide h2 {
  209. text-align: center;
  210. border:none;
  211. padding: 0;
  212. margin: 0;
  213. }
  214. </style>"
  215. "CSS styles to use for title page"
  216. :group 'org-export-deck
  217. :type 'string)
  218. (defun org-deck-toc (depth info)
  219. (concat
  220. "<div id=\"table-of-contents\" class=\"slide\">\n"
  221. (format "<h2>%s</h2>\n"
  222. (org-html--translate "Table of Contents" info))
  223. (org-html--toc-text
  224. (mapcar
  225. (lambda (headline)
  226. (let* ((class (org-element-property :HTML_CONTAINER_CLASS headline))
  227. (section-number
  228. (when
  229. (and (not (org-export-low-level-p headline info))
  230. (org-export-numbered-headline-p headline info))
  231. (concat
  232. (mapconcat
  233. 'number-to-string
  234. (org-export-get-headline-number headline info) ".") ". ")))
  235. (title
  236. (concat
  237. section-number
  238. (replace-regexp-in-string ; remove any links in headline...
  239. "</?a[^>]*>" ""
  240. (org-export-data
  241. (org-element-property :title headline) info)))))
  242. (cons
  243. (if (and class (string-match-p "\\<slide\\>" class))
  244. (format
  245. "<a href='#outline-container-%s'>%s</a>"
  246. (or (org-element-property :CUSTOM_ID headline)
  247. (mapconcat
  248. 'number-to-string
  249. (org-export-get-headline-number headline info) "-"))
  250. title)
  251. title)
  252. (org-export-get-relative-level headline info))))
  253. (org-export-collect-headlines info depth)))
  254. "</div>\n"))
  255. (defun org-deck--get-packages (info)
  256. (let ((prefix (concat (plist-get info :deck-base-url) "/"))
  257. (theme (plist-get info :deck-theme))
  258. (transition (plist-get info :deck-transition))
  259. (include (plist-get info :deck-include-extensions))
  260. (exclude (plist-get info :deck-exclude-extensions))
  261. (scripts '()) (sheets '()) (snippets '()))
  262. (add-to-list 'scripts (concat prefix "jquery-1.7.2.min.js"))
  263. (add-to-list 'scripts (concat prefix "core/deck.core.js"))
  264. (add-to-list 'scripts (concat prefix "modernizr.custom.js"))
  265. (add-to-list 'sheets (concat prefix "core/deck.core.css"))
  266. (mapc
  267. (lambda (extdir)
  268. (let* ((name (file-name-nondirectory extdir))
  269. (dir (file-name-as-directory extdir))
  270. (path (concat prefix "extensions/" name "/"))
  271. (base (format "deck.%s." name)))
  272. (when (and (or (eq nil include) (member name include))
  273. (not (member name exclude)))
  274. (when (file-exists-p (concat dir base "js"))
  275. (add-to-list 'scripts (concat path base "js")))
  276. (when (file-exists-p (concat dir base "css"))
  277. (add-to-list 'sheets (concat path base "css")))
  278. (when (file-exists-p (concat dir base "html"))
  279. (add-to-list 'snippets (concat dir base "html"))))))
  280. (org-deck--find-extensions))
  281. (if (not (string-match-p "^[[:space:]]*$" theme))
  282. (add-to-list 'sheets
  283. (if (file-name-directory theme) theme
  284. (format "%sthemes/style/%s" prefix theme))))
  285. (if (not (string-match-p "^[[:space:]]*$" transition))
  286. (add-to-list
  287. 'sheets
  288. (if (file-name-directory transition) transition
  289. (format "%sthemes/transition/%s" prefix transition))))
  290. (list :scripts (nreverse scripts) :sheets (nreverse sheets)
  291. :snippets snippets)))
  292. (defun org-deck-inner-template (contents info)
  293. "Return body of document string after HTML conversion.
  294. CONTENTS is the transcoded contents string. INFO is a plist
  295. holding export options."
  296. (concat contents "\n"))
  297. (defun org-deck-headline (headline contents info)
  298. (let ((org-html-toplevel-hlevel 2)
  299. (class (or (org-element-property :HTML_CONTAINER_CLASS headline) ""))
  300. (level (org-export-get-relative-level headline info)))
  301. (when (and (= 1 level) (not (string-match-p "\\<slide\\>" class)))
  302. (org-element-put-property headline :HTML_CONTAINER_CLASS (concat class " slide")))
  303. (org-html-headline headline contents info)))
  304. (defun org-deck-item (item contents info)
  305. "Transcode an ITEM element from Org to HTML.
  306. CONTENTS holds the contents of the item. INFO is a plist holding
  307. contextual information.
  308. If the containing headline has the property :slide, then
  309. the \"slide\" class will be added to the to the list element,
  310. which will make the list into a \"build\"."
  311. (let ((text (org-html-item item contents info)))
  312. (if (org-export-get-node-property :STEP item t)
  313. (replace-regexp-in-string "^<li>" "<li class='slide'>" text)
  314. text)))
  315. (defun org-deck-template-alist (info)
  316. (list
  317. `("title" . ,(car (plist-get info :title)))
  318. `("author" . ,(car (plist-get info :author)))
  319. `("email" . ,(plist-get info :email))
  320. `("date" . ,(nth 0 (plist-get info :date)))
  321. `("file" . ,(plist-get info :input-file))))
  322. (defun org-deck-template (contents info)
  323. "Return complete document string after HTML conversion.
  324. CONTENTS is the transcoded contents string. INFO is a plist
  325. holding export options."
  326. (let ((pkg-info (org-deck--get-packages info)))
  327. (mapconcat
  328. 'identity
  329. (list
  330. "<!DOCTYPE html>"
  331. (let ((lang (plist-get info :language)))
  332. (mapconcat
  333. (lambda (x)
  334. (apply
  335. 'format
  336. "<!--%s <html class='no-js %s' lang='%s'> %s<![endif]-->"
  337. x))
  338. (list `("[if lt IE 7]>" "ie6" ,lang "")
  339. `("[if IE 7]>" "ie7" ,lang "")
  340. `("[if IE 8]>" "ie8" ,lang "")
  341. `("[if gt IE 8]><!-->" "" ,lang "<!--")) "\n"))
  342. "<head>"
  343. (org-deck--build-meta-info info)
  344. (mapconcat
  345. (lambda (sheet)
  346. (format
  347. "<link rel='stylesheet' href='%s' type='text/css' />" sheet))
  348. (plist-get pkg-info :sheets) "\n")
  349. "<style type='text/css'>"
  350. "#table-of-contents a {color: inherit;}"
  351. "#table-of-contents ul {margin-bottom: 0;}"
  352. (when (plist-get info :section-numbers)
  353. "#table-of-contents ul li {list-style-type: none;}")
  354. "</style>"
  355. ""
  356. (mapconcat
  357. (lambda (script)
  358. (format
  359. "<script src='%s' type='text/javascript'></script>" script))
  360. (plist-get pkg-info :scripts) "\n")
  361. (org-html--build-mathjax-config info)
  362. "<script type='text/javascript'>"
  363. " $(document).ready(function () { $.deck('.slide'); });"
  364. "</script>"
  365. (org-html--build-head info)
  366. org-deck-title-page-style
  367. "</head>"
  368. "<body>"
  369. "<header class='deck-status'>"
  370. (org-fill-template
  371. org-deck-header-template (org-deck-template-alist info))
  372. "</header>"
  373. "<div class='deck-container'>"
  374. ;; title page
  375. (org-fill-template
  376. org-deck-title-page-template (org-deck-template-alist info))
  377. ;; toc page
  378. (let ((depth (plist-get info :with-toc)))
  379. (when depth (org-deck-toc depth info)))
  380. contents
  381. (mapconcat
  382. (lambda (snippet)
  383. (with-temp-buffer (insert-file-contents snippet)
  384. (buffer-string)))
  385. (plist-get pkg-info :snippets) "\n")
  386. "<footer class='deck-status'>"
  387. (org-fill-template
  388. org-deck-footer-template (org-deck-template-alist info))
  389. "</footer>"
  390. "</div>"
  391. "</body>"
  392. "</html>\n") "\n")))
  393. (defun org-deck--build-meta-info (info)
  394. "Return meta tags for exported document.
  395. INFO is a plist used as a communication channel."
  396. (let* ((title (org-export-data (plist-get info :title) info))
  397. (author (and (plist-get info :with-author)
  398. (let ((auth (plist-get info :author)))
  399. (and auth (org-export-data auth info)))))
  400. (date (and (plist-get info :with-date)
  401. (let ((date (plist-get info :date)))
  402. (and date (org-export-data date info)))))
  403. (description (plist-get info :description))
  404. (keywords (plist-get info :keywords)))
  405. (mapconcat
  406. 'identity
  407. (list
  408. (format "<title>%s</title>" title)
  409. (format "<meta charset='%s' />"
  410. (or (and org-html-coding-system
  411. (fboundp 'coding-system-get)
  412. (coding-system-get
  413. org-html-coding-system 'mime-charset))
  414. "iso-8859-1"))
  415. (mapconcat
  416. (lambda (attr)
  417. (when (< 0 (length (car attr)))
  418. (format "<meta name='%s' content='%s'/>\n"
  419. (nth 1 attr) (car attr))))
  420. (list '("Org-mode" "generator")
  421. `(,author "author")
  422. `(,description "description")
  423. `(,keywords "keywords")) "")) "\n")))
  424. (defun org-deck-export-as-html
  425. (&optional async subtreep visible-only body-only ext-plist)
  426. "Export current buffer to an HTML buffer.
  427. If narrowing is active in the current buffer, only export its
  428. narrowed part.
  429. If a region is active, export that region.
  430. A non-nil optional argument ASYNC means the process should happen
  431. asynchronously. The resulting buffer should be accessible
  432. through the `org-export-stack' interface.
  433. When optional argument SUBTREEP is non-nil, export the sub-tree
  434. at point, extracting information from the headline properties
  435. first.
  436. When optional argument VISIBLE-ONLY is non-nil, don't export
  437. contents of hidden elements.
  438. When optional argument BODY-ONLY is non-nil, only write code
  439. between \"<body>\" and \"</body>\" tags.
  440. EXT-PLIST, when provided, is a property list with external
  441. parameters overriding Org default settings, but still inferior to
  442. file-local settings.
  443. Export is done in a buffer named \"*Org deck.js Export*\", which
  444. will be displayed when `org-export-show-temporary-export-buffer'
  445. is non-nil."
  446. (interactive)
  447. (if async
  448. (org-export-async-start
  449. (lambda (output)
  450. (with-current-buffer (get-buffer-create "*Org deck.js Export*")
  451. (erase-buffer)
  452. (insert output)
  453. (goto-char (point-min))
  454. (nxml-mode)
  455. (org-export-add-to-stack (current-buffer) 'deck)))
  456. `(org-export-as 'deck ,subtreep ,visible-only ,body-only ',ext-plist))
  457. (let ((outbuf (org-export-to-buffer
  458. 'deck "*Org deck.js Export*"
  459. subtreep visible-only body-only ext-plist)))
  460. ;; Set major mode.
  461. (with-current-buffer outbuf (nxml-mode))
  462. (when org-export-show-temporary-export-buffer
  463. (switch-to-buffer-other-window outbuf)))))
  464. (defun org-deck-export-to-html
  465. (&optional async subtreep visible-only body-only ext-plist)
  466. "Export current buffer to a deck.js HTML file.
  467. If narrowing is active in the current buffer, only export its
  468. narrowed part.
  469. If a region is active, export that region.
  470. A non-nil optional argument ASYNC means the process should happen
  471. asynchronously. The resulting file should be accessible through
  472. the `org-export-stack' interface.
  473. When optional argument SUBTREEP is non-nil, export the sub-tree
  474. at point, extracting information from the headline properties
  475. first.
  476. When optional argument VISIBLE-ONLY is non-nil, don't export
  477. contents of hidden elements.
  478. When optional argument BODY-ONLY is non-nil, only write code
  479. between \"<body>\" and \"</body>\" tags.
  480. EXT-PLIST, when provided, is a property list with external
  481. parameters overriding Org default settings, but still inferior to
  482. file-local settings.
  483. Return output file's name."
  484. (interactive)
  485. (let* ((extension (concat "." org-html-extension))
  486. (file (org-export-output-file-name extension subtreep))
  487. (org-export-coding-system org-html-coding-system))
  488. (if async
  489. (org-export-async-start
  490. (lambda (f) (org-export-add-to-stack f 'deck))
  491. (let ((org-export-coding-system org-html-coding-system))
  492. `(expand-file-name
  493. (org-export-to-file
  494. 'deck ,file ,subtreep ,visible-only ,body-only ',ext-plist))))
  495. (let ((org-export-coding-system org-html-coding-system))
  496. (org-export-to-file
  497. 'deck file subtreep visible-only body-only ext-plist)))))
  498. (defun org-deck-publish-to-html (plist filename pub-dir)
  499. "Publish an org file to deck.js HTML Presentation.
  500. FILENAME is the filename of the Org file to be published. PLIST
  501. is the property list for the given project. PUB-DIR is the
  502. publishing directory. Returns output file name."
  503. (org-publish-org-to 'deck filename ".html" plist pub-dir))
  504. (provide 'ox-deck)