template.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package filesys
  17. import (
  18. "math"
  19. "text/template"
  20. "time"
  21. "github.com/88250/go-humanize"
  22. util2 "github.com/88250/lute/util"
  23. "github.com/Masterminds/sprig/v3"
  24. "github.com/araddon/dateparse"
  25. "github.com/siyuan-note/logging"
  26. "github.com/siyuan-note/siyuan/kernel/treenode"
  27. "github.com/siyuan-note/siyuan/kernel/util"
  28. "github.com/spf13/cast"
  29. )
  30. func BuiltInTemplateFuncs() (ret template.FuncMap) {
  31. ret = sprig.TxtFuncMap()
  32. // 因为安全原因移除一些函数 https://github.com/siyuan-note/siyuan/issues/13426
  33. delete(ret, "env")
  34. delete(ret, "expandenv")
  35. delete(ret, "getHostByName")
  36. ret["Weekday"] = util.Weekday
  37. ret["WeekdayCN"] = util.WeekdayCN
  38. ret["WeekdayCN2"] = util.WeekdayCN2
  39. ret["ISOWeek"] = util.ISOWeek
  40. ret["pow"] = pow
  41. ret["powf"] = powf
  42. ret["log"] = log
  43. ret["logf"] = logf
  44. ret["parseTime"] = parseTime
  45. ret["FormatFloat"] = FormatFloat
  46. ret["getHPathByID"] = getHPathByID
  47. ret["statBlock"] = StatBlock
  48. ret["runeCount"] = runeCount
  49. ret["wordCount"] = wordCount
  50. return
  51. }
  52. func runeCount(s string) (ret int) {
  53. ret, _ = util2.WordCount(s)
  54. return
  55. }
  56. func wordCount(s string) (ret int) {
  57. _, ret = util2.WordCount(s)
  58. return
  59. }
  60. func pow(a, b interface{}) int64 { return int64(math.Pow(cast.ToFloat64(a), cast.ToFloat64(b))) }
  61. func powf(a, b interface{}) float64 { return math.Pow(cast.ToFloat64(a), cast.ToFloat64(b)) }
  62. func log(a, b interface{}) int64 {
  63. return int64(math.Log(cast.ToFloat64(a)) / math.Log(cast.ToFloat64(b)))
  64. }
  65. func logf(a, b interface{}) float64 { return math.Log(cast.ToFloat64(a)) / math.Log(cast.ToFloat64(b)) }
  66. func parseTime(dateStr string) time.Time {
  67. now := time.Now()
  68. retTime, err := dateparse.ParseIn(dateStr, now.Location())
  69. if err != nil {
  70. logging.LogWarnf("parse date [%s] failed [%s], return current time instead", dateStr, err)
  71. return now
  72. }
  73. return retTime
  74. }
  75. func FormatFloat(format string, n float64) string {
  76. return humanize.FormatFloat(format, n)
  77. }
  78. func getHPathByID(id string) (ret string) {
  79. bt := treenode.GetBlockTree(id)
  80. if nil == bt {
  81. return
  82. }
  83. ret = bt.HPath
  84. return
  85. }