-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
raw.go
64 lines (51 loc) · 1.28 KB
/
raw.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
package parser
import (
"fmt"
"github.com/a-h/parse"
)
var styleElement = rawElementParser{
name: "style",
}
var scriptElement = rawElementParser{
name: "script",
}
type rawElementParser struct {
name string
}
func (p rawElementParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
start := pi.Index()
// <
if _, ok, err = lt.Parse(pi); err != nil || !ok {
return
}
// Element name.
var e RawElement
if e.Name, ok, err = parse.String(p.name).Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
if e.Attributes, ok, err = (attributesParser{}).Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
// Optional whitespace.
if _, _, err = parse.OptionalWhitespace.Parse(pi); err != nil {
pi.Seek(start)
return
}
// >
if _, ok, err = gt.Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
// Once we've got an open tag, parse anything until the end tag as the tag contents.
// It's going to be rendered out raw.
end := parse.All(parse.String("</"), parse.String(p.name), parse.String(">"))
if e.Contents, ok, err = parse.StringUntil(end).Parse(pi); err != nil || !ok {
err = parse.Error(fmt.Sprintf("<%s>: expected end tag not present", e.Name), pi.Position())
return
}
// Cut the end element.
_, _, _ = end.Parse(pi)
return e, true, nil
}