mirror of
https://github.com/fengxxc/wechatmp2markdown.git
synced 2026-03-02 10:40:47 +08:00
feat: 对文章Meta信息的支持
This commit is contained in:
@@ -3,6 +3,7 @@ package format
|
||||
import (
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/fengxxc/wechatmp2markdown/parse"
|
||||
)
|
||||
@@ -20,9 +21,9 @@ func Format(article parse.Article) string {
|
||||
return result
|
||||
}
|
||||
|
||||
func FormatAndSave(article parse.Article, filePath string) {
|
||||
func FormatAndSave(article parse.Article, filePath string) error {
|
||||
var result string = Format(article)
|
||||
ioutil.WriteFile(filePath, []byte(result), 0644)
|
||||
return ioutil.WriteFile(filePath, []byte(result), 0644)
|
||||
}
|
||||
|
||||
func formatTitle(piece parse.Piece) string {
|
||||
@@ -34,8 +35,8 @@ func formatTitle(piece parse.Piece) string {
|
||||
return prefix + " " + piece.Val.(string) + " \n"
|
||||
}
|
||||
|
||||
func formatMeta(meta string) string {
|
||||
return meta + " \n" // TODO
|
||||
func formatMeta(meta []string) string {
|
||||
return strings.Join(meta, " ") + " \n" // TODO
|
||||
}
|
||||
|
||||
func formatTags(tags string) string {
|
||||
|
||||
@@ -2,7 +2,7 @@ package parse
|
||||
|
||||
type Article struct {
|
||||
Title Piece
|
||||
Meta string
|
||||
Meta []string
|
||||
Tags string
|
||||
Content []Piece
|
||||
}
|
||||
@@ -24,19 +24,19 @@ type Piece struct {
|
||||
type PieceType int32
|
||||
|
||||
const (
|
||||
HEADER PieceType = iota // 标题
|
||||
LINK // 链接
|
||||
NORMAL_TEXT // 文字
|
||||
BOLD_TEXT // 粗体文字
|
||||
ITALIC_TEXT // 斜体文字
|
||||
BOLD_ITALIC_TEXT // 粗斜体
|
||||
IMAGE // 图片
|
||||
TABLE // 表格
|
||||
CODE_INLINE // 代码 内联
|
||||
CODE_BLOCK // 代码 块
|
||||
BLOCK_QUOTES // 引用
|
||||
O_LIST // 有序列表
|
||||
U_LIST // 无序列表
|
||||
HR // 分隔线
|
||||
BR // 换行
|
||||
HEADER PieceType = iota // 0 标题
|
||||
LINK // 1 链接
|
||||
NORMAL_TEXT // 2 文字
|
||||
BOLD_TEXT // 3 粗体文字
|
||||
ITALIC_TEXT // 4 斜体文字
|
||||
BOLD_ITALIC_TEXT // 5 粗斜体
|
||||
IMAGE // 6 图片
|
||||
TABLE // 7 表格
|
||||
CODE_INLINE // 8 代码 内联
|
||||
CODE_BLOCK // 9 代码 块
|
||||
BLOCK_QUOTES // 10 引用
|
||||
O_LIST // 11 有序列表
|
||||
U_LIST // 12 无序列表
|
||||
HR // 13 分隔线
|
||||
BR // 14 换行
|
||||
)
|
||||
|
||||
@@ -2,6 +2,7 @@ package parse
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
@@ -97,6 +98,22 @@ func parseBlockQuote(s *goquery.Selection) []Piece {
|
||||
return bq
|
||||
}
|
||||
|
||||
func parseMeta(s *goquery.Selection) []string {
|
||||
var res []string
|
||||
s.Children().Each(func(i int, sc *goquery.Selection) {
|
||||
if sc.Is("#profileBt") {
|
||||
res = append(res, removeBrAndBlank(sc.Find("#js_name").Text()))
|
||||
} else {
|
||||
// t := sc.Text()
|
||||
t := sc.Nodes[0].Data
|
||||
// t, _ := sc.Html()
|
||||
fmt.Println(t)
|
||||
res = append(res, t)
|
||||
}
|
||||
})
|
||||
return res
|
||||
}
|
||||
|
||||
func ParseFromReader(r io.Reader) Article {
|
||||
var article Article
|
||||
doc, err := goquery.NewDocumentFromReader(r)
|
||||
@@ -111,9 +128,9 @@ func ParseFromReader(r io.Reader) Article {
|
||||
article.Title = Piece{HEADER, removeBrAndBlank(title), attr}
|
||||
|
||||
// meta 细节待完善
|
||||
meta := mainContent.Find("#meta_content").Text()
|
||||
meta = removeBrAndBlank(meta)
|
||||
article.Meta = meta
|
||||
meta := mainContent.Find("#meta_content")
|
||||
metastring := parseMeta(meta)
|
||||
article.Meta = metastring
|
||||
|
||||
// tags 细节待完善
|
||||
tags := mainContent.Find("#js_tags").Text()
|
||||
|
||||
Reference in New Issue
Block a user