🎨 Add template func pow, powf, log and logf https://github.com/siyuan-note/siyuan/issues/9911

This commit is contained in:
Daniel 2023-12-18 12:24:39 +08:00
parent a6c1fd9408
commit 20462f302d
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
4 changed files with 49 additions and 29 deletions

View file

@ -412,7 +412,7 @@ func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av
}
goTpl := template.New("").Delims(".action{", "}")
goTpl = goTpl.Funcs(builtInTemplateFuncs())
goTpl = goTpl.Funcs(util.BuiltInTemplateFuncs())
tpl, tplErr := goTpl.Parse(tplContent)
if nil != tplErr {
logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)
@ -1801,7 +1801,6 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string,
bindBlockAv(tx, avID, val.BlockID)
} else { // 之前绑定的块和现在绑定的块一样
// 直接返回,因为锚文本不允许更改
return
}
}
}

View file

@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"io/fs"
"math"
"os"
"path/filepath"
"sort"
@ -33,7 +32,6 @@ import (
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
"github.com/88250/lute/render"
"github.com/Masterminds/sprig/v3"
"github.com/araddon/dateparse"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
@ -42,12 +40,11 @@ import (
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
"github.com/spf13/cast"
)
func RenderGoTemplate(templateContent string) (ret string, err error) {
tmpl := template.New("")
tmpl = tmpl.Funcs(builtInTemplateFuncs())
tmpl = tmpl.Funcs(util.BuiltInTemplateFuncs())
tpl, err := tmpl.Parse(templateContent)
if nil != err {
return "", errors.New(fmt.Sprintf(Conf.Language(44), err.Error()))
@ -220,7 +217,7 @@ func renderTemplate(p, id string, preview bool) (string, error) {
dataModel["alias"] = block.Alias
}
funcMap := builtInTemplateFuncs()
funcMap := util.BuiltInTemplateFuncs()
funcMap["queryBlocks"] = func(stmt string, args ...string) (ret []*sql.Block) {
for _, arg := range args {
stmt = strings.Replace(stmt, "?", arg, 1)
@ -417,23 +414,3 @@ func addBlockIALNodes(tree *parse.Tree, removeUpdated bool) {
block.InsertAfter(&ast.Node{Type: ast.NodeKramdownBlockIAL, Tokens: parse.IAL2Tokens(block.KramdownIAL)})
}
}
func builtInTemplateFuncs() (ret template.FuncMap) {
ret = sprig.TxtFuncMap()
ret["Weekday"] = util.Weekday
ret["WeekdayCN"] = util.WeekdayCN
ret["WeekdayCN2"] = util.WeekdayCN2
ret["ISOWeek"] = util.ISOWeek
ret["pow"] = pow
ret["powf"] = powf
ret["log"] = log
ret["logf"] = logf
return
}
func pow(a, b interface{}) int64 { return int64(math.Pow(cast.ToFloat64(a), cast.ToFloat64(b))) }
func powf(a, b interface{}) float64 { return math.Pow(cast.ToFloat64(a), cast.ToFloat64(b)) }
func log(a, b interface{}) int64 {
return int64(math.Log(cast.ToFloat64(a)) / math.Log(cast.ToFloat64(b)))
}
func logf(a, b interface{}) float64 { return math.Log(cast.ToFloat64(a)) / math.Log(cast.ToFloat64(b)) }

View file

@ -18,7 +18,6 @@ package treenode
import (
"bytes"
"github.com/Masterminds/sprig/v3"
"github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/cache"
"strings"
@ -834,7 +833,7 @@ func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av
ial["updated"] = time.UnixMilli(block.Block.Updated).Format("20060102150405")
}
funcMap := sprig.TxtFuncMap()
funcMap := util.BuiltInTemplateFuncs()
goTpl := template.New("").Delims(".action{", "}")
tpl, tplErr := goTpl.Funcs(funcMap).Parse(tplContent)
if nil != tplErr {

45
kernel/util/template.go Normal file
View file

@ -0,0 +1,45 @@
// SiYuan - Refactor your thinking
// Copyright (c) 2020-present, b3log.org
//
// 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 util
import (
"math"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/spf13/cast"
)
func BuiltInTemplateFuncs() (ret template.FuncMap) {
ret = sprig.TxtFuncMap()
ret["Weekday"] = Weekday
ret["WeekdayCN"] = WeekdayCN
ret["WeekdayCN2"] = WeekdayCN2
ret["ISOWeek"] = ISOWeek
ret["pow"] = pow
ret["powf"] = powf
ret["log"] = log
ret["logf"] = logf
return
}
func pow(a, b interface{}) int64 { return int64(math.Pow(cast.ToFloat64(a), cast.ToFloat64(b))) }
func powf(a, b interface{}) float64 { return math.Pow(cast.ToFloat64(a), cast.ToFloat64(b)) }
func log(a, b interface{}) int64 {
return int64(math.Log(cast.ToFloat64(a)) / math.Log(cast.ToFloat64(b)))
}
func logf(a, b interface{}) float64 { return math.Log(cast.ToFloat64(a)) / math.Log(cast.ToFloat64(b)) }