Ai
1 Star 0 Fork 8

哈哈哈/wego
暂停

forked from kzquu/wego 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
templatefunc.go 6.32 KB
一键复制 编辑 原始数据 按行查看 历史
kzquu 提交于 2019-04-23 15:43 +08:00 . 0
package template
import (
"fmt"
"html/template"
"regexp"
"strings"
"time"
"wego/util"
)
// funcMap 模板函数
var funcMap = make(template.FuncMap, 15)
// AddFuncMap 添加模板函数,需要在Init模板初始化之前添加才能生效
func AddFuncMap(name string, f interface{}) error {
funcMap[name] = f
return nil
}
// ****************************************************************************
func init() {
funcMap["UnHTML"] = UnHTML // func(x string) interface{}
funcMap["Split"] = strings.Split // func(s, sep string) []string
funcMap["SubStr"] = util.SubStr // func(str string, start int, length ...int) string
funcMap["HTML2str"] = HTML2str // func(html string) string
funcMap["DateFormat"] = DateFormat // func(t time.Time, layout string) (datestring string)
funcMap["DateParse"] = DateParse // func(dateString, format string) (time.Time, error)
funcMap["Date"] = Date // func(t time.Time, format string) string
funcMap["Compare"] = Compare // func(a, b interface{}) (equal bool)
funcMap["CompareNot"] = CompareNot // func(a, b interface{}) (equal bool)
funcMap["Empty"] = Empty // func(a interface{}) bool
funcMap["Htmlquote"] = Htmlquote // func(text string) string
funcMap["Htmlunquote"] = Htmlunquote // func(text string) string
}
// UnHTML template.HTML
func UnHTML(x string) interface{} {
return template.HTML(x)
}
// HTML2str html转化为字符串,剔除一些 script、css 之类的元素,返回纯文本信息,使用方法 {{html2str .Htmlinfo}}
func HTML2str(html string) string {
re, _ := regexp.Compile(`\<[\S\s]+?\>`)
html = re.ReplaceAllStringFunc(html, strings.ToLower)
//remove STYLE
re, _ = regexp.Compile(`\<style[\S\s]+?\</style\>`)
html = re.ReplaceAllString(html, "")
//remove SCRIPT
re, _ = regexp.Compile(`\<script[\S\s]+?\</script\>`)
html = re.ReplaceAllString(html, "")
re, _ = regexp.Compile(`\<[\S\s]+?\>`)
html = re.ReplaceAllString(html, "\n")
re, _ = regexp.Compile(`\s{2,}`)
html = re.ReplaceAllString(html, "\n")
return strings.TrimSpace(html)
}
// DateFormat 时间的格式化,返回字符串,使用方法 {{dateformat .Time “2006-01-02T15:04:05Z07:00”}}
func DateFormat(t time.Time, layout string) (datestring string) {
datestring = t.Format(layout)
return
}
// datePatterns pattern rules.
var datePatterns = []string{
// year
"Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003
"y", "06", //A two digit representation of a year Examples: 99 or 03
// month
"m", "01", // Numeric representation of a month, with leading zeros 01 through 12
"n", "1", // Numeric representation of a month, without leading zeros 1 through 12
"M", "Jan", // A short textual representation of a month, three letters Jan through Dec
"F", "January", // A full textual representation of a month, such as January or March January through December
// day
"d", "02", // Day of the month, 2 digits with leading zeros 01 to 31
"j", "2", // Day of the month without leading zeros 1 to 31
// week
"D", "Mon", // A textual representation of a day, three letters Mon through Sun
"l", "Monday", // A full textual representation of the day of the week Sunday through Saturday
// time
"g", "3", // 12-hour format of an hour without leading zeros 1 through 12
"G", "15", // 24-hour format of an hour without leading zeros 0 through 23
"h", "03", // 12-hour format of an hour with leading zeros 01 through 12
"H", "15", // 24-hour format of an hour with leading zeros 00 through 23
"a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm
"A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM
"i", "04", // Minutes with leading zeros 00 to 59
"s", "05", // Seconds, with leading zeros 00 through 59
// time zone
"T", "MST",
"P", "-07:00",
"O", "-0700",
// RFC 2822
"r", time.RFC1123Z,
}
// DateParse 兼容php语言风格的时间字符串解析
func DateParse(dateString, format string) (time.Time, error) {
replacer := strings.NewReplacer(datePatterns...)
format = replacer.Replace(format)
return time.ParseInLocation(format, dateString, time.Local)
}
// Date 兼容php语言风格的时间的格式化
func Date(t time.Time, format string) string {
replacer := strings.NewReplacer(datePatterns...)
format = replacer.Replace(format)
return t.Format(format)
}
// Compare 比较两个对象,如果相同返回 true,否者 false,使用方法 {{compare .A .B}}
func Compare(a, b interface{}) (equal bool) {
equal = false
if strings.TrimSpace(fmt.Sprintf("%v", a)) == strings.TrimSpace(fmt.Sprintf("%v", b)) {
equal = true
}
return
}
// CompareNot !Compare
func CompareNot(a, b interface{}) (equal bool) {
return !Compare(a, b)
}
// Empty 对象为空时返回true
func Empty(a interface{}) bool {
return Compare(a, nil)
}
// Htmlquote html字符转义,使用方法 {{htmlquote .quote}}
func Htmlquote(text string) string {
//HTML编码为实体符号
/*
Encodes `text` for raw use in HTML.
>>> htmlquote("<'&\\">")
'&lt;&#39;&amp;&quot;&gt;'
*/
text = strings.Replace(text, "&", "&amp;", -1) // Must be done first!
text = strings.Replace(text, "<", "&lt;", -1)
text = strings.Replace(text, ">", "&gt;", -1)
text = strings.Replace(text, "'", "&#39;", -1)
text = strings.Replace(text, "\"", "&quot;", -1)
text = strings.Replace(text, "“", "&ldquo;", -1)
text = strings.Replace(text, "”", "&rdquo;", -1)
text = strings.Replace(text, " ", "&nbsp;", -1)
return strings.TrimSpace(text)
}
// Htmlunquote html字符反转义,使用方法 {{htmlunquote .unquote}}
func Htmlunquote(text string) string {
//实体符号解释为HTML
/*
Decodes `text` that's HTML quoted.
>>> htmlunquote('&lt;&#39;&amp;&quot;&gt;')
'<\\'&">'
*/
// strings.Replace(s, old, new, n)
// 在s字符串中,把old字符串替换为new字符串,n表示替换的次数,小于0表示全部替换
text = strings.Replace(text, "&nbsp;", " ", -1)
text = strings.Replace(text, "&rdquo;", "”", -1)
text = strings.Replace(text, "&ldquo;", "“", -1)
text = strings.Replace(text, "&quot;", "\"", -1)
text = strings.Replace(text, "&#39;", "'", -1)
text = strings.Replace(text, "&gt;", ">", -1)
text = strings.Replace(text, "&lt;", "<", -1)
text = strings.Replace(text, "&amp;", "&", -1) // Must be done last!
return strings.TrimSpace(text)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jameson512/wego.git
git@gitee.com:jameson512/wego.git
jameson512
wego
wego
206da09742f2

搜索帮助