Skip to content

Commit 230ed01

Browse files
ryanlathsauerbraten
authored andcommitted
Adds option for custom comment delimiters
Fixes: #187
1 parent 41ef507 commit 230ed01

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

lex.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ const eof = -1
141141
const (
142142
defaultLeftDelim = "{{"
143143
defaultRightDelim = "}}"
144-
leftComment = "{*"
145-
rightComment = "*}"
144+
defaultLeftComment = "{*"
145+
defaultRightComment = "*}"
146146
leftTrimMarker = "- "
147147
rightTrimMarker = " -"
148148
trimMarkerLen = Pos(len(leftTrimMarker))
@@ -165,6 +165,8 @@ type lexer struct {
165165
lastType itemType
166166
leftDelim string
167167
rightDelim string
168+
leftComment string
169+
rightComment string
168170
trimRightDelim string
169171
}
170172

@@ -177,6 +179,15 @@ func (l *lexer) setDelimiters(leftDelim, rightDelim string) {
177179
}
178180
}
179181

182+
func (l *lexer) setCommentDelimiters(leftDelim, rightDelim string) {
183+
if leftDelim != "" {
184+
l.leftComment = leftDelim
185+
}
186+
if rightDelim != "" {
187+
l.rightComment = rightDelim
188+
}
189+
}
190+
180191
// next returns the next rune in the input.
181192
func (l *lexer) next() rune {
182193
if int(l.pos) >= len(l.input) {
@@ -266,6 +277,8 @@ func lex(name, input string, run bool) *lexer {
266277
items: make(chan item),
267278
leftDelim: defaultLeftDelim,
268279
rightDelim: defaultRightDelim,
280+
leftComment: defaultLeftComment,
281+
rightComment: defaultRightComment,
269282
trimRightDelim: rightTrimMarker + defaultRightDelim,
270283
}
271284
if run {
@@ -289,7 +302,7 @@ func lexText(l *lexer) stateFn {
289302
for {
290303
// without breaking the API, this seems like a reasonable workaround to correctly parse comments
291304
i := strings.IndexByte(l.input[l.pos:], l.leftDelim[0]) // index of suspected left delimiter
292-
ic := strings.IndexByte(l.input[l.pos:], leftComment[0]) // index of suspected left comment marker
305+
ic := strings.IndexByte(l.input[l.pos:], l.leftComment[0]) // index of suspected left comment marker
293306
if ic > -1 && ic < i { // use whichever is lower for future lexing
294307
i = ic
295308
}
@@ -313,7 +326,7 @@ func lexText(l *lexer) stateFn {
313326
l.ignore()
314327
return lexLeftDelim
315328
}
316-
if strings.HasPrefix(l.input[l.pos:], leftComment) {
329+
if strings.HasPrefix(l.input[l.pos:], l.leftComment) {
317330
if l.pos > l.start {
318331
l.emit(itemText)
319332
}
@@ -346,12 +359,12 @@ func lexLeftDelim(l *lexer) stateFn {
346359

347360
// lexComment scans a comment. The left comment marker is known to be present.
348361
func lexComment(l *lexer) stateFn {
349-
l.pos += Pos(len(leftComment))
350-
i := strings.Index(l.input[l.pos:], rightComment)
362+
l.pos += Pos(len(l.leftComment))
363+
i := strings.Index(l.input[l.pos:], l.rightComment)
351364
if i < 0 {
352365
return l.errorf("unclosed comment")
353366
}
354-
l.pos += Pos(i + len(rightComment))
367+
l.pos += Pos(i + len(l.rightComment))
355368
l.ignore()
356369
return lexText
357370
}

parse.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ func (s *Set) parse(name, text string, cacheAfterParsing bool) (t *Template, err
225225

226226
lexer := lex(name, text, false)
227227
lexer.setDelimiters(s.leftDelim, s.rightDelim)
228+
lexer.setCommentDelimiters(s.leftComment, s.rightComment)
228229
lexer.run()
229230
t.startParse(lexer)
230231
t.parseTemplate(cacheAfterParsing)

parse_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func TestParseTemplateWithCustomDelimiters(t *testing.T) {
131131
NewOSFileSystemLoader("./testData"),
132132
WithSafeWriter(nil),
133133
WithDelims("[[", "]]"),
134+
WithCommentDelims("[*", "*]"),
134135
)
135136
p := ParserTestCase{T: t, set: set}
136137
p.TestPrintFile("custom_delimiters.jet")

set.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type Set struct {
2323
developmentMode bool
2424
leftDelim string
2525
rightDelim string
26+
leftComment string
27+
rightComment string
2628
}
2729

2830
// Option is the type of option functions that can be used in NewSet().
@@ -84,6 +86,15 @@ func WithDelims(left, right string) Option {
8486
}
8587
}
8688

89+
// WithCommentDelims returns an option function that sets the comment delimiters to the specified strings.
90+
// Parsed templates will inherit the settings. Not setting them leaves them at the default: `{*` and `*}`.
91+
func WithCommentDelims(left, right string) Option {
92+
return func(s *Set) {
93+
s.leftComment = left
94+
s.rightComment = right
95+
}
96+
}
97+
8798
// WithTemplateNameExtensions returns an option function that sets the extensions to try when looking
8899
// up template names in the cache or loader. Default extensions are `""` (no extension), `".jet"`,
89100
// `".html.jet"`, `".jet.html"`. Extensions will be tried in the order they are defined in the slice.

testData/custom_delimiters.jet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{* comment *}[[ . ]]
1+
[* comment *][[ . ]]
22
[[ singleValue ]]
33
[[ nil ]]
44
[[ "" ]]

0 commit comments

Comments
 (0)