org-jsinfo.el 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. ;;; org-jsinfo.el --- Support for org-info.js Javascript in Org HTML export
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  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.19d
  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. (add-to-list 'org-export-inbuffer-options-extra '("INFOJS_OPT" :infojs-opt))
  36. (add-hook 'org-export-options-filters 'org-infojs-handle-options)
  37. (defgroup org-infojs nil
  38. "Options specific for using org-info.js in HTML export of Org-mode files."
  39. :tag "Org Export HTML INFOJS"
  40. :group 'org-export-html)
  41. (defcustom org-export-html-use-infojs 'when-configured
  42. "Should Sebastian Rose's Java Script org-info.js be linked into HTML files?
  43. This option can be nil or t to never or always use the script. It can
  44. also be the symbol `when-configured', meaning that the script will be
  45. linked into the export file if and only if there is a \"#+INFOJS_OPT:\"
  46. line in the buffer. See also the variable `org-infojs-options'."
  47. :group 'org-export-html
  48. :group 'org-infojs
  49. :type '(choice
  50. (const :tag "Never" nil)
  51. (const :tag "When configured in buffer" when-configured)
  52. (const :tag "Always" t)))
  53. (defconst org-infojs-opts-table
  54. '((path PATH "http://orgmode.org/org-info.js")
  55. (view VIEW "info")
  56. (toc TOC :table-of-contents)
  57. (ftoc FIXED_TOC "0")
  58. (tdepth TOC_DEPTH "max")
  59. (sdepth SECTION_DEPTH "max")
  60. (mouse MOUSE_HINT "underline")
  61. (buttons VIEW_BUTTONS "0")
  62. (ltoc LOCAL_TOC "1")
  63. (up LINK_UP :link-up)
  64. (home LINK_HOME :link-home))
  65. "JavaScript options, long form for script, default values.")
  66. (defvar org-infojs-options)
  67. (when (and (boundp 'org-infojs-options)
  68. (assq 'runs org-infojs-options))
  69. (setq org-infojs-options (delq (assq 'runs org-infojs-options)
  70. org-infojs-options)))
  71. (defcustom org-infojs-options
  72. (mapcar (lambda (x) (cons (car x) (nth 2 x)))
  73. org-infojs-opts-table)
  74. "Options settings for the INFOJS Javascript.
  75. Each of the options must have an entry in `org-export-html/infojs-opts-table'.
  76. The value can either be a string that will be passed to the script, or
  77. a property. This property is then assumed to be a property that is defined
  78. by the Export/Publishing setup of Org.
  79. The `sdepth' and `tdepth' parameters can also be set to \"max\", which
  80. means to use the maximum value consistent with other options."
  81. :group 'org-infojs
  82. :type
  83. `(set :greedy t :inline t
  84. ,@(mapcar
  85. (lambda (x)
  86. (list 'cons (list 'const (car x))
  87. '(choice
  88. (symbol :tag "Publishing/Export property")
  89. (string :tag "Value"))))
  90. org-infojs-opts-table)))
  91. (defcustom org-infojs-template
  92. "<script type=\"text/javascript\" src=\"%SCRIPT_PATH\"></script>
  93. <script type=\"text/javascript\" >
  94. <!--/*--><![CDATA[/*><!--*/
  95. %MANAGER_OPTIONS
  96. org_html_manager.setup(); // activate after the parameters are set
  97. /*]]>*/-->
  98. </script>"
  99. "The template for the export style additions when org-info.js is used.
  100. Option settings will replace the %MANAGER-OPTIONS cookie."
  101. :group 'org-infojs
  102. :type 'string)
  103. (defun org-infojs-handle-options (exp-plist)
  104. "Analyze JavaScript options in INFO-PLIST and modify EXP-PLIST accordingly."
  105. (if (or (not org-export-html-use-infojs)
  106. (and (eq org-export-html-use-infojs 'when-configured)
  107. (or (not (plist-get exp-plist :infojs-opt))
  108. (string-match "\\<view:nil\\>"
  109. (plist-get exp-plist :infojs-opt)))))
  110. ;; We do not want to use the script
  111. exp-plist
  112. ;; We do want to use the script, set it up
  113. (let ((template org-infojs-template)
  114. (ptoc (plist-get exp-plist :table-of-contents))
  115. (hlevels (plist-get exp-plist :headline-levels))
  116. tdepth sdepth s v e opt var val table default)
  117. (setq sdepth hlevels
  118. tdepth hlevels)
  119. (if (integerp ptoc) (setq tdepth (min ptoc tdepth)))
  120. (setq v (plist-get exp-plist :infojs-opt)
  121. table org-infojs-opts-table)
  122. (while (setq e (pop table))
  123. (setq opt (car e) var (nth 1 e)
  124. default (cdr (assoc opt org-infojs-options)))
  125. (and (symbolp default) (not (memq default '(t nil)))
  126. (setq default (plist-get exp-plist default)))
  127. (if (and v (string-match (format " %s:\\(\\S-+\\)" opt) v))
  128. (setq val (match-string 1 v))
  129. (setq val default))
  130. (cond
  131. ((eq opt 'path)
  132. (and (string-match "%SCRIPT_PATH" template)
  133. (setq template (replace-match val t t template))))
  134. ((eq opt 'sdepth)
  135. (if (integerp (read val))
  136. (setq sdepth (min (read val) hlevels))))
  137. ((eq opt 'tdepth)
  138. (if (integerp (read val))
  139. (setq tdepth (min (read val) hlevels))))
  140. (t
  141. (setq val
  142. (cond
  143. ((or (eq val t) (equal val "t")) "1")
  144. ((or (eq val nil) (equal val "nil")) "0")
  145. ((stringp val) val)
  146. (t (format "%s" val))))
  147. (push (cons var val) s))))
  148. ;; Now we set the depth of the *generated* TOC to SDEPTH, because the
  149. ;; toc will actually determine the splitting. How much of the toc will
  150. ;; actually be displayed is governed by the TDEPTH option.
  151. (setq exp-plist (plist-put exp-plist :table-of-contents sdepth))
  152. ;; The table of contents should ot show more sections then we generate
  153. (setq tdepth (min tdepth sdepth))
  154. (push (cons "TOC_DEPTH" tdepth) s)
  155. (setq s (mapconcat
  156. (lambda (x) (format "org_html_manager.set(\"%s\", \"%s\");"
  157. (car x) (cdr x)))
  158. s "\n"))
  159. (when (and s (> (length s) 0))
  160. (and (string-match "%MANAGER_OPTIONS" template)
  161. (setq s (replace-match s t t template))
  162. (setq exp-plist
  163. (plist-put
  164. exp-plist :style-extra
  165. (concat (or (plist-get exp-plist :style-extra) "") "\n" s)))))
  166. ;; This script absolutely needs the table of contents, to we change that
  167. ;; setting
  168. (if (not (plist-get exp-plist :table-of-contents))
  169. (setq exp-plist (plist-put exp-plist :table-of-contents t)))
  170. ;; Return the modified property list
  171. exp-plist)))
  172. (defun org-infojs-options-inbuffer-template ()
  173. (format "#+INFOJS_OPT: view:%s toc:%s ltoc:%s mouse:%s buttons:%s path:%s"
  174. (if (eq t org-export-html-use-infojs) (cdr (assoc 'view org-infojs-options)) nil)
  175. (let ((a (cdr (assoc 'toc org-infojs-options))))
  176. (cond ((memq a '(nil t)) a)
  177. (t (plist-get (org-infile-export-plist) :table-of-contents))))
  178. (if (equal (cdr (assoc 'ltoc org-infojs-options)) "1") t nil)
  179. (cdr (assoc 'mouse org-infojs-options))
  180. (cdr (assoc 'buttons org-infojs-options))
  181. (cdr (assoc 'path org-infojs-options))))
  182. (provide 'org-infojs)
  183. (provide 'org-jsinfo)
  184. ;; arch-tag: c71d1d85-3337-4817-a066-725e74ac9eac
  185. ;;; org-jsinfo.el ends here