org-exp-blocks.el 14 KB

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