[ok]handle tag ul、ol、li 处理步骤: 1、先给每个li标签里面的内容加上"- "或者"\t- " 2、提取li内容
(doc *goquery.Document)
| 236 | //1、先给每个li标签里面的内容加上"- "或者"\t- " |
| 237 | //2、提取li内容 |
| 238 | func handleLi(doc *goquery.Document) *goquery.Document { |
| 239 | var tags = []string{"ol", "ul", "li"} |
| 240 | doc.Find("li").Each(func(i int, selection *goquery.Selection) { |
| 241 | l := len(selection.ParentsFiltered("li").Nodes) |
| 242 | tab := strings.Join(make([]string, l+2), "{$@$space}") |
| 243 | selection.PrependHtml("\r$@$" + tab) |
| 244 | }) |
| 245 | for _, tag := range tags { |
| 246 | doc.Find(tag).Each(func(i int, selection *goquery.Selection) { |
| 247 | if tag == "ul" || tag == "ol" { |
| 248 | text := "\n" + selection.Text() + "\n" |
| 249 | if !(selection.Parent().Is("ul") || selection.Parent().Is("ol")) { |
| 250 | text = "\n" + text + "\n" |
| 251 | } |
| 252 | selection.BeforeHtml(text) |
| 253 | } else { |
| 254 | selection.BeforeHtml(selection.Text()) |
| 255 | } |
| 256 | selection.Remove() |
| 257 | }) |
| 258 | } |
| 259 | htmlstr, _ := doc.Find("body").Html() |
| 260 | for i := 10; i > 0; i-- { |
| 261 | oldTab := "$@$" + strings.Join(make([]string, i), "{$@$space}") |
| 262 | newTab := strings.Join(make([]string, i-1), " ") + "- " |
| 263 | htmlstr = strings.Replace(htmlstr, oldTab, newTab, -1) |
| 264 | } |
| 265 | doc, _ = goquery.NewDocumentFromReader(strings.NewReader(htmlstr)) |
| 266 | return doc |
| 267 | } |
| 268 | |
| 269 | //[ok]handle tag <hr/> |
| 270 | func handleHr(doc *goquery.Document) *goquery.Document { |