org-exp-blocks.el 16 KB

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