Skip to content

Commit

Permalink
v.parser: allow double quotes in @include template directives (#20628)
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrec committed Feb 1, 2024
1 parent 68bd9a9 commit 232560d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions vlib/v/parser/tests/tmpl/include.txt
@@ -0,0 +1,5 @@
some text

@include abc.txt

some more text
7 changes: 7 additions & 0 deletions vlib/v/parser/tests/tmpl_include_without_quotes.out
@@ -0,0 +1,7 @@
vlib/v/parser/tests/tmpl/include.txt:3:10: error: path for @include must be quoted with ' or "
1 | some text
2 |
3 | @include abc.txt
| ~~~~~~~
4 |
5 | some more text
6 changes: 6 additions & 0 deletions vlib/v/parser/tests/tmpl_include_without_quotes.vv
@@ -0,0 +1,6 @@
fn main() {
a := 'foo'
b := 'bar'
result := $tmpl('tmpl/include.txt')
assert result.trim_space() == 'foo\nbar'
}
23 changes: 22 additions & 1 deletion vlib/v/parser/tmpl.v
Expand Up @@ -164,7 +164,28 @@ fn vweb_tmpl_${fn_name}() string {
}
if line.contains('@include ') {
lines.delete(i)
mut file_name := line.split("'")[1]
// Allow single or double quoted paths.
mut file_name := if line.contains('"') {
line.split('"')[1]
} else if line.contains("'") {
line.split("'")[1]
} else {
s := '@include '
position := line.index(s) or { 0 }
p.error_with_error(errors.Error{
message: 'path for @include must be quoted with \' or "'
file_path: template_file
pos: token.Pos{
len: s.len
line_nr: tline_number
pos: start_of_line_pos + position + s.len
col: position + s.len
last_line: lines.len + 1
}
reporter: .parser
})
''
}
mut file_ext := os.file_ext(file_name)
if file_ext == '' {
file_ext = '.html'
Expand Down
2 changes: 2 additions & 0 deletions vlib/v/tests/tmpl/include.txt
@@ -0,0 +1,2 @@
@include "a.txt"
@include 'b.txt'
8 changes: 8 additions & 0 deletions vlib/v/tests/tmpl_with_double_quotes_test.v
@@ -0,0 +1,8 @@
fn test_tmpl_with_single_quotes() {
a := 'foo'
b := 'bar'
result := $tmpl('tmpl/include.txt')

assert result.trim_space() == 'foo
bar'
}

0 comments on commit 232560d

Please sign in to comment.