ox-deck.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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-style-extra "HTML_STYLE" nil org-html-style-extra newline)
  47. (:html-style-include-default "HTML_INCLUDE_DEFAULT" nil nil)
  48. (:html-style-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. (add-to-list 'sheets
  283. (if (file-name-directory theme) theme
  284. (format "%sthemes/style/%s" prefix theme)))
  285. (add-to-list
  286. 'sheets
  287. (if (file-name-directory transition) transition
  288. (format "%sthemes/transition/%s" prefix transition)))
  289. (list :scripts (nreverse scripts) :sheets (nreverse sheets)
  290. :snippets snippets)))
  291. (defun org-deck-inner-template (contents info)
  292. "Return body of document string after HTML conversion.
  293. CONTENTS is the transcoded contents string. INFO is a plist
  294. holding export options."
  295. (concat contents "\n"))
  296. (defun org-deck-headline (headline contents info)
  297. (let ((org-html-toplevel-hlevel 2)
  298. (class (or (org-element-property :HTML_CONTAINER_CLASS headline) ""))
  299. (level (org-export-get-relative-level headline info)))
  300. (when (and (= 1 level) (not (string-match-p "\\<slide\\>" class)))
  301. (org-element-put-property headline :HTML_CONTAINER_CLASS (concat class " slide")))
  302. (org-html-headline headline contents info)))
  303. (defun org-deck-item (item contents info)
  304. "Transcode an ITEM element from Org to HTML.
  305. CONTENTS holds the contents of the item. INFO is a plist holding
  306. contextual information.
  307. If the containing headline has the property :slide, then
  308. the \"slide\" class will be added to the to the list element,
  309. which will make the list into a \"build\"."
  310. (let ((text (org-html-item item contents info)))
  311. (if (org-export-get-node-property :STEP item t)
  312. (replace-regexp-in-string "^<li>" "<li class='slide'>" text)
  313. text)))
  314. (defun org-deck-template-alist (info)
  315. (list
  316. `("title" . ,(car (plist-get info :title)))
  317. `("author" . ,(car (plist-get info :author)))
  318. `("email" . ,(plist-get info :email))
  319. `("date" . ,(nth 0 (plist-get info :date)))
  320. `("file" . ,(plist-get info :input-file))))
  321. (defun org-deck-template (contents info)
  322. "Return complete document string after HTML conversion.
  323. CONTENTS is the transcoded contents string. INFO is a plist
  324. holding export options."
  325. (let ((pkg-info (org-deck--get-packages info)))
  326. (mapconcat
  327. 'identity
  328. (list
  329. "<!DOCTYPE html>"
  330. (let ((lang (plist-get info :language)))
  331. (mapconcat
  332. (lambda (x)
  333. (apply
  334. 'format
  335. "<!--%s <html class='no-js %s' lang='%s'> %s<![endif]-->"
  336. x))
  337. (list `("[if lt IE 7]>" "ie6" ,lang "")
  338. `("[if IE 7]>" "ie7" ,lang "")
  339. `("[if IE 8]>" "ie8" ,lang "")
  340. `("[if gt IE 8]><!-->" "" ,lang "<!--")) "\n"))
  341. "<head>"
  342. (org-deck--build-meta-info info)
  343. (mapconcat
  344. (lambda (sheet)
  345. (format
  346. "<link rel='stylesheet' href='%s' type='text/css' />" sheet))
  347. (plist-get pkg-info :sheets) "\n")
  348. "<style type='text/css'>"
  349. "#table-of-contents a {color: inherit;}"
  350. "#table-of-contents ul {margin-bottom: 0;}"
  351. (when (plist-get info :section-numbers)
  352. "#table-of-contents ul li {list-style-type: none;}")
  353. "</style>"
  354. ""
  355. (mapconcat
  356. (lambda (script)
  357. (format
  358. "<script src='%s' type='text/javascript'></script>" script))
  359. (plist-get pkg-info :scripts) "\n")
  360. (org-html--build-mathjax-config info)
  361. "<script type='text/javascript'>"
  362. " $(document).ready(function () { $.deck('.slide'); });"
  363. "</script>"
  364. (org-html--build-style info)
  365. org-deck-title-page-style
  366. "</head>"
  367. "<body>"
  368. "<header class='deck-status'>"
  369. (org-fill-template
  370. org-deck-header-template (org-deck-template-alist info))
  371. "</header>"
  372. "<div class='deck-container'>"
  373. ;; title page
  374. (org-fill-template
  375. org-deck-title-page-template (org-deck-template-alist info))
  376. ;; toc page
  377. (let ((depth (plist-get info :with-toc)))
  378. (when depth (org-deck-toc depth info)))
  379. contents
  380. (mapconcat
  381. (lambda (snippet)
  382. (with-temp-buffer (insert-file-contents snippet)
  383. (buffer-string)))
  384. (plist-get pkg-info :snippets) "\n")
  385. "<footer class='deck-status'>"
  386. (org-fill-template
  387. org-deck-footer-template (org-deck-template-alist info))
  388. "</footer>"
  389. "</div>"
  390. "</body>"
  391. "</html>\n") "\n")))
  392. (defun org-deck--build-meta-info (info)
  393. "Return meta tags for exported document.
  394. INFO is a plist used as a communication channel."
  395. (let* ((title (org-export-data (plist-get info :title) info))
  396. (author (and (plist-get info :with-author)
  397. (let ((auth (plist-get info :author)))
  398. (and auth (org-export-data auth info)))))
  399. (date (and (plist-get info :with-date)
  400. (let ((date (plist-get info :date)))
  401. (and date (org-export-data date info)))))
  402. (description (plist-get info :description))
  403. (keywords (plist-get info :keywords)))
  404. (mapconcat
  405. 'identity
  406. (list
  407. (format "<title>%s</title>" title)
  408. (format "<meta charset='%s' />"
  409. (or (and org-html-coding-system
  410. (fboundp 'coding-system-get)
  411. (coding-system-get
  412. org-html-coding-system 'mime-charset))
  413. "iso-8859-1"))
  414. (mapconcat
  415. (lambda (attr)
  416. (when (< 0 (length (car attr)))
  417. (format "<meta name='%s' content='%s'/>\n"
  418. (nth 1 attr) (car attr))))
  419. (list '("Org-mode" "generator")
  420. `(,author "author")
  421. `(,description "description")
  422. `(,keywords "keywords")) "")) "\n")))
  423. (defun org-deck-export-as-html
  424. (&optional async subtreep visible-only body-only ext-plist)
  425. "Export current buffer to an HTML buffer.
  426. If narrowing is active in the current buffer, only export its
  427. narrowed part.
  428. If a region is active, export that region.
  429. A non-nil optional argument ASYNC means the process should happen
  430. asynchronously. The resulting buffer should be accessible
  431. through the `org-export-stack' interface.
  432. When optional argument SUBTREEP is non-nil, export the sub-tree
  433. at point, extracting information from the headline properties
  434. first.
  435. When optional argument VISIBLE-ONLY is non-nil, don't export
  436. contents of hidden elements.
  437. When optional argument BODY-ONLY is non-nil, only write code
  438. between \"<body>\" and \"</body>\" tags.
  439. EXT-PLIST, when provided, is a property list with external
  440. parameters overriding Org default settings, but still inferior to
  441. file-local settings.
  442. Export is done in a buffer named \"*Org deck.js Export*\", which
  443. will be displayed when `org-export-show-temporary-export-buffer'
  444. is non-nil."
  445. (interactive)
  446. (if async
  447. (org-export-async-start
  448. (lambda (output)
  449. (with-current-buffer (get-buffer-create "*Org deck.js Export*")
  450. (erase-buffer)
  451. (insert output)
  452. (goto-char (point-min))
  453. (nxml-mode)
  454. (org-export-add-to-stack (current-buffer) 'deck)))
  455. `(org-export-as 'deck ,subtreep ,visible-only ,body-only ',ext-plist))
  456. (let ((outbuf (org-export-to-buffer
  457. 'deck "*Org deck.js Export*"
  458. subtreep visible-only body-only ext-plist)))
  459. ;; Set major mode.
  460. (with-current-buffer outbuf (nxml-mode))
  461. (when org-export-show-temporary-export-buffer
  462. (switch-to-buffer-other-window outbuf)))))
  463. (defun org-deck-export-to-html
  464. (&optional async subtreep visible-only body-only ext-plist)
  465. "Export current buffer to a deck.js HTML file.
  466. If narrowing is active in the current buffer, only export its
  467. narrowed part.
  468. If a region is active, export that region.
  469. A non-nil optional argument ASYNC means the process should happen
  470. asynchronously. The resulting file should be accessible through
  471. the `org-export-stack' interface.
  472. When optional argument SUBTREEP is non-nil, export the sub-tree
  473. at point, extracting information from the headline properties
  474. first.
  475. When optional argument VISIBLE-ONLY is non-nil, don't export
  476. contents of hidden elements.
  477. When optional argument BODY-ONLY is non-nil, only write code
  478. between \"<body>\" and \"</body>\" tags.
  479. EXT-PLIST, when provided, is a property list with external
  480. parameters overriding Org default settings, but still inferior to
  481. file-local settings.
  482. Return output file's name."
  483. (interactive)
  484. (let* ((extension (concat "." org-html-extension))
  485. (file (org-export-output-file-name extension subtreep))
  486. (org-export-coding-system org-html-coding-system))
  487. (if async
  488. (org-export-async-start
  489. (lambda (f) (org-export-add-to-stack f 'deck))
  490. (let ((org-export-coding-system org-html-coding-system))
  491. `(expand-file-name
  492. (org-export-to-file
  493. 'deck ,file ,subtreep ,visible-only ,body-only ',ext-plist))))
  494. (let ((org-export-coding-system org-html-coding-system))
  495. (org-export-to-file
  496. 'deck file subtreep visible-only body-only ext-plist)))))
  497. (defun org-deck-publish-to-html (plist filename pub-dir)
  498. "Publish an org file to deck.js HTML Presentation.
  499. FILENAME is the filename of the Org file to be published. PLIST
  500. is the property list for the given project. PUB-DIR is the
  501. publishing directory. Returns output file name."
  502. (org-publish-org-to 'deck filename ".html" plist pub-dir))
  503. (provide 'ox-deck)