代码拉取完成,页面将自动刷新
同步操作将从 kzquu/wego 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
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("<'&\\">")
'<'&">'
*/
text = strings.Replace(text, "&", "&", -1) // Must be done first!
text = strings.Replace(text, "<", "<", -1)
text = strings.Replace(text, ">", ">", -1)
text = strings.Replace(text, "'", "'", -1)
text = strings.Replace(text, "\"", """, -1)
text = strings.Replace(text, "“", "“", -1)
text = strings.Replace(text, "”", "”", -1)
text = strings.Replace(text, " ", " ", -1)
return strings.TrimSpace(text)
}
// Htmlunquote html字符反转义,使用方法 {{htmlunquote .unquote}}
func Htmlunquote(text string) string {
//实体符号解释为HTML
/*
Decodes `text` that's HTML quoted.
>>> htmlunquote('<'&">')
'<\\'&">'
*/
// strings.Replace(s, old, new, n)
// 在s字符串中,把old字符串替换为new字符串,n表示替换的次数,小于0表示全部替换
text = strings.Replace(text, " ", " ", -1)
text = strings.Replace(text, "”", "”", -1)
text = strings.Replace(text, "“", "“", -1)
text = strings.Replace(text, """, "\"", -1)
text = strings.Replace(text, "'", "'", -1)
text = strings.Replace(text, ">", ">", -1)
text = strings.Replace(text, "<", "<", -1)
text = strings.Replace(text, "&", "&", -1) // Must be done last!
return strings.TrimSpace(text)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。