org-exp-blocks.el 15 KB

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