ox-jsinfo.el 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. ;;; ox-jsinfo.el --- Org HTML Export Extension for org-info.js
  2. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file implements the support for Sebastian Rose's JavaScript
  23. ;; org-info.js to display an Org mode file exported to HTML in an
  24. ;; Info-like way, or using folding similar to the outline structure of
  25. ;; Org mode itself.
  26. ;; Documentation for using this module is in the Org manual. The
  27. ;; script itself is documented by Sebastian Rose in a file distributed
  28. ;; with the script. FIXME: Accurate pointers!
  29. ;;; Code:
  30. (require 'ox-html)
  31. (add-to-list 'org-export-filter-options-functions 'org-infojs-install-script)
  32. (defgroup org-export-infojs nil
  33. "Options specific for using org-info.js in HTML export."
  34. :tag "Org Export HTML INFOJS"
  35. :group 'org-export-html)
  36. (defcustom org-export-html-use-infojs 'when-configured
  37. "Non-nil when Sebastian Rose's Java Script org-info.js should be active.
  38. This option can be nil or t to never or always use the script.
  39. It can also be the symbol `when-configured', meaning that the
  40. script will be linked into the export file if and only if there
  41. is a \"#+INFOJS_OPT:\" line in the buffer. See also the variable
  42. `org-infojs-options'."
  43. :group 'org-export-html
  44. :group 'org-export-infojs
  45. :type '(choice
  46. (const :tag "Never" nil)
  47. (const :tag "When configured in buffer" when-configured)
  48. (const :tag "Always" t)))
  49. (defconst org-infojs-opts-table
  50. '((path PATH "http://orgmode.org/org-info.js")
  51. (view VIEW "info")
  52. (toc TOC :with-toc)
  53. (ftoc FIXED_TOC "0")
  54. (tdepth TOC_DEPTH "max")
  55. (sdepth SECTION_DEPTH "max")
  56. (mouse MOUSE_HINT "underline")
  57. (buttons VIEW_BUTTONS "0")
  58. (ltoc LOCAL_TOC "1")
  59. (up LINK_UP :html-link-up)
  60. (home LINK_HOME :html-link-home))
  61. "JavaScript options, long form for script, default values.")
  62. (defvar org-infojs-options)
  63. (when (and (boundp 'org-infojs-options)
  64. (assq 'runs org-infojs-options))
  65. (setq org-infojs-options (delq (assq 'runs org-infojs-options)
  66. org-infojs-options)))
  67. (defcustom org-infojs-options
  68. (mapcar (lambda (x) (cons (car x) (nth 2 x))) org-infojs-opts-table)
  69. "Options settings for the INFOJS JavaScript.
  70. Each of the options must have an entry in `org-export-html/infojs-opts-table'.
  71. The value can either be a string that will be passed to the script, or
  72. a property. This property is then assumed to be a property that is defined
  73. by the Export/Publishing setup of Org.
  74. The `sdepth' and `tdepth' parameters can also be set to \"max\", which
  75. means to use the maximum value consistent with other options."
  76. :group 'org-export-infojs
  77. :type
  78. `(set :greedy t :inline t
  79. ,@(mapcar
  80. (lambda (x)
  81. (list 'cons (list 'const (car x))
  82. '(choice
  83. (symbol :tag "Publishing/Export property")
  84. (string :tag "Value"))))
  85. org-infojs-opts-table)))
  86. (defcustom org-infojs-template
  87. "<script type=\"text/javascript\" src=\"%SCRIPT_PATH\">
  88. /**
  89. *
  90. * @source: %SCRIPT_PATH
  91. *
  92. * @licstart The following is the entire license notice for the
  93. * JavaScript code in %SCRIPT_PATH.
  94. *
  95. * Copyright (C) 2012-2013 Sebastian Rose
  96. *
  97. *
  98. * The JavaScript code in this tag is free software: you can
  99. * redistribute it and/or modify it under the terms of the GNU
  100. * General Public License (GNU GPL) as published by the Free Software
  101. * Foundation, either version 3 of the License, or (at your option)
  102. * any later version. The code is distributed WITHOUT ANY WARRANTY;
  103. * without even the implied warranty of MERCHANTABILITY or FITNESS
  104. * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  105. *
  106. * As additional permission under GNU GPL version 3 section 7, you
  107. * may distribute non-source (e.g., minimized or compacted) forms of
  108. * that code without the copy of the GNU GPL normally required by
  109. * section 4, provided you include this license notice and a URL
  110. * through which recipients can access the Corresponding Source.
  111. *
  112. * @licend The above is the entire license notice
  113. * for the JavaScript code in %SCRIPT_PATH.
  114. *
  115. */
  116. </script>
  117. <script type=\"text/javascript\">
  118. /*
  119. @licstart The following is the entire license notice for the
  120. JavaScript code in this tag.
  121. Copyright (C) 2012-2013 Free Software Foundation, Inc.
  122. The JavaScript code in this tag is free software: you can
  123. redistribute it and/or modify it under the terms of the GNU
  124. General Public License (GNU GPL) as published by the Free Software
  125. Foundation, either version 3 of the License, or (at your option)
  126. any later version. The code is distributed WITHOUT ANY WARRANTY;
  127. without even the implied warranty of MERCHANTABILITY or FITNESS
  128. FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  129. As additional permission under GNU GPL version 3 section 7, you
  130. may distribute non-source (e.g., minimized or compacted) forms of
  131. that code without the copy of the GNU GPL normally required by
  132. section 4, provided you include this license notice and a URL
  133. through which recipients can access the Corresponding Source.
  134. @licend The above is the entire license notice
  135. for the JavaScript code in this tag.
  136. */
  137. <!--/*--><![CDATA[/*><!--*/
  138. %MANAGER_OPTIONS
  139. org_html_manager.setup(); // activate after the parameters are set
  140. /*]]>*///-->
  141. </script>"
  142. "The template for the export style additions when org-info.js is used.
  143. Option settings will replace the %MANAGER-OPTIONS cookie."
  144. :group 'org-infojs
  145. :type 'string)
  146. (defun org-infojs-install-script (exp-plist backend)
  147. "Install script in export options when appropriate.
  148. EXP-PLIST is a plist containing export options. BACKEND is the
  149. export back-end currently used."
  150. (unless (or (not (org-export-derived-backend-p backend 'html))
  151. (not org-export-html-use-infojs)
  152. (and (eq org-export-html-use-infojs 'when-configured)
  153. (or (not (plist-get exp-plist :infojs-opt))
  154. (string-match "\\<view:nil\\>"
  155. (plist-get exp-plist :infojs-opt)))))
  156. (let* ((template org-infojs-template)
  157. (ptoc (plist-get exp-plist :with-toc))
  158. (hlevels (plist-get exp-plist :headline-levels))
  159. (sdepth hlevels)
  160. (tdepth (if (integerp ptoc) (min ptoc hlevels) hlevels))
  161. (options (plist-get exp-plist :infojs-opt))
  162. (table org-infojs-opts-table)
  163. style)
  164. (dolist (entry table)
  165. (let* ((opt (car entry))
  166. (var (nth 1 entry))
  167. ;; Compute default values for script option OPT from
  168. ;; `org-infojs-options' variable.
  169. (default
  170. (let ((default (cdr (assq opt org-infojs-options))))
  171. (if (and (symbolp default) (not (memq default '(t nil))))
  172. (plist-get exp-plist default)
  173. default)))
  174. ;; Value set through INFOJS_OPT keyword has precedence
  175. ;; over the default one.
  176. (val (if (and options
  177. (string-match (format "\\<%s:\\(\\S-+\\)" opt)
  178. options))
  179. (match-string 1 options)
  180. default)))
  181. (case opt
  182. (path (setq template
  183. (replace-regexp-in-string
  184. "%SCRIPT_PATH" val template t t)))
  185. (sdepth (when (integerp (read val))
  186. (setq sdepth (min (read val) sdepth))))
  187. (tdepth (when (integerp (read val))
  188. (setq tdepth (min (read val) tdepth))))
  189. (otherwise (setq val
  190. (cond
  191. ((or (eq val t) (equal val "t")) "1")
  192. ((or (eq val nil) (equal val "nil")) "0")
  193. ((stringp val) val)
  194. (t (format "%s" val))))
  195. (push (cons var val) style)))))
  196. ;; Now we set the depth of the *generated* TOC to SDEPTH,
  197. ;; because the toc will actually determine the splitting. How
  198. ;; much of the toc will actually be displayed is governed by the
  199. ;; TDEPTH option.
  200. (setq exp-plist (plist-put exp-plist :with-toc sdepth))
  201. ;; The table of contents should not show more sections than we
  202. ;; generate.
  203. (setq tdepth (min tdepth sdepth))
  204. (push (cons "TOC_DEPTH" tdepth) style)
  205. ;; Build style string.
  206. (setq style (mapconcat
  207. (lambda (x) (format "org_html_manager.set(\"%s\", \"%s\");"
  208. (car x)
  209. (cdr x)))
  210. style "\n"))
  211. (when (and style (> (length style) 0))
  212. (and (string-match "%MANAGER_OPTIONS" template)
  213. (setq style (replace-match style t t template))
  214. (setq exp-plist
  215. (plist-put
  216. exp-plist :html-style-extra
  217. (concat (or (plist-get exp-plist :html-style-extra) "")
  218. "\n"
  219. style)))))
  220. ;; This script absolutely needs the table of contents, so we
  221. ;; change that setting.
  222. (unless (plist-get exp-plist :with-toc)
  223. (setq exp-plist (plist-put exp-plist :with-toc t)))
  224. ;; Return the modified property list.
  225. exp-plist)))
  226. (provide 'ox-infojs)
  227. (provide 'ox-jsinfo)
  228. ;; Local variables:
  229. ;; generated-autoload-file: "org-loaddefs.el"
  230. ;; End:
  231. ;;; ox-jsinfo.el ends here