Skip to content

Commit

Permalink
Enable a Set to have custom delimiters
Browse files Browse the repository at this point in the history
This includes an integration test.
  • Loading branch information
annismckenzie committed Jul 3, 2018
1 parent e7bfa33 commit f9c97b5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
5 changes: 4 additions & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
26 changes: 19 additions & 7 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
9 changes: 9 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions testData/custom_delimiters.jet
Original file line number Diff line number Diff line change
@@ -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}}

0 comments on commit f9c97b5

Please sign in to comment.