org-jsinfo.el 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. ;;; org-jsinfo.el --- Support for org-info.js Javascript in Org HTML export
  2. ;; Copyright (C) 2004-2012 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\"></script>
  92. <script type=\"text/javascript\" >
  93. <!--/*--><![CDATA[/*><!--*/
  94. %MANAGER_OPTIONS
  95. org_html_manager.setup(); // activate after the parameters are set
  96. /*]]>*///-->
  97. </script>"
  98. "The template for the export style additions when org-info.js is used.
  99. Option settings will replace the %MANAGER-OPTIONS cookie."
  100. :group 'org-infojs
  101. :type 'string)
  102. (defun org-infojs-handle-options (exp-plist)
  103. "Analyze JavaScript options in INFO-PLIST and modify EXP-PLIST accordingly."
  104. (if (or (not org-export-html-use-infojs)
  105. (and (eq org-export-html-use-infojs 'when-configured)
  106. (or (not (plist-get exp-plist :infojs-opt))
  107. (string-match "\\<view:nil\\>"
  108. (plist-get exp-plist :infojs-opt)))))
  109. ;; We do not want to use the script
  110. exp-plist
  111. ;; We do want to use the script, set it up
  112. (let ((template org-infojs-template)
  113. (ptoc (plist-get exp-plist :table-of-contents))
  114. (hlevels (plist-get exp-plist :headline-levels))
  115. tdepth sdepth s v e opt var val table default)
  116. (setq sdepth hlevels
  117. tdepth hlevels)
  118. (if (integerp ptoc) (setq tdepth (min ptoc tdepth)))
  119. (setq v (plist-get exp-plist :infojs-opt)
  120. table org-infojs-opts-table)
  121. (while (setq e (pop table))
  122. (setq opt (car e) var (nth 1 e)
  123. default (cdr (assoc opt org-infojs-options)))
  124. (and (symbolp default) (not (memq default '(t nil)))
  125. (setq default (plist-get exp-plist default)))
  126. (if (and v (string-match (format " %s:\\(\\S-+\\)" opt) v))
  127. (setq val (match-string 1 v))
  128. (setq val default))
  129. (cond
  130. ((eq opt 'path)
  131. (and (string-match "%SCRIPT_PATH" template)
  132. (setq template (replace-match val t t template))))
  133. ((eq opt 'sdepth)
  134. (if (integerp (read val))
  135. (setq sdepth (min (read val) hlevels))))
  136. ((eq opt 'tdepth)
  137. (if (integerp (read val))
  138. (setq tdepth (min (read val) hlevels))))
  139. (t
  140. (setq val
  141. (cond
  142. ((or (eq val t) (equal val "t")) "1")
  143. ((or (eq val nil) (equal val "nil")) "0")
  144. ((stringp val) val)
  145. (t (format "%s" val))))
  146. (push (cons var val) s))))
  147. ;; Now we set the depth of the *generated* TOC to SDEPTH, because the
  148. ;; toc will actually determine the splitting. How much of the toc will
  149. ;; actually be displayed is governed by the TDEPTH option.
  150. (setq exp-plist (plist-put exp-plist :table-of-contents sdepth))
  151. ;; The table of contents should not show more sections then we generate
  152. (setq tdepth (min tdepth sdepth))
  153. (push (cons "TOC_DEPTH" tdepth) s)
  154. (setq s (mapconcat
  155. (lambda (x) (format "org_html_manager.set(\"%s\", \"%s\");"
  156. (car x) (cdr x)))
  157. s "\n"))
  158. (when (and s (> (length s) 0))
  159. (and (string-match "%MANAGER_OPTIONS" template)
  160. (setq s (replace-match s t t template))
  161. (setq exp-plist
  162. (plist-put
  163. exp-plist :style-extra
  164. (concat (or (plist-get exp-plist :style-extra) "") "\n" s)))))
  165. ;; This script absolutely needs the table of contents, to we change that
  166. ;; setting
  167. (if (not (plist-get exp-plist :table-of-contents))
  168. (setq exp-plist (plist-put exp-plist :table-of-contents t)))
  169. ;; Return the modified property list
  170. exp-plist)))
  171. (defun org-infojs-options-inbuffer-template ()
  172. (format "#+INFOJS_OPT: view:%s toc:%s ltoc:%s mouse:%s buttons:%s path:%s"
  173. (if (eq t org-export-html-use-infojs) (cdr (assoc 'view org-infojs-options)) nil)
  174. (let ((a (cdr (assoc 'toc org-infojs-options))))
  175. (cond ((memq a '(nil t)) a)
  176. (t (plist-get (org-infile-export-plist) :table-of-contents))))
  177. (if (equal (cdr (assoc 'ltoc org-infojs-options)) "1") t nil)
  178. (cdr (assoc 'mouse org-infojs-options))
  179. (cdr (assoc 'buttons org-infojs-options))
  180. (cdr (assoc 'path org-infojs-options))))
  181. (provide 'org-infojs)
  182. (provide 'org-jsinfo)
  183. ;;; org-jsinfo.el ends here