ob-ditaa.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ;;; ob-ditaa.el --- Babel Functions for ditaa -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  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 ditaa source code.
  19. ;;
  20. ;; This differs from most standard languages in that
  21. ;;
  22. ;; 1) there is no such thing as a "session" in ditaa
  23. ;;
  24. ;; 2) we are generally only going to return results of type "file"
  25. ;;
  26. ;; 3) we are adding the "file" and "cmdline" header arguments
  27. ;;
  28. ;; 4) there are no variables (at least for now)
  29. ;;; Code:
  30. (require 'org-macs)
  31. (org-assert-version)
  32. (require 'ob)
  33. (require 'org-compat)
  34. (defvar org-babel-default-header-args:ditaa
  35. '((:results . "file")
  36. (:exports . "results")
  37. (:java . "-Dfile.encoding=UTF-8"))
  38. "Default arguments for evaluating a ditaa source block.")
  39. (defcustom org-ditaa-jar-path (expand-file-name
  40. "ditaa.jar"
  41. (file-name-as-directory
  42. (expand-file-name
  43. "scripts"
  44. (file-name-as-directory
  45. (expand-file-name
  46. "../contrib"
  47. (file-name-directory (org-find-library-dir "org")))))))
  48. "Path to the ditaa jar executable."
  49. :group 'org-babel
  50. :type 'string)
  51. (defcustom org-babel-ditaa-java-cmd "java"
  52. "Java executable to use when evaluating ditaa blocks."
  53. :group 'org-babel
  54. :type 'string)
  55. (defcustom org-ditaa-eps-jar-path
  56. (expand-file-name "DitaaEps.jar" (file-name-directory org-ditaa-jar-path))
  57. "Path to the DitaaEps.jar executable."
  58. :group 'org-babel
  59. :version "24.4"
  60. :package-version '(Org . "8.0")
  61. :type 'string)
  62. (defcustom org-ditaa-jar-option "-jar"
  63. "Option for the ditaa jar file.
  64. Do not leave leading or trailing spaces in this string."
  65. :group 'org-babel
  66. :version "24.1"
  67. :type 'string)
  68. (defun org-babel-execute:ditaa (body params)
  69. "Execute a block of Ditaa code with org-babel.
  70. This function is called by `org-babel-execute-src-block'."
  71. (let* ((out-file (or (cdr (assq :file params))
  72. (error
  73. "ditaa code block requires :file header argument")))
  74. (cmdline (cdr (assq :cmdline params)))
  75. (java (cdr (assq :java params)))
  76. (in-file (org-babel-temp-file "ditaa-"))
  77. (eps (cdr (assq :eps params)))
  78. (eps-file (when eps
  79. (org-babel-process-file-name (concat in-file ".eps"))))
  80. (pdf-cmd (when (and (or (string= (file-name-extension out-file) "pdf")
  81. (cdr (assq :pdf params))))
  82. (concat
  83. "epstopdf"
  84. " " eps-file
  85. " -o=" (org-babel-process-file-name out-file))))
  86. (cmd (concat org-babel-ditaa-java-cmd
  87. " " java " " org-ditaa-jar-option " "
  88. (shell-quote-argument
  89. (expand-file-name
  90. (if eps org-ditaa-eps-jar-path org-ditaa-jar-path)))
  91. " " cmdline
  92. " " (org-babel-process-file-name in-file)
  93. " " (if pdf-cmd
  94. eps-file
  95. (org-babel-process-file-name out-file)))))
  96. (unless (file-exists-p org-ditaa-jar-path)
  97. (error "Could not find ditaa.jar at %s" org-ditaa-jar-path))
  98. (with-temp-file in-file (insert body))
  99. (message cmd) (shell-command cmd)
  100. (when pdf-cmd (message pdf-cmd) (shell-command pdf-cmd))
  101. nil)) ;; signal that output has already been written to file
  102. (defun org-babel-prep-session:ditaa (_session _params)
  103. "Return an error because ditaa does not support sessions."
  104. (error "Ditaa does not support sessions"))
  105. (provide 'ob-ditaa)
  106. ;;; ob-ditaa.el ends here