ob-dot.el 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. ;;; ob-dot.el --- Babel Functions for dot -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2021 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Maintainer: Justin Abrahms
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: https://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating dot source code.
  20. ;;
  21. ;; For information on dot see https://www.graphviz.org/
  22. ;;
  23. ;; This differs from most standard languages in that
  24. ;;
  25. ;; 1) there is no such thing as a "session" in dot
  26. ;;
  27. ;; 2) we are generally only going to return results of type "file"
  28. ;;
  29. ;; 3) we are adding the "file" and "cmdline" header arguments
  30. ;;
  31. ;; 4) there are no variables (at least for now)
  32. ;;; Code:
  33. (require 'ob)
  34. (defvar org-babel-default-header-args:dot
  35. '((:results . "file") (:exports . "results"))
  36. "Default arguments to use when evaluating a dot source block.")
  37. (defun org-babel-expand-body:dot (body params)
  38. "Expand BODY according to PARAMS, return the expanded body."
  39. (let ((vars (org-babel--get-vars params)))
  40. (mapc
  41. (lambda (pair)
  42. (let ((name (symbol-name (car pair)))
  43. (value (cdr pair)))
  44. (setq body
  45. (replace-regexp-in-string
  46. (concat "$" (regexp-quote name))
  47. (if (stringp value) value (format "%S" value))
  48. body
  49. t
  50. t))))
  51. vars)
  52. body))
  53. (defun org-babel-execute:dot (body params)
  54. "Execute a block of Dot code with org-babel.
  55. This function is called by `org-babel-execute-src-block'."
  56. (let* ((out-file (cdr (or (assq :file params)
  57. (error "You need to specify a :file parameter"))))
  58. (cmdline (or (cdr (assq :cmdline params))
  59. (format "-T%s" (file-name-extension out-file))))
  60. (cmd (or (cdr (assq :cmd params)) "dot"))
  61. (coding-system-for-read 'utf-8) ;use utf-8 with sub-processes
  62. (coding-system-for-write 'utf-8)
  63. (in-file (org-babel-temp-file "dot-")))
  64. (with-temp-file in-file
  65. (insert (org-babel-expand-body:dot body params)))
  66. (org-babel-eval
  67. (concat cmd
  68. " " (org-babel-process-file-name in-file)
  69. " " cmdline
  70. " -o " (org-babel-process-file-name out-file)) "")
  71. nil)) ;; signal that output has already been written to file
  72. (defun org-babel-prep-session:dot (_session _params)
  73. "Return an error because Dot does not support sessions."
  74. (error "Dot does not support sessions"))
  75. (provide 'ob-dot)
  76. ;;; ob-dot.el ends here