ox-deck.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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-postamble nil "html-postamble" nil t)
  44. (:html-preamble nil "html-preamble" nil t)
  45. (:html-head-include-default-style "HTML_INCLUDE_DEFAULT_STYLE" nil nil)
  46. (:html-head-include-scripts "HTML_INCLUDE_SCRIPTS" nil nil)
  47. (:deck-base-url "DECK_BASE_URL" nil org-deck-base-url)
  48. (:deck-theme "DECK_THEME" nil org-deck-theme)
  49. (:deck-transition "DECK_TRANSITION" nil org-deck-transition)
  50. (:deck-include-extensions "DECK_INCLUDE_EXTENSIONS" nil
  51. org-deck-include-extensions split)
  52. (:deck-exclude-extensions "DECK_EXCLUDE_EXTENSIONS" nil
  53. org-deck-exclude-extensions split))
  54. :translate-alist
  55. ((headline . org-deck-headline)
  56. (inner-template . org-deck-inner-template)
  57. (item . org-deck-item)
  58. (template . org-deck-template)))
  59. (defgroup org-export-deck nil
  60. "Options for exporting Org mode files to deck.js HTML Presentations."
  61. :tag "Org Export DECK"
  62. :group 'org-export-html)
  63. (defcustom org-deck-directories '("./deck.js")
  64. "Directories to search for deck.js components (jquery,
  65. modernizr; core, extensions and themes directories.)"
  66. :group 'org-export-deck
  67. :type '(repeat (string :tag "Directory")))
  68. (defun org-deck--cleanup-components (components)
  69. (remove-duplicates
  70. (car (remove 'nil components))
  71. :test (lambda (x y)
  72. (string= (file-name-nondirectory x)
  73. (file-name-nondirectory y)))))
  74. (defun org-deck--find-extensions ()
  75. "Returns a unique list of all extensions found in
  76. in the extensions directories under `org-deck-directories'"
  77. (org-deck--cleanup-components
  78. (mapcar ; extensions under existing dirs
  79. (lambda (dir)
  80. (when (file-directory-p dir) (directory-files dir t "^[^.]")))
  81. (mapcar ; possible extension directories
  82. (lambda (x) (expand-file-name "extensions" x))
  83. org-deck-directories))))
  84. (defun org-deck--find-css (type)
  85. "Return a unique list of all the css stylesheets in the themes/TYPE
  86. directories under `org-deck-directories'."
  87. (org-deck--cleanup-components
  88. (mapcar
  89. (lambda (dir)
  90. (let ((css-dir (expand-file-name
  91. (concat (file-name-as-directory "themes") type) dir)))
  92. (when (file-directory-p css-dir)
  93. (directory-files css-dir t "\\.css$"))))
  94. org-deck-directories)))
  95. (defun org-deck-list-components ()
  96. "List all available deck extensions, styles and
  97. transitions (with full paths) to a temporary buffer."
  98. (interactive)
  99. (let ((outbuf (get-buffer-create "*deck.js Extensions*")))
  100. (with-current-buffer outbuf
  101. (erase-buffer)
  102. (insert "Extensions\n----------\n")
  103. (insert (mapconcat 'identity (org-deck--find-extensions) "\n"))
  104. (insert "\n\nStyles\n------\n")
  105. (insert (mapconcat 'identity (org-deck--find-css "style") "\n"))
  106. (insert "\n\nTransitions\n----------\n")
  107. (insert (mapconcat 'identity (org-deck--find-css "transition") "\n")))
  108. (switch-to-buffer-other-window outbuf)))
  109. (defcustom org-deck-include-extensions nil
  110. "If non-nil, list of extensions to include instead of all available.
  111. Can be overriden or set with the DECK_INCLUDE_EXTENSIONS property.
  112. During output generation, the extensions found by
  113. `org-deck--find-extensions' are searched for the appropriate
  114. files (scripts and/or stylesheets) to include in the generated
  115. html. The href/src attributes are created relative to `org-deck-base-url'."
  116. :group 'org-export-deck
  117. :type '(repeat (string :tag "Extension")))
  118. (defcustom org-deck-exclude-extensions nil
  119. "If non-nil, list of extensions to exclude.
  120. Can be overriden or set with the DECK_EXCLUDE_EXTENSIONS property."
  121. :group 'org-export-deck
  122. :type '(repeat (string :tag "Extension")))
  123. (defcustom org-deck-theme "swiss.css"
  124. "deck.js theme. Can be overriden with the DECK_THEME property.
  125. If this value contains a path component (\"/\"), it is used as a
  126. literal path (url). Otherwise it is prepended with
  127. `org-deck-base-url'/themes/style/."
  128. :group 'org-export-deck
  129. :type 'string)
  130. (defcustom org-deck-transition "fade.css"
  131. "deck.js transition theme. Can be overriden with the
  132. DECK_TRANSITION property.
  133. If this value contains a path component (\"/\"), it is used as a
  134. literal path (url). Otherwise it is prepended with
  135. `org-deck-base-url'/themes/transition/."
  136. :group 'org-export-deck
  137. :type 'string)
  138. (defcustom org-deck-base-url "deck.js"
  139. "Url prefix to deck.js base directory containing the core, extensions
  140. and themes directories.
  141. Can be overriden with the DECK_BASE_URL property."
  142. :group 'org-export-deck
  143. :type 'string)
  144. (defcustom org-deck-footer-template
  145. "<h1>%author - %title</h1>"
  146. "Format template to specify footer div.
  147. Completed using `org-fill-template'.
  148. Optional keys include %author, %email, %file, %title and %date.
  149. This is included in a <footer> section."
  150. :group 'org-export-deck
  151. :type 'string)
  152. (defcustom org-deck-header-template ""
  153. "Format template to specify page. Completed using `org-fill-template'.
  154. Optional keys include %author, %email, %file, %title and %date.
  155. This is included in a <header> section."
  156. :group 'org-export-deck
  157. :type 'string)
  158. (defcustom org-deck-title-page-style
  159. "<style type='text/css'>
  160. header, footer { left: 5px; width: 100% }
  161. header { position: absolute; top: 10px; }
  162. #title-slide h1 {
  163. position: static; padding: 0;
  164. margin-top: 10%;
  165. -webkit-transform: none;
  166. -moz-transform: none;
  167. -ms-transform: none;
  168. -o-transform: none;
  169. transform: none;
  170. }
  171. #title-slide h2 {
  172. text-align: center;
  173. border:none;
  174. padding: 0;
  175. margin: 0;
  176. }
  177. </style>"
  178. "CSS styles to use for title page"
  179. :group 'org-export-deck
  180. :type 'string)
  181. (defcustom org-deck-title-page-template
  182. "<div class='slide' id='title-slide'>
  183. <h1>%title</h1>
  184. <h2>%author</h2>
  185. <h2>%email</h2>
  186. <h2>%date</h2>
  187. </div>"
  188. "Format template to specify title page div.
  189. Completed using `org-fill-template'.
  190. Optional keys include %author, %email, %file, %title and %date.
  191. Note that the wrapper div must include the class \"slide\"."
  192. :group 'org-export-deck
  193. :type 'string)
  194. (defcustom org-deck-toc-style
  195. "<style type='text/css'>
  196. header, footer { left: 5px; width: 100% }
  197. header { position: absolute; top: 10px; }
  198. #table-of-contents h1 {
  199. position: static; padding: 0;
  200. margin-top: 10%;
  201. -webkit-transform: none;
  202. -moz-transform: none;
  203. -ms-transform: none;
  204. -o-transform: none;
  205. Transform: none;
  206. }
  207. #title-slide h2 {
  208. text-align: center;
  209. border:none;
  210. padding: 0;
  211. margin: 0;
  212. }
  213. </style>"
  214. "CSS styles to use for title page"
  215. :group 'org-export-deck
  216. :type 'string)
  217. (defun org-deck-toc (depth info)
  218. (concat
  219. "<div id=\"table-of-contents\" class=\"slide\">\n"
  220. (format "<h2>%s</h2>\n"
  221. (org-html--translate "Table of Contents" info))
  222. (org-html--toc-text
  223. (mapcar
  224. (lambda (headline)
  225. (let* ((class (org-element-property :HTML_CONTAINER_CLASS headline))
  226. (section-number
  227. (when
  228. (and (not (org-export-low-level-p headline info))
  229. (org-export-numbered-headline-p headline info))
  230. (concat
  231. (mapconcat
  232. 'number-to-string
  233. (org-export-get-headline-number headline info) ".") ". ")))
  234. (title
  235. (concat
  236. section-number
  237. (replace-regexp-in-string ; remove any links in headline...
  238. "</?a[^>]*>" ""
  239. (org-export-data
  240. (org-element-property :title headline) info)))))
  241. (cons
  242. (if (and class (string-match-p "\\<slide\\>" class))
  243. (format
  244. "<a href='#outline-container-%s'>%s</a>"
  245. (or (org-element-property :CUSTOM_ID headline)
  246. (mapconcat
  247. 'number-to-string
  248. (org-export-get-headline-number headline info) "-"))
  249. title)
  250. title)
  251. (org-export-get-relative-level headline info))))
  252. (org-export-collect-headlines info depth)))
  253. "</div>\n"))
  254. (defun org-deck--get-packages (info)
  255. (let ((prefix (concat (plist-get info :deck-base-url) "/"))
  256. (theme (plist-get info :deck-theme))
  257. (transition (plist-get info :deck-transition))
  258. (include (plist-get info :deck-include-extensions))
  259. (exclude (plist-get info :deck-exclude-extensions))
  260. (scripts '()) (sheets '()) (snippets '()))
  261. (add-to-list 'scripts (concat prefix "jquery-1.7.2.min.js"))
  262. (add-to-list 'scripts (concat prefix "core/deck.core.js"))
  263. (add-to-list 'scripts (concat prefix "modernizr.custom.js"))
  264. (add-to-list 'sheets (concat prefix "core/deck.core.css"))
  265. (mapc
  266. (lambda (extdir)
  267. (let* ((name (file-name-nondirectory extdir))
  268. (dir (file-name-as-directory extdir))
  269. (path (concat prefix "extensions/" name "/"))
  270. (base (format "deck.%s." name)))
  271. (when (and (or (eq nil include) (member name include))
  272. (not (member name exclude)))
  273. (when (file-exists-p (concat dir base "js"))
  274. (add-to-list 'scripts (concat path base "js")))
  275. (when (file-exists-p (concat dir base "css"))
  276. (add-to-list 'sheets (concat path base "css")))
  277. (when (file-exists-p (concat dir base "html"))
  278. (add-to-list 'snippets (concat dir base "html"))))))
  279. (org-deck--find-extensions))
  280. (if (not (string-match-p "^[[:space:]]*$" theme))
  281. (add-to-list 'sheets
  282. (if (file-name-directory theme) theme
  283. (format "%sthemes/style/%s" prefix theme))))
  284. (if (not (string-match-p "^[[:space:]]*$" transition))
  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-head 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)