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

This commit is contained in:
Vanessa 2024-01-05 09:31:08 +08:00
commit 872ab78c07
6 changed files with 67 additions and 42 deletions

View file

@ -303,19 +303,35 @@ const (
func NewFormattedValueDate(content, content2 int64, format DateFormat, isNotTime bool) (ret *ValueDate) {
var formatted string
if isNotTime {
formatted = time.UnixMilli(content).Format("2006-01-02")
} else {
formatted = time.UnixMilli(content).Format("2006-01-02 15:04")
contentTime := time.UnixMilli(content)
if 0 == content || contentTime.IsZero() {
ret = &ValueDate{
Content: content,
Content2: content2,
HasEndDate: false,
IsNotTime: true,
FormattedContent: formatted,
}
return
}
if isNotTime {
formatted = contentTime.Format("2006-01-02")
} else {
formatted = contentTime.Format("2006-01-02 15:04")
}
if 0 < content2 {
var formattedContent2 string
content2Time := time.UnixMilli(content2)
if isNotTime {
formattedContent2 = time.UnixMilli(content2).Format("2006-01-02")
formattedContent2 = content2Time.Format("2006-01-02")
} else {
formattedContent2 = time.UnixMilli(content2).Format("2006-01-02 15:04")
formattedContent2 = content2Time.Format("2006-01-02 15:04")
}
if !content2Time.IsZero() {
formatted += " → " + formattedContent2
}
formatted += " → " + formattedContent2
}
switch format {
case DateFormatNone:

View file

@ -604,7 +604,9 @@ func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av
}
goTpl := template.New("").Delims(".action{", "}")
goTpl = goTpl.Funcs(util.BuiltInTemplateFuncs())
tplFuncMap := util.BuiltInTemplateFuncs()
SQLTemplateFuncs(&tplFuncMap)
goTpl = goTpl.Funcs(tplFuncMap)
tpl, tplErr := goTpl.Parse(tplContent)
if nil != tplErr {
logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)

View file

@ -169,7 +169,7 @@ func ExportAv2CSV(avID string) (zipPath string, err error) {
if nil != removeErr {
logging.LogErrorf("remove export folder [%s] failed: %s", exportFolder, removeErr)
}
zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))
zipPath = "/export/csv/" + url.PathEscape(filepath.Base(zipPath))
return
}

View file

@ -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
}
}

View file

@ -962,9 +962,11 @@ func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av
ial["updated"] = time.UnixMilli(block.Block.Updated).Format("20060102150405")
}
funcMap := util.BuiltInTemplateFuncs()
goTpl := template.New("").Delims(".action{", "}")
tpl, tplErr := goTpl.Funcs(funcMap).Parse(tplContent)
tplFuncMap := util.BuiltInTemplateFuncs()
// 这里存在依赖问题所以不支持 SQLTemplateFuncs(&tplFuncMap)
goTpl = goTpl.Funcs(tplFuncMap)
tpl, tplErr := goTpl.Funcs(tplFuncMap).Parse(tplContent)
if nil != tplErr {
logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)
return ""

View file

@ -17,11 +17,10 @@
package util
import (
"math"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/spf13/cast"
"math"
"text/template"
)
func BuiltInTemplateFuncs() (ret template.FuncMap) {