-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
templatefile.go
178 lines (160 loc) · 3.92 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
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
}
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 = parse.String("{% package").Parse(pi)
if err != nil {
return
}
if ok {
return tf, false, ErrLegacyFileFormat
}
// Required package.
// package name
from := pi.Position()
tf.Package, ok, err = pkg.Parse(pi)
if err != nil {
return
}
if !ok {
tf.Package = Package{
Expression: NewExpression("package "+p.DefaultPackage, from, pi.Position()),
}
}
// Optional whitespace.
_, _, _ = 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.
var code 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 = parse.StringUntil(parse.Or(parse.NewLine, parse.EOF[string]())).Parse(pi); err != nil {
return
}
hasTemplatePrefix := strings.HasPrefix(l, "templ ") || strings.HasPrefix(l, "css ") || strings.HasPrefix(l, "script ")
if hasTemplatePrefix && strings.HasSuffix(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, GoExpression{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, GoExpression{Expression: expr})
}
// Stop parsing.
break outer
}
}
}
return tf, true, nil
}