Avoid empty strings when splitting header line in org-exp-blocks.
[rgr-org-mode.git] / lisp / org-exp-blocks.el
blob258d863c7c88c98bd8121c45a0b07577802b85ac
1 ;;; org-exp-blocks.el --- pre-process blocks when exporting org files
3 ;; Copyright (C) 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Eric Schulte
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 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This is a utility for pre-processing blocks in org files before
26 ;; export using the `org-export-preprocess-hook'. It can be used for
27 ;; exporting new types of blocks from org-mode files and also for
28 ;; changing the default export behavior of existing org-mode blocks.
29 ;; The `org-export-blocks' and `org-export-interblocks' variables can
30 ;; be used to control how blocks and the spaces between blocks
31 ;; respectively are processed upon export.
33 ;; The type of a block is defined as the string following =#+begin_=,
34 ;; so for example the following block would be of type ditaa. Note
35 ;; that both upper or lower case are allowed in =#+BEGIN_= and
36 ;; =#+END_=.
38 ;; #+begin_ditaa blue.png -r -S
39 ;; +---------+
40 ;; | cBLU |
41 ;; | |
42 ;; | +----+
43 ;; | |cPNK|
44 ;; | | |
45 ;; +----+----+
46 ;; #+end_ditaa
48 ;;; Currently Implemented Block Types
50 ;; ditaa :: Convert ascii pictures to actual images using ditaa
51 ;; http://ditaa.sourceforge.net/. To use this set
52 ;; `org-ditaa-jar-path' to the path to ditaa.jar on your
53 ;; system (should be set automatically in most cases) .
55 ;; dot :: Convert graphs defined using the dot graphing language to
56 ;; images using the dot utility. For information on dot see
57 ;; http://www.graphviz.org/
59 ;; comment :: Wrap comments with titles and author information, in
60 ;; their own divs with author-specific ids allowing for css
61 ;; coloring of comments based on the author.
63 ;;; Adding new blocks
65 ;; When adding a new block type first define a formatting function
66 ;; along the same lines as `org-export-blocks-format-dot' and then use
67 ;; `org-export-blocks-add-block' to add your block type to
68 ;; `org-export-blocks'.
70 (eval-when-compile
71 (require 'cl))
72 (require 'org)
74 (defvar htmlp)
75 (defvar latexp)
76 (defvar docbookp)
77 (defvar asciip)
79 (defun org-export-blocks-set (var value)
80 "Set the value of `org-export-blocks' and install fontification."
81 (set var value)
82 (mapc (lambda (spec)
83 (if (nth 2 spec)
84 (setq org-protecting-blocks
85 (delete (symbol-name (car spec))
86 org-protecting-blocks))
87 (add-to-list 'org-protecting-blocks
88 (symbol-name (car spec)))))
89 value))
91 (defcustom org-export-blocks
92 '((comment org-export-blocks-format-comment t)
93 (ditaa org-export-blocks-format-ditaa nil)
94 (dot org-export-blocks-format-dot nil))
95 "Use this a-list to associate block types with block exporting
96 functions. The type of a block is determined by the text
97 immediately following the '#+BEGIN_' portion of the block header.
98 Each block export function should accept three argumets..."
99 :group 'org-export-general
100 :type '(repeat
101 (list
102 (symbol :tag "Block name")
103 (function :tag "Block formatter")
104 (boolean :tag "Fontify content as Org syntax")))
105 :set 'org-export-blocks-set)
107 (defun org-export-blocks-add-block (block-spec)
108 "Add a new block type to `org-export-blocks'. BLOCK-SPEC
109 should be a three element list the first element of which should
110 indicate the name of the block, the second element should be the
111 formatting function called by `org-export-blocks-preprocess' and
112 the third element a flag indicating whether these types of blocks
113 should be fontified in org-mode buffers (see
114 `org-protecting-blocks'). For example the BLOCK-SPEC for ditaa
115 blocks is as follows...
117 (ditaa org-export-blocks-format-ditaa nil)"
118 (unless (member block-spec org-export-blocks)
119 (setq org-export-blocks (cons block-spec org-export-blocks))
120 (org-export-blocks-set 'org-export-blocks org-export-blocks)))
122 (defcustom org-export-interblocks
124 "Use this a-list to associate block types with block exporting
125 functions. The type of a block is determined by the text
126 immediately following the '#+BEGIN_' portion of the block header.
127 Each block export function should accept three argumets..."
128 :group 'org-export-general
129 :type 'alist)
131 (defcustom org-export-blocks-witheld
132 '(hidden)
133 "List of block types (see `org-export-blocks') which should not
134 be exported."
135 :group 'org-export-general
136 :type 'list)
138 (defvar org-export-blocks-postblock-hooks nil "")
140 (defun org-export-blocks-html-quote (body &optional open close)
141 "Protext BODY from org html export. The optional OPEN and
142 CLOSE tags will be inserted around BODY."
143 (concat
144 "\n#+BEGIN_HTML\n"
145 (or open "")
146 body (if (string-match "\n$" body) "" "\n")
147 (or close "")
148 "#+END_HTML\n"))
150 (defun org-export-blocks-latex-quote (body &optional open close)
151 "Protext BODY from org latex export. The optional OPEN and
152 CLOSE tags will be inserted around BODY."
153 (concat
154 "\n#+BEGIN_LaTeX\n"
155 (or open "")
156 body (if (string-match "\n$" body) "" "\n")
157 (or close "")
158 "#+END_LaTeX\n"))
160 (defun org-export-blocks-preprocess ()
161 "Export all blocks acording to the `org-export-blocks' block
162 exportation alist. Does not export block types specified in
163 specified in BLOCKS which default to the value of
164 `org-export-blocks-witheld'."
165 (interactive)
166 (save-window-excursion
167 (let ((case-fold-search t)
168 (types '())
169 indentation type func start body headers preserve-indent)
170 (flet ((interblock (start end)
171 (mapcar (lambda (pair) (funcall (second pair) start end))
172 org-export-interblocks)))
173 (goto-char (point-min))
174 (setq start (point))
175 (while (re-search-forward
176 "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)[\r\n][ \t]*#\\+end_\\S-+.*" nil t)
177 (setq indentation (length (match-string 1)))
178 (setq type (intern (match-string 2)))
179 (setq headers (save-match-data (org-split-string (match-string 3) "[ \t]+")))
180 (setq body (match-string 4))
181 (setq preserve-indent (or org-src-preserve-indentation (member "-i" headers)))
182 (unless preserve-indent
183 (setq body (save-match-data (org-remove-indentation body))))
184 (unless (memq type types) (setq types (cons type types)))
185 (save-match-data (interblock start (match-beginning 0)))
186 (if (setq func (cadr (assoc type org-export-blocks)))
187 (progn
188 (replace-match (save-match-data
189 (if (memq type org-export-blocks-witheld) ""
190 (apply func body headers))) t t)
191 (unless preserve-indent
192 (indent-code-rigidly (match-beginning 0) (match-end 0) indentation))))
193 (setq start (match-end 0)))
194 (interblock start (point-max))))))
196 (add-hook 'org-export-preprocess-hook 'org-export-blocks-preprocess)
198 ;;================================================================================
199 ;; type specific functions
201 ;;--------------------------------------------------------------------------------
202 ;; ditaa: create images from ASCII art using the ditaa utility
203 (defvar org-ditaa-jar-path (expand-file-name
204 "ditaa.jar"
205 (file-name-as-directory
206 (expand-file-name
207 "scripts"
208 (file-name-as-directory
209 (expand-file-name
210 "../contrib"
211 (file-name-directory (or load-file-name buffer-file-name)))))))
212 "Path to the ditaa jar executable")
214 (defun org-export-blocks-format-ditaa (body &rest headers)
215 "Pass block BODY to the ditaa utility creating an image.
216 Specify the path at which the image should be saved as the first
217 element of headers, any additional elements of headers will be
218 passed to the ditaa utility as command line arguments."
219 (message "ditaa-formatting...")
220 (let ((out-file (if headers (car headers)))
221 (args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
222 (data-file (make-temp-file "org-ditaa")))
223 (unless (file-exists-p org-ditaa-jar-path)
224 (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
225 (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
226 body
227 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
228 (org-split-string body "\n")
229 "\n")))
230 (cond
231 ((or htmlp latexp docbookp)
232 (with-temp-file data-file (insert body))
233 (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
234 (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
235 (format "\n[[file:%s]]\n" out-file))
236 (t (concat
237 "\n#+BEGIN_EXAMPLE\n"
238 body (if (string-match "\n$" body) "" "\n")
239 "#+END_EXAMPLE\n")))))
241 ;;--------------------------------------------------------------------------------
242 ;; dot: create graphs using the dot graphing language
243 ;; (require the dot executable to be in your path)
244 (defun org-export-blocks-format-dot (body &rest headers)
245 "Pass block BODY to the dot graphing utility creating an image.
246 Specify the path at which the image should be saved as the first
247 element of headers, any additional elements of headers will be
248 passed to the dot utility as command line arguments. Don't
249 forget to specify the output type for the dot command, so if you
250 are exporting to a file with a name like 'image.png' you should
251 include a '-Tpng' argument, and your block should look like the
252 following.
254 #+begin_dot models.png -Tpng
255 digraph data_relationships {
256 \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
257 \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
258 \"data_requirement\" -> \"data_product\"
260 #+end_dot"
261 (message "dot-formatting...")
262 (let ((out-file (if headers (car headers)))
263 (args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
264 (data-file (make-temp-file "org-ditaa")))
265 (cond
266 ((or htmlp latexp docbookp)
267 (with-temp-file data-file (insert body))
268 (message (concat "dot " data-file " " args " -o " out-file))
269 (shell-command (concat "dot " data-file " " args " -o " out-file))
270 (format "\n[[file:%s]]\n" out-file))
271 (t (concat
272 "\n#+BEGIN_EXAMPLE\n"
273 body (if (string-match "\n$" body) "" "\n")
274 "#+END_EXAMPLE\n")))))
276 ;;--------------------------------------------------------------------------------
277 ;; comment: export comments in author-specific css-stylable divs
278 (defun org-export-blocks-format-comment (body &rest headers)
279 "Format comment BODY by OWNER and return it formatted for export.
280 Currently, this only does something for HTML export, for all
281 other backends, it converts the comment into an EXAMPLE segment."
282 (let ((owner (if headers (car headers)))
283 (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
284 (cond
285 (htmlp ;; We are exporting to HTML
286 (concat "#+BEGIN_HTML\n"
287 "<div class=\"org-comment\""
288 (if owner (format " id=\"org-comment-%s\" " owner))
289 ">\n"
290 (if owner (concat "<b>" owner "</b> ") "")
291 (if (and title (> (length title) 0)) (concat " -- " title "</br>\n") "</br>\n")
292 "<p>\n"
293 "#+END_HTML\n"
294 body
295 "#+BEGIN_HTML\n"
296 "</p>\n"
297 "</div>\n"
298 "#+END_HTML\n"))
299 (t ;; This is not HTML, so just make it an example.
300 (concat "#+BEGIN_EXAMPLE\n"
301 (if title (concat "Title:" title "\n") "")
302 (if owner (concat "By:" owner "\n") "")
303 body
304 (if (string-match "\n\\'" body) "" "\n")
305 "#+END_EXAMPLE\n")))))
307 (provide 'org-exp-blocks)
309 ;; arch-tag: 1c365fe9-8808-4f72-bb15-0b00f36d8024
310 ;;; org-exp-blocks.el ends here