org-exp-blocks.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. ;;; org-exp-blocks.el --- pre-process blocks when exporting org files
  2. ;; Copyright (C) 2008 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; This file is not currently part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or
  6. ;; modify it under the terms of the GNU General Public License as
  7. ;; published by the Free Software Foundation; either version 2, or (at
  8. ;; your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful, but
  10. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program ; see the file COPYING. If not, write to
  15. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. ;; Boston, MA 02111-1307, USA.
  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' alist can be
  24. ;; 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. (defcustom org-export-blocks
  62. '((comment org-export-blocks-format-comment)
  63. (ditaa org-export-blocks-format-ditaa)
  64. (dot org-export-blocks-format-dot)
  65. (r org-export-blocks-format-R)
  66. (R org-export-blocks-format-R))
  67. "Use this a-list to associate block types with block exporting
  68. functions. The type of a block is determined by the text
  69. immediately following the '#+BEGIN_' portion of the block header.
  70. Each block export function should accept three argumets..."
  71. :group 'org-export-general
  72. :type 'alist)
  73. (defcustom org-export-interblocks
  74. '(
  75. (r org-export-interblocks-format-R)
  76. (R org-export-interblocks-format-R)
  77. )
  78. "Use this a-list to associate block types with block exporting
  79. functions. The type of a block is determined by the text
  80. immediately following the '#+BEGIN_' portion of the block header.
  81. Each block export function should accept three argumets..."
  82. :group 'org-export-general
  83. :type 'alist)
  84. (defcustom org-export-blocks-witheld
  85. '(hidden)
  86. "List of block types (see `org-export-blocks') which should not
  87. be exported."
  88. :group 'org-export-general
  89. :type 'list)
  90. (defvar org-export-blocks-postblock-hooks nil "")
  91. (defun org-export-blocks-html-quote (body &optional open close)
  92. "Protext BODY from org html export. The optional OPEN and
  93. CLOSE tags will be inserted around BODY."
  94. (concat
  95. "\n#+BEGIN_HTML\n"
  96. (or open "")
  97. body (if (string-match "\n$" body) "" "\n")
  98. (or close "")
  99. "#+END_HTML\n"))
  100. (defun org-export-blocks-latex-quote (body &optional open close)
  101. "Protext BODY from org latex export. The optional OPEN and
  102. CLOSE tags will be inserted around BODY."
  103. (concat
  104. "\n#+BEGIN_LaTeX\n"
  105. (or open "")
  106. body (if (string-match "\n$" body) "" "\n")
  107. (or close "")
  108. "#+END_LaTeX\n"))
  109. (defun org-export-blocks-preprocess ()
  110. "Export all blocks acording to the `org-export-blocks' block
  111. exportation alist. Does not export block types specified in
  112. specified in BLOCKS which default to the value of
  113. `org-export-blocks-witheld'."
  114. (interactive)
  115. (save-window-excursion
  116. (let ((count 0)
  117. (blocks org-export-blocks-witheld)
  118. (case-fold-search t)
  119. (types '())
  120. type func start end)
  121. (flet ((interblock (start end type)
  122. (save-match-data
  123. (when (setf func (cadr (assoc type org-export-interblocks)))
  124. (funcall func start end)))))
  125. (goto-char (point-min))
  126. (setf start (point))
  127. (while (re-search-forward
  128. "^#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)#\\+end_\\S-*[\r\n]" nil t)
  129. (save-match-data (setf type (intern (match-string 1))))
  130. (unless (memq type types) (setf types (cons type types)))
  131. (setf end (save-match-data (match-beginning 0)))
  132. (interblock start end type)
  133. (if (setf func (cadr (assoc type org-export-blocks)))
  134. (replace-match (save-match-data
  135. (if (memq type blocks)
  136. ""
  137. (apply func (match-string 3) (split-string (match-string 2) " ")))) t t))
  138. (setf start (save-match-data (match-end 0))))
  139. (mapcar (lambda (type)
  140. (interblock start (point-max) type))
  141. types)))))
  142. (add-hook 'org-export-preprocess-hook 'org-export-blocks-preprocess)
  143. ;;================================================================================
  144. ;; type specific functions
  145. ;;--------------------------------------------------------------------------------
  146. ;; ditaa: create images from ASCII art using the ditaa utility
  147. (defvar org-ditaa-jar-path (expand-file-name
  148. "ditaa.jar"
  149. (file-name-as-directory
  150. (expand-file-name
  151. "scripts"
  152. (file-name-as-directory
  153. (expand-file-name
  154. ".."
  155. (file-name-directory (or load-file-name buffer-file-name)))))))
  156. "Path to the ditaa jar executable")
  157. (defun org-export-blocks-format-ditaa (body &rest headers)
  158. "Pass block BODY to the ditaa utility creating an image.
  159. Specify the path at which the image should be saved as the first
  160. element of headers, any additional elements of headers will be
  161. passed to the ditaa utility as command line arguments."
  162. (message "ditaa-formatting...")
  163. (let ((out-file (if headers (car headers)))
  164. (args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
  165. (data-file (make-temp-file "org-ditaa")))
  166. (unless (file-exists-p org-ditaa-jar-path)
  167. (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
  168. (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
  169. body
  170. (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
  171. (org-split-string body "\n")
  172. "\n")))
  173. (cond
  174. ((or htmlp latexp)
  175. (with-temp-file data-file (insert body))
  176. (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
  177. (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
  178. (format "\n[[file:%s]]\n" out-file))
  179. (t (concat
  180. "\n#+BEGIN_EXAMPLE\n"
  181. body (if (string-match "\n$" body) "" "\n")
  182. "#+END_EXAMPLE\n")))))
  183. ;;--------------------------------------------------------------------------------
  184. ;; dot: create graphs using the dot graphing language
  185. ;; (require the dot executable to be in your path)
  186. (defun org-export-blocks-format-dot (body &rest headers)
  187. "Pass block BODY to the dot graphing utility creating an image.
  188. Specify the path at which the image should be saved as the first
  189. element of headers, any additional elements of headers will be
  190. passed to the dot utility as command line arguments. Don't
  191. forget to specify the output type for the dot command, so if you
  192. are exporting to a file with a name like 'image.png' you should
  193. include a '-Tpng' argument, and your block should look like the
  194. following.
  195. #+begin_dot models.png -Tpng
  196. digraph data_relationships {
  197. \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
  198. \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
  199. \"data_requirement\" -> \"data_product\"
  200. }
  201. #+end_dot"
  202. (message "dot-formatting...")
  203. (let ((out-file (if headers (car headers)))
  204. (args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
  205. (data-file (make-temp-file "org-ditaa")))
  206. (cond
  207. ((or htmlp latexp)
  208. (with-temp-file data-file (insert body))
  209. (message (concat "dot " data-file " " args " -o " out-file))
  210. (shell-command (concat "dot " data-file " " args " -o " out-file))
  211. (format "\n[[file:%s]]\n" out-file))
  212. (t (concat
  213. "\n#+BEGIN_EXAMPLE\n"
  214. body (if (string-match "\n$" body) "" "\n")
  215. "#+END_EXAMPLE\n")))))
  216. ;;--------------------------------------------------------------------------------
  217. ;; comment: export comments in author-specific css-stylable divs
  218. (defun org-export-blocks-format-comment (body &rest headers)
  219. "Format comment BODY by OWNER and return it formatted for export.
  220. Currently, this only does something for HTML export, for all
  221. other backends, it converts the comment into an EXAMPLE segment."
  222. (let ((owner (if headers (car headers)))
  223. (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
  224. (cond
  225. (htmlp ;; We are exporting to HTML
  226. (concat "#+BEGIN_HTML\n"
  227. "<div class=\"org-comment\""
  228. (if owner (format " id=\"org-comment-%s\" " owner))
  229. ">\n"
  230. (if owner (concat "<b>" owner "</b> ") "")
  231. (if (and title (> (length title) 0)) (concat " -- " title "</br>\n") "</br>\n")
  232. "<p>\n"
  233. "#+END_HTML\n"
  234. body
  235. "#+BEGIN_HTML\n"
  236. "</p>\n"
  237. "</div>\n"
  238. "#+END_HTML\n"))
  239. (t ;; This is not HTML, so just make it an example.
  240. (concat "#+BEGIN_EXAMPLE\n"
  241. (if title (concat "Title:" title "\n") "")
  242. (if owner (concat "By:" owner "\n") "")
  243. body
  244. (if (string-match "\n\\'" body) "" "\n")
  245. "#+END_EXAMPLE\n")))))
  246. ;;--------------------------------------------------------------------------------
  247. ;; R: Sweave-type functionality
  248. (defvar interblock-R-buffer nil
  249. "Holds the buffer for the current R process")
  250. (defun org-export-blocks-format-R (body &rest headers)
  251. "Process R blocks and replace \R{} forms outside the blocks
  252. with their values as determined by R."
  253. (interactive)
  254. (message "R processing...")
  255. (let ((image-path (or (and (car headers)
  256. (string-match "\\(.?\\)\.\\(EPS\\|eps\\)" (car headers))
  257. (match-string 1 (car headers)))
  258. (and (> (length (car headers)) 0)
  259. (car headers))
  260. ;; create the default filename
  261. (format "Rplot-%03d" count)))
  262. (plot (string-match "plot" body))
  263. R-proc)
  264. (setf count (+ count 1))
  265. (interblock-initiate-R-buffer)
  266. (setf R-proc (get-buffer-process interblock-R-buffer))
  267. ;; send strings to the ESS process using `comint-send-string'
  268. (setf body (mapconcat (lambda (line)
  269. (interblock-R-input-command line) (concat "> " line))
  270. (butlast (split-string body "[\r\n]"))
  271. "\n"))
  272. ;; if there is a plot command, then create the images
  273. (when plot
  274. (interblock-R-input-command (format "dev.copy2eps(file=\"%s.eps\")" image-path)))
  275. (concat (cond
  276. (htmlp (org-export-blocks-html-quote body
  277. (format "<div id=\"R-%d\">\n<pre>\n" count)
  278. "</pre>\n</div>\n"))
  279. (latexp (org-export-blocks-latex-quote body
  280. "\\begin{Schunk}\n\\begin{Sinput}\n"
  281. "\\end{Sinput}\n\\end{Schunk}\n"))
  282. (t (insert ;; default export
  283. "#+begin_R " (mapconcat 'identity headers " ") "\n"
  284. body (if (string-match "\n$" body) "" "\n")
  285. "#+end_R\n")))
  286. (if plot
  287. (format "[[file:%s.eps]]\n" image-path)
  288. ""))))
  289. (defun org-export-interblocks-format-R (start end)
  290. "This is run over parts of the org-file which are between R
  291. blocks. It's main use is to expand the \R{stuff} chunks for
  292. export."
  293. (save-excursion
  294. (goto-char start)
  295. (interblock-initiate-R-buffer)
  296. (let (code replacement)
  297. (while (and (< (point) end) (re-search-forward "\\\\R{\\([^}]*\\)}" end t))
  298. (save-match-data (setf code (match-string 1)))
  299. (message (format "\R{%s}" code))
  300. (setf replacement (interblock-R-command-to-string code))
  301. (setf replacement (cond
  302. (htmlp replacement)
  303. (latexp replacement)
  304. (t replacement)))
  305. (setf end (+ end (- (length replacement) (length code))))
  306. (replace-match replacement t t)))))
  307. (defun interblock-initiate-R-buffer ()
  308. "If there is not a current R process then create one."
  309. (unless (and (buffer-live-p interblock-R-buffer) (get-buffer interblock-R-buffer))
  310. (save-excursion
  311. (R)
  312. (setf interblock-R-buffer (current-buffer))
  313. (interblock-R-wait-for-output)
  314. (interblock-R-input-command ""))))
  315. (defun interblock-R-command-to-string (command)
  316. "Send a command to R, and return the results as a string."
  317. (interblock-R-input-command command)
  318. (interblock-R-last-output))
  319. (defun interblock-R-input-command (command)
  320. "Pass COMMAND to the R process running in `interblock-R-buffer'."
  321. (save-excursion
  322. (save-match-data
  323. (set-buffer interblock-R-buffer)
  324. (goto-char (process-mark (get-buffer-process (current-buffer))))
  325. (insert command)
  326. (comint-send-input)
  327. (interblock-R-wait-for-output))))
  328. (defun interblock-R-wait-for-output ()
  329. "Wait until output arrives"
  330. (save-excursion
  331. (save-match-data
  332. (set-buffer interblock-R-buffer)
  333. (while (progn
  334. (goto-char comint-last-input-end)
  335. (not (re-search-forward comint-prompt-regexp nil t)))
  336. (accept-process-output (get-buffer-process (current-buffer)))))))
  337. (defun interblock-R-last-output ()
  338. "Return the last R output as a string"
  339. (save-excursion
  340. (save-match-data
  341. (set-buffer interblock-R-buffer)
  342. (goto-char (process-mark (get-buffer-process (current-buffer))))
  343. (forward-line 0)
  344. (let ((raw (buffer-substring comint-last-input-end (- (point) 1))))
  345. (if (string-match "\n" raw)
  346. raw
  347. (and (string-match "\\[[[:digit:]+]\\] *\\(.*\\)$" raw)
  348. (message raw)
  349. (message (match-string 1 raw))
  350. (match-string 1 raw)))))))
  351. (provide 'org-exp-blocks)
  352. ;;; org-exp-blocks.el ends here