-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathob-d2.el
73 lines (57 loc) · 2.44 KB
/
ob-d2.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
;;; ob-d2.el --- org-babel support for d2 evaluation
;; Copyright (C) 2022 Duncan Mac-Vicar P.
;; Copyright (C) 2022 Alexei Nunez
;; Author: Duncan Mac-Vicar P. <[email protected]>
;; URL: https://github.com/dmacvicar/ob-d2
;; Keywords: lisp
;; Version: 0
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Org-Babel support for evaluating d2 diagrams. Based on ob-mermaid code.
;;; Requirements:
;; d2 | https://github.com/terrastruct/d2
;;; Code:
(require 'ob)
(require 'ob-eval)
(defvar org-babel-default-header-args:d2
'((:results . "file") (:exports . "results"))
"Default arguments for evaluatiing d2 source block.")
(defcustom ob-d2-cli-path nil
"Path to d2 executable."
:group 'org-babel
:type 'string)
(defun org-babel-execute:d2 (body params)
(let* ((out-file (or (cdr (assoc :file params))
(error "d2 requires a \":file\" header argument")))
(theme (cdr (assoc :theme params)))
(layout (cdr (assoc :layout params)))
(temp-file (org-babel-temp-file "d2-"))
(d2c (or ob-d2-cli-path
(executable-find "d2")
(error "`ob-d2-cli-path' is not set and d2 is not in `exec-path'")))
(cmd (concat (shell-quote-argument (expand-file-name d2c))
(when theme
(concat " -t " theme))
(when layout
(concat " -l " layout))
" " (org-babel-process-file-name temp-file)
" " (org-babel-process-file-name out-file))))
(unless (file-executable-p d2c)
;; cannot happen with `executable-find', so we complain about
;; `ob-d2-cli-path'
(error "Cannot find or execute %s, please check `ob-d2-cli-path'" d2c))
(with-temp-file temp-file (insert body))
(message "%s" cmd)
(org-babel-eval cmd "")
nil))
(provide 'ob-d2)
;;; ob-d2.el ends here