ob-plantuml.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ;;; ob-plantuml.el --- Babel Functions for Plantuml -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. ;; Author: Zhang Weize
  4. ;; Keywords: literate programming, reproducible research
  5. ;; URL: https://orgmode.org
  6. ;; This file is part of GNU Emacs.
  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 <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating plantuml script.
  19. ;;
  20. ;; Inspired by Ian Yang's org-export-blocks-format-plantuml
  21. ;; https://www.emacswiki.org/emacs/org-export-blocks-format-plantuml.el
  22. ;;; Requirements:
  23. ;; plantuml | https://plantuml.com/
  24. ;; plantuml.jar | `org-plantuml-jar-path' should point to the jar file (when exec mode is `jar')
  25. ;;; Code:
  26. (require 'org-macs)
  27. (org-assert-version)
  28. (require 'ob)
  29. (defvar org-babel-default-header-args:plantuml
  30. '((:results . "file") (:exports . "results"))
  31. "Default arguments for evaluating a plantuml source block.")
  32. (defcustom org-plantuml-jar-path ""
  33. "Path to the plantuml.jar file."
  34. :group 'org-babel
  35. :version "24.1"
  36. :type 'string)
  37. (defcustom org-plantuml-exec-mode 'jar
  38. "Method to use for PlantUML diagram generation.
  39. `jar' means to use java together with the JAR.
  40. The JAR can be configured via `org-plantuml-jar-path'.
  41. `plantuml' means to use the PlantUML executable.
  42. The executable can be configured via `org-plantuml-executable-path'.
  43. You can also configure extra arguments via `org-plantuml-executable-args'."
  44. :group 'org-babel
  45. :package-version '(Org . "9.4")
  46. :type 'symbol
  47. :options '(jar plantuml))
  48. (defcustom org-plantuml-executable-path "plantuml"
  49. "File name of the PlantUML executable."
  50. :group 'org-babel
  51. :package-version '(Org . "9.4")
  52. :type 'string)
  53. (defcustom org-plantuml-args (list "-headless")
  54. "The arguments passed to plantuml when executing PlantUML."
  55. :group 'org-babel
  56. :package-version '(Org . "9.4")
  57. :type '(repeat string))
  58. (defcustom org-babel-plantuml-svg-text-to-path nil
  59. "When non-nil, export text in SVG images to paths using Inkscape."
  60. :group 'org-babel
  61. :package-version '(Org . "9.5")
  62. :type 'boolean)
  63. (defun org-babel-variable-assignments:plantuml (params)
  64. "Return a list of PlantUML statements assigning the block's variables.
  65. PARAMS is a property list of source block parameters, which may
  66. contain multiple entries for the key `:var'. `:var' entries in PARAMS
  67. are expected to be scalar variables."
  68. (mapcar
  69. (lambda (pair)
  70. (format "!define %s %s"
  71. (car pair)
  72. (replace-regexp-in-string "\"" "" (cdr pair))))
  73. (org-babel--get-vars params)))
  74. (defun org-babel-plantuml-make-body (body params)
  75. "Return PlantUML input string.
  76. BODY is the content of the source block and PARAMS is a property list
  77. of source block parameters. This function relies on the
  78. `org-babel-expand-body:generic' function to extract `:var' entries
  79. from PARAMS and on the `org-babel-variable-assignments:plantuml'
  80. function to convert variables to PlantUML assignments.
  81. If BODY does not contain @startXXX ... @endXXX clauses, @startuml
  82. ... @enduml will be added."
  83. (let ((full-body
  84. (org-babel-expand-body:generic
  85. body params (org-babel-variable-assignments:plantuml params))))
  86. (if (string-prefix-p "@start" body t) full-body
  87. (format "@startuml\n%s\n@enduml" full-body))))
  88. (defun org-babel-execute:plantuml (body params)
  89. "Execute a block of plantuml code with org-babel.
  90. This function is called by `org-babel-execute-src-block'."
  91. (let* ((do-export (member "file" (cdr (assq :result-params params))))
  92. (out-file (if do-export
  93. (or (cdr (assq :file params))
  94. (error "No :file provided but :results set to file. For plain text output, set :results to verbatim"))
  95. (org-babel-temp-file "plantuml-" ".txt")))
  96. (cmdline (cdr (assq :cmdline params)))
  97. (in-file (org-babel-temp-file "plantuml-"))
  98. (java (or (cdr (assq :java params)) ""))
  99. (executable (cond ((eq org-plantuml-exec-mode 'plantuml) org-plantuml-executable-path)
  100. (t "java")))
  101. (executable-args (cond ((eq org-plantuml-exec-mode 'plantuml) org-plantuml-args)
  102. ((string= "" org-plantuml-jar-path)
  103. (error "`org-plantuml-jar-path' is not set"))
  104. ((not (file-exists-p org-plantuml-jar-path))
  105. (error "Could not find plantuml.jar at %s" org-plantuml-jar-path))
  106. (t `(,java
  107. "-jar"
  108. ,(shell-quote-argument (expand-file-name org-plantuml-jar-path))
  109. ,@org-plantuml-args))))
  110. (full-body (org-babel-plantuml-make-body body params))
  111. (cmd (mapconcat #'identity
  112. (append
  113. (list executable)
  114. executable-args
  115. (pcase (file-name-extension out-file)
  116. ("png" '("-tpng"))
  117. ("svg" '("-tsvg"))
  118. ("eps" '("-teps"))
  119. ("pdf" '("-tpdf"))
  120. ("tex" '("-tlatex"))
  121. ("vdx" '("-tvdx"))
  122. ("xmi" '("-txmi"))
  123. ("scxml" '("-tscxml"))
  124. ("html" '("-thtml"))
  125. ("txt" '("-ttxt"))
  126. ("utxt" '("-utxt")))
  127. (list
  128. "-p"
  129. cmdline
  130. "<"
  131. (org-babel-process-file-name in-file)
  132. ">"
  133. (org-babel-process-file-name out-file)))
  134. " ")))
  135. (with-temp-file in-file (insert full-body))
  136. (message "%s" cmd) (org-babel-eval cmd "")
  137. (if (and (string= (file-name-extension out-file) "svg")
  138. org-babel-plantuml-svg-text-to-path)
  139. (org-babel-eval (format "inkscape %s -T -l %s" out-file out-file) ""))
  140. (unless do-export (with-temp-buffer
  141. (insert-file-contents out-file)
  142. (buffer-substring-no-properties
  143. (point-min) (point-max))))))
  144. (defun org-babel-prep-session:plantuml (_session _params)
  145. "Return an error because plantuml does not support sessions."
  146. (error "Plantuml does not support sessions"))
  147. (provide 'ob-plantuml)
  148. ;;; ob-plantuml.el ends here