Skip to content

Commit 953aa3a

Browse files
committed
refactor: improve parser
1 parent e7b5c6e commit 953aa3a

8 files changed

Lines changed: 86 additions & 78 deletions

File tree

builder/content/builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (b *Builder) findFiles(path string, pattern string) []string {
5151

5252
files := make([]string, 0)
5353
for _, m := range matches {
54-
if !b.parser.IsSupport(filepath.Ext(m)) {
54+
if !b.parser.IsSupported(filepath.Ext(m)) {
5555
continue
5656
}
5757
files = append(files, m)
@@ -164,7 +164,7 @@ func (b *Builder) Build(ctx context.Context) error {
164164
}
165165
return nil
166166
}
167-
if !b.parser.IsSupport(filepath.Ext(path)) {
167+
if !b.parser.IsSupported(filepath.Ext(path)) {
168168
b.insertAsset(path)
169169
return nil
170170
}

builder/content/parser/html/html.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"golang.org/x/net/html"
1111
)
1212

13-
type htmlReader struct {
13+
type htmlParser struct {
1414
conf config.Config
1515
}
1616

17-
func (d *htmlReader) parse(result *parser.Result, n *html.Node) error {
17+
func (d *htmlParser) parse(result *parser.Result, n *html.Node) error {
1818
if n.Type == html.ElementNode {
1919
switch n.Data {
2020
case "title":
@@ -75,7 +75,7 @@ func (d *htmlReader) parse(result *parser.Result, n *html.Node) error {
7575
return nil
7676
}
7777

78-
func (d *htmlReader) Read(r io.Reader) (*parser.Result, error) {
78+
func (d *htmlParser) Parse(r io.Reader) (*parser.Result, error) {
7979
doc, err := html.Parse(r)
8080
if err != nil {
8181
return nil, err
@@ -90,8 +90,8 @@ func (d *htmlReader) Read(r io.Reader) (*parser.Result, error) {
9090
return result, nil
9191
}
9292

93-
func New(conf config.Config) parser.Reader {
94-
return &htmlReader{conf}
93+
func New(conf config.Config) parser.MarkupParser {
94+
return &htmlParser{conf}
9595
}
9696

9797
func init() {

builder/content/parser/markdown/markdown.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010

1111
"github.com/flosch/pongo2/v6"
12-
"github.com/honmaple/snow/builder/content"
1312
"github.com/honmaple/snow/builder/content/parser"
1413
"github.com/honmaple/snow/builder/theme/template"
1514
"github.com/honmaple/snow/config"
@@ -24,17 +23,15 @@ var (
2423
MARKDOWN_META = regexp.MustCompile(`^([^:]+):(\s+(.*)|$)`)
2524
)
2625

27-
type markdown struct {
28-
conf config.Config
26+
type mdParser struct {
2927
opts []blackfriday.Option
3028
}
3129

32-
func readMeta(r io.Reader, contentBuf *bytes.Buffer, summaryBuf *bytes.Buffer) (content.Meta, error) {
30+
func readMeta(r io.Reader, content *bytes.Buffer, summary *bytes.Buffer, result *parser.Result) error {
3331
var (
3432
isMeta = true
3533
isFormat = true
3634
isSummery = true
37-
meta = make(content.Meta)
3835
scanner = bufio.NewScanner(r)
3936
)
4037

@@ -58,68 +55,70 @@ func readMeta(r io.Reader, contentBuf *bytes.Buffer, summaryBuf *bytes.Buffer) (
5855
}
5956

6057
if err := cf.ReadConfig(&b); err != nil {
61-
return nil, err
58+
return err
6259
}
6360
// 不要直接使用meta反序列化数据, 否则子元素map类型也会是page.Meta
64-
meta = content.Meta(cf.AllSettings())
61+
result.FrontMatter = cf.AllSettings()
6562
isFormat = false
6663
continue
6764
}
6865
isFormat = false
6966

7067
if isMeta {
7168
if match := MARKDOWN_META.FindStringSubmatch(line); match != nil {
72-
meta.Set(strings.ToLower(match[1]), strings.TrimSpace(match[3]))
69+
result.SetFrontMatter(strings.ToLower(match[1]), strings.TrimSpace(match[3]))
7370
continue
7471
}
7572
}
7673
isMeta = false
7774
if isSummery && MARKDOWN_MORE.MatchString(line) {
78-
summaryBuf.WriteString(contentBuf.String())
75+
summary.WriteString(content.String())
7976
isSummery = false
8077
}
81-
contentBuf.WriteString(line)
82-
contentBuf.WriteString("\n")
78+
content.WriteString(line)
79+
content.WriteString("\n")
8380
}
84-
return meta, nil
81+
return nil
8582
}
8683

87-
func (m *markdown) Read(r io.Reader) (*parser.Result, error) {
84+
func (m *mdParser) Parse(r io.Reader) (*parser.Result, error) {
8885
var (
8986
summary bytes.Buffer
9087
content bytes.Buffer
9188
)
92-
meta, err := readMeta(r, &content, &summary)
93-
if err != nil {
94-
return nil, err
95-
}
9689
result := &parser.Result{
97-
FrontMatter: meta,
98-
RawContent: content.String(),
90+
FrontMatter: make(map[string]any),
91+
}
92+
if err := readMeta(r, &content, &summary, result); err != nil {
93+
return nil, err
9994
}
10095
if summary.Len() > 0 {
10196
result.Summary = m.HTML(summary.Bytes())
10297
}
10398
result.Content = m.HTML(content.Bytes())
99+
result.RawContent = content.String()
104100
return result, nil
105101
}
106102

107-
func (m *markdown) HTML(data []byte) string {
103+
func (m *mdParser) HTML(data []byte) string {
108104
d := blackfriday.Run(data, m.opts...)
109105
return string(d)
110106
}
111107

112-
func New(conf config.Config) parser.Reader {
113-
opts := []blackfriday.Option{
114-
blackfriday.WithRenderer(NewChromaRenderer(conf.GetHighlightStyle())),
108+
func New(conf config.Config) parser.MarkupParser {
109+
return &mdParser{
110+
opts: []blackfriday.Option{
111+
blackfriday.WithRenderer(NewChromaRenderer(conf.GetHighlightStyle())),
112+
},
115113
}
116-
return &markdown{conf, opts}
117114
}
118115

119116
func NewPongo2Filter(conf config.Config) pongo2.FilterFunction {
120-
r := &markdown{conf, []blackfriday.Option{
121-
blackfriday.WithRenderer(NewChromaRenderer(conf.GetHighlightStyle())),
122-
}}
117+
r := &mdParser{
118+
opts: []blackfriday.Option{
119+
blackfriday.WithRenderer(NewChromaRenderer(conf.GetHighlightStyle())),
120+
},
121+
}
123122
return func(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
124123
v, ok := in.Interface().(string)
125124
if !ok {

builder/content/parser/markdown/markdown_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66
"testing"
77

8+
"github.com/honmaple/snow/builder/content/parser"
89
"github.com/honmaple/snow/utils"
910
"github.com/stretchr/testify/assert"
1011
)
@@ -16,7 +17,11 @@ func assertFunc(t *testing.T, text string) {
1617
summary bytes.Buffer
1718
)
1819

19-
meta, _ := readMeta(r, &content, &summary)
20+
result := &parser.Result{
21+
FrontMatter: make(map[string]any),
22+
}
23+
24+
_ = readMeta(r, &content, &summary, result)
2025

2126
date, _ := utils.ParseTime("2023-02-24 20:35:51")
2227
assert.Equal(t, map[string]any{
@@ -33,7 +38,7 @@ func assertFunc(t *testing.T, text string) {
3338
"formats.js": map[string]any{
3439
"atom": "index.xml",
3540
},
36-
}, map[string]any(meta))
41+
}, result.FrontMatter)
3742

3843
assert.Equal(t, "\nsummary\n", summary.String())
3944
assert.Equal(t, "\nsummary\n<!--more-->\ncontent\n", content.String())

builder/content/parser/orgmode/orgmode.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/flosch/pongo2/v6"
1212
"github.com/honmaple/org-golang"
1313
"github.com/honmaple/org-golang/render"
14-
"github.com/honmaple/snow/builder/content"
1514
"github.com/honmaple/snow/builder/content/parser"
1615
"github.com/honmaple/snow/builder/theme/template"
1716
"github.com/honmaple/snow/config"
@@ -24,13 +23,12 @@ var (
2423
ORGMODE_META = regexp.MustCompile(`^:([^:]+):(\s+(.*)|$)`)
2524
)
2625

27-
type orgmode struct {
26+
type orgParser struct {
2827
conf config.Config
2928
}
3029

31-
func readMeta(r io.Reader, contentBuf *bytes.Buffer, summaryBuf *bytes.Buffer) (content.Meta, error) {
30+
func readMeta(r io.Reader, content *bytes.Buffer, summary *bytes.Buffer, result *parser.Result) error {
3231
var (
33-
meta = make(content.Meta)
3432
scanner = bufio.NewScanner(r)
3533
isMeta = true
3634
isFormat = true
@@ -48,7 +46,7 @@ func readMeta(r io.Reader, contentBuf *bytes.Buffer, summaryBuf *bytes.Buffer) (
4846
if match == nil || match[1] == "END" {
4947
break
5048
}
51-
meta.Set(match[1], match[2])
49+
result.SetFrontMatter(match[1], match[2])
5250
}
5351
isFormat = false
5452
continue
@@ -62,45 +60,45 @@ func readMeta(r io.Reader, contentBuf *bytes.Buffer, summaryBuf *bytes.Buffer) (
6260
if len(s) > 1 {
6361
v = strings.TrimSpace(s[1])
6462
}
65-
meta.Set(k, v)
63+
result.SetFrontMatter(k, v)
6664
} else {
67-
meta.Set(strings.ToLower(match[1]), strings.TrimSpace(match[3]))
65+
result.SetFrontMatter(strings.ToLower(match[1]), strings.TrimSpace(match[3]))
6866
}
6967
continue
7068
}
7169
}
7270
isMeta = false
7371
if isSummary && ORGMODE_MORE.MatchString(line) {
74-
summaryBuf.WriteString(contentBuf.String())
72+
summary.WriteString(content.String())
7573
isSummary = false
7674
}
77-
contentBuf.WriteString(line)
78-
contentBuf.WriteString("\n")
75+
content.WriteString(line)
76+
content.WriteString("\n")
7977
}
80-
return meta, nil
78+
return nil
8179
}
8280

83-
func (m *orgmode) Read(r io.Reader) (*parser.Result, error) {
81+
func (m *orgParser) Parse(r io.Reader) (*parser.Result, error) {
8482
var (
8583
content bytes.Buffer
8684
summary bytes.Buffer
8785
)
88-
meta, err := readMeta(r, &content, &summary)
89-
if err != nil {
90-
return nil, err
91-
}
9286
result := &parser.Result{
93-
FrontMatter: meta,
94-
RawContent: content.String(),
87+
FrontMatter: make(map[string]any),
9588
}
89+
if err := readMeta(r, &content, &summary, result); err != nil {
90+
return nil, err
91+
}
92+
result.RawContent = content.String()
93+
9694
if summary.Len() > 0 {
9795
result.Summary = m.HTML(summary.Bytes(), false)
9896
}
9997
result.Content = m.HTML(content.Bytes(), true)
10098
return result, nil
10199
}
102100

103-
func (m *orgmode) HTML(data []byte, showToc bool) string {
101+
func (m *orgParser) HTML(data []byte, showToc bool) string {
104102
rd := render.HTML{
105103
Toc: showToc,
106104
Document: org.New(bytes.NewBuffer(data)),
@@ -109,12 +107,12 @@ func (m *orgmode) HTML(data []byte, showToc bool) string {
109107
return rd.String()
110108
}
111109

112-
func New(conf config.Config) parser.Reader {
113-
return &orgmode{conf}
110+
func New(conf config.Config) parser.MarkupParser {
111+
return &orgParser{conf}
114112
}
115113

116114
func NewPongo2Filter(conf config.Config) pongo2.FilterFunction {
117-
r := &orgmode{conf}
115+
r := &orgParser{conf}
118116
return func(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
119117
v, ok := in.Interface().(string)
120118
if !ok {

builder/content/parser/orgmode/orgmode_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66
"testing"
77

8+
"github.com/honmaple/snow/builder/content/parser"
89
"github.com/honmaple/snow/utils"
910
"github.com/stretchr/testify/assert"
1011
)
@@ -16,7 +17,11 @@ func assertFunc(t *testing.T, text string) {
1617
summary bytes.Buffer
1718
)
1819

19-
meta, _ := readMeta(r, &content, &summary)
20+
result := &parser.Result{
21+
FrontMatter: make(map[string]any),
22+
}
23+
24+
_ = readMeta(r, &content, &summary, result)
2025

2126
date, _ := utils.ParseTime("2023-02-24 20:35:51")
2227
assert.Equal(t, map[string]any{
@@ -33,7 +38,7 @@ func assertFunc(t *testing.T, text string) {
3338
"formats.js": map[string]any{
3439
"atom": "index.xml",
3540
},
36-
}, map[string]any(meta))
41+
}, result.FrontMatter)
3742

3843
assert.Equal(t, "\nsummary\n", summary.String())
3944
assert.Equal(t, "\nsummary\n#+MORE\ncontent\n", content.String())

builder/content/parser/orgmode/renderer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/honmaple/org-golang/render"
1212
)
1313

14-
func (m *orgmode) highlightCodeBlock(source, lang string) string {
14+
func (m *orgParser) highlightCodeBlock(source, lang string) string {
1515
theme := m.conf.GetHighlightStyle()
1616

1717
var w strings.Builder
@@ -36,7 +36,7 @@ func (m *orgmode) highlightCodeBlock(source, lang string) string {
3636
return w.String()
3737
}
3838

39-
func (m *orgmode) renderNode(r render.Renderer, n parser.Node) string {
39+
func (m *orgParser) renderNode(r render.Renderer, n parser.Node) string {
4040
switch node := n.(type) {
4141
case *parser.Block:
4242
if node.Type == "SRC" || node.Type == "EXAMPLE" {

0 commit comments

Comments
 (0)