ob-plantuml.el 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ;;; ob-plantuml.el --- Babel Functions for Plantuml -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2019 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. ;; http://www.emacswiki.org/emacs/org-export-blocks-format-plantuml.el
  22. ;;; Requirements:
  23. ;; plantuml | http://plantuml.sourceforge.net/
  24. ;; plantuml.jar | `org-plantuml-jar-path' should point to the jar file
  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. (defun org-babel-variable-assignments:plantuml (params)
  36. "Return a list of PlantUML statements assigning the block's variables.
  37. PARAMS is a property list of source block parameters, which may
  38. contain multiple entries for the key `:var'. `:var' entries in PARAMS
  39. are expected to be scalar variables."
  40. (mapcar
  41. (lambda (pair)
  42. (format "!define %s %s"
  43. (car pair)
  44. (replace-regexp-in-string "\"" "" (cdr pair))))
  45. (org-babel--get-vars params)))
  46. (defun org-babel-plantuml-make-body (body params)
  47. "Return PlantUML input string.
  48. BODY is the content of the source block and PARAMS is a property list
  49. of source block parameters. This function relies on the
  50. `org-babel-expand-body:generic' function to extract `:var' entries
  51. from PARAMS and on the `org-babel-variable-assignments:plantuml'
  52. function to convert variables to PlantUML assignments.
  53. If BODY does not contain @startXXX ... @endXXX clauses, @startuml
  54. ... @enduml will be added."
  55. (let ((assignments (org-babel-variable-assignments:plantuml params)))
  56. (if (string-prefix-p "@start" body t) assignments
  57. (format "@startuml\n%s\n@enduml"
  58. (org-babel-expand-body:generic body params assignments)))))
  59. (defun org-babel-execute:plantuml (body params)
  60. "Execute a block of plantuml code with org-babel.
  61. This function is called by `org-babel-execute-src-block'."
  62. (let* ((out-file (or (cdr (assq :file params))
  63. (error "PlantUML requires a \":file\" header argument")))
  64. (cmdline (cdr (assq :cmdline params)))
  65. (in-file (org-babel-temp-file "plantuml-"))
  66. (java (or (cdr (assq :java params)) ""))
  67. (full-body (org-babel-plantuml-make-body body params))
  68. (cmd (if (string= "" org-plantuml-jar-path)
  69. (error "`org-plantuml-jar-path' is not set")
  70. (concat "java " java " -jar "
  71. (shell-quote-argument
  72. (expand-file-name org-plantuml-jar-path))
  73. (if (string= (file-name-extension out-file) "png")
  74. " -tpng" "")
  75. (if (string= (file-name-extension out-file) "svg")
  76. " -tsvg" "")
  77. (if (string= (file-name-extension out-file) "eps")
  78. " -teps" "")
  79. (if (string= (file-name-extension out-file) "pdf")
  80. " -tpdf" "")
  81. (if (string= (file-name-extension out-file) "tex")
  82. " -tlatex" "")
  83. (if (string= (file-name-extension out-file) "vdx")
  84. " -tvdx" "")
  85. (if (string= (file-name-extension out-file) "xmi")
  86. " -txmi" "")
  87. (if (string= (file-name-extension out-file) "scxml")
  88. " -tscxml" "")
  89. (if (string= (file-name-extension out-file) "html")
  90. " -thtml" "")
  91. (if (string= (file-name-extension out-file) "txt")
  92. " -ttxt" "")
  93. (if (string= (file-name-extension out-file) "utxt")
  94. " -utxt" "")
  95. " -p " cmdline " < "
  96. (org-babel-process-file-name in-file)
  97. " > "
  98. (org-babel-process-file-name out-file)))))
  99. (unless (file-exists-p org-plantuml-jar-path)
  100. (error "Could not find plantuml.jar at %s" org-plantuml-jar-path))
  101. (with-temp-file in-file (insert full-body))
  102. (message "%s" cmd) (org-babel-eval cmd "")
  103. nil)) ;; signal that output has already been written to file
  104. (defun org-babel-prep-session:plantuml (_session _params)
  105. "Return an error because plantuml does not support sessions."
  106. (error "Plantuml does not support sessions"))
  107. (provide 'ob-plantuml)
  108. ;;; ob-plantuml.el ends here