org-infojs.el 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.00pre-5
  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. (mouse MOUSE_HINT "underline")
  59. (runs MAX_RUNS "5")
  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. (defcustom org-infojs-options
  66. (mapcar (lambda (x) (cons (car x) (nth 2 x)))
  67. org-infojs-opts-table)
  68. "Options settings for the INFOJS Javascript.
  69. Each of the options must have an entry in `org-export-html/infojs-opts-table'.
  70. The value can either be a string that will be passed to the script, or
  71. a property. This property is then assumed to be a property that is defined
  72. by the Export/Publishing setup of Org."
  73. :group 'org-infojs
  74. :type
  75. '(repeat
  76. (cons (symbol :tag "Option")
  77. (choice (symbol :tag "Publishing/Export property")
  78. (string :tag "Value")))))
  79. (defcustom org-infojs-template
  80. "<script type=\"text/javascript\" language=\"JavaScript\" src=\"%SCRIPT_PATH\"></script>
  81. <script type=\"text/javascript\" language=\"JavaScript\">
  82. /* <![CDATA[ */
  83. %MANAGER_OPTIONS
  84. org_html_manager.setup(); // activate after the parameterd are set
  85. /* ]]> */
  86. </script>"
  87. "The template for the export style additions when org-info.js is used.
  88. Option settings will replace the %MANAGER-OPTIONS cookie."
  89. :group 'org-infojs
  90. :type 'string)
  91. (defun org-infojs-handle-options (exp-plist)
  92. "Analyze JavaScript options in INFO-PLIST and modify EXP-PLIST accordingly."
  93. (if (or (not org-export-html-use-infojs)
  94. (and (eq org-export-html-use-infojs 'when-configured)
  95. (or (not (plist-get exp-plist :infojs-opt))
  96. (string-match "\\<view:nil\\>"
  97. (plist-get exp-plist :infojs-opt)))))
  98. ;; We do not want to use the script
  99. exp-plist
  100. ;; We do want to use the script, set it up
  101. (let ((template org-infojs-template)
  102. p1 s p v a1 tmp e opt var val table default)
  103. (setq v (plist-get exp-plist :infojs-opt)
  104. table org-infojs-opts-table)
  105. (while (setq e (pop table))
  106. (setq opt (car e) var (nth 1 e)
  107. default (cdr (assoc opt org-infojs-options)))
  108. (and (symbolp default) (not (memq default '(t nil)))
  109. (setq default (plist-get exp-plist default)))
  110. (if (string-match (format " %s:\\(\\S-+\\)" opt) v)
  111. (setq val (match-string 1 v))
  112. (setq val default))
  113. (cond
  114. ((eq opt 'path)
  115. (and (string-match "%SCRIPT_PATH" template)
  116. (setq template (replace-match val t t template))))
  117. (t
  118. (setq val
  119. (cond
  120. ((or (eq val t) (equal val "t")) "1")
  121. ((or (eq val nil) (equal val "nil")) "0")
  122. ((stringp val) val)
  123. (t (format "%s" val))))
  124. (push (cons var val) s))))
  125. (setq s (mapconcat
  126. (lambda (x) (format "org_html_manager.set(\"%s\", \"%s\");"
  127. (car x) (cdr x)))
  128. s "\n"))
  129. (when (and s (> (length s) 0))
  130. (and (string-match "%MANAGER_OPTIONS" template)
  131. (setq s (replace-match s t t template))
  132. (setq exp-plist
  133. (plist-put
  134. exp-plist :style
  135. (concat (or (plist-get exp-plist :style) "") "\n" s)))))
  136. ;; This script absolutely needs the table of contents, to we change that
  137. ;; setting
  138. (if (not (plist-get exp-plist :table-of-contents))
  139. (setq exp-plist (plist-put exp-plist :table-of-contents t)))
  140. ;; Return the modified property list
  141. exp-plist)))
  142. (defun org-infojs-options-inbuffer-template ()
  143. (format "#+INFOJS_OPT: view:%s toc:%s ltoc:%s runs:%s mouse:%s buttons:%s path:%s"
  144. (if (eq t org-export-html-use-infojs) (cdr (assoc 'view org-infojs-options)) nil)
  145. (let ((a (cdr (assoc 'toc org-infojs-options))))
  146. (cond ((memq a '(nil t)) a)
  147. (t (plist-get (org-infile-export-plist) :table-of-contents))))
  148. (if (equal (cdr (assoc 'ltoc org-infojs-options)) "1") t nil)
  149. (cdr (assoc 'runs org-infojs-options))
  150. (cdr (assoc 'mouse org-infojs-options))
  151. (cdr (assoc 'buttons org-infojs-options))
  152. (cdr (assoc 'path org-infojs-options))))
  153. (provide 'org-infojs)
  154. ;;; org-infojs.el ends here