-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
templatefile.go
193 lines (173 loc) · 4.25 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
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
package parser
import (
"errors"
"os"
"path/filepath"
"strings"
"unicode"
"github.com/a-h/parse"
)
func Parse(fileName string) (TemplateFile, error) {
fc, err := os.ReadFile(fileName)
if err != nil {
return TemplateFile{}, err
}
return ParseString(string(fc))
}
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) {
tf, ok, err := NewTemplateFileParser("main").Parse(parse.NewInput(template))
if err != nil {
return tf, err
}
if !ok {
err = ErrTemplateNotFound
}
return tf, err
}
// NewTemplateFileParser creates a new TemplateFileParser.
func NewTemplateFileParser(pkg string) TemplateFileParser {
return TemplateFileParser{
DefaultPackage: pkg,
}
}
var ErrLegacyFileFormat = errors.New("legacy file format - run templ migrate")
var ErrTemplateNotFound = errors.New("template not found")
type TemplateFileParser struct {
DefaultPackage string
}
var legacyPackageParser = parse.String("{% package")
func (p TemplateFileParser) Parse(pi *parse.Input) (tf TemplateFile, ok bool, err error) {
// If we're parsing a legacy file, complain that migration needs to happen.
_, ok, err = legacyPackageParser.Parse(pi)
if err != nil {
return
}
if ok {
return tf, false, ErrLegacyFileFormat
}
// Read until the package.
for {
// Package.
// package name
from := pi.Position()
tf.Package, ok, err = pkg.Parse(pi)
if err != nil {
return
}
if ok {
break
}
var line string
line, ok, err = stringUntilNewLine.Parse(pi)
if err != nil {
return
}
if !ok {
break
}
var newLine string
newLine, _, _ = parse.NewLine.Parse(pi)
tf.Header = append(tf.Header, TemplateFileGoExpression{Expression: NewExpression(line+newLine, from, pi.Position())})
}
// Strip any whitespace between the template declaration and the first template.
_, _, _ = parse.OptionalWhitespace.Parse(pi)
outer:
for {
// Optional templates, CSS, and script templates.
// templ Name(p Parameter)
var tn HTMLTemplate
tn, ok, err = template.Parse(pi)
if err != nil {
return tf, false, err
}
if ok {
tf.Nodes = append(tf.Nodes, tn)
_, _, _ = parse.OptionalWhitespace.Parse(pi)
continue
}
// css Name()
var cn CSSTemplate
cn, ok, err = cssParser.Parse(pi)
if err != nil {
return tf, false, err
}
if ok {
tf.Nodes = append(tf.Nodes, cn)
_, _, _ = parse.OptionalWhitespace.Parse(pi)
continue
}
// script Name()
var sn ScriptTemplate
sn, ok, err = scriptTemplateParser.Parse(pi)
if err != nil {
return tf, false, err
}
if ok {
tf.Nodes = append(tf.Nodes, sn)
_, _, _ = parse.OptionalWhitespace.Parse(pi)
continue
}
// Anything that isn't template content is Go code.
code := new(strings.Builder)
from := pi.Position()
inner:
for {
// Check to see if this line isn't Go code.
last := pi.Index()
var l string
if l, ok, err = stringUntilNewLineOrEOF.Parse(pi); err != nil {
return
}
hasTemplatePrefix := strings.HasPrefix(l, "templ ") || strings.HasPrefix(l, "css ") || strings.HasPrefix(l, "script ")
if hasTemplatePrefix && strings.Contains(l, "(") {
// Unread the line.
pi.Seek(last)
// Take the code so far.
if code.Len() > 0 {
expr := NewExpression(strings.TrimSpace(code.String()), from, pi.Position())
tf.Nodes = append(tf.Nodes, TemplateFileGoExpression{Expression: expr})
}
// Carry on parsing.
break inner
}
code.WriteString(l)
// Eat the newline or EOF that we read until.
var newLine string
if newLine, ok, err = parse.NewLine.Parse(pi); err != nil {
return
}
code.WriteString(newLine)
if _, isEOF, _ := parse.EOF[string]().Parse(pi); isEOF {
if code.Len() > 0 {
expr := NewExpression(strings.TrimSpace(code.String()), from, pi.Position())
tf.Nodes = append(tf.Nodes, TemplateFileGoExpression{Expression: expr})
}
// Stop parsing.
break outer
}
}
}
return tf, true, nil
}