-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
templatefile.go
163 lines (147 loc) · 3.55 KB
/
templatefile.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
package parser
import (
"bufio"
"io"
"os"
"path/filepath"
"unicode"
"github.com/a-h/lexical/input"
"github.com/a-h/lexical/parse"
)
const maxBufferSize = 1024 * 1024 * 10 // 10MB
func Parse(fileName string) (TemplateFile, error) {
f, err := os.Open(fileName)
if err != nil {
return TemplateFile{}, err
}
fi, err := f.Stat()
if err != nil {
return TemplateFile{}, err
}
bufferSize := maxBufferSize
if fi.Size() < int64(bufferSize) {
bufferSize = int(fi.Size())
}
reader := bufio.NewReader(f)
tfr := NewTemplateFileParser(getDefaultPackageName(fileName)).Parse(input.NewWithBufferSize(reader, bufferSize))
if tfr.Error != nil {
return TemplateFile{}, tfr.Error
}
return tfr.Item.(TemplateFile), nil
}
func getDefaultPackageName(fileName string) (pkg string) {
parent := filepath.Base(filepath.Dir(fileName))
if !isGoIdentifier(parent) {
return "main"
}
return parent
}
func isGoIdentifier(s string) bool {
if len(s) == 0 {
return false
}
for i, r := range s {
if unicode.IsLetter(r) || r == '_' {
continue
}
if i > 0 && unicode.IsDigit(r) {
continue
}
return false
}
return true
}
func ParseString(template string) (TemplateFile, error) {
tfr := NewTemplateFileParser("main").Parse(input.NewFromString(template))
if tfr.Error != nil {
return TemplateFile{}, tfr.Error
}
return tfr.Item.(TemplateFile), nil
}
// NewTemplateFileParser creates a new TemplateFileParser.
func NewTemplateFileParser(pkg string) TemplateFileParser {
return TemplateFileParser{
DefaultPackage: pkg,
}
}
type TemplateFileParser struct {
DefaultPackage string
}
func (p TemplateFileParser) Parse(pi parse.Input) parse.Result {
var tf TemplateFile
// Required package.
// {% package name %}
pr := newPackageParser().Parse(pi)
if pr.Error != nil {
return pr
}
pkg, ok := pr.Item.(Package)
if !ok {
pkg = Package{
Expression: NewExpression(p.DefaultPackage, NewPosition(), NewPosition()),
}
}
tf.Package = pkg
// Optional whitespace.
parse.Optional(parse.WithStringConcatCombiner, whitespaceParser)(pi)
// Optional imports.
// {% import "strings" %}
ip := newImportParser()
for {
ipr := ip.Parse(pi)
if ipr.Error != nil {
return ipr
}
if !ipr.Success {
break
}
tf.Imports = append(tf.Imports, ipr.Item.(Import))
// Eat optional whitespace.
parse.Optional(parse.WithStringConcatCombiner, whitespaceParser)(pi)
}
// Optional templates, CSS, and script templates.
// {% templ Name(p Parameter) %}
// {% css Name() %}
// {% script Name() %}
tp := newTemplateParser()
cssp := newCSSParser()
stp := newScriptTemplateParser()
for {
// Try for a template.
tpr := tp.Parse(pi)
if tpr.Error != nil && tpr.Error != io.EOF {
return tpr
}
if tpr.Success {
tf.Nodes = append(tf.Nodes, tpr.Item.(HTMLTemplate))
// Eat optional whitespace.
parse.Optional(parse.WithStringConcatCombiner, whitespaceParser)(pi)
continue
}
// Try for css.
cssr := cssp.Parse(pi)
if cssr.Error != nil && cssr.Error != io.EOF {
return cssr
}
if cssr.Success {
tf.Nodes = append(tf.Nodes, cssr.Item.(CSSTemplate))
// Eat optional whitespace.
parse.Optional(parse.WithStringConcatCombiner, whitespaceParser)(pi)
continue
}
// Try for script.
stpr := stp.Parse(pi)
if stpr.Error != nil && stpr.Error != io.EOF {
return stpr
}
if stpr.Success {
tf.Nodes = append(tf.Nodes, stpr.Item.(ScriptTemplate))
// Eat optional whitespace.
parse.Optional(parse.WithStringConcatCombiner, whitespaceParser)(pi)
continue
}
break
}
// Success.
return parse.Success("template file", tf, nil)
}