Skip to content

Commit

Permalink
rename confusing func naming
Browse files Browse the repository at this point in the history
  • Loading branch information
bobiverse committed Jan 17, 2024
1 parent 771bb7e commit f202a9a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 43 deletions.
48 changes: 25 additions & 23 deletions Template.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,26 +490,26 @@ func (t *Template) fixBrokenPlaceholders(xnode *xmlNode) {
return
}

var isMatchSingleLeftPlaceholder bool
var isMatchSingleRightPlaceholder bool
var isBrokenLeftPlaceholder bool
var isBrokenRightPlaceholder bool
contents := xnode.AllContents()
xnode.Walk(func(xnode *xmlNode) {
if xnode.Content == nil || len(xnode.Content) == 0 {
return
}
// Match right }} to sub or delete
if isMatchSingleLeftPlaceholder {
isMatchSingleRightPlaceholder = t.matchSingleRightPlaceholder(string(xnode.Content))
if isMatchSingleRightPlaceholder {
if isBrokenLeftPlaceholder {
isBrokenRightPlaceholder = t.matchBrokenRightPlaceholder(string(xnode.Content))
if isBrokenRightPlaceholder {
xnode.Content = xnode.Content[bytes.Index(xnode.Content, []byte("}}"))+2:]
} else {
xnode.delete()
return
}
}
// Match left {{ to fix broken
isMatchSingleLeftPlaceholder = t.matchSingleLeftPlaceholder(string(xnode.Content))
if isMatchSingleLeftPlaceholder {
isBrokenLeftPlaceholder = t.matchBrokenLeftPlaceholder(string(xnode.Content))
if isBrokenLeftPlaceholder {
xnode.Content = append(xnode.Content, contents[bytes.Index(contents, xnode.Content)+len(xnode.Content):bytes.Index(contents, []byte("}}"))+2]...)
}
contents = contents[bytes.Index(contents, xnode.Content)+len(xnode.Content):]
Expand Down Expand Up @@ -602,40 +602,42 @@ func (t *Template) Placeholders() []string {
return arr
}

// Match single left placeholder ({{)
func (t *Template) matchSingleLeftPlaceholder(content string) bool {
stack := make([]string, 0, 2)
// Match left part placeholder `{{`
func (t *Template) matchBrokenLeftPlaceholder(content string) bool {
stack := make([]string, 0)

for i, char := range content {
if i > 0 {
if char == '{' && content[i-1] == '{' {
stack = append(stack, "{{")
} else if char == '}' && content[i-1] == '}' {
if len(stack) > 0 {
stack = stack[:len(stack)-1]
} else {
return true
}
} else if char == '}' && content[i-1] == '}' && len(stack) > 0 {
stack = stack[:len(stack)-1]
}
}
}

return false
return len(stack) > 0
}

// Match single right placeholder (}})
func (t *Template) matchSingleRightPlaceholder(content string) bool {
stack := make([]string, 0, 2)
// Match right placeholder part `}}`
func (t *Template) matchBrokenRightPlaceholder(content string) bool {
stack := make([]string, 0)

for i, char := range content {
if i > 0 {
if char == '{' && content[i-1] == '{' {
stack = append(stack, "{{")
} else if char == '}' && content[i-1] == '}' && len(stack) > 0 {
stack = stack[:len(stack)-1]
} else if char == '}' && content[i-1] == '}' {
if len(stack) > 0 {
stack = stack[:len(stack)-1]
} else {
return true
}
}
}
}

return len(stack) > 0
return false
}

// Plaintext - return as plaintext
Expand Down
19 changes: 0 additions & 19 deletions Template_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.2
v1.2.2

0 comments on commit f202a9a

Please sign in to comment.