parser: make the $tmpl subparser more robust. The legacy .html mode, is now ON, only for .html template files.

Implement a .simple default mode, with *minimum* heuristcs, and only
supporting expansion of @expressions, @include, @if, @else, @for, @end.

The existing .html mode, with its complex state transitions for html/js/css
and heuristics, is now used *only* for template files, that have the `.html`
extension.
pull/13215/head
Delyan Angelov 2022-01-18 13:52:54 +02:00
parent f0b7e5049b
commit 40a5c5c1a0
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
4 changed files with 201 additions and 68 deletions

View File

@ -9,12 +9,29 @@ import os
import strings import strings
enum State { enum State {
html simple // default - no special interpretation of tags, *at all*!
// That is suitable for the general case of text template interpolation,
// for example for interpolating arbitrary source code (even V source) templates.
//
html // default, only when the template extension is .html
css // <style> css // <style>
js // <script> js // <script>
// span // span.{ // span // span.{
} }
fn (mut state State) update(line string) {
trimmed_line := line.trim_space()
if is_html_open_tag('style', line) {
state = .css
} else if trimmed_line == '</style>' {
state = .html
} else if is_html_open_tag('script', line) {
state = .js
} else if trimmed_line == '</script>' {
state = .html
}
}
const tmpl_str_end = "')\n" const tmpl_str_end = "')\n"
// check HTML open tag `<name attr="x" >` // check HTML open tag `<name attr="x" >`
@ -79,7 +96,7 @@ pub fn (mut p Parser) compile_template_file(template_file string, fn_name string
} }
basepath := os.dir(template_file) basepath := os.dir(template_file)
lstartlength := lines.len * 30 lstartlength := lines.len * 30
tmpl_str_start := "sb_${fn_name}.write_string('" tmpl_str_start := "\tsb_${fn_name}.write_string('"
mut source := strings.new_builder(1000) mut source := strings.new_builder(1000)
source.writeln(' source.writeln('
import strings import strings
@ -89,25 +106,24 @@ fn vweb_tmpl_${fn_name}() string {
') ')
source.write_string(tmpl_str_start) source.write_string(tmpl_str_start)
mut state := State.html //
mut state := State.simple
template_ext := os.file_ext(template_file)
if template_ext.to_lower() == '.html' {
state = .html
}
//
mut in_span := false mut in_span := false
mut end_of_line_pos := 0 mut end_of_line_pos := 0
mut start_of_line_pos := 0 mut start_of_line_pos := 0
mut tline_number := -1 // keep the original line numbers, even after insert/delete ops on lines; `i` changes mut tline_number := -1 // keep the original line numbers, even after insert/delete ops on lines; `i` changes
for i := 0; i < lines.len; i++ { for i := 0; i < lines.len; i++ {
line := lines[i] line := lines[i]
trimmed_line := line.trim_space()
tline_number++ tline_number++
start_of_line_pos = end_of_line_pos start_of_line_pos = end_of_line_pos
end_of_line_pos += line.len + 1 end_of_line_pos += line.len + 1
if is_html_open_tag('style', line) { if state != .simple {
state = .css state.update(line)
} else if trimmed_line == '</style>' {
state = .html
} else if is_html_open_tag('script', line) {
state = .js
} else if trimmed_line == '</script>' {
state = .html
} }
$if trace_tmpl ? { $if trace_tmpl ? {
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') 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')
@ -125,7 +141,9 @@ fn vweb_tmpl_${fn_name}() string {
} }
reporter: .parser reporter: .parser
}) })
} else if line.contains('@footer') { continue
}
if line.contains('@footer') {
position := line.index('@footer') or { 0 } position := line.index('@footer') or { 0 }
p.error_with_error(errors.Error{ p.error_with_error(errors.Error{
message: "Please use @include 'footer' instead of @footer (deprecated)" message: "Please use @include 'footer' instead of @footer (deprecated)"
@ -138,6 +156,7 @@ fn vweb_tmpl_${fn_name}() string {
} }
reporter: .parser reporter: .parser
}) })
continue
} }
if line.contains('@include ') { if line.contains('@include ') {
lines.delete(i) lines.delete(i)
@ -179,54 +198,86 @@ fn vweb_tmpl_${fn_name}() string {
lines.insert(i, f) lines.insert(i, f)
} }
i-- i--
} else if line.contains('@js ') { continue
pos := line.index('@js') or { continue } }
source.write_string('<script src="') if line.contains('@if ') {
source.write_string(line[pos + 5..line.len - 1])
source.writeln('"></script>')
} else if line.contains('@css ') {
pos := line.index('@css') or { continue }
source.write_string('<link href="')
source.write_string(line[pos + 6..line.len - 1])
source.writeln('" rel="stylesheet" type="text/css">')
} else if line.contains('@if ') {
source.writeln(parser.tmpl_str_end) source.writeln(parser.tmpl_str_end)
pos := line.index('@if') or { continue } pos := line.index('@if') or { continue }
source.writeln('if ' + line[pos + 4..] + '{') source.writeln('if ' + line[pos + 4..] + '{')
source.writeln(tmpl_str_start) source.writeln(tmpl_str_start)
} else if line.contains('@end') { continue
}
if line.contains('@end') {
// Remove new line byte // Remove new line byte
source.go_back(1) source.go_back(1)
source.writeln(parser.tmpl_str_end) source.writeln(parser.tmpl_str_end)
source.writeln('}') source.writeln('}')
source.writeln(tmpl_str_start) source.writeln(tmpl_str_start)
} else if line.contains('@else') { continue
}
if line.contains('@else') {
// Remove new line byte // Remove new line byte
source.go_back(1) source.go_back(1)
source.writeln(parser.tmpl_str_end) source.writeln(parser.tmpl_str_end)
source.writeln(' } else { ') source.writeln(' } else { ')
source.writeln(tmpl_str_start) source.writeln(tmpl_str_start)
} else if line.contains('@for') { continue
}
if line.contains('@for') {
source.writeln(parser.tmpl_str_end) source.writeln(parser.tmpl_str_end)
pos := line.index('@for') or { continue } pos := line.index('@for') or { continue }
source.writeln('for ' + line[pos + 4..] + '{') source.writeln('for ' + line[pos + 4..] + '{')
source.writeln(tmpl_str_start) source.writeln(tmpl_str_start)
} else if state == .html && line.starts_with('span.') && line.ends_with('{') { continue
}
if state == .simple {
// by default, just copy 1:1
source.writeln(insert_template_code(fn_name, tmpl_str_start, line))
continue
}
// The .simple mode ends here. The rest handles .html/.css/.js state transitions.
if state != .simple {
if line.contains('@js ') {
pos := line.index('@js') or { continue }
source.write_string('<script src="')
source.write_string(line[pos + 5..line.len - 1])
source.writeln('"></script>')
continue
}
if line.contains('@css ') {
pos := line.index('@css') or { continue }
source.write_string('<link href="')
source.write_string(line[pos + 6..line.len - 1])
source.writeln('" rel="stylesheet" type="text/css">')
continue
}
}
match state {
.html {
if line.starts_with('span.') && line.ends_with('{') {
// `span.header {` => `<span class='header'>` // `span.header {` => `<span class='header'>`
class := line.find_between('span.', '{').trim_space() class := line.find_between('span.', '{').trim_space()
source.writeln('<span class="$class">') source.writeln('<span class="$class">')
in_span = true in_span = true
} else if state == .html && line.trim_space().starts_with('.') && line.ends_with('{') { continue
}
if line.trim_space().starts_with('.') && line.ends_with('{') {
// `.header {` => `<div class='header'>` // `.header {` => `<div class='header'>`
class := line.find_between('.', '{').trim_space() class := line.find_between('.', '{').trim_space()
trimmed := line.trim_space() trimmed := line.trim_space()
source.write_string(strings.repeat(`\t`, line.len - trimmed.len)) // add the necessary indent to keep <div><div><div> code clean source.write_string(strings.repeat(`\t`, line.len - trimmed.len)) // add the necessary indent to keep <div><div><div> code clean
source.writeln('<div class="$class">') source.writeln('<div class="$class">')
} else if state == .html && line.starts_with('#') && line.ends_with('{') { continue
}
if line.starts_with('#') && line.ends_with('{') {
// `#header {` => `<div id='header'>` // `#header {` => `<div id='header'>`
class := line.find_between('#', '{').trim_space() class := line.find_between('#', '{').trim_space()
source.writeln('<div id="$class">') source.writeln('<div id="$class">')
} else if state == .html && line.trim_space() == '}' { continue
}
if line.trim_space() == '}' {
trimmed := line.trim_space() trimmed := line.trim_space()
source.write_string(strings.repeat(`\t`, line.len - trimmed.len)) // add the necessary indent to keep <div><div><div> code clean source.write_string(strings.repeat(`\t`, line.len - trimmed.len)) // add the necessary indent to keep <div><div><div> code clean
if in_span { if in_span {
@ -235,7 +286,10 @@ fn vweb_tmpl_${fn_name}() string {
} else { } else {
source.writeln('</div>') source.writeln('</div>')
} }
} else if state == .js { continue
}
}
.js {
if line.contains('//V_TEMPLATE') { if line.contains('//V_TEMPLATE') {
source.writeln(insert_template_code(fn_name, tmpl_str_start, line)) source.writeln(insert_template_code(fn_name, tmpl_str_start, line))
} else { } else {
@ -243,19 +297,32 @@ fn vweb_tmpl_${fn_name}() string {
source.writeln(line.replace(r'$', r'\$').replace(r'$$', r'@').replace(r'.$', source.writeln(line.replace(r'$', r'\$').replace(r'$$', r'@').replace(r'.$',
r'.@').replace(r"'", r"\'")) r'.@').replace(r"'", r"\'"))
} }
} else if state == .css { continue
}
.css {
// disable template variable declaration in inline stylesheet // disable template variable declaration in inline stylesheet
// because of some CSS rules prefixed with `@`. // because of some CSS rules prefixed with `@`.
source.writeln(line.replace(r'.$', r'.@').replace(r"'", r"\'")) source.writeln(line.replace(r'.$', r'.@').replace(r"'", r"\'"))
} else { continue
}
else {}
}
// by default, just copy 1:1
source.writeln(insert_template_code(fn_name, tmpl_str_start, line)) source.writeln(insert_template_code(fn_name, tmpl_str_start, line))
} }
}
source.writeln(parser.tmpl_str_end) source.writeln(parser.tmpl_str_end)
source.writeln('_tmpl_res_$fn_name := sb_${fn_name}.str() ') source.writeln('\t_tmpl_res_$fn_name := sb_${fn_name}.str() ')
source.writeln('return _tmpl_res_$fn_name') source.writeln('\treturn _tmpl_res_$fn_name')
source.writeln('}') source.writeln('}')
source.writeln('// === end of vweb html template ===') source.writeln('// === end of vweb html template_file: $template_file ===')
result := source.str() result := source.str()
$if trace_tmpl_expansion ? {
eprintln('>>>>>>> template expanded to:')
eprintln(result)
eprintln('-----------------------------')
}
return result return result
} }

View File

@ -0,0 +1,15 @@
module data
//object: @object
//object_u: @object_u
//objects: @objects
//objects_u: @objects_u
pub struct @object {
pub mut:
id int
name string
description string
tags []string
remarks []int
}

View File

@ -0,0 +1,33 @@
module data
//object: user
//object_u: User
//objects: users
//objects_u: Users
pub struct user {
pub mut:
id int
name string
description string
tags []string
remarks []int
}
module data
//object: circle
//object_u: Circle
//objects: circles
//objects_u: Circles
pub struct circle {
pub mut:
id int
name string
description string
tags []string
remarks []int
}
OK

View File

@ -0,0 +1,18 @@
module main
const codepath_file = @FILE
fn main() {
togenerate := ["user","circle"]
for name in togenerate{
object := name
object_u := name.capitalize()
objects := name+"s"
objects_u := name.capitalize()+"s"
mut txt := $tmpl('data_obj.v.templ')
txt = txt.replace("/////////// THIS IS THE TEMPLATE, THIS CAN BE MODIFIED","")
println(txt)
}
println('OK')
}