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