ob-ditaa.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. ;; 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 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 'ob)
  31. (require 'org-compat)
  32. (defvar org-babel-default-header-args:ditaa
  33. '((:results . "file")
  34. (:exports . "results")
  35. (:java . "-Dfile.encoding=UTF-8"))
  36. "Default arguments for evaluating a ditaa source block.")
  37. (defcustom org-ditaa-jar-path (expand-file-name
  38. "ditaa.jar"
  39. (file-name-as-directory
  40. (expand-file-name
  41. "scripts"
  42. (file-name-as-directory
  43. (expand-file-name
  44. "../contrib"
  45. (file-name-directory (org-find-library-dir "org")))))))
  46. "Path to the ditaa jar executable."
  47. :group 'org-babel
  48. :type 'string)
  49. (defcustom org-babel-ditaa-java-cmd "java"
  50. "Java executable to use when evaluating ditaa blocks."
  51. :group 'org-babel
  52. :type 'string)
  53. (defcustom org-ditaa-eps-jar-path
  54. (expand-file-name "DitaaEps.jar" (file-name-directory org-ditaa-jar-path))
  55. "Path to the DitaaEps.jar executable."
  56. :group 'org-babel
  57. :version "24.4"
  58. :package-version '(Org . "8.0")
  59. :type 'string)
  60. (defcustom org-ditaa-jar-option "-jar"
  61. "Option for the ditaa jar file.
  62. Do not leave leading or trailing spaces in this string."
  63. :group 'org-babel
  64. :version "24.1"
  65. :type 'string)
  66. (defun org-babel-execute:ditaa (body params)
  67. "Execute a block of Ditaa code with org-babel.
  68. This function is called by `org-babel-execute-src-block'."
  69. (let* ((out-file (or (cdr (assq :file params))
  70. (error
  71. "ditaa code block requires :file header argument")))
  72. (cmdline (cdr (assq :cmdline params)))
  73. (java (cdr (assq :java params)))
  74. (in-file (org-babel-temp-file "ditaa-"))
  75. (eps (cdr (assq :eps params)))
  76. (eps-file (when eps
  77. (org-babel-process-file-name (concat in-file ".eps"))))
  78. (pdf-cmd (when (and (or (string= (file-name-extension out-file) "pdf")
  79. (cdr (assq :pdf params))))
  80. (concat
  81. "epstopdf"
  82. " " eps-file
  83. " -o=" (org-babel-process-file-name out-file))))
  84. (cmd (concat org-babel-ditaa-java-cmd
  85. " " java " " org-ditaa-jar-option " "
  86. (shell-quote-argument
  87. (expand-file-name
  88. (if eps org-ditaa-eps-jar-path org-ditaa-jar-path)))
  89. " " cmdline
  90. " " (org-babel-process-file-name in-file)
  91. " " (if pdf-cmd
  92. eps-file
  93. (org-babel-process-file-name out-file)))))
  94. (unless (file-exists-p org-ditaa-jar-path)
  95. (error "Could not find ditaa.jar at %s" org-ditaa-jar-path))
  96. (with-temp-file in-file (insert body))
  97. (message cmd) (shell-command cmd)
  98. (when pdf-cmd (message pdf-cmd) (shell-command pdf-cmd))
  99. nil)) ;; signal that output has already been written to file
  100. (defun org-babel-prep-session:ditaa (_session _params)
  101. "Return an error because ditaa does not support sessions."
  102. (error "Ditaa does not support sessions"))
  103. (provide 'ob-ditaa)
  104. ;;; ob-ditaa.el ends here