org-exp-blocks.el 12 KB

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