-
Notifications
You must be signed in to change notification settings - Fork 808
/
Copy pathhtml2markdown.go
344 lines (312 loc) · 9.23 KB
/
html2markdown.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//Author:TruthHun
//Email: TruthHun@QQ.COM
//Date: 2018-02-03
package html2md
import (
"strings"
"fmt"
"regexp"
"github.com/PuerkitoBio/goquery"
"github.com/astaxie/beego/logs"
)
var tag2tag = map[string]string{
"b": "strong",
"i": "em",
"dfn": "em",
"var": "em",
"cite": "em",
}
var blockTag = []string{
"address", "div", "figure", "p", "figcaption", "br",
"article", "aside", "nav", "footer", "fieldset", "menu",
"header", "section", "center", "frameset", "details", "summary",
}
var nextlineTag = []string{
"pre", "blockquote", "table",
}
//convert html to markdown
//将html转成markdown
func Convert(htmlstr string) (md string) {
var maps map[string]string
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(htmlstr))
doc = trimAttr(doc)
doc, maps = compress(doc)
doc = handleNextLine(doc) //<div>...
doc = handleBlockTag(doc) //<div>...
doc = handleA(doc) //<a>
doc = handleImg(doc) //<img>
doc = handleHead(doc) //h1~h6
doc = handleTag2Tag(doc) //<strong>、<i>、eg..
doc = handleHr(doc) //<hr>
doc = handleLi(doc) //<li>
md, _ = doc.Find("body").Html()
md = depress(md, maps)
return
}
// 解压,释放code和pre
func depress(md string, maps map[string]string) string {
// 先替换pre,再替换code,因为有的code在pre标签里面
for key, val := range maps {
if strings.HasPrefix(key, "{$blockquote") {
md = strings.Replace(md, key, "\n\r"+val+"\n\r", -1)
}
}
for key, val := range maps {
if strings.HasPrefix(key, "{$pre") {
md = strings.Replace(md, key, "\n\r"+val+"\n\r", -1)
}
}
for key, val := range maps {
if strings.HasPrefix(key, "{$code") || strings.HasPrefix(key, "{$textarea") {
md = strings.Replace(md, key, val, -1)
}
}
if doc, err := goquery.NewDocumentFromReader(strings.NewReader(md)); err == nil {
doc = trimAttr(doc)
backslashes := []string{"+", "-", "_", "*"}
doc.Find("code").Each(func(i int, selection *goquery.Selection) {
if !selection.Parent().Is("pre") {
text := selection.Text()
for _, item := range backslashes {
text = strings.Replace(text, item, "\\"+item, -1)
}
selection.SetHtml(text)
}
})
md, _ = doc.Find("body").Html()
md = strings.Replace(md, "<span>", "", -1)
md = strings.Replace(md, "</span>", "", -1)
}
return md
}
// trip attr
func trimAttr(doc *goquery.Document) *goquery.Document {
attrs := []string{
"border", "colspan", "rowspan", "style", "cellspacing",
"cellpadding", "bgcolor", "width", "align", "frame", "id", "class",
}
elements := []string{
"table", "thead", "tbody", "tr", "td", "th", "h1", "h2", "h3", "h4", "img",
"h5", "h6", "i", "em", "strong", "span", "br", "hr", "ul", "li", "ol",
}
elements = append(elements, blockTag...)
elements = append(elements, nextlineTag...)
for _, tag := range elements {
doc.Find(tag).Each(func(i int, selection *goquery.Selection) {
for _, attr := range attrs {
selection.RemoveAttr(attr)
}
})
}
return doc
}
//压缩html
func compress(doc *goquery.Document) (*goquery.Document, map[string]string) {
//blockquote、pre、code,并替换 span 为空
var maps = make(map[string]string)
if ele := doc.Find("textarea"); len(ele.Nodes) > 0 {
ele.Each(func(i int, selection *goquery.Selection) {
key := fmt.Sprintf("{$textarea%v}", i)
cont := "<textarea>" + getInnerHtml(selection) + "</textarea>"
selection.BeforeHtml(key)
selection.Remove()
maps[key] = cont
})
}
if ele := doc.Find("code"); len(ele.Nodes) > 0 {
ele.Each(func(i int, selection *goquery.Selection) {
key := fmt.Sprintf("{$code%v}", i)
cont := "<code>" + getInnerHtml(selection) + "</code>"
selection.BeforeHtml(key)
selection.Remove()
maps[key] = cont
})
}
if ele := doc.Find("pre"); len(ele.Nodes) > 0 {
ele.Each(func(i int, selection *goquery.Selection) {
key := fmt.Sprintf("{$pre%v}", i)
cont := "<pre>" + getInnerHtml(selection) + "</pre>"
selection.BeforeHtml(key)
selection.Remove()
maps[key] = cont
})
}
if ele := doc.Find("blockquote"); len(ele.Nodes) > 0 {
ele.Each(func(i int, selection *goquery.Selection) {
key := fmt.Sprintf("{$blockquote%v}", i)
cont := "<blockquote>" + getInnerHtml(selection) + "</blockquote>"
selection.BeforeHtml(key)
selection.Remove()
maps[key] = cont
})
}
replaces := map[string]string{
"\n": " ", "\r": " ", "\t": " ", "<dl": "<ul",
"</dl": "</ul", "<dt": "<li", "</dt": "</li",
"<dd": "<li", "</dd": "</li",
}
htmlstr, _ := doc.Html()
for old, new := range replaces {
htmlstr = strings.Replace(htmlstr, old, new, -1)
}
//正则匹配,把“>”和“<”直接的空格全部去掉
//去除标签之间的空格,如果是存在代码预览的页面,不要替换空格,否则预览的代码会错乱
r, _ := regexp.Compile(">\\s+<")
htmlstr = r.ReplaceAllString(htmlstr, "> <")
//多个空格替换成一个空格
r2, _ := regexp.Compile("\\s+")
htmlstr = r2.ReplaceAllString(htmlstr, " ")
doc, _ = goquery.NewDocumentFromReader(strings.NewReader(htmlstr))
return doc, maps
}
func handleBlockTag(doc *goquery.Document) *goquery.Document {
for _, tag := range blockTag {
hasTag := true
for hasTag {
if tagEle := doc.Find(tag); len(tagEle.Nodes) > 0 {
tagEle.Each(func(i int, selection *goquery.Selection) {
selection.BeforeHtml("\n" + getInnerHtml(selection) + "\n")
selection.Remove()
})
} else {
hasTag = false
}
}
}
return doc
}
//func handleBlockquote(doc *goquery.Document) *goquery.Document {
// if tagEle := doc.Find("blockquote"); len(tagEle.Nodes) > 0 {
// tagEle.Each(func(i int, selection *goquery.Selection) {
// cont := getInnerHtml(selection)
// cont = strings.Replace(cont, "\r", "", -1)
// cont = strings.Replace(cont, "\n", "", -1)
// selection.BeforeHtml("\r\n<blockquote>" + cont + "\n</blockquote>\n")
// selection.Remove()
// })
// }
//
// doc.Find("code").Each(func(i int, selection *goquery.Selection) {
// fmt.Println(selection.Html())
// })
//
// return doc
//}
//[ok]handle tag <a>
func handleA(doc *goquery.Document) *goquery.Document {
doc.Find("a").Each(func(i int, selection *goquery.Selection) {
if href, ok := selection.Attr("href"); ok {
if cont, err := selection.Html(); err == nil {
md := fmt.Sprintf(`[%v](%v)`, cont, href)
selection.BeforeHtml(md)
selection.Remove()
}
}
})
return doc
}
//[ok]handle tag ul、ol、li
//处理步骤:
//1、先给每个li标签里面的内容加上"- "或者"\t- "
//2、提取li内容
func handleLi(doc *goquery.Document) *goquery.Document {
var tags = []string{"ol", "ul", "li"}
doc.Find("li").Each(func(i int, selection *goquery.Selection) {
l := len(selection.ParentsFiltered("li").Nodes)
tab := strings.Join(make([]string, l+2), "{$@$space}")
selection.PrependHtml("\r$@$" + tab)
})
for _, tag := range tags {
doc.Find(tag).Each(func(i int, selection *goquery.Selection) {
if tag == "ul" || tag == "ol" {
text := "\n" + selection.Text() + "\n"
if !(selection.Parent().Is("ul") || selection.Parent().Is("ol")) {
text = "\n" + text + "\n"
}
selection.BeforeHtml(text)
} else {
selection.BeforeHtml(selection.Text())
}
selection.Remove()
})
}
htmlstr, _ := doc.Find("body").Html()
for i := 10; i > 0; i-- {
oldTab := "$@$" + strings.Join(make([]string, i), "{$@$space}")
newTab := strings.Join(make([]string, i-1), " ") + "- "
htmlstr = strings.Replace(htmlstr, oldTab, newTab, -1)
}
doc, _ = goquery.NewDocumentFromReader(strings.NewReader(htmlstr))
return doc
}
//[ok]handle tag <hr/>
func handleHr(doc *goquery.Document) *goquery.Document {
doc.Find("hr").Each(func(i int, selection *goquery.Selection) {
selection.BeforeHtml("\n- - -\n")
selection.Remove()
})
return doc
}
//[ok]handle tag <img/>
func handleImg(doc *goquery.Document) *goquery.Document {
doc.Find("img").Each(func(i int, selection *goquery.Selection) {
if src, ok := selection.Attr("src"); ok {
alt := ""
if val, ok := selection.Attr("alt"); ok {
alt = val
}
md := fmt.Sprintf(``, alt, src)
selection.BeforeHtml(md)
selection.Remove()
}
})
return doc
}
//[ok]handle tag h1~h6
func handleHead(doc *goquery.Document) *goquery.Document {
heads := map[string]string{
"title": "# ",
"h1": "# ",
"h2": "## ",
"h3": "### ",
"h4": "#### ",
"h5": "##### ",
"h6": "###### ",
}
for tag, replace := range heads {
doc.Find(tag).Each(func(i int, selection *goquery.Selection) {
text, _ := selection.Html()
selection.BeforeHtml("\n\r" + replace + text + "\n\r")
selection.Remove()
})
}
return doc
}
func handleTag2Tag(doc *goquery.Document) *goquery.Document {
for tag, toTag := range tag2tag {
doc.Find(tag).Each(func(i int, selection *goquery.Selection) {
if text, _ := selection.Html(); strings.TrimSpace(text) != "" {
selection.BeforeHtml(fmt.Sprintf("<%v>%v</%v>", toTag, text, toTag))
}
selection.Remove()
})
}
return doc
}
func handleNextLine(doc *goquery.Document) *goquery.Document {
for _, tag := range nextlineTag {
doc.Find(tag).Each(func(i int, selection *goquery.Selection) {
selection.BeforeHtml("\n\n")
selection.AfterHtml("\n\n")
})
}
return doc
}
func getInnerHtml(selection *goquery.Selection) (html string) {
var err error
html, _ = selection.Html()
if err != nil {
logs.Error(err)
}
return
}