refactor: 精简数据结构

This commit is contained in:
fengxxc
2021-12-01 11:00:24 +08:00
parent 7fd81d440d
commit bdd8cb4b10
3 changed files with 51 additions and 50 deletions

View File

@@ -36,10 +36,9 @@ func formatTags(tags string) string {
return tags + " \n" // TODO return tags + " \n" // TODO
} }
func formatContent(blocks []parse.Paragraph) string { func formatContent(pieces []parse.Piece) string {
var contentMdStr string var contentMdStr string
for _, block := range blocks { for _, piece := range pieces {
for _, piece := range block.Pieces {
var pieceMdStr string var pieceMdStr string
switch piece.Type { switch piece.Type {
case parse.HEADER: case parse.HEADER:
@@ -65,11 +64,11 @@ func formatContent(blocks []parse.Paragraph) string {
case parse.O_LIST: case parse.O_LIST:
case parse.U_LIST: case parse.U_LIST:
case parse.HR: case parse.HR:
case parse.BR:
pieceMdStr = " \n"
} }
contentMdStr += pieceMdStr contentMdStr += pieceMdStr
} }
contentMdStr += " \n"
}
return contentMdStr return contentMdStr
} }

View File

@@ -4,7 +4,7 @@ type Article struct {
Title Piece Title Piece
Meta string Meta string
Tags string Tags string
Content []Paragraph Content []Piece
} }
type Header struct { type Header struct {
@@ -12,10 +12,6 @@ type Header struct {
Text string Text string
} }
type Paragraph struct {
Pieces []Piece
}
// go不资瓷泛型可真是难受... // go不资瓷泛型可真是难受...
type Value interface{} type Value interface{}
@@ -42,4 +38,5 @@ const (
O_LIST // 有序列表 O_LIST // 有序列表
U_LIST // 无序列表 U_LIST // 无序列表
HR // 分隔线 HR // 分隔线
BR // 换行
) )

View File

@@ -14,7 +14,7 @@ import (
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
) )
func parseSection(s *goquery.Selection) Paragraph { func parseSection(s *goquery.Selection) []Piece {
var piece []Piece var piece []Piece
s.Children().Each(func(i int, s *goquery.Selection) { s.Children().Each(func(i int, s *goquery.Selection) {
var p Piece var p Piece
@@ -40,10 +40,10 @@ func parseSection(s *goquery.Selection) Paragraph {
// fmt.Printf("%+v\n", t) // fmt.Printf("%+v\n", t)
piece = append(piece, p) piece = append(piece, p)
}) })
return Paragraph{piece} return piece
} }
func parseHeader(s *goquery.Selection) Paragraph { func parseHeader(s *goquery.Selection) []Piece {
var level int var level int
switch { switch {
case s.Is("h1"): case s.Is("h1"):
@@ -61,16 +61,16 @@ func parseHeader(s *goquery.Selection) Paragraph {
} }
attr := map[string]string{"level": strconv.Itoa(level)} attr := map[string]string{"level": strconv.Itoa(level)}
p := Piece{HEADER, removeBrAndBlank(s.Text()), attr} p := Piece{HEADER, removeBrAndBlank(s.Text()), attr}
return Paragraph{[]Piece{p}} return []Piece{p}
} }
func parsePre(s *goquery.Selection) Paragraph { func parsePre(s *goquery.Selection) []Piece {
var codeRows []string var codeRows []string
s.Find("code").Each(func(i int, s *goquery.Selection) { s.Find("code").Each(func(i int, s *goquery.Selection) {
codeRows = append(codeRows, s.Text()) codeRows = append(codeRows, s.Text())
}) })
p := Piece{CODE_BLOCK, codeRows, nil} p := Piece{CODE_BLOCK, codeRows, nil}
return Paragraph{[]Piece{p}} return []Piece{p}
} }
func ParseFromReader(r io.Reader) Article { func ParseFromReader(r io.Reader) Article {
@@ -101,25 +101,30 @@ func ParseFromReader(r io.Reader) Article {
// p[style="line-height: 1.5em;"] => 项目列表(有序/无序) // p[style="line-height: 1.5em;"] => 项目列表(有序/无序)
// section[style=".*text-align:center"]>img => 居中段落(图片) // section[style=".*text-align:center"]>img => 居中段落(图片)
content := mainContent.Find("#js_content") content := mainContent.Find("#js_content")
var sections []Paragraph // var sections []Paragraph
var pieces []Piece
content.Children().Each(func(i int, s *goquery.Selection) { content.Children().Each(func(i int, s *goquery.Selection) {
var paragraph Paragraph // var paragraph Paragraph
if s.Is("pre") || s.Is("section.code-snippet__fix") { if s.Is("pre") || s.Is("section.code-snippet__fix") {
// 代码块 // 代码块
paragraph = parsePre(s) // paragraph = parsePre(s)
pieces = append(pieces, parsePre(s)...)
} else if s.Is("p") || s.Is("section") { } else if s.Is("p") || s.Is("section") {
paragraph = parseSection(s) // paragraph = parseSection(s)
pieces = append(pieces, parseSection(s)...)
} else if s.Is("h1") || s.Is("h2") || s.Is("h3") || s.Is("h4") || s.Is("h5") || s.Is("h6") { } else if s.Is("h1") || s.Is("h2") || s.Is("h3") || s.Is("h4") || s.Is("h5") || s.Is("h6") {
paragraph = parseHeader(s) // paragraph = parseHeader(s)
pieces = append(pieces, parseHeader(s)...)
} else if s.Is("ol") { } else if s.Is("ol") {
// TODO // TODO
} else if s.Is("ul") { } else if s.Is("ul") {
// TODO // TODO
} }
// sections[i] = block // sections = append(sections, paragraph)
sections = append(sections, paragraph) pieces = append(pieces, Piece{BR, nil, nil})
}) })
article.Content = sections // article.Content = sections
article.Content = pieces
return article return article
} }