-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsimple_format_filter.go
More file actions
41 lines (32 loc) · 913 Bytes
/
simple_format_filter.go
File metadata and controls
41 lines (32 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package pipeline
import (
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
var (
simpleFormatAllBreak = regexp.MustCompile("\r\n?")
simpleFormatParagraphMark = regexp.MustCompile("\n\n+")
)
// SimpleFormatFilter covnert simple plain text into breakable html
type SimpleFormatFilter struct {
}
func (f SimpleFormatFilter) Call(doc *goquery.Document) (err error) {
html, err := doc.Find("body").Html()
if err != nil {
return
}
outs := []string{}
for _, paragraph := range f.splitParagraphs(html) {
paragraph = strings.ReplaceAll(paragraph, "\n", "<br/>")
outs = append(outs, "<p>"+paragraph+"</p>")
}
html = strings.Join(outs, "")
doc.Find("body").SetHtml(html)
return
}
func (f SimpleFormatFilter) splitParagraphs(html string) (paragraphs []string) {
html = simpleFormatAllBreak.ReplaceAllString(html, "\n")
paragraphs = simpleFormatParagraphMark.Split(html, -1)
return
}