htmlclear.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package util
  2. import (
  3. mc "app.yhyue.com/moapp/jybase/common"
  4. "regexp"
  5. "strings"
  6. )
  7. //
  8. type Cut struct {
  9. scripttag *regexp.Regexp
  10. sa *regexp.Regexp
  11. replBlankLine *regexp.Regexp
  12. replStartWrap *regexp.Regexp
  13. multiCR *regexp.Regexp
  14. //annotate *regexp.Regexp
  15. //tag *regexp.Regexp
  16. //inputag *regexp.Regexp
  17. //isborder *regexp.Regexp
  18. //hiddentag *regexp.Regexp
  19. //styletag *regexp.Regexp
  20. //colstag *regexp.Regexp
  21. //rowstag *regexp.Regexp
  22. //display *regexp.Regexp
  23. //replTags2CR []string
  24. //retainTags2CR []string
  25. }
  26. //
  27. func NewCut() *Cut {
  28. scs := regexp.MustCompile("(?s)<(script|style)[^>]*>.+?</(script|style)>")
  29. sa := regexp.MustCompile("<[a|A]\\s*[^>]*>(.*?)</[a|A]>")
  30. bl := regexp.MustCompile("\\s+[\r\n]")
  31. sw := regexp.MustCompile("^[\u3000\u2003\u00a0\\s]+|[\u3000\u2003\u00a0\\s]+$")
  32. m, _ := regexp.Compile("([\r\n][\u3000\u2003\u00a0\\s]*)+|[\r\n]+")
  33. //t, _ := regexp.Compile("<[^>]+>")
  34. ////sc, _ := regexp.Compile("\\<script[^\\>]*\\>*[^\\>]+\\</script\\>")
  35. ////ss, _ := regexp.Compile("\\<style[^\\>]*\\>*[^\\>]+\\</style\\>")
  36. //at := regexp.MustCompile("(?s)<(!%-%-|!--).*?(%-%-|--)>") //注释 css
  37. //hiddentag := regexp.MustCompile(`<\s*input[^<]*type=("|')hidden("|')[^<]*>`)
  38. //input := regexp.MustCompile(`<\s*input[^<]*value=("|')([^>"']*)[^<]*>`)
  39. //cols, _ := regexp.Compile(`colspan="\d+"`)
  40. //rows, _ := regexp.Compile(`rowspan="\d+"`)
  41. //border, _ := regexp.Compile(`(border="(\d+)")|(cellpadding="(\d+)")|(cellspacing="(\d+)")`)
  42. //dis, _ := regexp.Compile(`display:none`)
  43. return &Cut{
  44. scripttag: scs,
  45. sa: sa,
  46. replBlankLine: bl,
  47. replStartWrap: sw,
  48. multiCR: m,
  49. //annotate: at,
  50. //tag: t,
  51. //hiddentag: hiddentag,
  52. //inputag: input,
  53. //colstag: cols,
  54. //isborder: border,
  55. //rowstag: rows,
  56. //display: dis,
  57. //replTags2CR: []string{"div", "p", "br", "h1", "h2", "h3", "h4", "h5"},
  58. //retainTags2CR: []string{"table", "thead", "tfoot", "tbody", "th", "td", "tr"},
  59. }
  60. }
  61. //清理HTML标签
  62. func (c *Cut) ClearHtml(src string) string {
  63. //清script,style
  64. src = c.scripttag.ReplaceAllString(src, "")
  65. src = c.sa.ReplaceAllString(src, "$1")
  66. src = c.replStartWrap.ReplaceAllString(src, "")
  67. src = c.replBlankLine.ReplaceAllString(src, "\n")
  68. //清除多余换行
  69. c.multiCR.ReplaceAllString(src, "\n")
  70. return strings.Replace(src, "\n", "<br/>", -1)
  71. }
  72. //判断table是否加表格线
  73. func isHasBoder(con string, reg *regexp.Regexp) bool {
  74. res := reg.FindAllStringSubmatch(con, -1)
  75. hasBorder := false
  76. for _, v := range res {
  77. for k, val := range v {
  78. if k > 0 && k%2 == 0 && mc.IntAll(val) > 0 {
  79. hasBorder = true
  80. break
  81. }
  82. }
  83. }
  84. return hasBorder
  85. }