解压,释放code和pre
(md string, maps map[string]string)
| 54 | |
| 55 | // 解压,释放code和pre |
| 56 | func depress(md string, maps map[string]string) string { |
| 57 | // 先替换pre,再替换code,因为有的code在pre标签里面 |
| 58 | for key, val := range maps { |
| 59 | if strings.HasPrefix(key, "{$blockquote") { |
| 60 | md = strings.Replace(md, key, "\n\r"+val+"\n\r", -1) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | for key, val := range maps { |
| 65 | if strings.HasPrefix(key, "{$pre") { |
| 66 | md = strings.Replace(md, key, "\n\r"+val+"\n\r", -1) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | for key, val := range maps { |
| 71 | if strings.HasPrefix(key, "{$code") || strings.HasPrefix(key, "{$textarea") { |
| 72 | md = strings.Replace(md, key, val, -1) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if doc, err := goquery.NewDocumentFromReader(strings.NewReader(md)); err == nil { |
| 77 | doc = trimAttr(doc) |
| 78 | backslashes := []string{"+", "-", "_", "*"} |
| 79 | doc.Find("code").Each(func(i int, selection *goquery.Selection) { |
| 80 | if !selection.Parent().Is("pre") { |
| 81 | text := selection.Text() |
| 82 | for _, item := range backslashes { |
| 83 | text = strings.Replace(text, item, "\\"+item, -1) |
| 84 | } |
| 85 | selection.SetHtml(text) |
| 86 | } |
| 87 | }) |
| 88 | md, _ = doc.Find("body").Html() |
| 89 | md = strings.Replace(md, "<span>", "", -1) |
| 90 | md = strings.Replace(md, "</span>", "", -1) |
| 91 | } |
| 92 | return md |
| 93 | } |
| 94 | |
| 95 | // trip attr |
| 96 | func trimAttr(doc *goquery.Document) *goquery.Document { |