v2 / vlib / x / templating / dtm / tmpl.v
358 lines · 332 sloc · 11.03 KB · 3eff1b83cf719199d0ff6f63524da63c9294ffd5
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4
5/*
6This source code originates from the internal V compiler 'vlib/v/parser/tmpl.v' and
7has been heavily modified for the needs of the Dynamic Template Manager. Thanks to its original author, Alexander Medvednikov.
8*/
9
10module dtm
11
12import os
13import strings
14
15enum State {
16 simple // default - no special interpretation of tags, *at all*!
17 // That is suitable for the general case of text template interpolation,
18 // for example for interpolating arbitrary source code (even V source) templates.
19
20 html // default, only when the template extension is .html
21 css // <style>
22 js // <script>
23 // span // span.{
24}
25
26fn (mut state State) update(line string) {
27 trimmed_line := line.trim_space()
28 if is_html_open_tag('style', line) {
29 state = .css
30 } else if trimmed_line == '</style>' {
31 state = .html
32 } else if is_html_open_tag('script', line) {
33 state = .js
34 } else if trimmed_line == '</script>' {
35 state = .html
36 }
37}
38
39const tmpl_str_end = "')\n"
40
41// check HTML open tag `<name attr="x" >`
42fn is_html_open_tag(name string, s string) bool {
43 trimmed_line := s.trim_space()
44 mut len := trimmed_line.len
45
46 if len < name.len {
47 return false
48 }
49
50 mut sub := trimmed_line[0..1]
51 if sub != '<' { // not start with '<'
52 return false
53 }
54 sub = trimmed_line[len - 1..len]
55 if sub != '>' { // not end with '<'
56 return false
57 }
58 sub = trimmed_line[len - 2..len - 1]
59 if sub == '/' { // self-closing
60 return false
61 }
62 sub = trimmed_line[1..len - 1]
63 if sub.contains_any('<>') { // `<name <bad> >`
64 return false
65 }
66 if sub == name { // `<name>`
67 return true
68 } else {
69 len = name.len
70 if sub.len <= len { // `<nam>` or `<meme>`
71 return false
72 }
73 if sub[..len + 1] != '${name} ' { // not `<name ...>`
74 return false
75 }
76 return true
77 }
78}
79
80fn replace_placeholders_with_data(line string, data &map[string]DtmMultiTypeMap, state State) string {
81 mut rline := line
82 if data.len == 0 {
83 return rline
84 }
85 mut need_include_html := false
86
87 for key, value in data {
88 mut placeholder := '$${key}'
89
90 if placeholder.ends_with(include_html_key_tag) {
91 placeholder = placeholder.all_before_last(include_html_key_tag)
92 need_include_html = true
93 }
94 if !rline.contains(placeholder) {
95 need_include_html = false
96 continue
97 }
98 mut val_str := ''
99 match value {
100 i8, i16, int, i64, u8, u16, u32, u64, f32, f64 {
101 // Converts value to string
102 temp_val := value.str()
103 // Filters the string value for safe insertion
104 val_str = filter(temp_val)
105 }
106 string {
107 // Checks if the placeholder allows HTML inclusion
108 if need_include_html {
109 if state == State.html {
110 // Escape the whole value first, then restore only the explicit allow-list.
111 // This preserves the documented opt-in HTML behavior without allowing
112 // arbitrary raw tags through `_#includehtml`.
113 val_str = filter(value)
114 for tag in allowed_tags {
115 escaped_tag := filter(tag)
116 val_str = val_str.replace(escaped_tag, tag)
117 }
118 } else {
119 val_str = filter(value)
120 }
121 need_include_html = false
122 } else {
123 // Filters the string value for safe insertion
124 val_str = filter(value)
125 }
126 }
127 }
128
129 rline = rline.replace(placeholder, val_str)
130 }
131 return rline
132}
133
134fn insert_template_code(fn_name string, tmpl_str_start string, line string, data &map[string]DtmMultiTypeMap,
135 state State) string {
136 // HTML, may include `@var`
137 // escaped by cgen, unless it's a `veb.RawHtml` string
138 trailing_bs := tmpl_str_end + 'sb_${fn_name}.write_u8(92)\n' + tmpl_str_start
139 mut rline := line.replace('\\', '\\\\').replace("'", "\\'").replace('@', '$')
140 rline = rline.replace(r'$$', r'\@').replace(r'.$', r'.@')
141 comptime_call_str := rline.find_between('\${', '}')
142 if comptime_call_str.contains("\\'") {
143 rline = rline.replace(comptime_call_str, comptime_call_str.replace("\\'", r"'"))
144 }
145 if rline.ends_with('\\') {
146 rline = rline[0..rline.len - 2] + trailing_bs
147 }
148 if rline.contains('$') {
149 rline = replace_placeholders_with_data(rline, data, state)
150 }
151 return rline
152}
153
154// compile_file compiles the content of a file by the given path as a template
155fn compile_template_file(template_file string, fn_name string, data &map[string]DtmMultiTypeMap) string {
156 template_content := os.read_file(template_file) or {
157 eprintln('${message_signature_error} Template generator can not reading from ${template_file} file')
158 return internat_server_error
159 }
160 mut lines := template_content.split_into_lines()
161
162 basepath := os.dir(template_file)
163
164 tmpl_str_start := "\tsb_${fn_name}.write_string('"
165 mut source := strings.new_builder(1000)
166
167 mut state := State.simple
168 template_ext := os.file_ext(template_file)
169 if template_ext.to_lower() == '.html' {
170 state = .html
171 }
172
173 mut in_span := false
174 mut end_of_line_pos := 0
175 mut start_of_line_pos := 0
176 mut tline_number := -1 // keep the original line numbers, even after insert/delete ops on lines; `i` changes
177 for i := 0; i < lines.len; i++ {
178 line := lines[i]
179 tline_number++
180 start_of_line_pos = end_of_line_pos
181 end_of_line_pos += line.len + 1
182 if state != .simple {
183 state.update(line)
184 }
185 $if trace_tmpl ? {
186 eprintln('>>> tfile: ${template_file}, spos: ${start_of_line_pos:6}, epos:${end_of_line_pos:6}, fi: ${tline_number:5}, i: ${i:5}, state: ${state:10}, line: ${line}')
187 }
188 if line.contains('@header') {
189 position := line.index('@header') or { 0 }
190 eprintln("${message_signature_warn} Please use @include 'header' instead of @header (deprecated), position : ${position}")
191 continue
192 }
193 if line.contains('@footer') {
194 position := line.index('@footer') or { 0 }
195 eprintln("${message_signature_warn} Please use @include 'footer' instead of @footer (deprecated), position : ${position}")
196 continue
197 }
198 if line.contains('@include ') {
199 lines.delete(i)
200 // Allow single or double quoted paths.
201 mut file_name := if line.contains('"') {
202 line.split('"')[1]
203 } else if line.contains("'") {
204 line.split("'")[1]
205 } else {
206 s := '@include '
207 position := line.index(s) or { 0 }
208 eprintln("${message_signature_error} path for @include must be quoted with ' or \" without line breaks or extraneous characters between @include and the quotes, position : ${position}")
209 return internat_server_error
210 }
211 mut file_ext := os.file_ext(file_name)
212 if file_ext == '' {
213 file_ext = '.html'
214 }
215 file_name = file_name.replace(file_ext, '')
216 // relative path, starting with the current folder
217 mut templates_folder := os.real_path(basepath)
218 if file_name.contains('/') && file_name.starts_with('/') {
219 // an absolute path
220 templates_folder = ''
221 }
222 file_path := os.real_path(os.join_path_single(templates_folder,
223 '${file_name}${file_ext}'))
224 $if trace_tmpl ? {
225 eprintln('>>> basepath: "${basepath}" , template_file: "${template_file}" , fn_name: "${fn_name}" , @include line: "${line}" , file_name: "${file_name}" , file_ext: "${file_ext}" , templates_folder: "${templates_folder}" , file_path: "${file_path}"')
226 }
227 file_content := os.read_file(file_path) or {
228 position := line.index('@include ') or { 0 } + '@include '.len
229 eprintln('${message_signature_error} Reading @include file "${file_name}" from path: ${file_path} failed, position : ${position}')
230 return internat_server_error
231 }
232
233 file_splitted := file_content.split_into_lines().reverse()
234 for f in file_splitted {
235 tline_number--
236 lines.insert(i, f)
237 }
238 i--
239 continue
240 }
241 if line.contains('@if ') {
242 /* source.writeln(dtm.tmpl_str_end)
243 pos := line.index('@if') or { continue }
244 source.writeln('if ' + line[pos + 4..] + '{')
245 source.writeln(tmpl_str_start) */
246 continue
247 }
248 if line.contains('@end') {
249 /* // Remove new line byte
250 source.go_back(1)
251 source.writeln(dtm.tmpl_str_end)
252 source.writeln('}')
253 source.writeln(tmpl_str_start) */
254 continue
255 }
256 if line.contains('@else') {
257 // Remove new line byte
258 source.go_back(1)
259 /* source.writeln(dtm.tmpl_str_end)
260 source.writeln(' } else { ')
261 source.writeln(tmpl_str_start) */
262 continue
263 }
264 if line.contains('@for') {
265 /* source.writeln(dtm.tmpl_str_end)
266 pos := line.index('@for') or { continue }
267 source.writeln('for ' + line[pos + 4..] + '{')
268 source.writeln(tmpl_str_start) */
269 continue
270 }
271 if state == .simple {
272 // by default, just copy 1:1
273 source.writeln(insert_template_code(fn_name, tmpl_str_start, line, data, state))
274 continue
275 }
276 // The .simple mode ends here. The rest handles .html/.css/.js state transitions.
277
278 if state != .simple {
279 if line.contains('@js ') {
280 pos := line.index('@js') or { continue }
281 source.write_string('<script src="')
282 source.write_string(line[pos + 5..line.len - 1])
283 source.writeln('"></script>')
284 continue
285 }
286 if line.contains('@css ') {
287 pos := line.index('@css') or { continue }
288 source.write_string('<link href="')
289 source.write_string(line[pos + 6..line.len - 1])
290 source.writeln('" rel="stylesheet" type="text/css">')
291 continue
292 }
293 }
294
295 match state {
296 .html {
297 line_t := line.trim_space()
298 if line_t.starts_with('span.') && line.ends_with('{') {
299 //`span.header {` => `<span class='header'>`
300 class := line.find_between('span.', '{').trim_space()
301 source.writeln('<span class="${class}">')
302 in_span = true
303 continue
304 } else if line_t.starts_with('.') && line.ends_with('{') {
305 //`.header {` => `<div class='header'>`
306 class := line.find_between('.', '{').trim_space()
307 trimmed := line.trim_space()
308 source.write_string(strings.repeat(`\t`, line.len - trimmed.len)) // add the necessary indent to keep <div><div><div> code clean
309 source.writeln('<div class="${class}">')
310 continue
311 } else if line_t.starts_with('#') && line.ends_with('{') {
312 //`#header {` => `<div id='header'>`
313 class := line.find_between('#', '{').trim_space()
314 source.writeln('<div id="${class}">')
315 continue
316 } else if line_t == '}' {
317 source.write_string(strings.repeat(`\t`, line.len - line_t.len)) // add the necessary indent to keep <div><div><div> code clean
318 if in_span {
319 source.writeln('</span>')
320 in_span = false
321 } else {
322 source.writeln('</div>')
323 }
324 continue
325 }
326 }
327 .js {
328 // if line.contains('//V_TEMPLATE') {
329 source.writeln(insert_template_code(fn_name, tmpl_str_start, line, data, state))
330 //} else {
331 // replace `$` to `\$` at first to escape JavaScript template literal syntax
332 // source.writeln(line.replace(r'$', r'\$').replace(r'$$', r'@').replace(r'.$',
333 // r'.@').replace(r"'", r"\'"))
334 //}
335 continue
336 }
337 .css {
338 // disable template variable declaration in inline stylesheet
339 // because of some CSS rules prefixed with `@`.
340 source.writeln(line.replace(r'.$', r'.@').replace(r"'", r"\'"))
341 continue
342 }
343 else {}
344 }
345
346 // by default, just copy 1:1
347 source.writeln(insert_template_code(fn_name, tmpl_str_start, line, data, state))
348 }
349
350 result := source.str()
351 $if trace_tmpl_expansion ? {
352 eprintln('>>>>>>> template expanded to:')
353 eprintln(result)
354 eprintln('-----------------------------')
355 }
356
357 return result
358}
359