tmpl: don't trim whitespace and escape \ (#11393)

pull/11407/head
Dialga 2021-09-06 12:12:53 +12:00 committed by GitHub
parent 2cfc6e007d
commit 87f38bf6e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 11 deletions

View File

@ -10,7 +10,7 @@ import strings
const tmpl_str_start = "sb.write_string('"
const tmpl_str_end = "' ) "
const tmpl_str_end = "')\n"
enum State {
html
@ -78,14 +78,13 @@ mut sb := strings.new_builder($lstartlength)\n
mut start_of_line_pos := 0
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++ {
oline := lines[i]
line := lines[i]
tline_number++
start_of_line_pos = end_of_line_pos
end_of_line_pos += oline.len + 1
end_of_line_pos += line.len + 1
$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}, line: $oline')
eprintln('>>> tfile: $template_file, spos: ${start_of_line_pos:6}, epos:${end_of_line_pos:6}, fi: ${tline_number:5}, i: ${i:5}, line: $line')
}
line := oline.trim_space()
if is_html_open_tag('style', line) {
state = .css
} else if line == '</style>' {
@ -225,8 +224,8 @@ mut sb := strings.new_builder($lstartlength)\n
} else {
// HTML, may include `@var`
// escaped by cgen, unless it's a `vweb.RawHtml` string
source.writeln(line.replace(r'@', r'$').replace(r'$$', r'@').replace(r'.$',
r'.@').replace(r"'", r"\'"))
source.writeln(line.replace_each([r'@', r'$', r'$$', r'@', r'.$', r'.@', r"'", r"\'",
'\\', parser.tmpl_str_end + 'sb.write_b(92)\n' + parser.tmpl_str_start]))
}
}
source.writeln(parser.tmpl_str_end)

View File

@ -32,6 +32,16 @@ const classes = `header ${ isLargeScreen() ? '' :
<header>
<h1>@{title}</h1>
</header>
<pre>
_
| |
_____ ____ _ _ __ ___ _ __ | | ___
/ _ \ \/ / _` | '_ ` _ \| '_ \| |/ _ \
| __/> < (_| | | | | | | |_) | | __/
\___/_/\_\__,_|_| |_| |_| .__/|_|\___|
| |
|_|
</pre>
</body>
</html>

View File

@ -8,8 +8,8 @@ color: green;
}
@keyframes mymove {
from {top: 0px;}
to {top: 200px;}
from {top: 0px;}
to {top: 200px;}
}
</style>
<style media="print">
@ -32,6 +32,16 @@ const classes = `header ${ isLargeScreen() ? '' :
<header>
<h1>TEST</h1>
</header>
<pre>
_
| |
_____ ____ _ _ __ ___ _ __ | | ___
/ _ \ \/ / _` | '_ ` _ \| '_ \| |/ _ \
| __/> < (_| | | | | | | |_) | | __/
\___/_/\_\__,_|_| |_| |_| .__/|_|\___|
| |
|_|
</pre>
</body>
</html>