2026-03-02 03:15:36 +07:00

43 lines
738 B
Go

package formatter
import (
"bufio"
"strings"
)
type service struct{}
func NewFormatter() IFormatter {
return &service{}
}
func (s *service) FormatText(text string) string {
scanner := bufio.NewScanner(strings.NewReader(text))
scanner.Split(bufio.ScanLines)
lines := []string{}
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
var res strings.Builder
for i, line := range lines {
l := strings.TrimSpace(line)
if strings.HasPrefix(l, "--") {
l = strings.Replace(l, "--", "—", 1)
}
if i == 0 && strings.HasPrefix(l, "—") {
res.WriteString(" ")
}
if i > 0 {
res.WriteString("\n")
if len(l) > 0 {
res.WriteString(" ")
}
}
res.WriteString(l)
}
return res.String()
}