generated from VLADIMIR/template
clear
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
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 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()
|
||||
}
|
||||
Reference in New Issue
Block a user