From f9c97b55cee396d93850f4c47cd927abb1879551 Mon Sep 17 00:00:00 2001 From: Daniel Lohse Date: Wed, 4 Jul 2018 00:26:24 +0200 Subject: [PATCH] Enable a Set to have custom delimiters This includes an integration test. --- parse.go | 5 ++++- parse_test.go | 26 +++++++++++++++++++------- template.go | 9 +++++++++ testData/custom_delimiters.jet | 21 +++++++++++++++++++++ 4 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 testData/custom_delimiters.jet diff --git a/parse.go b/parse.go index afe538d..fb9a691 100644 --- a/parse.go +++ b/parse.go @@ -167,7 +167,10 @@ func (s *Set) parse(name, text string) (t *Template, err error) { defer t.recover(&err) t.ParseName = t.Name - t.startParse(lex(t.Name, text)) + lexer := lex(t.Name, text, false) + lexer.setDelimiters(s.leftDelim, s.rightDelim) + lexer.run() + t.startParse(lexer) t.parseTemplate() t.stopParse() diff --git a/parse_test.go b/parse_test.go index fc2992a..3003bed 100644 --- a/parse_test.go +++ b/parse_test.go @@ -25,10 +25,15 @@ var parseSet = NewSet(nil, "./testData") type ParserTestCase struct { *testing.T + set *Set } func (t ParserTestCase) ExpectPrintName(name, input, output string) { - template, err := parseSet.parse(name, input) + set := parseSet + if t.set != nil { + set = t.set + } + template, err := set.parse(name, input) if err != nil { t.Errorf("%q %s", input, err.Error()) return @@ -58,36 +63,43 @@ func (t ParserTestCase) ExpectPrintSame(input string) { } func TestParseTemplateAndImport(t *testing.T) { - p := ParserTestCase{t} + p := ParserTestCase{T: t} p.TestPrintFile("extends.jet") p.TestPrintFile("imports.jet") } func TestParseTemplateControl(t *testing.T) { - p := ParserTestCase{t} + p := ParserTestCase{T: t} p.TestPrintFile("if.jet") p.TestPrintFile("range.jet") } func TestParseTemplateExpressions(t *testing.T) { - p := ParserTestCase{t} + p := ParserTestCase{T: t} p.TestPrintFile("simple_expression.jet") p.TestPrintFile("additive_expression.jet") p.TestPrintFile("multiplicative_expression.jet") } func TestParseTemplateBlockYield(t *testing.T) { - p := ParserTestCase{t} + p := ParserTestCase{T: t} p.TestPrintFile("block_yield.jet") p.TestPrintFile("new_block_yield.jet") } func TestParseTemplateIndexSliceExpression(t *testing.T) { - p := ParserTestCase{t} + p := ParserTestCase{T: t} p.TestPrintFile("index_slice_expression.jet") } func TestParseTemplateAssignment(t *testing.T) { - p := ParserTestCase{t} + p := ParserTestCase{T: t} p.TestPrintFile("assignment.jet") } + +func TestParseTemplateWithCustomDelimiters(t *testing.T) { + set := NewSet(nil, "./testData") + set.Delims("[[", "]]") + p := ParserTestCase{T: t, set: set} + p.TestPrintFile("custom_delimiters.jet") +} diff --git a/template.go b/template.go index ed91657..8afaaf5 100644 --- a/template.go +++ b/template.go @@ -41,6 +41,8 @@ type Set struct { gmx *sync.RWMutex // global variables map mutex defaultExtensions []string developmentMode bool + leftDelim string + rightDelim string } // SetDevelopmentMode set's development mode on/off, in development mode template will be recompiled on every run @@ -111,6 +113,13 @@ func (s *Set) AddGopathPath(path string) { } } +// Delims sets the delimiters to the specified strings. Parsed templates will +// inherit the settings. Not setting them leaves them at the default: {{ or }}. +func (s *Set) Delims(left, right string) { + s.leftDelim = left + s.rightDelim = right +} + // resolveName try to resolve a template name, the steps as follow // 1. try provided path // 2. try provided path+defaultExtensions diff --git a/testData/custom_delimiters.jet b/testData/custom_delimiters.jet new file mode 100644 index 0000000..46d326a --- /dev/null +++ b/testData/custom_delimiters.jet @@ -0,0 +1,21 @@ +[[ . ]] +[[ singleValue ]] +[[ nil ]] +[[ "" ]] +[[ val.Field ]] +[[ url: "" ]] +[[ url: "","" |pipe ]] +[[ url("") |pipe |pipe ]] +[[ url("","").Field |pipe ]] +[[ url("","").Method("") |pipe ]] +=== +{{.}} +{{singleValue}} +{{nil}} +{{""}} +{{val.Field}} +{{url:""}} +{{url:"", "" | pipe}} +{{url("") | pipe | pipe}} +{{url("", "").Field | pipe}} +{{url("", "").Method("") | pipe}}