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