Commit b54fc102 authored by astaxie's avatar astaxie

Merge pull request #19 from Unknwon/master

add func Html2str()
parents e6ab2f73 63d8881f
......@@ -10,6 +10,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strings"
"time"
)
......@@ -30,6 +31,7 @@ func init() {
beegoTplFuncMap["date"] = Date
beegoTplFuncMap["compare"] = Compare
beegoTplFuncMap["substr"] = Substr
beegoTplFuncMap["html2str"] = Html2str
}
// 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 {
return string(bt[start:end])
}
// Html2str() returns escaping text convert from html
func Html2str(html string) string {
src := string(html)
//将HTML标签全转换成小写
re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
src = re.ReplaceAllStringFunc(src, strings.ToLower)
//去除STYLE
re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
src = re.ReplaceAllString(src, "")
//去除SCRIPT
re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
src = re.ReplaceAllString(src, "")
//去除所有尖括号内的HTML代码,并换成换行符
re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
src = re.ReplaceAllString(src, "\n")
//去除连续的换行符
re, _ = regexp.Compile("\\s{2,}")
src = re.ReplaceAllString(src, "\n")
return strings.TrimSpace(src)
}
// DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat"
func DateFormat(t time.Time, layout string) (datestring string) {
datestring = t.Format(layout)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment