代码拉取完成,页面将自动刷新
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package helper
import (
"fmt"
"regexp"
"strings"
"github.com/webx-top/com"
"github.com/webx-top/echo/param"
)
const (
fileCSPattern = `(?:[^"'#\(\)]+)` //文件名
fileStartPattern = `["'\(]` //起始符号
fileEndPattern = `["'\)]` //终止符号
)
var (
//defaultFilePattern = `["'\(]([^"'#\(\)]+)#FileID-(\d+)["'\)]`
filePattern = fileStartPattern + `(` + fileCSPattern + `\.(?:[\w]+)` + fileCSPattern + `?)` + fileEndPattern
fileRGX = regexp.MustCompile(filePattern)
)
// ReplaceEmbeddedResID 替换正文中的资源网址
func ReplaceEmbeddedResID(v string, reses map[uint64]string) (r string) {
for fid, rurl := range reses {
re := regexp.MustCompile(`(` + fileStartPattern + `)` + fileCSPattern + `#FileID-` + fmt.Sprint(fid) + `(` + fileEndPattern + `)`)
v = re.ReplaceAllString(v, `${1}`+rurl+`${2}`)
}
return v
}
// ReplaceRelatedResID 替换字段中的资源网址
func ReplaceRelatedResID(v string, reses map[uint64]string, seperator ...string) (r string) {
var fileList []string
var sep string
if len(seperator) > 0 && len(seperator[0]) > 0 {
sep = seperator[0]
fileList = strings.Split(v, sep)
} else {
fileList = append(fileList, v)
}
replaced := map[int]struct{}{}
for fid, rurl := range reses {
suffix := `#FileID-` + fmt.Sprint(fid)
for key, file := range fileList {
if _, ok := replaced[key]; ok {
continue
}
if strings.HasSuffix(file, suffix) {
fileList[key] = rurl
replaced[key] = struct{}{}
}
}
}
v = strings.Join(fileList, sep)
return v
}
// ReplaceEmbeddedRes 替换正文中的资源网址
func ReplaceEmbeddedRes(v string, reses map[string]string) (r string) {
for furl, rurl := range reses {
re := regexp.MustCompile(`(` + fileStartPattern + `)` + regexp.QuoteMeta(furl) + `(` + fileEndPattern + `)`)
v = re.ReplaceAllString(v, `${1}`+rurl+`${2}`)
}
return v
}
// ReplaceRelatedRes 替换字段中的资源网址
func ReplaceRelatedRes(v string, reses map[string]string, seperator ...string) (r string) {
var fileList []string
var sep string
if len(seperator) > 0 && len(seperator[0]) > 0 {
sep = seperator[0]
fileList = strings.Split(v, sep)
} else {
fileList = append(fileList, v)
}
for key, file := range fileList {
rurl, ok := reses[file]
if !ok {
continue
}
fileList[key] = rurl
}
v = strings.Join(fileList, sep)
return v
}
// EmbeddedRes 获取正文中的资源
func EmbeddedRes(v string, fn func(string, int64)) [][]string {
if len(v) == 0 {
return nil
}
list := fileRGX.FindAllStringSubmatch(v, -1)
if fn == nil {
return list
}
for _, a := range list {
resource := a[1]
var fileID int64
if len(a) > 2 {
fileID = param.AsInt64(a[2])
}
fn(resource, fileID)
}
return list
}
// RelatedRes 获取字段中关联的资源
func RelatedRes(v string, fn func(string, int64), seperator ...string) {
if len(v) == 0 {
return
}
var fileList []string
if len(seperator) > 0 && len(seperator[0]) > 0 {
fileList = strings.Split(v, seperator[0])
} else {
fileList = append(fileList, v)
}
for _, file := range fileList {
file = strings.TrimSpace(file)
if len(file) == 0 {
fn(file, 0)
continue
}
p := strings.LastIndex(file, `#FileID-`)
if p < 0 {
fn(file, 0)
continue
}
var fid int64
fileID := file[p+8:]
if len(fileID) > 0 {
fid = com.Int64(fileID)
}
file = file[0:p]
fn(file, fid)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。