-
Notifications
You must be signed in to change notification settings - Fork 33
/
ob-mermaid.el
93 lines (77 loc) · 3.32 KB
/
ob-mermaid.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
;;; ob-mermaid.el --- org-babel support for mermaid evaluation
;; Copyright (C) 2018 Alexei Nunez
;; Author: Alexei Nunez <[email protected]>
;; URL: https://github.com/arnm/ob-mermaid
;; 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 mermaid diagrams.
;;; Requirements:
;; mermaid.cli | https://github.com/mermaidjs/mermaid.cli
;;; Code:
(require 'ob)
(require 'ob-eval)
(defvar org-babel-default-header-args:mermaid
'((:results . "file") (:exports . "results"))
"Default arguments for evaluatiing a mermaid source block.")
(defcustom ob-mermaid-cli-path nil
"Path to mermaid.cli executable."
:group 'org-babel
:type 'string)
(defun org-babel-execute:mermaid (body params)
(let* ((out-file (or (cdr (assoc :file params))
(error "mermaid requires a \":file\" header argument")))
(theme (cdr (assoc :theme params)))
(width (cdr (assoc :width params)))
(height (cdr (assoc :height params)))
(scale (cdr (assoc :scale params)))
(background-color (cdr (assoc :background-color params)))
(mermaid-config-file (cdr (assoc :mermaid-config-file params)))
(css-file (cdr (assoc :css-file params)))
(puppeteer-config-file (cdr (assoc :puppeteer-config-file params)))
(pdf-fit (assoc :pdf-fit params))
(temp-file (org-babel-temp-file "mermaid-"))
(mmdc (or ob-mermaid-cli-path
(executable-find "mmdc")
(error "`ob-mermaid-cli-path' is not set and mmdc is not in `exec-path'")))
(cmd (concat (shell-quote-argument (expand-file-name mmdc))
" -i " (org-babel-process-file-name temp-file)
" -o " (org-babel-process-file-name out-file)
(when theme
(concat " -t " theme))
(when background-color
(concat " -b " background-color))
(when width
(concat " -w " width))
(when height
(concat " -H " height))
(when scale
(concat " -s " (number-to-string scale)))
(when pdf-fit
(concat " -f "))
(when mermaid-config-file
(concat " -c " (org-babel-process-file-name mermaid-config-file)))
(when css-file
(concat " -C " (org-babel-process-file-name css-file)))
(when puppeteer-config-file
(concat " -p " (org-babel-process-file-name puppeteer-config-file))))))
(unless (file-executable-p mmdc)
;; cannot happen with `executable-find', so we complain about
;; `ob-mermaid-cli-path'
(error "Cannot find or execute %s, please check `ob-mermaid-cli-path'" mmdc))
(with-temp-file temp-file (insert body))
(message "%s" cmd)
(org-babel-eval cmd "")
nil))
(provide 'ob-mermaid)
;;; ob-mermaid.el ends here