Prechádzať zdrojové kódy

Merge remote-tracking branch 'origin/dev' into dev

Vanessa 1 rok pred
rodič
commit
a6c1fd9408
2 zmenil súbory, kde vykonal 26 pridanie a 15 odobranie
  1. 2 3
      kernel/model/attribute_view.go
  2. 24 12
      kernel/model/template.go

+ 2 - 3
kernel/model/attribute_view.go

@@ -29,7 +29,6 @@ import (
 	"github.com/88250/gulu"
 	"github.com/88250/lute/ast"
 	"github.com/88250/lute/parse"
-	"github.com/Masterminds/sprig/v3"
 	"github.com/siyuan-note/dejavu/entity"
 	"github.com/siyuan-note/filelock"
 	"github.com/siyuan-note/logging"
@@ -412,9 +411,9 @@ func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av
 		ial["updated"] = time.UnixMilli(block.Block.Updated).Format("20060102150405")
 	}
 
-	funcMap := sprig.TxtFuncMap()
 	goTpl := template.New("").Delims(".action{", "}")
-	tpl, tplErr := goTpl.Funcs(funcMap).Parse(tplContent)
+	goTpl = goTpl.Funcs(builtInTemplateFuncs())
+	tpl, tplErr := goTpl.Parse(tplContent)
 	if nil != tplErr {
 		logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)
 		return ""

+ 24 - 12
kernel/model/template.go

@@ -21,6 +21,7 @@ import (
 	"errors"
 	"fmt"
 	"io/fs"
+	"math"
 	"os"
 	"path/filepath"
 	"sort"
@@ -41,17 +42,12 @@ 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(sprig.TxtFuncMap())
-	tmpl = tmpl.Funcs(template.FuncMap{
-		"Weekday":    util.Weekday,
-		"WeekdayCN":  util.WeekdayCN,
-		"WeekdayCN2": util.WeekdayCN2,
-		"ISOWeek":    util.ISOWeek,
-	})
+	tmpl = tmpl.Funcs(builtInTemplateFuncs())
 	tpl, err := tmpl.Parse(templateContent)
 	if nil != err {
 		return "", errors.New(fmt.Sprintf(Conf.Language(44), err.Error()))
@@ -224,7 +220,7 @@ func renderTemplate(p, id string, preview bool) (string, error) {
 		dataModel["alias"] = block.Alias
 	}
 
-	funcMap := sprig.TxtFuncMap()
+	funcMap := builtInTemplateFuncs()
 	funcMap["queryBlocks"] = func(stmt string, args ...string) (ret []*sql.Block) {
 		for _, arg := range args {
 			stmt = strings.Replace(stmt, "?", arg, 1)
@@ -248,10 +244,6 @@ func renderTemplate(p, id string, preview bool) (string, error) {
 		}
 		return ret
 	}
-	funcMap["Weekday"] = util.Weekday
-	funcMap["WeekdayCN"] = util.WeekdayCN
-	funcMap["WeekdayCN2"] = util.WeekdayCN2
-	funcMap["ISOWeek"] = util.ISOWeek
 
 	goTpl := template.New("").Delims(".action{", "}")
 	tpl, err := goTpl.Funcs(funcMap).Parse(gulu.Str.FromBytes(md))
@@ -425,3 +417,23 @@ 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)) }