org-infojs.el 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.02pre02
  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, or (at your option)
  13. ;; 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; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This file implements the support for Sebastian Rose's Javascript
  26. ;; org-info.js to display an org-mode file exported to HTML in an
  27. ;; Info-like way, or using folding similar to the outline structure
  28. ;; org org-mode itself.
  29. ;; Documentation for using this module is in the Org manual. The script
  30. ;; itself is documented by Sebastian Rose in a file distributed with
  31. ;; the script. FIXME: Accurate pointers!
  32. ;; Org-mode loads this module by default - if this is not what you want,
  33. ;; configure the variable `org-modules'.
  34. ;;; Code:
  35. (require 'org-exp)
  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 Sebasian 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. (tdepth TOC_DEPTH "max")
  59. (sdepth SECTION_DEPTH "max")
  60. (mouse MOUSE_HINT "underline")
  61. (runs MAX_RUNS "5")
  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. (defcustom org-infojs-options
  68. (mapcar (lambda (x) (cons (car x) (nth 2 x)))
  69. org-infojs-opts-table)
  70. "Options settings for the INFOJS Javascript.
  71. Each of the options must have an entry in `org-export-html/infojs-opts-table'.
  72. The value can either be a string that will be passed to the script, or
  73. a property. This property is then assumed to be a property that is defined
  74. by the Export/Publishing setup of Org.
  75. The `sdepth' and `tdepth' parameters can also be set to \"max\", which
  76. means to use the maximum value consistent with other options."
  77. :group 'org-infojs
  78. :type
  79. `(set :greedy t :inline t
  80. ,@(mapcar
  81. (lambda (x)
  82. (list 'cons (list 'const (car x))
  83. '(choice
  84. (symbol :tag "Publishing/Export property")
  85. (string :tag "Value"))))
  86. org-infojs-opts-table)))
  87. (defcustom org-infojs-template
  88. "<script type=\"text/javascript\" language=\"JavaScript\" src=\"%SCRIPT_PATH\"></script>
  89. <script type=\"text/javascript\" language=\"JavaScript\">
  90. /* <![CDATA[ */
  91. %MANAGER_OPTIONS
  92. org_html_manager.setup(); // activate after the parameterd are set
  93. /* ]]> */
  94. </script>"
  95. "The template for the export style additions when org-info.js is used.
  96. Option settings will replace the %MANAGER-OPTIONS cookie."
  97. :group 'org-infojs
  98. :type 'string)
  99. (defun org-infojs-handle-options (exp-plist)
  100. "Analyze JavaScript options in INFO-PLIST and modify EXP-PLIST accordingly."
  101. (if (or (not org-export-html-use-infojs)
  102. (and (eq org-export-html-use-infojs 'when-configured)
  103. (or (not (plist-get exp-plist :infojs-opt))
  104. (string-match "\\<view:nil\\>"
  105. (plist-get exp-plist :infojs-opt)))))
  106. ;; We do not want to use the script
  107. exp-plist
  108. ;; We do want to use the script, set it up
  109. (let ((template org-infojs-template)
  110. (ptoc (plist-get exp-plist :table-of-contents))
  111. (hlevels (plist-get exp-plist :headline-levels))
  112. tdepth sdepth p1 s p v a1 tmp e opt var val table default)
  113. (setq sdepth hlevels
  114. tdepth hlevels)
  115. (if (integerp ptoc) (setq tdepth (min ptoc tdepth)))
  116. (setq v (plist-get exp-plist :infojs-opt)
  117. table org-infojs-opts-table)
  118. (while (setq e (pop table))
  119. (setq opt (car e) var (nth 1 e)
  120. default (cdr (assoc opt org-infojs-options)))
  121. (and (symbolp default) (not (memq default '(t nil)))
  122. (setq default (plist-get exp-plist default)))
  123. (if (string-match (format " %s:\\(\\S-+\\)" opt) v)
  124. (setq val (match-string 1 v))
  125. (setq val default))
  126. (cond
  127. ((eq opt 'path)
  128. (and (string-match "%SCRIPT_PATH" template)
  129. (setq template (replace-match val t t template))))
  130. ((eq opt 'sdepth)
  131. (if (integerp (read val))
  132. (setq sdepth (min (read val) hlevels))))
  133. ((eq opt 'tdepth)
  134. (if (integerp (read val))
  135. (setq tdepth (min (read val) hlevels))))
  136. (t
  137. (setq val
  138. (cond
  139. ((or (eq val t) (equal val "t")) "1")
  140. ((or (eq val nil) (equal val "nil")) "0")
  141. ((stringp val) val)
  142. (t (format "%s" val))))
  143. (push (cons var val) s))))
  144. ;; Now we set the depth of the *generated* TOC to SDEPTH, because the
  145. ;; toc will actually determine the splitting. How much of the toc will
  146. ;; actually be displayed is governed by the TDEPTH option.
  147. (setq exp-plist (plist-put exp-plist :table-of-contents sdepth))
  148. ;; The table of contents should ot show more sections then we generate
  149. (setq tdepth (min tdepth sdepth))
  150. (push (cons "TOC_DEPTH" tdepth) s)
  151. (setq s (mapconcat
  152. (lambda (x) (format "org_html_manager.set(\"%s\", \"%s\");"
  153. (car x) (cdr x)))
  154. s "\n"))
  155. (when (and s (> (length s) 0))
  156. (and (string-match "%MANAGER_OPTIONS" template)
  157. (setq s (replace-match s t t template))
  158. (setq exp-plist
  159. (plist-put
  160. exp-plist :style
  161. (concat (or (plist-get exp-plist :style) "") "\n" s)))))
  162. ;; This script absolutely needs the table of contents, to we change that
  163. ;; setting
  164. (if (not (plist-get exp-plist :table-of-contents))
  165. (setq exp-plist (plist-put exp-plist :table-of-contents t)))
  166. ;; Return the modified property list
  167. exp-plist)))
  168. (defun org-infojs-options-inbuffer-template ()
  169. (format "#+INFOJS_OPT: view:%s toc:%s ltoc:%s runs:%s mouse:%s buttons:%s path:%s"
  170. (if (eq t org-export-html-use-infojs) (cdr (assoc 'view org-infojs-options)) nil)
  171. (let ((a (cdr (assoc 'toc org-infojs-options))))
  172. (cond ((memq a '(nil t)) a)
  173. (t (plist-get (org-infile-export-plist) :table-of-contents))))
  174. (if (equal (cdr (assoc 'ltoc org-infojs-options)) "1") t nil)
  175. (cdr (assoc 'runs org-infojs-options))
  176. (cdr (assoc 'mouse org-infojs-options))
  177. (cdr (assoc 'buttons org-infojs-options))
  178. (cdr (assoc 'path org-infojs-options))))
  179. (provide 'org-infojs)
  180. ;;; org-infojs.el ends here