mk/server.mk: rename orgplus to org-plus-contrib
[org-mode/org-tableheadings.git] / lisp / org-jsinfo.el
blob35d43dec8da4ee48917dea7842d53413ef616572
1 ;;; org-jsinfo.el --- Support for org-info.js Javascript in Org HTML export
3 ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
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.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file implements the support for Sebastian Rose's JavaScript
28 ;; org-info.js to display an org-mode file exported to HTML in an
29 ;; Info-like way, or using folding similar to the outline structure
30 ;; org org-mode itself.
32 ;; Documentation for using this module is in the Org manual. The script
33 ;; itself is documented by Sebastian Rose in a file distributed with
34 ;; the script. FIXME: Accurate pointers!
36 ;; Org-mode loads this module by default - if this is not what you want,
37 ;; configure the variable `org-modules'.
39 ;;; Code:
41 (require 'org-exp)
42 (require 'org-html)
44 (add-to-list 'org-export-inbuffer-options-extra '("INFOJS_OPT" :infojs-opt))
45 (add-hook 'org-export-options-filters 'org-infojs-handle-options)
47 (defgroup org-infojs nil
48 "Options specific for using org-info.js in HTML export of Org-mode files."
49 :tag "Org Export HTML INFOJS"
50 :group 'org-export-html)
52 (defcustom org-export-html-use-infojs 'when-configured
53 "Should Sebastian Rose's Java Script org-info.js be linked into HTML files?
54 This option can be nil or t to never or always use the script. It can
55 also be the symbol `when-configured', meaning that the script will be
56 linked into the export file if and only if there is a \"#+INFOJS_OPT:\"
57 line in the buffer. See also the variable `org-infojs-options'."
58 :group 'org-export-html
59 :group 'org-infojs
60 :type '(choice
61 (const :tag "Never" nil)
62 (const :tag "When configured in buffer" when-configured)
63 (const :tag "Always" t)))
65 (defconst org-infojs-opts-table
66 '((path PATH "http://orgmode.org/org-info.js")
67 (view VIEW "info")
68 (toc TOC :table-of-contents)
69 (ftoc FIXED_TOC "0")
70 (tdepth TOC_DEPTH "max")
71 (sdepth SECTION_DEPTH "max")
72 (mouse MOUSE_HINT "underline")
73 (buttons VIEW_BUTTONS "0")
74 (ltoc LOCAL_TOC "1")
75 (up LINK_UP :link-up)
76 (home LINK_HOME :link-home))
77 "JavaScript options, long form for script, default values.")
79 (defvar org-infojs-options)
80 (when (and (boundp 'org-infojs-options)
81 (assq 'runs org-infojs-options))
82 (setq org-infojs-options (delq (assq 'runs org-infojs-options)
83 org-infojs-options)))
85 (defcustom org-infojs-options
86 (mapcar (lambda (x) (cons (car x) (nth 2 x)))
87 org-infojs-opts-table)
88 "Options settings for the INFOJS JavaScript.
89 Each of the options must have an entry in `org-export-html/infojs-opts-table'.
90 The value can either be a string that will be passed to the script, or
91 a property. This property is then assumed to be a property that is defined
92 by the Export/Publishing setup of Org.
93 The `sdepth' and `tdepth' parameters can also be set to \"max\", which
94 means to use the maximum value consistent with other options."
95 :group 'org-infojs
96 :type
97 `(set :greedy t :inline t
98 ,@(mapcar
99 (lambda (x)
100 (list 'cons (list 'const (car x))
101 '(choice
102 (symbol :tag "Publishing/Export property")
103 (string :tag "Value"))))
104 org-infojs-opts-table)))
106 (defcustom org-infojs-template
107 "<script type=\"text/javascript\" src=\"%SCRIPT_PATH\">
110 * @source: %SCRIPT_PATH
112 * @licstart The following is the entire license notice for the
113 * JavaScript code in %SCRIPT_PATH.
115 * Copyright (C) 2012 Sebastian Rose
118 * The JavaScript code in this tag is free software: you can
119 * redistribute it and/or modify it under the terms of the GNU
120 * General Public License (GNU GPL) as published by the Free Software
121 * Foundation, either version 3 of the License, or (at your option)
122 * any later version. The code is distributed WITHOUT ANY WARRANTY;
123 * without even the implied warranty of MERCHANTABILITY or FITNESS
124 * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
126 * As additional permission under GNU GPL version 3 section 7, you
127 * may distribute non-source (e.g., minimized or compacted) forms of
128 * that code without the copy of the GNU GPL normally required by
129 * section 4, provided you include this license notice and a URL
130 * through which recipients can access the Corresponding Source.
132 * @licend The above is the entire license notice
133 * for the JavaScript code in %SCRIPT_PATH.
136 </script>
138 <script type=\"text/javascript\">
141 @licstart The following is the entire license notice for the
142 JavaScript code in this tag.
144 Copyright (C) 2012 Free Software Foundation, Inc.
146 The JavaScript code in this tag is free software: you can
147 redistribute it and/or modify it under the terms of the GNU
148 General Public License (GNU GPL) as published by the Free Software
149 Foundation, either version 3 of the License, or (at your option)
150 any later version. The code is distributed WITHOUT ANY WARRANTY;
151 without even the implied warranty of MERCHANTABILITY or FITNESS
152 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
154 As additional permission under GNU GPL version 3 section 7, you
155 may distribute non-source (e.g., minimized or compacted) forms of
156 that code without the copy of the GNU GPL normally required by
157 section 4, provided you include this license notice and a URL
158 through which recipients can access the Corresponding Source.
161 @licend The above is the entire license notice
162 for the JavaScript code in this tag.
165 <!--/*--><![CDATA[/*><!--*/
166 %MANAGER_OPTIONS
167 org_html_manager.setup(); // activate after the parameters are set
168 /*]]>*///-->
169 </script>"
170 "The template for the export style additions when org-info.js is used.
171 Option settings will replace the %MANAGER-OPTIONS cookie."
172 :group 'org-infojs
173 :type 'string)
175 (defun org-infojs-handle-options (exp-plist)
176 "Analyze JavaScript options in INFO-PLIST and modify EXP-PLIST accordingly."
177 (if (or (not org-export-html-use-infojs)
178 (and (eq org-export-html-use-infojs 'when-configured)
179 (or (not (plist-get exp-plist :infojs-opt))
180 (string-match "\\<view:nil\\>"
181 (plist-get exp-plist :infojs-opt)))))
182 ;; We do not want to use the script
183 exp-plist
184 ;; We do want to use the script, set it up
185 (let ((template org-infojs-template)
186 (ptoc (plist-get exp-plist :table-of-contents))
187 (hlevels (plist-get exp-plist :headline-levels))
188 tdepth sdepth s v e opt var val table default)
189 (setq sdepth hlevels
190 tdepth hlevels)
191 (if (integerp ptoc) (setq tdepth (min ptoc tdepth)))
192 (setq v (plist-get exp-plist :infojs-opt)
193 table org-infojs-opts-table)
194 (while (setq e (pop table))
195 (setq opt (car e) var (nth 1 e)
196 default (cdr (assoc opt org-infojs-options)))
197 (and (symbolp default) (not (memq default '(t nil)))
198 (setq default (plist-get exp-plist default)))
199 (if (and v (string-match (format " %s:\\(\\S-+\\)" opt) v))
200 (setq val (match-string 1 v))
201 (setq val default))
202 (cond
203 ((eq opt 'path)
204 (setq template
205 (replace-regexp-in-string "%SCRIPT_PATH" val template t t)))
206 ((eq opt 'sdepth)
207 (if (integerp (read val))
208 (setq sdepth (min (read val) hlevels))))
209 ((eq opt 'tdepth)
210 (if (integerp (read val))
211 (setq tdepth (min (read val) hlevels))))
213 (setq val
214 (cond
215 ((or (eq val t) (equal val "t")) "1")
216 ((or (eq val nil) (equal val "nil")) "0")
217 ((stringp val) val)
218 (t (format "%s" val))))
219 (push (cons var val) s))))
221 ;; Now we set the depth of the *generated* TOC to SDEPTH, because the
222 ;; toc will actually determine the splitting. How much of the toc will
223 ;; actually be displayed is governed by the TDEPTH option.
224 (setq exp-plist (plist-put exp-plist :table-of-contents sdepth))
226 ;; The table of contents should not show more sections then we generate
227 (setq tdepth (min tdepth sdepth))
228 (push (cons "TOC_DEPTH" tdepth) s)
230 (setq s (mapconcat
231 (lambda (x) (format "org_html_manager.set(\"%s\", \"%s\");"
232 (car x) (cdr x)))
233 s "\n"))
234 (when (and s (> (length s) 0))
235 (and (string-match "%MANAGER_OPTIONS" template)
236 (setq s (replace-match s t t template))
237 (setq exp-plist
238 (plist-put
239 exp-plist :style-extra
240 (concat (or (plist-get exp-plist :style-extra) "") "\n" s)))))
241 ;; This script absolutely needs the table of contents, to we change that
242 ;; setting
243 (if (not (plist-get exp-plist :table-of-contents))
244 (setq exp-plist (plist-put exp-plist :table-of-contents t)))
245 ;; Return the modified property list
246 exp-plist)))
248 (defun org-infojs-options-inbuffer-template ()
249 (format "#+INFOJS_OPT: view:%s toc:%s ltoc:%s mouse:%s buttons:%s path:%s"
250 (if (eq t org-export-html-use-infojs) (cdr (assoc 'view org-infojs-options)) nil)
251 (let ((a (cdr (assoc 'toc org-infojs-options))))
252 (cond ((memq a '(nil t)) a)
253 (t (plist-get (org-infile-export-plist) :table-of-contents))))
254 (if (equal (cdr (assoc 'ltoc org-infojs-options)) "1") t nil)
255 (cdr (assoc 'mouse org-infojs-options))
256 (cdr (assoc 'buttons org-infojs-options))
257 (cdr (assoc 'path org-infojs-options))))
259 (provide 'org-infojs)
260 (provide 'org-jsinfo)
262 ;;; org-jsinfo.el ends here