org-exp-blocks.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. ;;; org-exp-blocks.el --- pre-process blocks when exporting org files
  2. ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Version: 7.7
  5. ;; This file is part of GNU Emacs.
  6. ;;
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;
  19. ;; This is a utility for pre-processing blocks in org files before
  20. ;; export using the `org-export-preprocess-hook'. It can be used for
  21. ;; exporting new types of blocks from org-mode files and also for
  22. ;; changing the default export behavior of existing org-mode blocks.
  23. ;; The `org-export-blocks' and `org-export-interblocks' variables can
  24. ;; be used to control how blocks and the spaces between blocks
  25. ;; respectively are processed upon export.
  26. ;;
  27. ;; The type of a block is defined as the string following =#+begin_=,
  28. ;; so for example the following block would be of type ditaa. Note
  29. ;; that both upper or lower case are allowed in =#+BEGIN_= and
  30. ;; =#+END_=.
  31. ;;
  32. ;; #+begin_ditaa blue.png -r -S
  33. ;; +---------+
  34. ;; | cBLU |
  35. ;; | |
  36. ;; | +----+
  37. ;; | |cPNK|
  38. ;; | | |
  39. ;; +----+----+
  40. ;; #+end_ditaa
  41. ;;
  42. ;;; Currently Implemented Block Types
  43. ;;
  44. ;; ditaa :: (DEPRECATED--use "#+begin_src ditaa" code blocks) Convert
  45. ;; ascii pictures to actual images using ditaa
  46. ;; http://ditaa.sourceforge.net/. To use this set
  47. ;; `org-ditaa-jar-path' to the path to ditaa.jar on your
  48. ;; system (should be set automatically in most cases) .
  49. ;;
  50. ;; dot :: (DEPRECATED--use "#+begin_src dot" code blocks) Convert
  51. ;; graphs defined using the dot graphing language to images
  52. ;; using the dot utility. For information on dot see
  53. ;; http://www.graphviz.org/
  54. ;;
  55. ;; comment :: Wrap comments with titles and author information, in
  56. ;; their own divs with author-specific ids allowing for css
  57. ;; coloring of comments based on the author.
  58. ;;
  59. ;;; Adding new blocks
  60. ;;
  61. ;; When adding a new block type first define a formatting function
  62. ;; along the same lines as `org-export-blocks-format-dot' and then use
  63. ;; `org-export-blocks-add-block' to add your block type to
  64. ;; `org-export-blocks'.
  65. ;;; Code:
  66. (eval-when-compile
  67. (require 'cl))
  68. (require 'org)
  69. (defun org-export-blocks-set (var value)
  70. "Set the value of `org-export-blocks' and install fontification."
  71. (set var value)
  72. (mapc (lambda (spec)
  73. (if (nth 2 spec)
  74. (setq org-protecting-blocks
  75. (delete (symbol-name (car spec))
  76. org-protecting-blocks))
  77. (add-to-list 'org-protecting-blocks
  78. (symbol-name (car spec)))))
  79. value))
  80. (defcustom org-export-blocks
  81. '((comment org-export-blocks-format-comment t)
  82. (ditaa org-export-blocks-format-ditaa nil)
  83. (dot org-export-blocks-format-dot nil))
  84. "Use this alist to associate block types with block exporting functions.
  85. The type of a block is determined by the text immediately
  86. following the '#+BEGIN_' portion of the block header. Each block
  87. export function should accept three arguments."
  88. :group 'org-export-general
  89. :type '(repeat
  90. (list
  91. (symbol :tag "Block name")
  92. (function :tag "Block formatter")
  93. (boolean :tag "Fontify content as Org syntax")))
  94. :set 'org-export-blocks-set)
  95. (defun org-export-blocks-add-block (block-spec)
  96. "Add a new block type to `org-export-blocks'.
  97. BLOCK-SPEC should be a three element list the first element of
  98. which should indicate the name of the block, the second element
  99. should be the formatting function called by
  100. `org-export-blocks-preprocess' and the third element a flag
  101. indicating whether these types of blocks should be fontified in
  102. org-mode buffers (see `org-protecting-blocks'). For example the
  103. BLOCK-SPEC for ditaa blocks is as follows.
  104. (ditaa org-export-blocks-format-ditaa nil)"
  105. (unless (member block-spec org-export-blocks)
  106. (setq org-export-blocks (cons block-spec org-export-blocks))
  107. (org-export-blocks-set 'org-export-blocks org-export-blocks)))
  108. (defcustom org-export-interblocks
  109. '()
  110. "Use this a-list to associate block types with block exporting functions.
  111. The type of a block is determined by the text immediately
  112. following the '#+BEGIN_' portion of the block header. Each block
  113. export function should accept three arguments."
  114. :group 'org-export-general
  115. :type 'alist)
  116. (defcustom org-export-blocks-witheld
  117. '(hidden)
  118. "List of block types (see `org-export-blocks') which should not be exported."
  119. :group 'org-export-general
  120. :type 'list)
  121. (defcustom org-export-blocks-postblock-hook nil
  122. "Run after blocks have been processed with `org-export-blocks-preprocess'."
  123. :group 'org-export-general
  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 (match-beginning 0))
  161. (body-start (match-end 0))
  162. (indentation (length (match-string 1)))
  163. (inner-re (format "[\r\n]*[ \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 (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. (setq start (point))))
  197. (interblock start (point-max))
  198. (run-hooks 'org-export-blocks-postblock-hook)))))
  199. ;;================================================================================
  200. ;; 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.")
  213. (defvar org-export-current-backend) ; dynamically bound in org-exp.el
  214. (defun org-export-blocks-format-ditaa (body &rest headers)
  215. "DEPRECATED: use begin_src ditaa code blocks
  216. Pass block BODY to the ditaa utility creating an image.
  217. Specify the path at which the image should be saved as the first
  218. element of headers, any additional elements of headers will be
  219. passed to the ditaa utility as command line arguments."
  220. (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks")
  221. (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
  222. (data-file (make-temp-file "org-ditaa"))
  223. (hash (progn
  224. (set-text-properties 0 (length body) nil body)
  225. (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))))
  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. (prog1
  240. (cond
  241. ((member org-export-current-backend '(html latex docbook))
  242. (unless (file-exists-p out-file)
  243. (mapc ;; remove old hashed versions of this file
  244. (lambda (file)
  245. (when (and (string-match (concat (regexp-quote (car out-file-parts))
  246. "_\\([[:alnum:]]+\\)\\."
  247. (regexp-quote (cdr out-file-parts)))
  248. file)
  249. (= (length (match-string 1 out-file)) 40))
  250. (delete-file (expand-file-name file
  251. (file-name-directory out-file)))))
  252. (directory-files (or (file-name-directory out-file)
  253. default-directory)))
  254. (with-temp-file data-file (insert body))
  255. (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
  256. (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file)))
  257. (format "\n[[file:%s]]\n" out-file))
  258. (t (concat
  259. "\n#+BEGIN_EXAMPLE\n"
  260. body (if (string-match "\n$" body) "" "\n")
  261. "#+END_EXAMPLE\n")))
  262. (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks"))))
  263. ;;--------------------------------------------------------------------------------
  264. ;; dot: create graphs using the dot graphing language
  265. ;; (require the dot executable to be in your path)
  266. (defun org-export-blocks-format-dot (body &rest headers)
  267. "DEPRECATED: use \"#+begin_src dot\" code blocks
  268. Pass block BODY to the dot graphing utility creating an image.
  269. Specify the path at which the image should be saved as the first
  270. element of headers, any additional elements of headers will be
  271. passed to the dot utility as command line arguments. Don't
  272. forget to specify the output type for the dot command, so if you
  273. are exporting to a file with a name like 'image.png' you should
  274. include a '-Tpng' argument, and your block should look like the
  275. following.
  276. #+begin_dot models.png -Tpng
  277. digraph data_relationships {
  278. \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
  279. \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
  280. \"data_requirement\" -> \"data_product\"
  281. }
  282. #+end_dot"
  283. (message "begin_dot blocks are DEPRECATED, use begin_src blocks")
  284. (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
  285. (data-file (make-temp-file "org-ditaa"))
  286. (hash (progn
  287. (set-text-properties 0 (length body) nil body)
  288. (sha1 (prin1-to-string (list body args)))))
  289. (raw-out-file (if headers (car headers)))
  290. (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
  291. (cons (match-string 1 raw-out-file)
  292. (match-string 2 raw-out-file))
  293. (cons raw-out-file "png")))
  294. (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
  295. (prog1
  296. (cond
  297. ((member org-export-current-backend '(html latex docbook))
  298. (unless (file-exists-p out-file)
  299. (mapc ;; remove old hashed versions of this file
  300. (lambda (file)
  301. (when (and (string-match (concat (regexp-quote (car out-file-parts))
  302. "_\\([[:alnum:]]+\\)\\."
  303. (regexp-quote (cdr out-file-parts)))
  304. file)
  305. (= (length (match-string 1 out-file)) 40))
  306. (delete-file (expand-file-name file
  307. (file-name-directory out-file)))))
  308. (directory-files (or (file-name-directory out-file)
  309. default-directory)))
  310. (with-temp-file data-file (insert body))
  311. (message (concat "dot " data-file " " args " -o " out-file))
  312. (shell-command (concat "dot " data-file " " args " -o " out-file)))
  313. (format "\n[[file:%s]]\n" out-file))
  314. (t (concat
  315. "\n#+BEGIN_EXAMPLE\n"
  316. body (if (string-match "\n$" body) "" "\n")
  317. "#+END_EXAMPLE\n")))
  318. (message "begin_dot blocks are DEPRECATED, use begin_src blocks"))))
  319. ;;--------------------------------------------------------------------------------
  320. ;; comment: export comments in author-specific css-stylable divs
  321. (defun org-export-blocks-format-comment (body &rest headers)
  322. "Format comment BODY by OWNER and return it formatted for export.
  323. Currently, this only does something for HTML export, for all
  324. other backends, it converts the comment into an EXAMPLE segment."
  325. (let ((owner (if headers (car headers)))
  326. (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
  327. (cond
  328. ((eq org-export-current-backend 'html) ;; We are exporting to HTML
  329. (concat "#+BEGIN_HTML\n"
  330. "<div class=\"org-comment\""
  331. (if owner (format " id=\"org-comment-%s\" " owner))
  332. ">\n"
  333. (if owner (concat "<b>" owner "</b> ") "")
  334. (if (and title (> (length title) 0)) (concat " -- " title "<br/>\n") "<br/>\n")
  335. "<p>\n"
  336. "#+END_HTML\n"
  337. body
  338. "\n#+BEGIN_HTML\n"
  339. "</p>\n"
  340. "</div>\n"
  341. "#+END_HTML\n"))
  342. (t ;; This is not HTML, so just make it an example.
  343. (concat "#+BEGIN_EXAMPLE\n"
  344. (if title (concat "Title:" title "\n") "")
  345. (if owner (concat "By:" owner "\n") "")
  346. body
  347. (if (string-match "\n\\'" body) "" "\n")
  348. "#+END_EXAMPLE\n")))))
  349. (provide 'org-exp-blocks)
  350. ;; arch-tag: 1c365fe9-8808-4f72-bb15-0b00f36d8024
  351. ;;; org-exp-blocks.el ends here