ob-plantuml.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. ;; Homepage: 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 'ob)
  27. (defvar org-babel-default-header-args:plantuml
  28. '((:results . "file") (:exports . "results"))
  29. "Default arguments for evaluating a plantuml source block.")
  30. (defcustom org-plantuml-jar-path ""
  31. "Path to the plantuml.jar file."
  32. :group 'org-babel
  33. :version "24.1"
  34. :type 'string)
  35. (defcustom org-plantuml-exec-mode 'jar
  36. "Method to use for PlantUML diagram generation.
  37. `jar' means to use java together with the JAR.
  38. The JAR can be configured via `org-plantuml-jar-path'.
  39. `plantuml' means to use the PlantUML executable.
  40. The executable can be configured via `org-plantuml-executable-path'.
  41. You can also configure extra arguments via `org-plantuml-executable-args'."
  42. :group 'org-babel
  43. :package-version '(Org . "9.4")
  44. :type 'symbol
  45. :options '(jar plantuml))
  46. (defcustom org-plantuml-executable-path "plantuml"
  47. "File name of the PlantUML executable."
  48. :group 'org-babel
  49. :package-version '(Org . "9.4")
  50. :type 'string)
  51. (defcustom org-plantuml-executable-args (list "-headless")
  52. "The arguments passed to plantuml executable when executing PlantUML."
  53. :group 'org-babel
  54. :package-version '(Org . "9.4")
  55. :type '(repeat string))
  56. (defcustom org-babel-plantuml-svg-text-to-path nil
  57. "When non-nil, export text in SVG images to paths using Inkscape."
  58. :group 'org-babel
  59. :package-version '(Org . "9.5")
  60. :type 'boolean)
  61. (defun org-babel-variable-assignments:plantuml (params)
  62. "Return a list of PlantUML statements assigning the block's variables.
  63. PARAMS is a property list of source block parameters, which may
  64. contain multiple entries for the key `:var'. `:var' entries in PARAMS
  65. are expected to be scalar variables."
  66. (mapcar
  67. (lambda (pair)
  68. (format "!define %s %s"
  69. (car pair)
  70. (replace-regexp-in-string "\"" "" (cdr pair))))
  71. (org-babel--get-vars params)))
  72. (defun org-babel-plantuml-make-body (body params)
  73. "Return PlantUML input string.
  74. BODY is the content of the source block and PARAMS is a property list
  75. of source block parameters. This function relies on the
  76. `org-babel-expand-body:generic' function to extract `:var' entries
  77. from PARAMS and on the `org-babel-variable-assignments:plantuml'
  78. function to convert variables to PlantUML assignments.
  79. If BODY does not contain @startXXX ... @endXXX clauses, @startuml
  80. ... @enduml will be added."
  81. (let ((full-body
  82. (org-babel-expand-body:generic
  83. body params (org-babel-variable-assignments:plantuml params))))
  84. (if (string-prefix-p "@start" body t) full-body
  85. (format "@startuml\n%s\n@enduml" full-body))))
  86. (defun org-babel-execute:plantuml (body params)
  87. "Execute a block of plantuml code with org-babel.
  88. This function is called by `org-babel-execute-src-block'."
  89. (let* ((out-file (or (cdr (assq :file params))
  90. (error "PlantUML requires a \":file\" header argument")))
  91. (cmdline (cdr (assq :cmdline params)))
  92. (in-file (org-babel-temp-file "plantuml-"))
  93. (java (or (cdr (assq :java params)) ""))
  94. (executable (cond ((eq org-plantuml-exec-mode 'plantuml) org-plantuml-executable-path)
  95. (t "java")))
  96. (executable-args (cond ((eq org-plantuml-exec-mode 'plantuml) org-plantuml-executable-args)
  97. ((string= "" org-plantuml-jar-path)
  98. (error "`org-plantuml-jar-path' is not set"))
  99. ((not (file-exists-p org-plantuml-jar-path))
  100. (error "Could not find plantuml.jar at %s" org-plantuml-jar-path))
  101. (t (list java
  102. "-jar"
  103. (shell-quote-argument (expand-file-name org-plantuml-jar-path))))))
  104. (full-body (org-babel-plantuml-make-body body params))
  105. (cmd (mapconcat #'identity
  106. (append
  107. (list executable)
  108. executable-args
  109. (pcase (file-name-extension out-file)
  110. ("png" '("-tpng"))
  111. ("svg" '("-tsvg"))
  112. ("eps" '("-teps"))
  113. ("pdf" '("-tpdf"))
  114. ("tex" '("-tlatex"))
  115. ("vdx" '("-tvdx"))
  116. ("xmi" '("-txmi"))
  117. ("scxml" '("-tscxml"))
  118. ("html" '("-thtml"))
  119. ("txt" '("-ttxt"))
  120. ("utxt" '("-utxt")))
  121. (list
  122. "-p"
  123. cmdline
  124. "<"
  125. (org-babel-process-file-name in-file)
  126. ">"
  127. (org-babel-process-file-name out-file)))
  128. " ")))
  129. (with-temp-file in-file (insert full-body))
  130. (message "%s" cmd) (org-babel-eval cmd "")
  131. (if (and (string= (file-name-extension out-file) "svg")
  132. org-babel-plantuml-svg-text-to-path)
  133. (org-babel-eval (format "inkscape %s -T -l %s" out-file out-file) ""))
  134. nil)) ;; signal that output has already been written to file
  135. (defun org-babel-prep-session:plantuml (_session _params)
  136. "Return an error because plantuml does not support sessions."
  137. (error "Plantuml does not support sessions"))
  138. (provide 'ob-plantuml)
  139. ;;; ob-plantuml.el ends here