This repository has been archived by the owner on Nov 10, 2020. It is now read-only.
forked from russross/blackfriday
-
Notifications
You must be signed in to change notification settings - Fork 24
/
ial.go
226 lines (207 loc) · 4.8 KB
/
ial.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// IAL implements inline attributes.
package mmark
import (
"bytes"
"sort"
)
// One or more of these can be attached to block elements
type inlineAttr struct {
id string // #id
class map[string]bool // 0 or more .class
attr map[string]string // key=value pairs
}
func newInlineAttr() *inlineAttr {
return &inlineAttr{class: make(map[string]bool), attr: make(map[string]string)}
}
// Parsing and thus detecting an IAL.
// IAL can have #id, .class or key=value element seperated by spaces, that may be escaped
func (p *parser) isInlineAttr(data []byte) int {
esc := false
quote := false
ialB := 0
ial := newInlineAttr()
for i := 0; i < len(data); i++ {
switch data[i] {
case ' ':
if quote {
continue
}
chunk := data[ialB+1 : i]
if len(chunk) == 0 {
ialB = i
continue
}
switch {
case chunk[0] == '.':
ial.class[string(chunk[1:])] = true
case chunk[0] == '#':
ial.id = string(chunk[1:])
default:
k, v := parseKeyValue(chunk)
if k != "" {
ial.attr[k] = v
} else {
// this is illegal in an IAL, discard the posibility
return 0
}
}
ialB = i
case '"':
if esc {
esc = !esc
continue
}
quote = !quote
case '\\':
esc = !esc
case '}':
if esc {
esc = !esc
continue
}
chunk := data[ialB+1 : i]
if len(chunk) == 0 {
return i + 1
}
switch {
case chunk[0] == '.':
ial.class[string(chunk[1:])] = true
case chunk[0] == '#':
ial.id = string(chunk[1:])
default:
k, v := parseKeyValue(chunk)
if k != "" {
ial.attr[k] = v
} else {
// this is illegal in an IAL, discard the posibility
return 0
}
}
p.ial = p.ial.add(ial)
return i + 1
default:
esc = false
}
}
return 0
}
func parseKeyValue(chunk []byte) (string, string) {
chunks := bytes.SplitN(chunk, []byte{'='}, 2)
if len(chunks) != 2 {
return "", ""
}
chunks[1] = bytes.Replace(chunks[1], []byte{'"'}, nil, -1)
return string(chunks[0]), string(chunks[1])
}
// Add IAL to another, overwriting the #id, collapsing classes and attributes
func (i *inlineAttr) add(j *inlineAttr) *inlineAttr {
if i == nil {
return j
}
if j.id != "" {
i.id = j.id
}
for k, c := range j.class {
i.class[k] = c
}
for k, a := range j.attr {
i.attr[k] = a
}
return i
}
func (i *inlineAttr) SetAttr(key, value string) {
i.attr[key] = value
}
func (i *inlineAttr) SortClasses() []string {
keys := make([]string, 0, len(i.class))
for k := range i.class {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func (i *inlineAttr) SortAttributes() []string {
keys := make([]string, 0, len(i.attr))
for k := range i.attr {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
// GetOrDefaultAttr sets the value under key if is is not set or
// use the value already in there. The boolean returns indicates
// if the value has been overwritten.
func (i *inlineAttr) GetOrDefaultAttr(key, def string) bool {
v := i.attr[key]
if v != "" {
return false
}
if def == "" {
return false
}
i.attr[key] = def
return true
}
// GetOrDefaulClass sets the class class. The boolean returns indicates
// if the value has been overwritten.
func (i *inlineAttr) GetOrDefaultClass(class string) bool {
_, ok := i.class[class]
i.class[class] = true
return ok
}
// GetOrDefaultID sets the id in i if it is not set. The boolean
// indicates if the id as set in i.
func (i *inlineAttr) GetOrDefaultId(id string) bool {
if i.id != "" {
return false
}
if id == "" {
return false
}
i.id = id
return true
}
// This returning a " " is not particularly nice...
// Key returns the value of a specific key as a ' key="value"' string. If not found
// an string containing a space is returned.
func (i *inlineAttr) Key(key string) string {
if v, ok := i.attr[key]; ok {
return " " + key + "=\"" + v + "\""
}
return " "
}
// Value returns the value of a specific key as value. If not found
// an empty string is returned. TODO(miek): should be " " or change Key() above.
func (i *inlineAttr) Value(key string) string {
if v, ok := i.attr[key]; ok {
return v
}
return ""
}
// DropAttr will drop the attribute under key from i.
// The returned boolean indicates if the key was found in i.
func (i *inlineAttr) DropAttr(key string) bool {
_, ok := i.attr[key]
delete(i.attr, key)
return ok
}
// KeepAttr will drop all attributes, except the ones listed under keys.
func (i *inlineAttr) KeepAttr(keys []string) {
newattr := make(map[string]string)
for _, k := range keys {
if v, ok := i.attr[k]; ok {
newattr[k] = v
}
}
i.attr = newattr
}
// KeepClass will drop all classes, except the ones listed under keys.
func (i *inlineAttr) KeepClass(keys []string) {
newclass := make(map[string]bool)
for _, k := range keys {
if v, ok := i.class[k]; ok {
newclass[k] = v
}
}
i.class = newclass
}