org-jsinfo.el 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. ;;; org-jsinfo.el --- Support for org-info.js Javascript in Org HTML export
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 6.35e
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file implements the support for Sebastian Rose's Javascript
  25. ;; org-info.js to display an org-mode file exported to HTML in an
  26. ;; Info-like way, or using folding similar to the outline structure
  27. ;; org org-mode itself.
  28. ;; Documentation for using this module is in the Org manual. The script
  29. ;; itself is documented by Sebastian Rose in a file distributed with
  30. ;; the script. FIXME: Accurate pointers!
  31. ;; Org-mode loads this module by default - if this is not what you want,
  32. ;; configure the variable `org-modules'.
  33. ;;; Code:
  34. (require 'org-exp)
  35. (require 'org-html)
  36. (add-to-list 'org-export-inbuffer-options-extra '("INFOJS_OPT" :infojs-opt))
  37. (add-hook 'org-export-options-filters 'org-infojs-handle-options)
  38. (defgroup org-infojs nil
  39. "Options specific for using org-info.js in HTML export of Org-mode files."
  40. :tag "Org Export HTML INFOJS"
  41. :group 'org-export-html)
  42. (defcustom org-export-html-use-infojs 'when-configured
  43. "Should Sebastian Rose's Java Script org-info.js be linked into HTML files?
  44. This option can be nil or t to never or always use the script. It can
  45. also be the symbol `when-configured', meaning that the script will be
  46. linked into the export file if and only if there is a \"#+INFOJS_OPT:\"
  47. line in the buffer. See also the variable `org-infojs-options'."
  48. :group 'org-export-html
  49. :group 'org-infojs
  50. :type '(choice
  51. (const :tag "Never" nil)
  52. (const :tag "When configured in buffer" when-configured)
  53. (const :tag "Always" t)))
  54. (defconst org-infojs-opts-table
  55. '((path PATH "http://orgmode.org/org-info.js")
  56. (view VIEW "info")
  57. (toc TOC :table-of-contents)
  58. (ftoc FIXED_TOC "0")
  59. (tdepth TOC_DEPTH "max")
  60. (sdepth SECTION_DEPTH "max")
  61. (mouse MOUSE_HINT "underline")
  62. (buttons VIEW_BUTTONS "0")
  63. (ltoc LOCAL_TOC "1")
  64. (up LINK_UP :link-up)
  65. (home LINK_HOME :link-home))
  66. "JavaScript options, long form for script, default values.")
  67. (defvar org-infojs-options)
  68. (when (and (boundp 'org-infojs-options)
  69. (assq 'runs org-infojs-options))
  70. (setq org-infojs-options (delq (assq 'runs org-infojs-options)
  71. org-infojs-options)))
  72. (defcustom org-infojs-options
  73. (mapcar (lambda (x) (cons (car x) (nth 2 x)))
  74. org-infojs-opts-table)
  75. "Options settings for the INFOJS Javascript.
  76. Each of the options must have an entry in `org-export-html/infojs-opts-table'.
  77. The value can either be a string that will be passed to the script, or
  78. a property. This property is then assumed to be a property that is defined
  79. by the Export/Publishing setup of Org.
  80. The `sdepth' and `tdepth' parameters can also be set to \"max\", which
  81. means to use the maximum value consistent with other options."
  82. :group 'org-infojs
  83. :type
  84. `(set :greedy t :inline t
  85. ,@(mapcar
  86. (lambda (x)
  87. (list 'cons (list 'const (car x))
  88. '(choice
  89. (symbol :tag "Publishing/Export property")
  90. (string :tag "Value"))))
  91. org-infojs-opts-table)))
  92. (defcustom org-infojs-template
  93. "<script type=\"text/javascript\" src=\"%SCRIPT_PATH\"></script>
  94. <script type=\"text/javascript\" >
  95. <!--/*--><![CDATA[/*><!--*/
  96. %MANAGER_OPTIONS
  97. org_html_manager.setup(); // activate after the parameters are set
  98. /*]]>*///-->
  99. </script>"
  100. "The template for the export style additions when org-info.js is used.
  101. Option settings will replace the %MANAGER-OPTIONS cookie."
  102. :group 'org-infojs
  103. :type 'string)
  104. (defun org-infojs-handle-options (exp-plist)
  105. "Analyze JavaScript options in INFO-PLIST and modify EXP-PLIST accordingly."
  106. (if (or (not org-export-html-use-infojs)
  107. (and (eq org-export-html-use-infojs 'when-configured)
  108. (or (not (plist-get exp-plist :infojs-opt))
  109. (string-match "\\<view:nil\\>"
  110. (plist-get exp-plist :infojs-opt)))))
  111. ;; We do not want to use the script
  112. exp-plist
  113. ;; We do want to use the script, set it up
  114. (let ((template org-infojs-template)
  115. (ptoc (plist-get exp-plist :table-of-contents))
  116. (hlevels (plist-get exp-plist :headline-levels))
  117. tdepth sdepth s v e opt var val table default)
  118. (setq sdepth hlevels
  119. tdepth hlevels)
  120. (if (integerp ptoc) (setq tdepth (min ptoc tdepth)))
  121. (setq v (plist-get exp-plist :infojs-opt)
  122. table org-infojs-opts-table)
  123. (while (setq e (pop table))
  124. (setq opt (car e) var (nth 1 e)
  125. default (cdr (assoc opt org-infojs-options)))
  126. (and (symbolp default) (not (memq default '(t nil)))
  127. (setq default (plist-get exp-plist default)))
  128. (if (and v (string-match (format " %s:\\(\\S-+\\)" opt) v))
  129. (setq val (match-string 1 v))
  130. (setq val default))
  131. (cond
  132. ((eq opt 'path)
  133. (and (string-match "%SCRIPT_PATH" template)
  134. (setq template (replace-match val t t template))))
  135. ((eq opt 'sdepth)
  136. (if (integerp (read val))
  137. (setq sdepth (min (read val) hlevels))))
  138. ((eq opt 'tdepth)
  139. (if (integerp (read val))
  140. (setq tdepth (min (read val) hlevels))))
  141. (t
  142. (setq val
  143. (cond
  144. ((or (eq val t) (equal val "t")) "1")
  145. ((or (eq val nil) (equal val "nil")) "0")
  146. ((stringp val) val)
  147. (t (format "%s" val))))
  148. (push (cons var val) s))))
  149. ;; Now we set the depth of the *generated* TOC to SDEPTH, because the
  150. ;; toc will actually determine the splitting. How much of the toc will
  151. ;; actually be displayed is governed by the TDEPTH option.
  152. (setq exp-plist (plist-put exp-plist :table-of-contents sdepth))
  153. ;; The table of contents should not show more sections then we generate
  154. (setq tdepth (min tdepth sdepth))
  155. (push (cons "TOC_DEPTH" tdepth) s)
  156. (setq s (mapconcat
  157. (lambda (x) (format "org_html_manager.set(\"%s\", \"%s\");"
  158. (car x) (cdr x)))
  159. s "\n"))
  160. (when (and s (> (length s) 0))
  161. (and (string-match "%MANAGER_OPTIONS" template)
  162. (setq s (replace-match s t t template))
  163. (setq exp-plist
  164. (plist-put
  165. exp-plist :style-extra
  166. (concat (or (plist-get exp-plist :style-extra) "") "\n" s)))))
  167. ;; This script absolutely needs the table of contents, to we change that
  168. ;; setting
  169. (if (not (plist-get exp-plist :table-of-contents))
  170. (setq exp-plist (plist-put exp-plist :table-of-contents t)))
  171. ;; Return the modified property list
  172. exp-plist)))
  173. (defun org-infojs-options-inbuffer-template ()
  174. (format "#+INFOJS_OPT: view:%s toc:%s ltoc:%s mouse:%s buttons:%s path:%s"
  175. (if (eq t org-export-html-use-infojs) (cdr (assoc 'view org-infojs-options)) nil)
  176. (let ((a (cdr (assoc 'toc org-infojs-options))))
  177. (cond ((memq a '(nil t)) a)
  178. (t (plist-get (org-infile-export-plist) :table-of-contents))))
  179. (if (equal (cdr (assoc 'ltoc org-infojs-options)) "1") t nil)
  180. (cdr (assoc 'mouse org-infojs-options))
  181. (cdr (assoc 'buttons org-infojs-options))
  182. (cdr (assoc 'path org-infojs-options))))
  183. (provide 'org-infojs)
  184. (provide 'org-jsinfo)
  185. ;; arch-tag: c71d1d85-3337-4817-a066-725e74ac9eac
  186. ;;; org-jsinfo.el ends here