ob-dot.el 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ;;; ob-dot.el --- Babel Functions for dot -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2018 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 dot source code.
  19. ;;
  20. ;; For information on dot see http://www.graphviz.org/
  21. ;;
  22. ;; This differs from most standard languages in that
  23. ;;
  24. ;; 1) there is no such thing as a "session" in dot
  25. ;;
  26. ;; 2) we are generally only going to return results of type "file"
  27. ;;
  28. ;; 3) we are adding the "file" and "cmdline" header arguments
  29. ;;
  30. ;; 4) there are no variables (at least for now)
  31. ;;; Code:
  32. (require 'ob)
  33. (defvar org-babel-default-header-args:dot
  34. '((:results . "file") (:exports . "results"))
  35. "Default arguments to use when evaluating a dot source block.")
  36. (defun org-babel-expand-body:dot (body params)
  37. "Expand BODY according to PARAMS, return the expanded body."
  38. (let ((vars (org-babel--get-vars params)))
  39. (mapc
  40. (lambda (pair)
  41. (let ((name (symbol-name (car pair)))
  42. (value (cdr pair)))
  43. (setq body
  44. (replace-regexp-in-string
  45. (concat "$" (regexp-quote name))
  46. (if (stringp value) value (format "%S" value))
  47. body
  48. t
  49. t))))
  50. vars)
  51. body))
  52. (defun org-babel-execute:dot (body params)
  53. "Execute a block of Dot code with org-babel.
  54. This function is called by `org-babel-execute-src-block'."
  55. (let* ((out-file (cdr (or (assq :file params)
  56. (error "You need to specify a :file parameter"))))
  57. (cmdline (or (cdr (assq :cmdline params))
  58. (format "-T%s" (file-name-extension out-file))))
  59. (cmd (or (cdr (assq :cmd params)) "dot"))
  60. (in-file (org-babel-temp-file "dot-")))
  61. (with-temp-file in-file
  62. (insert (org-babel-expand-body:dot body params)))
  63. (org-babel-eval
  64. (concat cmd
  65. " " (org-babel-process-file-name in-file)
  66. " " cmdline
  67. " -o " (org-babel-process-file-name out-file)) "")
  68. nil)) ;; signal that output has already been written to file
  69. (defun org-babel-prep-session:dot (_session _params)
  70. "Return an error because Dot does not support sessions."
  71. (error "Dot does not support sessions"))
  72. (provide 'ob-dot)
  73. ;;; ob-dot.el ends here