|
@@ -20,6 +20,7 @@ import (
|
|
|
"bytes"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
+ "github.com/araddon/dateparse"
|
|
|
"io/fs"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
@@ -32,7 +33,6 @@ import (
|
|
|
"github.com/88250/lute/ast"
|
|
|
"github.com/88250/lute/parse"
|
|
|
"github.com/88250/lute/render"
|
|
|
- "github.com/araddon/dateparse"
|
|
|
"github.com/siyuan-note/filelock"
|
|
|
"github.com/siyuan-note/logging"
|
|
|
"github.com/siyuan-note/siyuan/kernel/av"
|
|
@@ -44,7 +44,9 @@ import (
|
|
|
|
|
|
func RenderGoTemplate(templateContent string) (ret string, err error) {
|
|
|
tmpl := template.New("")
|
|
|
- tmpl = tmpl.Funcs(util.BuiltInTemplateFuncs())
|
|
|
+ tplFuncMap := util.BuiltInTemplateFuncs()
|
|
|
+ SQLTemplateFuncs(&tplFuncMap)
|
|
|
+ tmpl = tmpl.Funcs(tplFuncMap)
|
|
|
tpl, err := tmpl.Parse(templateContent)
|
|
|
if nil != err {
|
|
|
return "", errors.New(fmt.Sprintf(Conf.Language(44), err.Error()))
|
|
@@ -217,33 +219,11 @@ func renderTemplate(p, id string, preview bool) (string, error) {
|
|
|
dataModel["alias"] = block.Alias
|
|
|
}
|
|
|
|
|
|
- funcMap := util.BuiltInTemplateFuncs()
|
|
|
- funcMap["queryBlocks"] = func(stmt string, args ...string) (ret []*sql.Block) {
|
|
|
- for _, arg := range args {
|
|
|
- stmt = strings.Replace(stmt, "?", arg, 1)
|
|
|
- }
|
|
|
- ret = sql.SelectBlocksRawStmt(stmt, 1, Conf.Search.Limit)
|
|
|
- return
|
|
|
- }
|
|
|
- funcMap["querySpans"] = func(stmt string, args ...string) (ret []*sql.Span) {
|
|
|
- for _, arg := range args {
|
|
|
- stmt = strings.Replace(stmt, "?", arg, 1)
|
|
|
- }
|
|
|
- ret = sql.SelectSpansRawStmt(stmt, Conf.Search.Limit)
|
|
|
- return
|
|
|
- }
|
|
|
- funcMap["parseTime"] = func(dateStr string) time.Time {
|
|
|
- now := time.Now()
|
|
|
- ret, err := dateparse.ParseIn(dateStr, now.Location())
|
|
|
- if nil != err {
|
|
|
- logging.LogWarnf("parse date [%s] failed [%s], return current time instead", dateStr, err)
|
|
|
- return now
|
|
|
- }
|
|
|
- return ret
|
|
|
- }
|
|
|
-
|
|
|
goTpl := template.New("").Delims(".action{", "}")
|
|
|
- tpl, err := goTpl.Funcs(funcMap).Parse(gulu.Str.FromBytes(md))
|
|
|
+ tplFuncMap := util.BuiltInTemplateFuncs()
|
|
|
+ SQLTemplateFuncs(&tplFuncMap)
|
|
|
+ goTpl = goTpl.Funcs(tplFuncMap)
|
|
|
+ tpl, err := goTpl.Funcs(tplFuncMap).Parse(gulu.Str.FromBytes(md))
|
|
|
if nil != err {
|
|
|
return "", errors.New(fmt.Sprintf(Conf.Language(44), err.Error()))
|
|
|
}
|
|
@@ -414,3 +394,29 @@ func addBlockIALNodes(tree *parse.Tree, removeUpdated bool) {
|
|
|
block.InsertAfter(&ast.Node{Type: ast.NodeKramdownBlockIAL, Tokens: parse.IAL2Tokens(block.KramdownIAL)})
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func SQLTemplateFuncs(templateFuncMap *template.FuncMap) {
|
|
|
+ (*templateFuncMap)["queryBlocks"] = func(stmt string, args ...string) (retBlocks []*sql.Block) {
|
|
|
+ for _, arg := range args {
|
|
|
+ stmt = strings.Replace(stmt, "?", arg, 1)
|
|
|
+ }
|
|
|
+ retBlocks = sql.SelectBlocksRawStmt(stmt, 1, 512)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ (*templateFuncMap)["querySpans"] = func(stmt string, args ...string) (retSpans []*sql.Span) {
|
|
|
+ for _, arg := range args {
|
|
|
+ stmt = strings.Replace(stmt, "?", arg, 1)
|
|
|
+ }
|
|
|
+ retSpans = sql.SelectSpansRawStmt(stmt, 512)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ (*templateFuncMap)["parseTime"] = func(dateStr string) time.Time {
|
|
|
+ now := time.Now()
|
|
|
+ retTime, err := dateparse.ParseIn(dateStr, now.Location())
|
|
|
+ if nil != err {
|
|
|
+ logging.LogWarnf("parse date [%s] failed [%s], return current time instead", dateStr, err)
|
|
|
+ return now
|
|
|
+ }
|
|
|
+ return retTime
|
|
|
+ }
|
|
|
+}
|