Use `delete-char' instead of `delete-backward-char'
[org-mode.git] / lisp / org-exp-blocks.el
blobbc8d289eb6eea3ce5e6308464bd93e7472a1a2d4
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.5
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 org-exp-blocks-block-regexp
80 (concat
81 "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)"
82 "[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)[\r\n]*[ \t]*"
83 "#\\+end_\\S-+.*[\r\n]?")
84 "Regular expression used to match blocks by org-exp-blocks.")
86 (defun org-export-blocks-set (var value)
87 "Set the value of `org-export-blocks' and install fontification."
88 (set var value)
89 (mapc (lambda (spec)
90 (if (nth 2 spec)
91 (setq org-protecting-blocks
92 (delete (symbol-name (car spec))
93 org-protecting-blocks))
94 (add-to-list 'org-protecting-blocks
95 (symbol-name (car spec)))))
96 value))
98 (defcustom org-export-blocks
99 '((comment org-export-blocks-format-comment t)
100 (ditaa org-export-blocks-format-ditaa nil)
101 (dot org-export-blocks-format-dot nil))
102 "Use this alist to associate block types with block exporting functions.
103 The type of a block is determined by the text immediately
104 following the '#+BEGIN_' portion of the block header. Each block
105 export function should accept three arguments."
106 :group 'org-export-general
107 :type '(repeat
108 (list
109 (symbol :tag "Block name")
110 (function :tag "Block formatter")
111 (boolean :tag "Fontify content as Org syntax")))
112 :set 'org-export-blocks-set)
114 (defun org-export-blocks-add-block (block-spec)
115 "Add a new block type to `org-export-blocks'.
116 BLOCK-SPEC should be a three element list the first element of
117 which should indicate the name of the block, the second element
118 should be the formatting function called by
119 `org-export-blocks-preprocess' and the third element a flag
120 indicating whether these types of blocks should be fontified in
121 org-mode buffers (see `org-protecting-blocks'). For example the
122 BLOCK-SPEC for ditaa blocks is as follows.
124 (ditaa org-export-blocks-format-ditaa nil)"
125 (unless (member block-spec org-export-blocks)
126 (setq org-export-blocks (cons block-spec org-export-blocks))
127 (org-export-blocks-set 'org-export-blocks org-export-blocks)))
129 (defcustom org-export-interblocks
131 "Use this a-list to associate block types with block exporting functions.
132 The type of a block is determined by the text immediately
133 following the '#+BEGIN_' portion of the block header. Each block
134 export function should accept three arguments."
135 :group 'org-export-general
136 :type 'alist)
138 (defcustom org-export-blocks-witheld
139 '(hidden)
140 "List of block types (see `org-export-blocks') which should not be exported."
141 :group 'org-export-general
142 :type 'list)
144 (defcustom org-export-blocks-postblock-hook nil
145 "Run after blocks have been processed with `org-export-blocks-preprocess'."
146 :group 'org-export-general
147 :type 'hook)
149 (defun org-export-blocks-html-quote (body &optional open close)
150 "Protect BODY from org html export.
151 The optional OPEN and CLOSE tags will be inserted around BODY."
153 (concat
154 "\n#+BEGIN_HTML\n"
155 (or open "")
156 body (if (string-match "\n$" body) "" "\n")
157 (or close "")
158 "#+END_HTML\n"))
160 (defun org-export-blocks-latex-quote (body &optional open close)
161 "Protect BODY from org latex export.
162 The optional OPEN and CLOSE tags will be inserted around BODY."
163 (concat
164 "\n#+BEGIN_LaTeX\n"
165 (or open "")
166 body (if (string-match "\n$" body) "" "\n")
167 (or close "")
168 "#+END_LaTeX\n"))
170 (defun org-export-blocks-preprocess ()
171 "Export all blocks according to the `org-export-blocks' block export alist.
172 Does not export block types specified in specified in BLOCKS
173 which defaults to the value of `org-export-blocks-witheld'."
174 (interactive)
175 (save-window-excursion
176 (let ((case-fold-search t)
177 (types '())
178 indentation type func start body headers preserve-indent progress-marker)
179 (flet ((interblock (start end)
180 (mapcar (lambda (pair) (funcall (second pair) start end))
181 org-export-interblocks)))
182 (goto-char (point-min))
183 (setq start (point))
184 (while (re-search-forward org-exp-blocks-block-regexp nil t)
185 (setq indentation (length (match-string 1)))
186 (setq type (intern (downcase (match-string 2))))
187 (setq headers (save-match-data (org-split-string (match-string 3) "[ \t]+")))
188 (setq body (match-string 4))
189 (setq preserve-indent (or org-src-preserve-indentation (member "-i" headers)))
190 (unless preserve-indent
191 (setq body (save-match-data (org-remove-indentation body))))
192 (unless (memq type types) (setq types (cons type types)))
193 (save-match-data (interblock start (match-beginning 0)))
194 (when (setq func (cadr (assoc type org-export-blocks)))
195 (let ((replacement (save-match-data
196 (if (memq type org-export-blocks-witheld) ""
197 (apply func body headers)))))
198 (when replacement
199 (replace-match replacement t t)
200 (unless preserve-indent
201 (indent-code-rigidly
202 (match-beginning 0) (match-end 0) indentation)))))
203 (setq start (match-end 0)))
204 (interblock start (point-max))
205 (run-hooks 'org-export-blocks-postblock-hook)))))
207 ;;================================================================================
208 ;; type specific functions
210 ;;--------------------------------------------------------------------------------
211 ;; ditaa: create images from ASCII art using the ditaa utility
212 (defvar org-ditaa-jar-path (expand-file-name
213 "ditaa.jar"
214 (file-name-as-directory
215 (expand-file-name
216 "scripts"
217 (file-name-as-directory
218 (expand-file-name
219 "../contrib"
220 (file-name-directory (or load-file-name buffer-file-name)))))))
221 "Path to the ditaa jar executable.")
223 (defvar org-export-current-backend) ; dynamically bound in org-exp.el
224 (defun org-export-blocks-format-ditaa (body &rest headers)
225 "DEPRECATED: use begin_src ditaa code blocks
227 Pass block BODY to the ditaa utility creating an image.
228 Specify the path at which the image should be saved as the first
229 element of headers, any additional elements of headers will be
230 passed to the ditaa utility as command line arguments."
231 (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks")
232 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
233 (data-file (make-temp-file "org-ditaa"))
234 (hash (progn
235 (set-text-properties 0 (length body) nil body)
236 (sha1 (prin1-to-string (list body args)))))
237 (raw-out-file (if headers (car headers)))
238 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
239 (cons (match-string 1 raw-out-file)
240 (match-string 2 raw-out-file))
241 (cons raw-out-file "png")))
242 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
243 (unless (file-exists-p org-ditaa-jar-path)
244 (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
245 (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
246 body
247 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
248 (org-split-string body "\n")
249 "\n")))
250 (prog1
251 (cond
252 ((member org-export-current-backend '(html latex docbook))
253 (unless (file-exists-p out-file)
254 (mapc ;; remove old hashed versions of this file
255 (lambda (file)
256 (when (and (string-match (concat (regexp-quote (car out-file-parts))
257 "_\\([[:alnum:]]+\\)\\."
258 (regexp-quote (cdr out-file-parts)))
259 file)
260 (= (length (match-string 1 out-file)) 40))
261 (delete-file (expand-file-name file
262 (file-name-directory out-file)))))
263 (directory-files (or (file-name-directory out-file)
264 default-directory)))
265 (with-temp-file data-file (insert body))
266 (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
267 (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file)))
268 (format "\n[[file:%s]]\n" out-file))
269 (t (concat
270 "\n#+BEGIN_EXAMPLE\n"
271 body (if (string-match "\n$" body) "" "\n")
272 "#+END_EXAMPLE\n")))
273 (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks"))))
275 ;;--------------------------------------------------------------------------------
276 ;; dot: create graphs using the dot graphing language
277 ;; (require the dot executable to be in your path)
278 (defun org-export-blocks-format-dot (body &rest headers)
279 "DEPRECATED: use \"#+begin_src dot\" code blocks
281 Pass block BODY to the dot graphing utility creating an image.
282 Specify the path at which the image should be saved as the first
283 element of headers, any additional elements of headers will be
284 passed to the dot utility as command line arguments. Don't
285 forget to specify the output type for the dot command, so if you
286 are exporting to a file with a name like 'image.png' you should
287 include a '-Tpng' argument, and your block should look like the
288 following.
290 #+begin_dot models.png -Tpng
291 digraph data_relationships {
292 \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
293 \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
294 \"data_requirement\" -> \"data_product\"
296 #+end_dot"
297 (message "begin_dot blocks are DEPRECATED, use begin_src blocks")
298 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
299 (data-file (make-temp-file "org-ditaa"))
300 (hash (progn
301 (set-text-properties 0 (length body) nil body)
302 (sha1 (prin1-to-string (list body args)))))
303 (raw-out-file (if headers (car headers)))
304 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
305 (cons (match-string 1 raw-out-file)
306 (match-string 2 raw-out-file))
307 (cons raw-out-file "png")))
308 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
309 (prog1
310 (cond
311 ((member org-export-current-backend '(html latex docbook))
312 (unless (file-exists-p out-file)
313 (mapc ;; remove old hashed versions of this file
314 (lambda (file)
315 (when (and (string-match (concat (regexp-quote (car out-file-parts))
316 "_\\([[:alnum:]]+\\)\\."
317 (regexp-quote (cdr out-file-parts)))
318 file)
319 (= (length (match-string 1 out-file)) 40))
320 (delete-file (expand-file-name file
321 (file-name-directory out-file)))))
322 (directory-files (or (file-name-directory out-file)
323 default-directory)))
324 (with-temp-file data-file (insert body))
325 (message (concat "dot " data-file " " args " -o " out-file))
326 (shell-command (concat "dot " data-file " " args " -o " out-file)))
327 (format "\n[[file:%s]]\n" out-file))
328 (t (concat
329 "\n#+BEGIN_EXAMPLE\n"
330 body (if (string-match "\n$" body) "" "\n")
331 "#+END_EXAMPLE\n")))
332 (message "begin_dot blocks are DEPRECATED, use begin_src blocks"))))
334 ;;--------------------------------------------------------------------------------
335 ;; comment: export comments in author-specific css-stylable divs
336 (defun org-export-blocks-format-comment (body &rest headers)
337 "Format comment BODY by OWNER and return it formatted for export.
338 Currently, this only does something for HTML export, for all
339 other backends, it converts the comment into an EXAMPLE segment."
340 (let ((owner (if headers (car headers)))
341 (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
342 (cond
343 ((eq org-export-current-backend 'html) ;; We are exporting to HTML
344 (concat "#+BEGIN_HTML\n"
345 "<div class=\"org-comment\""
346 (if owner (format " id=\"org-comment-%s\" " owner))
347 ">\n"
348 (if owner (concat "<b>" owner "</b> ") "")
349 (if (and title (> (length title) 0)) (concat " -- " title "</br>\n") "</br>\n")
350 "<p>\n"
351 "#+END_HTML\n"
352 body
353 "\n#+BEGIN_HTML\n"
354 "</p>\n"
355 "</div>\n"
356 "#+END_HTML\n"))
357 (t ;; This is not HTML, so just make it an example.
358 (concat "#+BEGIN_EXAMPLE\n"
359 (if title (concat "Title:" title "\n") "")
360 (if owner (concat "By:" owner "\n") "")
361 body
362 (if (string-match "\n\\'" body) "" "\n")
363 "#+END_EXAMPLE\n")))))
365 (provide 'org-exp-blocks)
367 ;; arch-tag: 1c365fe9-8808-4f72-bb15-0b00f36d8024
368 ;;; org-exp-blocks.el ends here