-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
html.go
179 lines (150 loc) · 4.53 KB
/
html.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
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"html"
"io"
"strings"
)
// NB(directxman12): we use this instead of templates to avoid
// weird issues with whitespace in elements rendered as inline.
// Writing with templates was getting tricky to do without
// compromising readability.
//
// This isn't an amazing solution, but it's good enough™
// toHTML knows how to write itself as HTML to an output.
type toHTML interface {
// WriteHTML writes this as HTML to the given Writer.
WriteHTML(io.Writer) error
}
// Text is a chunk of text in an HTML doc.
type Text string
// WriteHTML writes the string as HTML to the given Writer while accounting for mdBook's handling
// of backticks. Given mdBook's behavior of treating backticked content as raw text, this function
// ensures proper rendering by preventing unnecessary HTML escaping within code snippets. Chunks
// outside of backticks are HTML-escaped for security, while chunks inside backticks remain as raw
// text, preserving mdBook's intended rendering of code content.
func (t Text) WriteHTML(w io.Writer) error {
textChunks := strings.Split(string(t), "`")
for i, chunk := range textChunks {
if i%2 == 0 { // Outside backticks, escape and write HTML
_, err := io.WriteString(w, html.EscapeString(chunk))
if err != nil {
return err
}
} else { // Inside backticks, write raw HTML
_, err := io.WriteString(w, "`"+chunk+"`")
if err != nil {
return err
}
}
}
return nil
}
// Tag is some tag with contents and attributes in an HTML doc.
type Tag struct {
Name string
Attrs Attrs
Children []toHTML
}
// WriteHTML writes the tag as HTML to the given Writer
func (t Tag) WriteHTML(w io.Writer) error {
attrsOut := ""
if t.Attrs != nil {
attrsOut = t.Attrs.ToAttrs()
}
if _, err := fmt.Fprintf(w, "<%s %s>", t.Name, attrsOut); err != nil {
return err
}
for _, child := range t.Children {
if err := child.WriteHTML(w); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "</%s>", t.Name); err != nil {
return err
}
return nil
}
// Fragment is some series of tags, text, etc in an HTML doc.
type Fragment []toHTML
// WriteHTML writes the fragment as HTML to the given Writer
func (f Fragment) WriteHTML(w io.Writer) error {
for _, item := range f {
if err := item.WriteHTML(w); err != nil {
return err
}
}
return nil
}
// Attrs knows how to convert itself to HTML attributes.
type Attrs interface {
// ToAttrs returns `key1="value1" key2="value2"` etc to be placed into an HTML tag.
ToAttrs() string
}
// classes sets the class attribute to these class names.
type classes []string
// ToAttrs implements Attrs
func (c classes) ToAttrs() string { return fmt.Sprintf("class=%q", strings.Join(c, " ")) }
// optionalClasses sets the class attribute to these class names, if their values are true.
type optionalClasses map[string]bool
// ToAttrs implements Attrs
func (c optionalClasses) ToAttrs() string {
actualClasses := make([]string, 0, len(c))
for class, active := range c {
if active {
actualClasses = append(actualClasses, class)
}
}
return classes(actualClasses).ToAttrs()
}
// attrs joins together one or more Attrs.
type attrs []Attrs
// ToAttrs implements Attrs
func (a attrs) ToAttrs() string {
parts := make([]string, len(a))
for i, attr := range a {
parts[i] = attr.ToAttrs()
}
return strings.Join(parts, " ")
}
// dataAttr represents some `data-*` attribute.
type dataAttr struct {
Name string
Value string
}
// ToAttrs implements Attrs
func (d dataAttr) ToAttrs() string {
return fmt.Sprintf("data-%s=%q", d.Name, d.Value)
}
// makeTag produces a function that makes tags of the given
// type.
func makeTag(name string) func(Attrs, ...toHTML) Tag {
return func(attrs Attrs, children ...toHTML) Tag {
return Tag{
Name: name,
Attrs: attrs,
Children: children,
}
}
}
var (
dd = makeTag("dd")
dt = makeTag("dt")
dl = makeTag("dl")
details = makeTag("details")
summary = makeTag("summary")
span = makeTag("span")
div = makeTag("div")
)