org-exp-blocks.el 15 KB

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