forked from kubernetes-el/kubernetes-el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes-yaml.el
79 lines (59 loc) · 2.36 KB
/
kubernetes-yaml.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
;;; kubernetes-yaml.el --- YAML pretty-printing. -*- lexical-binding: t; -*-
;;; Commentary:
;;; Code:
(require 'dash)
(require 'subr-x)
(require 'kubernetes-ast)
(require 'kubernetes-modes)
;; Compile parsed JSON into an AST representation for rendering.
(defun kubernetes-yaml--render-helper (json)
(pcase json
;; Literals
('nil "null")
('t "true")
(:json-false "false")
((pred stringp) json)
((pred numberp) (number-to-string json))
((pred symbolp) (symbol-name json))
;; Lists
((pred vectorp)
`(list ,@(seq-map (lambda (it)
`(section (item nil) (line ,(kubernetes-yaml--render-helper it))))
json)))
;; Objects
((pred listp)
(seq-map (-lambda ((k . v))
(let ((k (kubernetes-yaml--render-helper k))
(v (kubernetes-yaml--render-helper v)))
`(section (object-kvp nil)
,(cond
;; Indent multiline strings.
((and (stringp v) (string-match-p "\n" (string-trim-right v)))
`(copy-prop ,v
(heading ,(concat (propertize (format "%s:" k) 'face 'magit-section-heading) " |-"))
(indent ,@(--map `(line ,it) (split-string (string-trim-right v) "\n")))))
((stringp v)
`(key-value 0 ,k ,v))
(t
`((heading ,(concat (propertize (format "%s:" k) 'face 'magit-section-heading) " "))
(indent ,v)))))))
json))
(_
(error "Don't know how to render %s" json))))
(defun kubernetes-yaml-render (json)
"Process some parsed JSON into a YAML AST for rendering."
`(section (json-root nil)
,(kubernetes-yaml--render-helper json)
(padding)))
;; Drawing utilites
(defun kubernetes-yaml-make-buffer (bufname parsed-json)
(let ((buf (get-buffer-create bufname)))
(with-current-buffer buf
(kubernetes-display-thing-mode)
(let ((inhibit-read-only t))
(erase-buffer)
(kubernetes-ast-eval (kubernetes-yaml-render parsed-json))
(goto-char (point-min))))
buf))
(provide 'kubernetes-yaml)
;;; kubernetes-yaml.el ends here