Compare commits

...

2 commits

Author SHA1 Message Date
fa2c75ebca
add extra indent conditions 2024-07-08 18:48:50 +01:00
dcc8cadaf3
checkpoint: indent ordered lists 2024-07-08 18:31:39 +01:00
2 changed files with 34 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,10 +5,16 @@
package printer
import (
"regexp"
"strings"
"unicode"
)
type extraIndentConditiion struct {
pattern *regexp.Regexp
indent string
}
func (p Printer) wrapLines(text string, nIndent int) string {
if nIndent >= p.lineWrapCharacterLimit {
nIndent = 0
@ -24,16 +30,25 @@ func (p Printer) wrapLines(text string, nIndent int) string {
var builder strings.Builder
for i, line := range lines {
extraIndent := ""
extraIndentConditions := []extraIndentConditiion{
{
pattern: regexp.MustCompile(`^` + symbolBullet + `\s.*$`),
indent: " ",
},
{
pattern: regexp.MustCompile(`^[0-9]{1}\.\s.*$`),
indent: " ",
},
{
pattern: regexp.MustCompile(`^[0-9]{2}\.\s.*$`),
indent: " ",
},
}
if strings.HasPrefix(line, symbolBullet) {
extraIndent = " "
}
for ind, line := range lines {
builder.WriteString(wrapLine(line, separator+extraIndent(line, extraIndentConditions), 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 +84,13 @@ func wrapLine(line, separator string, charLimit int) string {
return builder.String()
}
func extraIndent(line string, conditions []extraIndentConditiion) string {
for ind := range conditions {
if conditions[ind].pattern.MatchString(line) {
return conditions[ind].indent
}
}
return ""
}