ox-deck.el 20 KB

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