org-jsinfo.el 9.7 KB

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