This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tag.go
210 lines (192 loc) · 5.1 KB
/
tag.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Package tag provides the data type for (X)HTML tags/DOM nodes.
package tag
import (
"fmt"
"github.com/bouncepaw/mycomarkup/v3/util/lines"
"sort"
"strings"
)
// tagKind is the kind of a Tag. The way the tag is rendered depends on the kind.
type tagKind int
const (
// closed is a tag that looks like that: <t>children</t>.
closed tagKind = iota
// unclosed is a tag that looks like that: <t/>
unclosed
// wrapper is a tag that looks like that: children
wrapper
)
// Tag represents an HTML tag/DOM node.
type Tag struct {
name string
kind tagKind
attributes map[string]string
contents []lines.Line
children []Tag
}
// NewUnclosed returns a new unclosed tag.
func NewUnclosed(name string) Tag {
return Tag{
name: name,
kind: unclosed,
attributes: nil,
contents: nil,
children: nil,
}
}
// NewClosed returns a new closed tag.
func NewClosed(name string) Tag {
return Tag{
name: name,
kind: closed,
attributes: nil,
contents: nil,
children: nil,
}
}
// NewWrapper returns a new wrapper tag.
func NewWrapper() Tag {
return Tag{
name: "",
kind: wrapper,
attributes: nil,
contents: nil,
children: nil,
}
}
// WithChildren returns the tag but with the given children. Previous children of the tag are discarded.
//
// This is a no-op for unclosed tags.
func (t Tag) WithChildren(children ...Tag) Tag {
if t.kind == unclosed {
return t
}
t.children = children
return t
}
// WithAttrs return the tag but with the given attributes. Previous attributes of the tag are discarded.
//
// This is a no-op for wrapper tags.
func (t Tag) WithAttrs(attributes map[string]string) Tag {
if t.kind == wrapper {
return t
}
t.attributes = attributes
return t
}
// WithContentsLines returns the tag but with the given lines of contents.
//
// Contents is like children, but just text, not tags.
//
// This is a no-op for unclosed tags.
func (t Tag) WithContentsLines(lines ...lines.Line) Tag {
if t.kind == unclosed {
return t
}
t.contents = lines
return t
}
// WithContentsStrings is like WithContentsLines but it wraps strings into indented lines for you.
func (t Tag) WithContentsStrings(strs ...string) Tag {
var contentsLines []lines.Line
for _, str := range strs {
contentsLines = append(contentsLines, lines.IndentableFrom(str))
}
return t.WithContentsLines(contentsLines...)
}
// String returns a string representation of the tag.
func (t Tag) String() string {
var res string
for _, line := range t.Lines() {
res += line.String()
}
return res
}
// Lines returns rendered lines of the tag.
func (t Tag) Lines() (res []lines.Line) {
switch t.kind {
case unclosed:
return []lines.Line{
lines.IndentableFrom(fmt.Sprintf("<%s%s/>", t.name, attrs(t.attributes))),
}
case wrapper:
res = t.contents
for _, child := range t.children {
res = append(res, child.Lines()...)
}
return res
case closed:
// codeblock-specific stuff first. This is pain 🥲
if t.name == "pre" {
for i, child := range t.children {
childLines := child.Lines()
for j, line := range childLines {
text := line.Contents()
if i+j == 0 { // First line
text = fmt.Sprintf(`<pre%s>%s`, attrs(t.attributes), text)
}
if i+j == len(t.children)+len(childLines)-2 { // Last line
text += `</pre>`
}
res = append(res, lines.UnindentableFrom(text))
}
}
return res
}
if t.name == "code" {
// open tag
if len(t.contents) > 0 {
t.contents[0] = lines.IndentableFrom(fmt.Sprintf(`<%s%s>%s`, t.name, attrs(t.attributes), t.contents[0].Contents()))
} else { // empty codeblock
t.contents = []lines.Line{
lines.IndentableFrom(fmt.Sprintf(`<%s%s>`, t.name, attrs(t.attributes))),
}
}
// close tag
// t.contents has at least one element here
lastElemIdx := len(t.contents) - 1
text := fmt.Sprintf("%s</%s>", t.contents[lastElemIdx].Contents(), t.name)
if t.contents[lastElemIdx].IsIndentable() {
t.contents[lastElemIdx] = lines.IndentableFrom(text)
} else {
t.contents[lastElemIdx] = lines.UnindentableFrom(text)
}
return t.contents
}
// more pain
if (t.name == "a") && (len(t.children) == 0) && (len(t.contents) == 1) {
res = []lines.Line{
lines.IndentableFrom(fmt.Sprintf(`<%[1]s%[2]s>%[3]s</%[1]s>`, t.name, attrs(t.attributes), t.contents[0].Contents())),
}
return res
}
// normal closed tags:
res = []lines.Line{
lines.IndentableFrom(fmt.Sprintf("<%s%s>", t.name, attrs(t.attributes))),
}
res = append(res, t.contents...)
for _, child := range t.children {
for _, line := range child.Lines() {
res = append(res, line.Indented())
}
}
res = append(res, lines.IndentableFrom(fmt.Sprintf("</%s>\n", t.name)))
return res
default:
res = append(res, lines.UnindentableFrom("ERROR"))
}
return res
}
func attrs(m map[string]string) (res string) {
if len(m) == 0 {
return ""
}
var parts []string
for k, v := range m {
// TODO: perform some escaping?
parts = append(parts, fmt.Sprintf(` %s="%s"`, k, v))
}
// Sort so the output is the same for the same input.
sort.Strings(parts)
return strings.Join(parts, "")
}