org-exp-blocks.el 15 KB

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