From 232560dcac7e43b8db5f9168d785d118e7d08c43 Mon Sep 17 00:00:00 2001 From: Pierre Curto Date: Thu, 1 Feb 2024 11:44:36 +0100 Subject: [PATCH] v.parser: allow double quotes in @include template directives (#20628) --- vlib/v/parser/tests/tmpl/include.txt | 5 ++++ .../tests/tmpl_include_without_quotes.out | 7 ++++++ .../tests/tmpl_include_without_quotes.vv | 6 +++++ vlib/v/parser/tmpl.v | 23 ++++++++++++++++++- vlib/v/tests/tmpl/include.txt | 2 ++ vlib/v/tests/tmpl_with_double_quotes_test.v | 8 +++++++ 6 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 vlib/v/parser/tests/tmpl/include.txt create mode 100644 vlib/v/parser/tests/tmpl_include_without_quotes.out create mode 100644 vlib/v/parser/tests/tmpl_include_without_quotes.vv create mode 100644 vlib/v/tests/tmpl/include.txt create mode 100644 vlib/v/tests/tmpl_with_double_quotes_test.v diff --git a/vlib/v/parser/tests/tmpl/include.txt b/vlib/v/parser/tests/tmpl/include.txt new file mode 100644 index 000000000..04885c4a8 --- /dev/null +++ b/vlib/v/parser/tests/tmpl/include.txt @@ -0,0 +1,5 @@ +some text + +@include abc.txt + +some more text diff --git a/vlib/v/parser/tests/tmpl_include_without_quotes.out b/vlib/v/parser/tests/tmpl_include_without_quotes.out new file mode 100644 index 000000000..c1d7ac934 --- /dev/null +++ b/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 diff --git a/vlib/v/parser/tests/tmpl_include_without_quotes.vv b/vlib/v/parser/tests/tmpl_include_without_quotes.vv new file mode 100644 index 000000000..44208f5b0 --- /dev/null +++ b/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' +} diff --git a/vlib/v/parser/tmpl.v b/vlib/v/parser/tmpl.v index 3d66f6650..62edd3100 100644 --- a/vlib/v/parser/tmpl.v +++ b/vlib/v/parser/tmpl.v @@ -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' diff --git a/vlib/v/tests/tmpl/include.txt b/vlib/v/tests/tmpl/include.txt new file mode 100644 index 000000000..98ba4f760 --- /dev/null +++ b/vlib/v/tests/tmpl/include.txt @@ -0,0 +1,2 @@ +@include "a.txt" +@include 'b.txt' diff --git a/vlib/v/tests/tmpl_with_double_quotes_test.v b/vlib/v/tests/tmpl_with_double_quotes_test.v new file mode 100644 index 000000000..77cd1933d --- /dev/null +++ b/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' +} -- 2.39.5