-
Notifications
You must be signed in to change notification settings - Fork 6
/
backcompat.go
366 lines (341 loc) · 9.48 KB
/
backcompat.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright (c) The microformats project authors.
// SPDX-License-Identifier: MIT
// This file includes backwards compatibility support for microformats v1.
package microformats
import (
"net/url"
"path"
"strings"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
var (
// map microformats v1 root classes to their v2 equivalent.
backcompatRootMap = map[string]string{
"adr": "h-adr",
"geo": "h-geo",
"hentry": "h-entry",
"hfeed": "h-feed",
"hnews": "h-news",
"hproduct": "h-product",
"hrecipe": "h-recipe",
"hresume": "h-resume",
"hreview": "h-review",
"hreview-aggregate": "h-review-aggregate",
"vcard": "h-card",
"vevent": "h-event",
}
// map microformats v1 property classes to their v2 equivalent. These
// mappings are root-specific.
backcompatPropertyMap = map[string]map[string]string{
"h-adr": {
"country-name": "p-country-name",
"extended-address": "p-extended-address",
"locality": "p-locality",
"post-office-box": "p-post-office-box",
"postal-code": "p-postal-code",
"region": "p-region",
"street-address": "p-street-address",
},
"h-card": {
"additional-name": "p-additional-name",
"adr": "p-adr",
"agent": "p-agent",
"bday": "dt-bday",
"category": "p-category",
"class": "p-class",
"email": "u-email",
"family-name": "p-family-name",
"fn": "p-name",
"geo": "p-geo",
"given-name": "p-given-name",
"honorific-prefix": "p-honorific-prefix",
"honorific-suffix": "p-honorific-suffix",
"key": "u-key",
"label": "p-label",
"logo": "u-logo",
"mailer": "p-mailer",
"nickname": "p-nickname",
"note": "p-note",
"org": "p-org",
"photo": "u-photo",
"rev": "dt-rev",
"role": "p-role",
"sort-string": "p-sort-string",
"sound": "u-sound",
"tel": "p-tel",
"title": "p-job-title",
"tz": "p-tz",
"uid": "u-uid",
"url": "u-url",
},
"h-entry": {
"author": "p-author",
"category": "p-category",
"entry-content": "e-content",
"entry-summary": "p-summary",
"entry-title": "p-name",
"published": "dt-published",
"summary": "p-summary",
"updated": "dt-updated",
},
"h-event": {
"attendee": "p-attendee",
"category": "p-category",
"description": "p-description",
"dtend": "dt-end",
"dtstart": "dt-start",
"duration": "dt-duration",
"geo": "p-geo",
"location": "p-location",
"summary": "p-name",
"url": "u-url",
},
"h-feed": {
"author": "p-author",
"entry": "p-entry",
"photo": "u-photo",
"url": "u-url",
},
"h-geo": {
"latitude": "p-latitude",
"longitude": "p-longitude",
},
"h-item": {
"fn": "p-name",
"photo": "u-photo",
"url": "u-url",
},
"h-news": {
"dateline": "p-dateline",
"entry": "p-entry",
"geo": "p-geo",
"source-org": "p-source-org",
},
"h-product": {
"brand": "p-brand",
"category": "p-category",
"description": "p-description",
"fn": "p-name",
"identifier": "u-identifier",
"listing": "p-listing",
"photo": "u-photo",
"price": "p-price",
"review": "p-review",
"url": "u-url",
},
"h-recipe": {
"author": "p-author",
"category": "p-category",
"duration": "dt-duration",
"fn": "p-name",
"ingredient": "p-ingredient",
"instructions": "e-instructions",
"nutrition": "p-nutrition",
"photo": "u-photo",
"summary": "p-summary",
"yield": "p-yield",
},
"h-resume": {
"affiliation": "p-affiliation",
"contact": "p-contact",
"education": "p-education",
"experience": "p-experience",
"publications": "p-publications",
"skill": "p-skill",
"summary": "p-summary",
},
"h-review": {
"best": "p-best",
"description": "e-content",
"dtreviewed": "dt-reviewed",
"item": "p-item",
"rating": "p-rating",
"reviewer": "p-author",
"summary": "p-name",
"worst": "p-worst",
},
"h-review-aggregate": {
"average": "p-average",
"best": "p-best",
"count": "p-count",
"item": "p-item",
"rating": "p-rating",
"summary": "p-name",
"votes": "p-votes",
"worst": "p-worst",
},
}
// map microformats v1 rel values to their v2 property equivalent. These
// mappings are root-specific.
backcompatRelMap = map[string]map[string]string{
"h-card": {
"tag": "u-category",
},
"h-entry": {
"bookmark": "u-url",
"tag": "u-category",
},
"h-feed": {
"tag": "u-category",
},
"h-news": {
"principles": "u-principles",
},
"h-recipe": {
"tag": "u-category",
},
"h-review": {
"bookmark": "u-url",
"tag": "u-category",
},
}
)
// backcompatRootClasses returns the v2 root classes for the backcompat v1
// roots in the provided classes. parent identifies the parent microformat, if
// present, since some root mappings are context-specific.
func backcompatRootClasses(classes []string, parent *Microformat) []string {
var rootclasses []string
var itemClass bool
for _, class := range classes {
if c, ok := backcompatRootMap[class]; ok {
rootclasses = append(rootclasses, c)
}
if class == "item" {
itemClass = true
}
}
// handle implied h-item microformat inside of h-review
if len(rootclasses) == 0 && parent != nil && itemClass {
for _, t := range parent.Type {
if t == "h-review" || t == "h-review-aggregate" {
rootclasses = append(rootclasses, "h-item")
}
}
}
return rootclasses
}
// backcompatPropertyClasses returns the v2 property classes for the backcompat
// v1 properties in the provided classes and rel values. context identifies
// the v2 microformat types (h-card, h-adr, etc) the parsed property belongs to.
func backcompatPropertyClasses(classes []string, rels []string, context []string) []string {
var classmap = make(map[string]string)
for _, class := range classes {
for _, ctx := range context {
if c, ok := backcompatPropertyMap[ctx][class]; ok {
parts := strings.SplitN(c, "-", 2)
classmap[parts[1]] = c
}
}
}
for _, rel := range rels {
for _, ctx := range context {
if c, ok := backcompatRelMap[ctx][rel]; ok {
parts := strings.SplitN(c, "-", 2)
classmap[parts[1]] = c
}
}
}
var propertyclasses []string
for _, c := range classmap {
propertyclasses = append(propertyclasses, c)
}
return propertyclasses
}
// strip provided URL to its last path segment to serve as a category value.
func backcompatURLCategory(s string) string {
if s == "" {
return s
}
if p, err := url.Parse(s); err == nil {
return path.Base(p.Path)
}
return s
}
// backcompatIncludeRefs returns references found using the include pattern.
//
// refs includes the IDs of referenced nodes (without any leading '#')
//
// replace is true if the referenced node was identified using a pattern which
// instructs the referencing node to be replaced entirely, rather than the
// referenced node being amended.
//
// See http://microformats.org/wiki/include-pattern
// See http://microformats.org/wiki/microdata
func (p *parser) backcompatIncludeRefs(node *html.Node) (refs []string, replace bool) {
classes := getClasses(node)
for _, class := range classes {
if class == "include" {
var id string
if node.DataAtom == atom.A {
id = getAttr(node, "href")
} else if node.DataAtom == atom.Object {
id = getAttr(node, "data")
}
if !strings.HasPrefix(id, "#") {
// skip links not within the current page
continue
}
id = strings.TrimPrefix(id, "#")
if id == "" {
continue
}
return append(refs, id), true
}
}
if node.DataAtom == atom.Td {
refs = append(refs, strings.Fields(getAttr(node, "headers"))...)
}
refs = append(refs, strings.Fields(getAttr(node, "itemref"))...)
return refs, false
}
// backcompatIncludeNode includes referenced notes following the include
// pattern.
//
// see backcompatIncludeRefs for information on refs and replace parameters.
func (p *parser) backcompatIncludeNode(node *html.Node, refs []string, replace bool) *html.Node {
if len(refs) == 0 {
return node
}
for _, ref := range refs {
if n := findNodeByID(p.root, ref); n != nil {
if node != n && !isAncestorNode(node, n) {
if replace {
return n
}
node.AppendChild(cloneNode(n))
}
}
}
return node
}
// findNodeByID searches node and its children, returning the node with the
// specified id value.
func findNodeByID(node *html.Node, id string) *html.Node {
if getAttr(node, "id") == id {
return node
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
if n := findNodeByID(c, id); n != nil {
return n
}
}
return nil
}
// isAncestorNode returns true if parent is an ancestor node of child.
func isAncestorNode(child, parent *html.Node) bool {
for c := child; c != nil; c = c.Parent {
if c == parent {
return true
}
}
return false
}
// cloneNode makes a copy of node, detaching any parent or sibling nodes.
func cloneNode(node *html.Node) *html.Node {
clone := *node
clone.Parent = nil
clone.PrevSibling = nil
clone.NextSibling = nil
return &clone
}