-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
162 lines (161 loc) · 5.84 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<title>Go template validator</title>
<meta name="description" content="Online go template validator">
<meta name="keywords" content="go golang template validation validator">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
margin: 1em;
}
.description {
max-width: 600px;
}
.line::before {
content: " ";
display: inline-block;
width: {{.LineNumSpacing}}em;
margin-right: 0.5em;
}
.line[data-line-no]::before {
content: attr(data-line-no);
color: gray;
}
.line.error::before {
background-color: crimson;
color: white;
}
.line.error::before {
text-align: center;
content: "!";
}
.line.error + .line.error::before {
content: " ";
}
.error {
color: crimson;
}
label {
display: block;
font-size: 14px;
}
textarea {
width: 100%;
box-sizing: border-box;
font-family: monospace;
}
input {
font-family: monospace;
}
summary h3 {
display: inline-block;
}
pre {
overflow-x: auto;
max-width: 100%;
}
footer {
margin-top: 2em;
margin-bottom: 1em;
}
/* dark mode is trendy */
@media (prefers-color-scheme: dark) {
body {
background-color: black;
color: gainsboro;
}
a[href] {
color: skyblue;
}
a:visited {
color: violet;
}
}
</style>
</head>
<body>
<h2>Go template validation</h2>
<section class="description">
<p>
When working with the <code><a href="https://golang.org/pkg/text/template/">"text/template"</a></code> and
<code><a href="https://golang.org/pkg/html/template/">"html/template"</a></code> packages, I often have a hard
time understanding go's errors, especially when they're inline in code. This is a simple tool to visually
show where validation errors are happening.
</p>
<p>
To use, choose a file, insert your template code directly, or copy and paste a string literal from source. You can add mock data in the form of JSON.
</p>
</section>
<details open>
<summary><h3>Input</h3></summary>
<form method="POST" enctype="multipart/form-data">
<p>
<label for="from-file">Upload file</label>
<input type="file" name="from-file" id="from-file" />
</p>
<p>
<label for="from-raw-text">Template</label>
<textarea wrap="off" name="from-raw-text" id="from-raw-text" placeholder="The bot says {{"{{"}}.Value{{"}}"}}">{{.RawText}}</textarea>
<label title="Check this if you copied a string literal from source code, including wrapping quotes.">
<input type="checkbox" name="unquote" id="unquote" />
String literal<sup><a href="https://golang.org/ref/spec#String_literals" target="_blank" title="String literal help">﹖</a></sup>
(include the wrapping quotes in text area)
</label>
</p>
<p>
<label for="data">Data (json)</label>
<textarea wrap="off" name="data" id="data" placeholder='{"Value": "hello world"}'>{{.RawData}}</textarea>
</p>
<p>
<label for="functions">Function names (comma separated list)</label>
<input type="text" name="functions" id="functions" value="{{.RawFunctions}}" />
</p>
<p>
<button type="submit">Submit</button>
</p>
</form>
</details>
{{if or .RawText .Errors -}}
<details open>
<summary><h3>Results</h3></summary>
{{if not (len .Errors) -}}
<p>No errors found</p>
{{- else -}}
<p>{{len .Errors}} error{{if ne (len .Errors) 1}}s{{end}} found</p>
{{- end}}
{{range $ei, $e := $.Errors -}}
{{if eq $e.Line -1 -}}<p class="error">{{$e.Description}} [{{$e.Level}}]</p>{{- end}}
{{- end}}
<pre>
{{- range $i, $l := .TextLines -}}
<span class="line{{- range $ei, $e := $.Errors}}{{if eq $i $e.Line}} with-error{{end}}{{end}}" data-line-no="{{$i}}">
{{- $l -}}
</span>{{nl}}
{{- range $ei, $e := $.Errors -}}
{{if eq $i $e.Line -}}
{{- range $si, $s := split $e.Description -}}
<span class="line error {{$e.Level}}">
{{- if ne $e.Char -1 -}}
{{- range $_ := intRange 1 $e.Char}}{{" "}}{{end -}}
{{- if eq $si 0}}{{"↑ " -}}{{else}}{{range $_ := intRange 0 $si }}{{" "}}{{end}}{{end -}}
{{- end -}}
{{- $s -}}
</span>{{nl}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
</pre>
</details>
{{- end}}
{{if .Output -}}
<details>
<summary><h3>Output</h3></summary>
<pre>{{- .Output -}}</pre>
</details>
{{- end}}
<footer>Made by <a href="https://camlittle.com">Cameron Little</a>. Contribute on <a href="https://github.com/apexskier/go-template-validation">GitHub</a>.</footer>
</body>
</html>