/
xml2text.go
109 lines (92 loc) · 2.71 KB
/
xml2text.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
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
package utils
import (
"bytes"
"encoding/xml"
"io"
"strings"
"text/template"
"github.com/antchfx/xmlquery"
"github.com/jaytaylor/html2text"
"github.com/pkg/errors"
)
func XmlNodeAsMarkdownPreRender(node *xmlquery.Node, needsSpace bool) string {
return xmlToMarkdown(node.OutputXML(false), true, needsSpace)
}
func XmlNodeAsMarkdown(node *xmlquery.Node) string {
return xmlToMarkdown(node.OutputXML(false), false, false)
}
func xmlToMarkdown(in string, preRender bool, needsSpace bool) string {
text, err := html2text.FromString(xmlToHtml(in, preRender, needsSpace), html2text.Options{PrettyTables: true, OmitLinks: false})
if err != nil {
return in
}
return text
}
func xmlToHtml(in string, preRender bool, needsSpace bool) string {
builder := strings.Builder{}
decoder := xml.NewDecoder(strings.NewReader(in))
for {
// Read tokens from the XML document in a stream.
t, err := decoder.Token()
if err == io.EOF {
break
} else if err != nil {
// ignore errors and try to format as much as possible
continue
}
switch tok := t.(type) {
case xml.StartElement:
if preRender && tok.Name.Local == "sub" && len(tok.Attr) > 1 {
if strings.HasPrefix(tok.Attr[0].Value, valuePrefix) {
// Have the check in nested if statment to avoid array out of bond
builder.WriteString(formateXccdfVar(tok.Attr[0].Value, needsSpace))
} else {
builder.WriteString(formatElement(tok.Name, "<"))
}
} else {
builder.WriteString(formatElement(tok.Name, "<"))
}
case xml.EndElement:
builder.WriteString(formatElement(tok.Name, "</"))
case xml.CharData:
builder.Write(tok)
}
}
return builder.String()
}
func formateXccdfVar(in string, needsSpace bool) string {
if needsSpace {
return " {{." + strings.TrimPrefix(in, valuePrefix) + "}} "
}
return "{{." + strings.TrimPrefix(in, valuePrefix) + "}}"
}
func formatElement(elName xml.Name, tag string) string {
// just pass non-html tags through
var t string
if elName.Space != "html" {
t = tag + elName.Space + ":" + elName.Local + ">"
} else {
// enclose pre in a paragraph to force a line break
if elName.Local == "pre" && tag == "<" {
t = tag + "p>"
}
t += tag + elName.Local + ">"
if elName.Local == "pre" && tag == "</" {
t += tag + "p>"
}
}
return t
}
func RenderValues(in string, valuesList map[string]string) (string, []string, error) {
t, err := template.New("").Option("missingkey=zero").Parse(in)
if err != nil {
return in, nil, errors.Wrap(err, "wrongly formatted context: ")
}
buf := &bytes.Buffer{}
err = t.Execute(buf, valuesList)
if err != nil {
return in, nil, errors.Wrap(err, "error while parsing variables into values: ")
}
out := buf.String()
return out, getParsedValueName(t), nil
}