ox-jsinfo.el 9.4 KB

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