org-infojs.el 7.7 KB

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