org-infojs.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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-4
  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 "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. (not (plist-get exp-plist :infojs-opt))))
  96. ;; We do not want to use the script
  97. exp-plist
  98. ;; We do want to use the script, set it up
  99. (let ((template org-infojs-template)
  100. p1 s p v a1 tmp e opt var val table default)
  101. (setq v (plist-get exp-plist :infojs-opt)
  102. table org-infojs-opts-table)
  103. (while (setq e (pop table))
  104. (setq opt (car e) var (nth 1 e)
  105. default (cdr (assoc opt org-infojs-options)))
  106. (and (symbolp default) (not (memq default '(t nil)))
  107. (setq default (plist-get exp-plist default)))
  108. (if (string-match (format " %s:\\(\\S-+\\)" opt) v)
  109. (setq val (match-string 1 v))
  110. (setq val default))
  111. (cond
  112. ((eq opt 'path)
  113. (and (string-match "%SCRIPT_PATH" template)
  114. (setq template (replace-match val t t template))))
  115. (t
  116. (setq val
  117. (cond
  118. ((or (eq val t) (equal val "t")) "1")
  119. ((or (eq val nil) (equal val "nil")) "0")
  120. ((stringp val) val)
  121. (t (format "%s" val))))
  122. (push (cons var val) s))))
  123. (setq s (mapconcat
  124. (lambda (x) (format "org_html_manager.set(\"%s\", \"%s\");"
  125. (car x) (cdr x)))
  126. s "\n"))
  127. (when (and s (> (length s) 0))
  128. (and (string-match "%MANAGER_OPTIONS" template)
  129. (setq s (replace-match s t t template))
  130. (setq exp-plist
  131. (plist-put
  132. exp-plist :style
  133. (concat (or (plist-get exp-plist :style) "") "\n" s)))))
  134. ;; This script absolutely needs the table of contents, to we change that
  135. ;; setting
  136. (if (not (plist-get exp-plist :table-of-contents))
  137. (setq exp-plist (plist-put exp-plist :table-of-contents t)))
  138. ;; Return the modified property list
  139. exp-plist)))
  140. (provide 'org-infojs)
  141. ;;; org-infojs.el ends here