-
Notifications
You must be signed in to change notification settings - Fork 1
/
ob-kroki.el
147 lines (128 loc) · 5.27 KB
/
ob-kroki.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
;;; ob-kroki.el --- org-babel functions for kroki evaluation
;; Copyright (C) 2009-2021 Free Software Foundation, Inc.
;; Author: Shawn Murphy
;; Keywords: literate programming, reproducible research
;; Homepage: http:https://orgmode.org
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs. If not, see <http:https://www.gnu.org/licenses/>.
;;; Commentary:
;; Org-Babel support for generating images using kroki
;;
;; From http:https://kroki.io/
;; "Kroki provides a unified API with support for
;; BlockDiag (BlockDiag, SeqDiag, ActDiag, NwDiag, PacketDiag, RackDiag),
;; BPMN, Bytefield, C4 (with PlantUML), Ditaa, Erd, Excalidraw, GraphViz,
;; Mermaid, Nomnoml, Pikchr, PlantUML, SvgBob, UMLet, Vega, Vega-Lite, WaveDrom.
;; ... and more to come!"
;;
;; This implementation was based on ob-dot.el
;; https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-dot.html
;;
;; This differs from most standard languages in that
;;
;; 1) there is no such thing as a "session" in kroki
;;
;; 2) we are generally only going to return results of type "file"
;;
;; 3) we are adding the "file" and "cmdline" header arguments
;;
;; 4) there are no variables
;;
;; Installation:
;; 1) put the following in your .emacs
;;
;; (if (file-exists-p "~/.emacs.d/ob-kroki/ob-kroki.el")
;; (let ((load-path load-path))
;; (add-to-list 'load-path "~/.emacs.d/ob-kroki")
;; (require 'ob-kroki)))
;; 2) Install kroki-cli
;; from https://docs.kroki.io/kroki/setup/kroki-cli/
;; It enables command lines such as:
;; kroki convert simple.er --out-file out.png
;; kroki convert --help
;; Convert text diagram to image
;; Usage:
;; kroki convert file [flags]
;;
;; (note that "file" above can equal "-" to indicate input from STDIN)
;;
;; Flags:
;; -c, --config string alternate config file [env KROKI_CONFIG]
;; -f, --format string output format [base64 jpeg pdf png svg]
;; (default: infer from output file extension otherwise svg)
;; -h, --help help for convert
;; -o, --out-file string output file (default: based on path of input file); use - to output to STDOUT
;; -t, --type string diagram type [actdiag blockdiag bpmn bytefield
;; c4plantuml ditaa erd excalidraw graphviz mermaid nomnoml
;; nwdiag packetdiag plantuml rackdiag seqdiag svgbob umlet
;; vega vegalite wavedrom] (default: infer from file extension)
;;
;; SOME MINIMALIST EXAMPLES OF kroki INVOCATION
;; echo "digraph offon {on -> off -> on; }" \
;; | kroki convert - --out-file off_on.png --type graphviz && open off_on.png
;;
;; kroki convert beauty_truth.dot --out-file beauty_truth.png --type graphviz
;;
;; kroki convert beauty_truth.dot --out-file beauty_truth.doh --type graphviz --format png
;; # This demonstrates that the output format dominates the out-file extension
;;; Code:
(require 'ob)
(defvar org-babel-default-header-args:kroki
'((:results . "file") (:exports . "results"))
"Default arguments to use when evaluating a kroki source block.")
(defun org-babel-expand-body:kroki (body params)
"Expand BODY according to PARAMS, return the expanded body."
(let ((vars (org-babel--get-vars params)))
(mapc
(lambda (pair)
(let ((name (symbol-name (car pair)))
(value (cdr pair)))
(setq body
(replace-regexp-in-string
(concat "$" (regexp-quote name))
(if (stringp value) value (format "%S" value))
body
t
t))))
vars)
body))
(defun org-babel-execute:kroki (body params)
"Execute a block of Kroki code with org-babel.
This function is called by `org-babel-execute-src-block'."
(let* ((out-file (cdr (or (assq :file params)
(error "You need to specify a :file parameter"))))
(cmdline (or (cdr (assq :cmdline params))
(format "--format %s" (file-name-extension out-file))))
(cmd (or (cdr (assq :cmd params)) "kroki"))
(coding-system-for-read 'utf-8) ;use utf-8 with sub-processes
(coding-system-for-write 'utf-8)
(in-file (org-babel-temp-file "kroki-")))
(with-temp-file in-file
(insert (org-babel-expand-body:kroki body params)))
(org-babel-eval
(concat cmd
" convert " (org-babel-process-file-name in-file)
" " cmdline
" --out-file " (org-babel-process-file-name out-file)) "")
nil)) ;; signal that output has already been written to file
(defun org-babel-prep-session:kroki (_session _params)
"Return an error because Kroki does not support sessions."
(error "Kroki does not support sessions"))
(provide 'ob-kroki)
;; Sample Invocation
;; #+BEGIN_SRC kroki :file /tmp/truth_beauty.png :cmdline --type graphviz :exports results
;; digraph bt {
;; rankdir="LR"
;; truth -> beauty -> truth;
;; }
;; #+END_SRC
;;; ob-kroki.el ends here