checkpoint: indent ordered lists

This commit is contained in:
Dan Anglin 2024-07-08 18:31:39 +01:00
parent 527fd069df
commit dcc8cadaf3
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 23 additions and 8 deletions

View file

@ -40,6 +40,7 @@ func (p Printer) convertHTMLToText(text string, wrapLines bool) string {
if wrapLines { if wrapLines {
return p.wrapLines(builder.String(), 0) return p.wrapLines(builder.String(), 0)
} }
return builder.String() return builder.String()
case html.TextToken: case html.TextToken:
text := token.Token().Data text := token.Token().Data

View file

@ -5,6 +5,7 @@
package printer package printer
import ( import (
"regexp"
"strings" "strings"
"unicode" "unicode"
) )
@ -24,16 +25,13 @@ func (p Printer) wrapLines(text string, nIndent int) string {
var builder strings.Builder var builder strings.Builder
for i, line := range lines { singleDigitListItemRegex := regexp.MustCompile(`^[0-9]{1}\.\s.*$`)
extraIndent := "" doubleDigitListItemRegex := regexp.MustCompile(`^[0-9]{2}\.\s.*$`)
if strings.HasPrefix(line, symbolBullet) { for ind, line := range lines {
extraIndent = " " builder.WriteString(wrapLine(line, separator+extraIndent(line, singleDigitListItemRegex, doubleDigitListItemRegex), p.lineWrapCharacterLimit-nIndent))
}
builder.WriteString(wrapLine(line, separator+extraIndent, p.lineWrapCharacterLimit-nIndent)) if ind < len(lines)-1 {
if i < len(lines)-1 {
builder.WriteString(separator) builder.WriteString(separator)
} }
} }
@ -69,3 +67,19 @@ func wrapLine(line, separator string, charLimit int) string {
return builder.String() return builder.String()
} }
func extraIndent(line string, singleDigitListItemRegex, doubleDigitListItemRegex *regexp.Regexp) string {
if strings.HasPrefix(line, symbolBullet) {
return " "
}
if singleDigitListItemRegex.MatchString(line) {
return " "
}
if doubleDigitListItemRegex.MatchString(line) {
return " "
}
return ""
}