Skip to content

Commit 595798e

Browse files
authored
add support for template/if/range/with (#6)
* add support for template/if/range/with * test * go mod
1 parent 36ada61 commit 595798e

File tree

9 files changed

+529
-206
lines changed

9 files changed

+529
-206
lines changed

.github/workflows/test.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
fetch-depth: 2
1212
- uses: actions/setup-go@v2
1313
with:
14-
go-version: "1.20"
14+
go-version: "1.23"
1515
- name: Run coverage
1616
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
1717
- name: Upload coverage to Codecov

cmd/xtemplate/go.mod

+11-16
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,19 @@ module github.com/youthlin/t/cmd/xtemplate
33
go 1.17
44

55
require (
6-
github.com/cockroachdb/errors v1.8.6
7-
github.com/smartystreets/goconvey v1.6.4
8-
github.com/youthlin/t v0.0.8
6+
github.com/smartystreets/goconvey v1.8.1
7+
github.com/youthlin/t v0.0.9
98
)
109

1110
require (
12-
github.com/Xuanwo/go-locale v1.0.0 // indirect
13-
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210803070921-b358b509191a // indirect
14-
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
15-
github.com/cockroachdb/redact v1.1.1 // indirect
16-
github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2 // indirect
17-
github.com/gogo/protobuf v1.3.2 // indirect
18-
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
11+
github.com/Xuanwo/go-locale v1.1.0 // indirect
12+
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
13+
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/gopherjs/gopherjs v1.17.2 // indirect
1915
github.com/jtolds/gls v4.20.0+incompatible // indirect
20-
github.com/kr/pretty v0.2.1 // indirect
21-
github.com/kr/text v0.2.0 // indirect
22-
github.com/pkg/errors v0.9.1 // indirect
23-
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
24-
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 // indirect
25-
golang.org/x/text v0.3.7 // indirect
16+
github.com/smarty/assertions v1.15.0 // indirect
17+
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect
18+
golang.org/x/sys v0.6.0 // indirect
19+
golang.org/x/text v0.14.0 // indirect
20+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
2621
)

cmd/xtemplate/go.sum

+474-181
Large diffs are not rendered by default.

cmd/xtemplate/internal/run.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,24 @@ func resolveTmpl(filename string, ctx *Context, tmpl *template.Template) {
7676
}
7777
root := tmpl.Tree.Root
7878
for _, node := range root.Nodes {
79-
// param.debugPrint(" > node=%#v", node)
79+
ctx.debugPrint(" > node=%#v", node)
8080
// comment 会被忽略,这里拿不到注释信息
81-
if node.Type() == parse.NodeAction {
82-
// 只需要关注 action 节点
81+
switch node.Type() {
82+
case parse.NodeAction:
8383
actionNode := node.(*parse.ActionNode)
8484
resolvePipe(filename, actionNode.Line, ctx, actionNode.Pipe)
85+
case parse.NodeIf:
86+
branchNode := node.(*parse.IfNode)
87+
resolvePipe(filename, branchNode.Line, ctx, branchNode.Pipe)
88+
case parse.NodeRange:
89+
branchNode := node.(*parse.RangeNode)
90+
resolvePipe(filename, branchNode.Line, ctx, branchNode.Pipe)
91+
case parse.NodeWith:
92+
withNode := node.(*parse.WithNode)
93+
resolvePipe(filename, withNode.Line, ctx, withNode.Pipe)
94+
case parse.NodeTemplate:
95+
templateNode := node.(*parse.TemplateNode)
96+
resolvePipe(filename, templateNode.Line, ctx, templateNode.Pipe)
8597
}
8698
}
8799
}

cmd/xtemplate/internal/run_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestGlob(t *testing.T) {
1919

2020
func TestFile(t *testing.T) {
2121
Convey("resolveOneFile", t, func() {
22-
resolveOneFile("testdata/base.tmpl", &Context{
22+
resolveOneFile("testdata/index.tmpl", &Context{
2323
Param: &Param{
2424
Left: "{{",
2525
Right: "}}",
@@ -36,6 +36,7 @@ func TestFile(t *testing.T) {
3636
})
3737
})
3838
}
39+
3940
func Test_run(t *testing.T) {
4041
Convey("run", t, func() {
4142
Run(&Param{
@@ -45,6 +46,7 @@ func Test_run(t *testing.T) {
4546
Keyword: "T;X:1c,2;N:1,2;XN:1c,2,3",
4647
Function: "T",
4748
OutputFile: "-",
49+
Debug: true,
4850
})
4951
})
5052
}

cmd/xtemplate/internal/testdata/base.tmpl

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
<html>
44

55
<head>
6-
<title>{{- .t.T "Hello, World" -}}</title>// FieldNode=[t, T], StringNode="txt"
7-
</head>
6+
{{- end -}}
87

8+
{{- define "title" -}}
9+
<title>{{.}}</title>
10+
</head>
911
<body>
12+
{{- end -}}
13+
1014
{{ "id2" | .XN "ctxt" "id" }}// Cmd | Cmd // StringNode | FieldNode=[XN], StringNode="ctxt", StringNOde="id"
1115
{{ .args | .XN "ctxt" "id" "id2" }}// FieldNode=[args] | FieldNode=[XN], StringNode=ctxt, id, id2
1216
{{ .XN "ctxt" "id" `id2` }}// FieldNode=[XN], StringNode=ctxt, id, id2
@@ -22,14 +26,13 @@
2226
{{ T ("id") }}// Ident PipeNode=() 不支持
2327
{{ or (T "id") (T "id") }}// Ident=or PipeNode=() PipeNode=()
2428

25-
{{- end -}}
2629

2730
1. PipeNode
2831
2. Cmd.Args FieldNode[last] > StringNode
2932
3. Cmd.Args Ident, Ident > String
3033
4. Cmd.Args has PipeNode -> 1.
3134

32-
{{- define "footer" -}}
35+
{{- define "footer" -}}
3336
</body>
3437

3538
</html>

cmd/xtemplate/internal/testdata/index.tmpl

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
{{- template "header" . -}}
2+
{{- template "title" .T "Title" -}}
3+
{{- /*Comments*/ -}}
4+
5+
{{with $foo := .T "foo"}}
6+
{{$foo}}
7+
{{end}}
8+
{{if .T "ok"}}
9+
{{end}}
10+
{{range .T "range"}}
11+
<li>{{.}}</li>
12+
{{end}}
213

314
{{ .T "Hello, World" }}
415
{{ .T "Hello, %v" }}

go.work

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
go 1.23.5
2+
3+
use (
4+
.
5+
./cmd/xtemplate
6+
)

go.work.sum

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github.com/youthlin/t v0.0.9/go.mod h1:rW0l6z4hnkQSWZ2MYatEDwE3XEsQ2eep8yCD1umwvWg=

0 commit comments

Comments
 (0)