add extra indent conditions

This commit is contained in:
Dan Anglin 2024-07-08 18:48:50 +01:00
parent dcc8cadaf3
commit fa2c75ebca
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638

View file

@ -10,6 +10,11 @@ import (
"unicode" "unicode"
) )
type extraIndentConditiion struct {
pattern *regexp.Regexp
indent string
}
func (p Printer) wrapLines(text string, nIndent int) string { func (p Printer) wrapLines(text string, nIndent int) string {
if nIndent >= p.lineWrapCharacterLimit { if nIndent >= p.lineWrapCharacterLimit {
nIndent = 0 nIndent = 0
@ -25,11 +30,23 @@ func (p Printer) wrapLines(text string, nIndent int) string {
var builder strings.Builder var builder strings.Builder
singleDigitListItemRegex := regexp.MustCompile(`^[0-9]{1}\.\s.*$`) extraIndentConditions := []extraIndentConditiion{
doubleDigitListItemRegex := regexp.MustCompile(`^[0-9]{2}\.\s.*$`) {
pattern: regexp.MustCompile(`^` + symbolBullet + `\s.*$`),
indent: " ",
},
{
pattern: regexp.MustCompile(`^[0-9]{1}\.\s.*$`),
indent: " ",
},
{
pattern: regexp.MustCompile(`^[0-9]{2}\.\s.*$`),
indent: " ",
},
}
for ind, line := range lines { for ind, line := range lines {
builder.WriteString(wrapLine(line, separator+extraIndent(line, singleDigitListItemRegex, doubleDigitListItemRegex), p.lineWrapCharacterLimit-nIndent)) builder.WriteString(wrapLine(line, separator+extraIndent(line, extraIndentConditions), p.lineWrapCharacterLimit-nIndent))
if ind < len(lines)-1 { if ind < len(lines)-1 {
builder.WriteString(separator) builder.WriteString(separator)
@ -68,17 +85,11 @@ func wrapLine(line, separator string, charLimit int) string {
return builder.String() return builder.String()
} }
func extraIndent(line string, singleDigitListItemRegex, doubleDigitListItemRegex *regexp.Regexp) string { func extraIndent(line string, conditions []extraIndentConditiion) string {
if strings.HasPrefix(line, symbolBullet) { for ind := range conditions {
return " " if conditions[ind].pattern.MatchString(line) {
} return conditions[ind].indent
}
if singleDigitListItemRegex.MatchString(line) {
return " "
}
if doubleDigitListItemRegex.MatchString(line) {
return " "
} }
return "" return ""