package util

import (
	"regexp"
	"strings"
)

//
type Cut struct {
	tag           *regexp.Regexp
	scripttag     *regexp.Regexp
	inputag       *regexp.Regexp
	hiddentag     *regexp.Regexp
	styletag      *regexp.Regexp
	colstag       *regexp.Regexp
	rowstag       *regexp.Regexp
	display       *regexp.Regexp
	multiCR       *regexp.Regexp
	replBlankLine *regexp.Regexp
	replStartWrap *regexp.Regexp
	replTags2CR   []string
	retainTags2CR []string
}

//
func NewCut() *Cut {
	t, _ := regexp.Compile("<[^>]+>")
	m, _ := regexp.Compile("([\r\n][\u3000\u2003\u00a0\\s]*)+|[\r\n]+")
	//sc, _ := regexp.Compile("\\<script[^\\>]*\\>*[^\\>]+\\</script\\>")
	//ss, _ := regexp.Compile("\\<style[^\\>]*\\>*[^\\>]+\\</style\\>")
	scs := regexp.MustCompile("<(script|style)[^>]*>[^>]+</(script|style)>")
	hiddentag := regexp.MustCompile(`<\s*input[^<]*type=("|')hidden("|')[^<]*>`)
	input := regexp.MustCompile(`<\s*input[^<]*value=("|')([^>"']*)[^<]*>`)
	cols, _ := regexp.Compile(`colspan="\d+"`)
	rows, _ := regexp.Compile(`rowspan="\d+"`)
	dis, _ := regexp.Compile(`display:none`)
	return &Cut{
		tag:           t,
		scripttag:     scs,
		hiddentag:     hiddentag,
		inputag:       input,
		colstag:       cols,
		rowstag:       rows,
		display:       dis,
		multiCR:       m,
		replBlankLine: regexp.MustCompile("\\s+[\r\n]"),
		replStartWrap: regexp.MustCompile("^[\u3000\u2003\u00a0\\s]+|[\u3000\u2003\u00a0\\s]+$"),
		replTags2CR:   []string{"div", "p", "br", "h1", "h2", "h3", "h4", "h5"},
		retainTags2CR: []string{"table", "thead", "tfoot", "tbody", "th", "td", "tr"},
	}
}

//清理HTML标签
func (c *Cut) ClearHtml(src string) string {
	src = strings.Replace(src, ">\n", ">", -1)
	//标签全转小写
	src = c.tag.ReplaceAllStringFunc(src, strings.ToLower)
	//清script,style
	src = c.scripttag.ReplaceAllString(src, "")
	//清理input
	src = c.hiddentag.ReplaceAllString(src, "")
	src = c.inputag.ReplaceAllString(src, "$2")
	//换结束标签
	src = c.tag.ReplaceAllStringFunc(src, func(tmp string) string {
		tmp = strings.Replace(tmp, " ", "", -1)
		//保留这些标签
		for _, v := range c.retainTags2CR {
			if "<"+v+">" == tmp || "</"+v+">" == tmp {
				if tmp == "</table>" {
					return tmp + "\n"
				}
				return tmp
			}
			if strings.HasPrefix(tmp, "<"+v) {
				dispstrs := c.display.FindAllString(tmp, -1)
				rowstrs := c.rowstag.FindAllString(tmp, -1)
				colstrs := c.colstag.FindAllString(tmp, -1)
				c := "<" + v
				if len(colstrs) > 0 { //处理多列合并
					c += " " + colstrs[0]
				}
				if len(rowstrs) > 0 { //处理多行合并
					c += " " + rowstrs[0]
				}
				if len(dispstrs) > 0 {
					c += " style=\"" + dispstrs[0] + "\""
				}
				return c + ">"

			}
		}
		if tmp == "<br>" || tmp == "<br/>" {
			return "\n"
		}
		if tmp[1] != 47 { //开始标签
			for _, v := range c.replTags2CR {
				if v == tmp[1:len(tmp)-1] {
					return "\n"
				}
			}
			return ""
		}
		for _, v := range c.replTags2CR {
			if v == tmp[2:len(tmp)-1] {
				return "\n"
			}
		}
		return ""
	})
	src = c.replStartWrap.ReplaceAllString(src, "")
	src = c.replBlankLine.ReplaceAllString(src, "\n")
	//清除多余换行
	return c.multiCR.ReplaceAllString(src, "\n")
	//return strings.Replace(src, "\n", "<br/>", -1)
}
