Merge branch 'master' of orgmode.org:org-mode
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-exp-blocks.el
blob498322769af495be278b79ea57dd000e6b1d70e3
1 ;;; org-exp-blocks.el --- pre-process blocks when exporting org files
3 ;; Copyright (C) 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Eric Schulte
7 ;; Version: 7.4
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/>.
24 ;;; Commentary:
26 ;; This is a utility for pre-processing blocks in org files before
27 ;; export using the `org-export-preprocess-hook'. It can be used for
28 ;; exporting new types of blocks from org-mode files and also for
29 ;; changing the default export behavior of existing org-mode blocks.
30 ;; The `org-export-blocks' and `org-export-interblocks' variables can
31 ;; be used to control how blocks and the spaces between blocks
32 ;; respectively are processed upon export.
34 ;; The type of a block is defined as the string following =#+begin_=,
35 ;; so for example the following block would be of type ditaa. Note
36 ;; that both upper or lower case are allowed in =#+BEGIN_= and
37 ;; =#+END_=.
39 ;; #+begin_ditaa blue.png -r -S
40 ;; +---------+
41 ;; | cBLU |
42 ;; | |
43 ;; | +----+
44 ;; | |cPNK|
45 ;; | | |
46 ;; +----+----+
47 ;; #+end_ditaa
49 ;;; Currently Implemented Block Types
51 ;; ditaa :: (DEPRECATED--use "#+begin_src ditaa" code blocks) Convert
52 ;; ascii pictures to actual images using ditaa
53 ;; http://ditaa.sourceforge.net/. To use this set
54 ;; `org-ditaa-jar-path' to the path to ditaa.jar on your
55 ;; system (should be set automatically in most cases) .
57 ;; dot :: (DEPRECATED--use "#+begin_src dot" code blocks) Convert
58 ;; graphs defined using the dot graphing language to images
59 ;; using the dot utility. For information on dot see
60 ;; http://www.graphviz.org/
62 ;; comment :: Wrap comments with titles and author information, in
63 ;; their own divs with author-specific ids allowing for css
64 ;; coloring of comments based on the author.
66 ;;; Adding new blocks
68 ;; When adding a new block type first define a formatting function
69 ;; along the same lines as `org-export-blocks-format-dot' and then use
70 ;; `org-export-blocks-add-block' to add your block type to
71 ;; `org-export-blocks'.
73 ;;; Code:
75 (eval-when-compile
76 (require 'cl))
77 (require 'org)
79 (defvar htmlp)
80 (defvar latexp)
81 (defvar docbookp)
82 (defvar asciip)
84 (defun org-export-blocks-set (var value)
85 "Set the value of `org-export-blocks' and install fontification."
86 (set var value)
87 (mapc (lambda (spec)
88 (if (nth 2 spec)
89 (setq org-protecting-blocks
90 (delete (symbol-name (car spec))
91 org-protecting-blocks))
92 (add-to-list 'org-protecting-blocks
93 (symbol-name (car spec)))))
94 value))
96 (defcustom org-export-blocks
97 '((comment org-export-blocks-format-comment t)
98 (ditaa org-export-blocks-format-ditaa nil)
99 (dot org-export-blocks-format-dot nil))
100 "Use this alist to associate block types with block exporting functions.
101 The type of a block is determined by the text immediately
102 following the '#+BEGIN_' portion of the block header. Each block
103 export function should accept three arguments."
104 :group 'org-export-general
105 :type '(repeat
106 (list
107 (symbol :tag "Block name")
108 (function :tag "Block formatter")
109 (boolean :tag "Fontify content as Org syntax")))
110 :set 'org-export-blocks-set)
112 (defun org-export-blocks-add-block (block-spec)
113 "Add a new block type to `org-export-blocks'.
114 BLOCK-SPEC should be a three element list the first element of
115 which should indicate the name of the block, the second element
116 should be the formatting function called by
117 `org-export-blocks-preprocess' and the third element a flag
118 indicating whether these types of blocks should be fontified in
119 org-mode buffers (see `org-protecting-blocks'). For example the
120 BLOCK-SPEC for ditaa blocks is as follows.
122 (ditaa org-export-blocks-format-ditaa nil)"
123 (unless (member block-spec org-export-blocks)
124 (setq org-export-blocks (cons block-spec org-export-blocks))
125 (org-export-blocks-set 'org-export-blocks org-export-blocks)))
127 (defcustom org-export-interblocks
129 "Use this a-list to associate block types with block exporting functions.
130 The type of a block is determined by the text immediately
131 following the '#+BEGIN_' portion of the block header. Each block
132 export function should accept three arguments."
133 :group 'org-export-general
134 :type 'alist)
136 (defcustom org-export-blocks-witheld
137 '(hidden)
138 "List of block types (see `org-export-blocks') which should not be exported."
139 :group 'org-export-general
140 :type 'list)
142 (defcustom org-export-blocks-postblock-hook nil
143 "Run after blocks have been processed with `org-export-blocks-preprocess'."
144 :group 'org-export-general
145 :type 'hook)
147 (defun org-export-blocks-html-quote (body &optional open close)
148 "Protect BODY from org html export.
149 The optional OPEN and CLOSE tags will be inserted around BODY."
151 (concat
152 "\n#+BEGIN_HTML\n"
153 (or open "")
154 body (if (string-match "\n$" body) "" "\n")
155 (or close "")
156 "#+END_HTML\n"))
158 (defun org-export-blocks-latex-quote (body &optional open close)
159 "Protect BODY from org latex export.
160 The optional OPEN and CLOSE tags will be inserted around BODY."
161 (concat
162 "\n#+BEGIN_LaTeX\n"
163 (or open "")
164 body (if (string-match "\n$" body) "" "\n")
165 (or close "")
166 "#+END_LaTeX\n"))
168 (defun org-export-blocks-preprocess ()
169 "Export all blocks according to the `org-export-blocks' block export alist.
170 Does not export block types specified in specified in BLOCKS
171 which defaults to the value of `org-export-blocks-witheld'."
172 (interactive)
173 (save-window-excursion
174 (let ((case-fold-search t)
175 (types '())
176 indentation type func start body headers preserve-indent progress-marker)
177 (flet ((interblock (start end)
178 (mapcar (lambda (pair) (funcall (second pair) start end))
179 org-export-interblocks)))
180 (goto-char (point-min))
181 (setq start (point))
182 (while (re-search-forward
183 "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)[\r\n][ \t]*#\\+end_\\S-+.*[\r\n]?" nil t)
184 (setq indentation (length (match-string 1)))
185 (setq type (intern (downcase (match-string 2))))
186 (setq headers (save-match-data (org-split-string (match-string 3) "[ \t]+")))
187 (setq body (match-string 4))
188 (setq preserve-indent (or org-src-preserve-indentation (member "-i" headers)))
189 (unless preserve-indent
190 (setq body (save-match-data (org-remove-indentation body))))
191 (unless (memq type types) (setq types (cons type types)))
192 (save-match-data (interblock start (match-beginning 0)))
193 (when (setq func (cadr (assoc type org-export-blocks)))
194 (let ((replacement (save-match-data
195 (if (memq type org-export-blocks-witheld) ""
196 (apply func body headers)))))
197 (when replacement
198 (replace-match replacement t t)
199 (unless preserve-indent
200 (indent-code-rigidly
201 (match-beginning 0) (match-end 0) indentation)))))
202 (setq start (match-end 0)))
203 (interblock start (point-max))
204 (run-hooks 'org-export-blocks-postblock-hook)))))
206 ;;================================================================================
207 ;; type specific functions
209 ;;--------------------------------------------------------------------------------
210 ;; ditaa: create images from ASCII art using the ditaa utility
211 (defvar org-ditaa-jar-path (expand-file-name
212 "ditaa.jar"
213 (file-name-as-directory
214 (expand-file-name
215 "scripts"
216 (file-name-as-directory
217 (expand-file-name
218 "../contrib"
219 (file-name-directory (or load-file-name buffer-file-name)))))))
220 "Path to the ditaa jar executable.")
222 (defun org-export-blocks-format-ditaa (body &rest headers)
223 "DEPRECATED: use begin_src ditaa code blocks
225 Pass block BODY to the ditaa utility creating an image.
226 Specify the path at which the image should be saved as the first
227 element of headers, any additional elements of headers will be
228 passed to the ditaa utility as command line arguments."
229 (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks")
230 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
231 (data-file (make-temp-file "org-ditaa"))
232 (hash (progn
233 (set-text-properties 0 (length body) nil body)
234 (sha1 (prin1-to-string (list body args)))))
235 (raw-out-file (if headers (car headers)))
236 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
237 (cons (match-string 1 raw-out-file)
238 (match-string 2 raw-out-file))
239 (cons raw-out-file "png")))
240 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
241 (unless (file-exists-p org-ditaa-jar-path)
242 (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
243 (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
244 body
245 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
246 (org-split-string body "\n")
247 "\n")))
248 (prog1
249 (cond
250 ((or htmlp latexp docbookp)
251 (unless (file-exists-p out-file)
252 (mapc ;; remove old hashed versions of this file
253 (lambda (file)
254 (when (and (string-match (concat (regexp-quote (car out-file-parts))
255 "_\\([[:alnum:]]+\\)\\."
256 (regexp-quote (cdr out-file-parts)))
257 file)
258 (= (length (match-string 1 out-file)) 40))
259 (delete-file (expand-file-name file
260 (file-name-directory out-file)))))
261 (directory-files (or (file-name-directory out-file)
262 default-directory)))
263 (with-temp-file data-file (insert body))
264 (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
265 (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file)))
266 (format "\n[[file:%s]]\n" out-file))
267 (t (concat
268 "\n#+BEGIN_EXAMPLE\n"
269 body (if (string-match "\n$" body) "" "\n")
270 "#+END_EXAMPLE\n")))
271 (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks"))))
273 ;;--------------------------------------------------------------------------------
274 ;; dot: create graphs using the dot graphing language
275 ;; (require the dot executable to be in your path)
276 (defun org-export-blocks-format-dot (body &rest headers)
277 "DEPRECATED: use \"#+begin_src dot\" code blocks
279 Pass block BODY to the dot graphing utility creating an image.
280 Specify the path at which the image should be saved as the first
281 element of headers, any additional elements of headers will be
282 passed to the dot utility as command line arguments. Don't
283 forget to specify the output type for the dot command, so if you
284 are exporting to a file with a name like 'image.png' you should
285 include a '-Tpng' argument, and your block should look like the
286 following.
288 #+begin_dot models.png -Tpng
289 digraph data_relationships {
290 \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
291 \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
292 \"data_requirement\" -> \"data_product\"
294 #+end_dot"
295 (message "begin_dot blocks are DEPRECATED, use begin_src blocks")
296 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
297 (data-file (make-temp-file "org-ditaa"))
298 (hash (progn
299 (set-text-properties 0 (length body) nil body)
300 (sha1 (prin1-to-string (list body args)))))
301 (raw-out-file (if headers (car headers)))
302 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
303 (cons (match-string 1 raw-out-file)
304 (match-string 2 raw-out-file))
305 (cons raw-out-file "png")))
306 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
307 (prog1
308 (cond
309 ((or htmlp latexp docbookp)
310 (unless (file-exists-p out-file)
311 (mapc ;; remove old hashed versions of this file
312 (lambda (file)
313 (when (and (string-match (concat (regexp-quote (car out-file-parts))
314 "_\\([[:alnum:]]+\\)\\."
315 (regexp-quote (cdr out-file-parts)))
316 file)
317 (= (length (match-string 1 out-file)) 40))
318 (delete-file (expand-file-name file
319 (file-name-directory out-file)))))
320 (directory-files (or (file-name-directory out-file)
321 default-directory)))
322 (with-temp-file data-file (insert body))
323 (message (concat "dot " data-file " " args " -o " out-file))
324 (shell-command (concat "dot " data-file " " args " -o " out-file)))
325 (format "\n[[file:%s]]\n" out-file))
326 (t (concat
327 "\n#+BEGIN_EXAMPLE\n"
328 body (if (string-match "\n$" body) "" "\n")
329 "#+END_EXAMPLE\n")))
330 (message "begin_dot blocks are DEPRECATED, use begin_src blocks"))))
332 ;;--------------------------------------------------------------------------------
333 ;; comment: export comments in author-specific css-stylable divs
334 (defun org-export-blocks-format-comment (body &rest headers)
335 "Format comment BODY by OWNER and return it formatted for export.
336 Currently, this only does something for HTML export, for all
337 other backends, it converts the comment into an EXAMPLE segment."
338 (let ((owner (if headers (car headers)))
339 (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
340 (cond
341 (htmlp ;; We are exporting to HTML
342 (concat "#+BEGIN_HTML\n"
343 "<div class=\"org-comment\""
344 (if owner (format " id=\"org-comment-%s\" " owner))
345 ">\n"
346 (if owner (concat "<b>" owner "</b> ") "")
347 (if (and title (> (length title) 0)) (concat " -- " title "</br>\n") "</br>\n")
348 "<p>\n"
349 "#+END_HTML\n"
350 body
351 "#+BEGIN_HTML\n"
352 "</p>\n"
353 "</div>\n"
354 "#+END_HTML\n"))
355 (t ;; This is not HTML, so just make it an example.
356 (concat "#+BEGIN_EXAMPLE\n"
357 (if title (concat "Title:" title "\n") "")
358 (if owner (concat "By:" owner "\n") "")
359 body
360 (if (string-match "\n\\'" body) "" "\n")
361 "#+END_EXAMPLE\n")))))
363 (provide 'org-exp-blocks)
365 ;; arch-tag: 1c365fe9-8808-4f72-bb15-0b00f36d8024
366 ;;; org-exp-blocks.el ends here