-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmarkdown.go
64 lines (55 loc) · 1.24 KB
/
markdown.go
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
package main
import "bytes"
func markdown(m *CombinedResultMap) (string, error) {
var buffer bytes.Buffer
for h1, v1 := range *m {
if len(v1) == 0 || h1 == "summary" {
continue
}
heading(h1, 2, &buffer)
for h2, v2 := range v1 {
if len(v2) == 0 {
continue
}
heading(h2, 3, &buffer)
table(v2, &buffer)
}
}
return buffer.String(), nil
}
func heading(s string, level int, b *bytes.Buffer) {
if level < 3 {
b.WriteString(s + "\n")
for i := 0; i < len(s); i++ {
underline := "="
if level > 1 {
underline = "-"
}
b.WriteString(underline)
}
} else {
for i := 0; i < level; i++ {
b.WriteString("#")
}
b.WriteString(" " + s)
}
b.WriteString("\n\n")
}
func table(s ContainerSet, b *bytes.Buffer) {
//alloc string table
count := len(s)
rows := make([][]string, count+1) //set length plus header row
//header row
rows[0] = []string{"Namespace", "Name", "Container"}
//special case: one empty spec
if count == 1 && s[0].Namespace == "" && s[0].Name == "" && s[0].Container == "" {
//exit without writing to buffer
return
}
//content rows
for i := 0; i < count; i++ {
spec := s[i]
rows[i+1] = []string{spec.Namespace, spec.Name, spec.Container}
}
b.WriteString(markdownTable(&rows))
}