format.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 model
  17. import (
  18. "os"
  19. "path/filepath"
  20. "github.com/88250/gulu"
  21. "github.com/88250/lute/ast"
  22. "github.com/88250/lute/parse"
  23. "github.com/88250/lute/render"
  24. "github.com/siyuan-note/filelock"
  25. "github.com/siyuan-note/logging"
  26. "github.com/siyuan-note/siyuan/kernel/util"
  27. )
  28. func AutoSpace(rootID string) (err error) {
  29. tree, err := loadTreeByBlockID(rootID)
  30. if nil != err {
  31. return
  32. }
  33. util.PushProtyleLoading(rootID, Conf.Language(116))
  34. defer util.PushProtyleReload(rootID)
  35. WaitForWritingFiles()
  36. generateFormatHistory(tree)
  37. luteEngine := NewLute()
  38. // 合并相邻的同类行级节点
  39. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  40. if entering {
  41. switch n.Type {
  42. case ast.NodeTextMark:
  43. luteEngine.MergeSameTextMark(n)
  44. }
  45. }
  46. return ast.WalkContinue
  47. })
  48. rootIAL := tree.Root.KramdownIAL
  49. addBlockIALNodes(tree, false)
  50. // 第一次格式化为了合并相邻的文本节点
  51. formatRenderer := render.NewFormatRenderer(tree, luteEngine.RenderOptions)
  52. md := formatRenderer.Render()
  53. newTree := parseKTree(md)
  54. newTree.Root.Spec = "1"
  55. // 第二次格式化启用自动空格
  56. luteEngine.SetAutoSpace(true)
  57. formatRenderer = render.NewFormatRenderer(newTree, luteEngine.RenderOptions)
  58. md = formatRenderer.Render()
  59. newTree = parseKTree(md)
  60. newTree.Root.Spec = "1"
  61. newTree.Root.ID = tree.ID
  62. newTree.Root.KramdownIAL = rootIAL
  63. newTree.ID = tree.ID
  64. newTree.Path = tree.Path
  65. newTree.HPath = tree.HPath
  66. newTree.Box = tree.Box
  67. err = writeJSONQueue(newTree)
  68. if nil != err {
  69. return
  70. }
  71. util.RandomSleep(500, 700)
  72. return
  73. }
  74. func generateFormatHistory(tree *parse.Tree) {
  75. historyDir, err := GetHistoryDir(HistoryOpFormat)
  76. if nil != err {
  77. logging.LogErrorf("get history dir failed: %s", err)
  78. return
  79. }
  80. historyPath := filepath.Join(historyDir, tree.Box, tree.Path)
  81. if err = os.MkdirAll(filepath.Dir(historyPath), 0755); nil != err {
  82. logging.LogErrorf("generate history failed: %s", err)
  83. return
  84. }
  85. var data []byte
  86. if data, err = filelock.ReadFile(filepath.Join(util.DataDir, tree.Box, tree.Path)); err != nil {
  87. logging.LogErrorf("generate history failed: %s", err)
  88. return
  89. }
  90. if err = gulu.File.WriteFileSafer(historyPath, data, 0644); err != nil {
  91. logging.LogErrorf("generate history failed: %s", err)
  92. return
  93. }
  94. indexHistoryDir(filepath.Base(historyDir), util.NewLute())
  95. }