Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: control open and close delimiters #17

Merged
merged 1 commit into from
May 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions placeholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ import (
"strings"
)

const (
var (
// OpenDelimiter defines the opening delimiter for the placeholders used inside a docx-document.
OpenDelimiter rune = '{'
// CloseDelimiter defines the closing delimiter for the placeholders used inside a docx-document.
CloseDelimiter rune = '}'
)

// ChangeOpenCloseDelimiter is used for change the open and close delimiters
func ChangeOpenCloseDelimiter(openDelimiter, closeDelimiter rune) {
OpenDelimiter = openDelimiter
CloseDelimiter = closeDelimiter
}

var (
// OpenDelimiterRegex is used to quickly match the opening delimiter and find it'str positions.
OpenDelimiterRegex = regexp.MustCompile(string(OpenDelimiter))
Expand Down Expand Up @@ -91,7 +97,6 @@ func ParsePlaceholders(runs DocumentRuns, docBytes []byte) (placeholders []*Plac
openPos := delimPositions(openDelimPositions)
closePos := delimPositions(closeDelimPositions)


// In case there are the same amount of open and close delimiters.
// Here we will have three three different sub-cases.
// Case 1 (default):
Expand Down Expand Up @@ -128,7 +133,7 @@ func ParsePlaceholders(runs DocumentRuns, docBytes []byte) (placeholders []*Plac
if len(openPos) == 1 {
return false
}
if openPos[0] < closePos [0] &&
if openPos[0] < closePos[0] &&
openPos[1] < closePos[0] {
return true
}
Expand All @@ -153,7 +158,7 @@ func ParsePlaceholders(runs DocumentRuns, docBytes []byte) (placeholders []*Plac
}

// everything up to firstClosePos belongs to the currently open placeholder
fragment := NewPlaceholderFragment(0, Position{0, int64(firstClosePos)+1}, run)
fragment := NewPlaceholderFragment(0, Position{0, int64(firstClosePos) + 1}, run)
unclosedPlaceholder.Fragments = append(unclosedPlaceholder.Fragments, fragment)
placeholders = append(placeholders, unclosedPlaceholder)

Expand Down