Release 6.25c
[org-mode.git] / contrib / lisp / org-exp-blocks.el
blobb4e72412d3aaab47a98b66c8389f2f5ba8a2cbab
1 ;;; org-exp-blocks.el --- pre-process blocks when exporting org files
3 ;; Copyright (C) 2008 Eric Schulte
5 ;; Author: Eric Schulte
7 ;; This file is not currently part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or
10 ;; modify it under the terms of the GNU General Public License as
11 ;; published by the Free Software Foundation; either version 2, or (at
12 ;; your option) any later version.
14 ;; This program is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program ; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
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' alist can be
31 ;; 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 :: Convert ascii pictures to actual images using ditaa
52 ;; http://ditaa.sourceforge.net/. To use this set
53 ;; `org-ditaa-jar-path' to the path to ditaa.jar on your
54 ;; system (should be set automatically in most cases) .
56 ;; dot :: Convert graphs defined using the dot graphing language to
57 ;; images using the dot utility. For information on dot see
58 ;; http://www.graphviz.org/
60 ;; comment :: Wrap comments with titles and author information, in
61 ;; their own divs with author-specific ids allowing for css
62 ;; coloring of comments based on the author.
64 ;; R :: Implements Sweave type exporting, evaluates blocks of R code,
65 ;; and also replaces \R{} chunks in the file with their result
66 ;; when passed to R. This require the `R' command which is
67 ;; provided by ESS (Emacs Speaks Statistics).
69 (defcustom org-export-blocks
70 '((comment org-export-blocks-format-comment)
71 (ditaa org-export-blocks-format-ditaa)
72 (dot org-export-blocks-format-dot)
73 (r org-export-blocks-format-R)
74 (R org-export-blocks-format-R))
75 "Use this a-list to associate block types with block exporting
76 functions. The type of a block is determined by the text
77 immediately following the '#+BEGIN_' portion of the block header.
78 Each block export function should accept three argumets..."
79 :group 'org-export-general
80 :type 'alist)
82 (defcustom org-export-interblocks
83 '((r org-export-interblocks-format-R)
84 (R org-export-interblocks-format-R))
85 "Use this a-list to associate block types with block exporting
86 functions. The type of a block is determined by the text
87 immediately following the '#+BEGIN_' portion of the block header.
88 Each block export function should accept three argumets..."
89 :group 'org-export-general
90 :type 'alist)
92 (defcustom org-export-blocks-witheld
93 '(hidden)
94 "List of block types (see `org-export-blocks') which should not
95 be exported."
96 :group 'org-export-general
97 :type 'list)
99 (defvar org-export-blocks-postblock-hooks nil "")
101 (defun org-export-blocks-html-quote (body &optional open close)
102 "Protext BODY from org html export. The optional OPEN and
103 CLOSE tags will be inserted around BODY."
104 (concat
105 "\n#+BEGIN_HTML\n"
106 (or open "")
107 body (if (string-match "\n$" body) "" "\n")
108 (or close "")
109 "#+END_HTML\n"))
111 (defun org-export-blocks-latex-quote (body &optional open close)
112 "Protext BODY from org latex export. The optional OPEN and
113 CLOSE tags will be inserted around BODY."
114 (concat
115 "\n#+BEGIN_LaTeX\n"
116 (or open "")
117 body (if (string-match "\n$" body) "" "\n")
118 (or close "")
119 "#+END_LaTeX\n"))
121 (defun org-export-blocks-preprocess ()
122 "Export all blocks acording to the `org-export-blocks' block
123 exportation alist. Does not export block types specified in
124 specified in BLOCKS which default to the value of
125 `org-export-blocks-witheld'."
126 (interactive)
127 (save-window-excursion
128 (let ((count 0)
129 (blocks org-export-blocks-witheld)
130 (case-fold-search t)
131 (types '())
132 type func start end)
133 (flet ((interblock (start end type)
134 (save-match-data
135 (when (setf func (cadr (assoc type org-export-interblocks)))
136 (funcall func start end)))))
137 (goto-char (point-min))
138 (setf start (point))
139 (while (re-search-forward
140 "^#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)#\\+end_\\S-*[\r\n]" nil t)
141 (save-match-data (setf type (intern (match-string 1))))
142 (unless (memq type types) (setf types (cons type types)))
143 (setf end (save-match-data (match-beginning 0)))
144 (interblock start end type)
145 (if (setf func (cadr (assoc type org-export-blocks)))
146 (replace-match (save-match-data
147 (if (memq type blocks)
149 (apply func (match-string 3) (split-string (match-string 2) " ")))) t t))
150 (setf start (save-match-data (match-end 0))))
151 (mapcar (lambda (type)
152 (interblock start (point-max) type))
153 types)))))
155 (add-hook 'org-export-preprocess-hook 'org-export-blocks-preprocess)
157 ;;================================================================================
158 ;; type specific functions
160 ;;--------------------------------------------------------------------------------
161 ;; ditaa: create images from ASCII art using the ditaa utility
162 (defvar org-ditaa-jar-path (expand-file-name
163 "ditaa.jar"
164 (file-name-as-directory
165 (expand-file-name
166 "scripts"
167 (file-name-as-directory
168 (expand-file-name
169 ".."
170 (file-name-directory (or load-file-name buffer-file-name)))))))
171 "Path to the ditaa jar executable")
173 (defun org-export-blocks-format-ditaa (body &rest headers)
174 "Pass block BODY to the ditaa utility creating an image.
175 Specify the path at which the image should be saved as the first
176 element of headers, any additional elements of headers will be
177 passed to the ditaa utility as command line arguments."
178 (message "ditaa-formatting...")
179 (let ((out-file (if headers (car headers)))
180 (args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
181 (data-file (make-temp-file "org-ditaa")))
182 (unless (file-exists-p org-ditaa-jar-path)
183 (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
184 (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
185 body
186 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
187 (org-split-string body "\n")
188 "\n")))
189 (cond
190 ((or htmlp latexp)
191 (with-temp-file data-file (insert body))
192 (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
193 (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
194 (format "\n[[file:%s]]\n" out-file))
195 (t (concat
196 "\n#+BEGIN_EXAMPLE\n"
197 body (if (string-match "\n$" body) "" "\n")
198 "#+END_EXAMPLE\n")))))
200 ;;--------------------------------------------------------------------------------
201 ;; dot: create graphs using the dot graphing language
202 ;; (require the dot executable to be in your path)
203 (defun org-export-blocks-format-dot (body &rest headers)
204 "Pass block BODY to the dot graphing utility creating an image.
205 Specify the path at which the image should be saved as the first
206 element of headers, any additional elements of headers will be
207 passed to the dot utility as command line arguments. Don't
208 forget to specify the output type for the dot command, so if you
209 are exporting to a file with a name like 'image.png' you should
210 include a '-Tpng' argument, and your block should look like the
211 following.
213 #+begin_dot models.png -Tpng
214 digraph data_relationships {
215 \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
216 \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
217 \"data_requirement\" -> \"data_product\"
219 #+end_dot"
220 (message "dot-formatting...")
221 (let ((out-file (if headers (car headers)))
222 (args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
223 (data-file (make-temp-file "org-ditaa")))
224 (cond
225 ((or htmlp latexp)
226 (with-temp-file data-file (insert body))
227 (message (concat "dot " data-file " " args " -o " out-file))
228 (shell-command (concat "dot " data-file " " args " -o " out-file))
229 (format "\n[[file:%s]]\n" out-file))
230 (t (concat
231 "\n#+BEGIN_EXAMPLE\n"
232 body (if (string-match "\n$" body) "" "\n")
233 "#+END_EXAMPLE\n")))))
235 ;;--------------------------------------------------------------------------------
236 ;; comment: export comments in author-specific css-stylable divs
237 (defun org-export-blocks-format-comment (body &rest headers)
238 "Format comment BODY by OWNER and return it formatted for export.
239 Currently, this only does something for HTML export, for all
240 other backends, it converts the comment into an EXAMPLE segment."
241 (let ((owner (if headers (car headers)))
242 (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
243 (cond
244 (htmlp ;; We are exporting to HTML
245 (concat "#+BEGIN_HTML\n"
246 "<div class=\"org-comment\""
247 (if owner (format " id=\"org-comment-%s\" " owner))
248 ">\n"
249 (if owner (concat "<b>" owner "</b> ") "")
250 (if (and title (> (length title) 0)) (concat " -- " title "</br>\n") "</br>\n")
251 "<p>\n"
252 "#+END_HTML\n"
253 body
254 "#+BEGIN_HTML\n"
255 "</p>\n"
256 "</div>\n"
257 "#+END_HTML\n"))
258 (t ;; This is not HTML, so just make it an example.
259 (concat "#+BEGIN_EXAMPLE\n"
260 (if title (concat "Title:" title "\n") "")
261 (if owner (concat "By:" owner "\n") "")
262 body
263 (if (string-match "\n\\'" body) "" "\n")
264 "#+END_EXAMPLE\n")))))
266 ;;--------------------------------------------------------------------------------
267 ;; R: Sweave-type functionality
268 (defvar interblock-R-buffer nil
269 "Holds the buffer for the current R process")
271 (defun org-export-blocks-format-R (body &rest headers)
272 "Process R blocks and replace \R{} forms outside the blocks
273 with their values as determined by R."
274 (interactive)
275 (message "R processing...")
276 (let ((image-path (or (and (car headers)
277 (string-match "\\(.?\\)\.\\(EPS\\|eps\\)" (car headers))
278 (match-string 1 (car headers)))
279 (and (> (length (car headers)) 0)
280 (car headers))
281 ;; create the default filename
282 (format "Rplot-%03d" count)))
283 (plot (string-match "plot" body))
284 R-proc)
285 (setf count (+ count 1))
286 (interblock-initiate-R-buffer)
287 (setf R-proc (get-buffer-process interblock-R-buffer))
288 ;; send strings to the ESS process using `comint-send-string'
289 (setf body (mapconcat (lambda (line)
290 (interblock-R-input-command line) (concat "> " line))
291 (butlast (split-string body "[\r\n]"))
292 "\n"))
293 ;; if there is a plot command, then create the images
294 (when plot
295 (interblock-R-input-command (format "dev.copy2eps(file=\"%s.eps\")" image-path)))
296 (concat (cond
297 (htmlp (org-export-blocks-html-quote body
298 (format "<div id=\"R-%d\">\n<pre>\n" count)
299 "</pre>\n</div>\n"))
300 (latexp (org-export-blocks-latex-quote body
301 "\\begin{Schunk}\n\\begin{Sinput}\n"
302 "\\end{Sinput}\n\\end{Schunk}\n"))
303 (t (insert ;; default export
304 "#+begin_R " (mapconcat 'identity headers " ") "\n"
305 body (if (string-match "\n$" body) "" "\n")
306 "#+end_R\n")))
307 (if plot
308 (format "[[file:%s.eps]]\n" image-path)
309 ""))))
311 (defun org-export-interblocks-format-R (start end)
312 "This is run over parts of the org-file which are between R
313 blocks. It's main use is to expand the \R{stuff} chunks for
314 export."
315 (save-excursion
316 (goto-char start)
317 (interblock-initiate-R-buffer)
318 (let (code replacement)
319 (while (and (< (point) end) (re-search-forward "\\\\R{\\(.*\\)}" end t))
320 (save-match-data (setf code (match-string 1)))
321 (setf replacement (interblock-R-command-to-string code))
322 (setf replacement (cond
323 (htmlp replacement)
324 (latexp replacement)
325 (t replacement)))
326 (setf end (+ end (- (length replacement) (length code))))
327 (replace-match replacement t t)))))
329 (defun interblock-initiate-R-buffer ()
330 "If there is not a current R process then create one."
331 (unless (and (buffer-live-p interblock-R-buffer) (get-buffer interblock-R-buffer))
332 (save-excursion
334 (setf interblock-R-buffer (current-buffer))
335 (interblock-R-wait-for-output)
336 (interblock-R-input-command ""))))
338 (defun interblock-R-command-to-string (command)
339 "Send a command to R, and return the results as a string."
340 (interblock-R-input-command command)
341 (interblock-R-last-output))
343 (defun interblock-R-input-command (command)
344 "Pass COMMAND to the R process running in `interblock-R-buffer'."
345 (save-excursion
346 (save-match-data
347 (set-buffer interblock-R-buffer)
348 (goto-char (process-mark (get-buffer-process (current-buffer))))
349 (insert command)
350 (comint-send-input)
351 (interblock-R-wait-for-output))))
353 (defun interblock-R-wait-for-output ()
354 "Wait until output arrives"
355 (save-excursion
356 (save-match-data
357 (set-buffer interblock-R-buffer)
358 (while (progn
359 (goto-char comint-last-input-end)
360 (not (re-search-forward comint-prompt-regexp nil t)))
361 (accept-process-output (get-buffer-process (current-buffer)))))))
363 (defun interblock-R-last-output ()
364 "Return the last R output as a string"
365 (save-excursion
366 (save-match-data
367 (set-buffer interblock-R-buffer)
368 (goto-char (process-mark (get-buffer-process (current-buffer))))
369 (forward-line 0)
370 (let ((raw (buffer-substring comint-last-input-end (- (point) 1))))
371 (if (string-match "\n" raw)
373 (and (string-match "\\[[[:digit:]+]\\] *\\(.*\\)$" raw)
374 (message raw)
375 (message (match-string 1 raw))
376 (match-string 1 raw)))))))
378 (provide 'org-exp-blocks)
380 ;;; org-exp-blocks.el ends here