Merge pull request #19 from Unknwon/master
add func Html2str()
Showing
1 changed file
with
29 additions
and
0 deletions
| ... | @@ -10,6 +10,7 @@ import ( | ... | @@ -10,6 +10,7 @@ import ( |
| 10 | "os" | 10 | "os" |
| 11 | "path" | 11 | "path" |
| 12 | "path/filepath" | 12 | "path/filepath" |
| 13 | "regexp" | ||
| 13 | "strings" | 14 | "strings" |
| 14 | "time" | 15 | "time" |
| 15 | ) | 16 | ) |
| ... | @@ -30,6 +31,7 @@ func init() { | ... | @@ -30,6 +31,7 @@ func init() { |
| 30 | beegoTplFuncMap["date"] = Date | 31 | beegoTplFuncMap["date"] = Date |
| 31 | beegoTplFuncMap["compare"] = Compare | 32 | beegoTplFuncMap["compare"] = Compare |
| 32 | beegoTplFuncMap["substr"] = Substr | 33 | beegoTplFuncMap["substr"] = Substr |
| 34 | beegoTplFuncMap["html2str"] = Html2str | ||
| 33 | } | 35 | } |
| 34 | 36 | ||
| 35 | // MarkDown parses a string in MarkDown format and returns HTML. Used by the template parser as "markdown" | 37 | // MarkDown parses a string in MarkDown format and returns HTML. Used by the template parser as "markdown" |
| ... | @@ -54,6 +56,33 @@ func Substr(s string, start, length int) string { | ... | @@ -54,6 +56,33 @@ func Substr(s string, start, length int) string { |
| 54 | return string(bt[start:end]) | 56 | return string(bt[start:end]) |
| 55 | } | 57 | } |
| 56 | 58 | ||
| 59 | // Html2str() returns escaping text convert from html | ||
| 60 | func Html2str(html string) string { | ||
| 61 | src := string(html) | ||
| 62 | |||
| 63 | //将HTML标签全转换成小写 | ||
| 64 | re, _ := regexp.Compile("\\<[\\S\\s]+?\\>") | ||
| 65 | src = re.ReplaceAllStringFunc(src, strings.ToLower) | ||
| 66 | |||
| 67 | //去除STYLE | ||
| 68 | re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>") | ||
| 69 | src = re.ReplaceAllString(src, "") | ||
| 70 | |||
| 71 | //去除SCRIPT | ||
| 72 | re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>") | ||
| 73 | src = re.ReplaceAllString(src, "") | ||
| 74 | |||
| 75 | //去除所有尖括号内的HTML代码,并换成换行符 | ||
| 76 | re, _ = regexp.Compile("\\<[\\S\\s]+?\\>") | ||
| 77 | src = re.ReplaceAllString(src, "\n") | ||
| 78 | |||
| 79 | //去除连续的换行符 | ||
| 80 | re, _ = regexp.Compile("\\s{2,}") | ||
| 81 | src = re.ReplaceAllString(src, "\n") | ||
| 82 | |||
| 83 | return strings.TrimSpace(src) | ||
| 84 | } | ||
| 85 | |||
| 57 | // DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat" | 86 | // DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat" |
| 58 | func DateFormat(t time.Time, layout string) (datestring string) { | 87 | func DateFormat(t time.Time, layout string) (datestring string) { |
| 59 | datestring = t.Format(layout) | 88 | datestring = t.Format(layout) | ... | ... |
-
Please register or sign in to post a comment