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 {
return p.wrapLines(builder.String(), 0)
}
return builder.String()
case html.TextToken:
text := token.Token().Data

View file

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