tree.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "errors"
  19. "io/fs"
  20. "path"
  21. "path/filepath"
  22. "strings"
  23. "time"
  24. "github.com/88250/lute"
  25. "github.com/88250/lute/ast"
  26. "github.com/88250/lute/parse"
  27. "github.com/siyuan-note/filelock"
  28. "github.com/siyuan-note/logging"
  29. "github.com/siyuan-note/siyuan/kernel/filesys"
  30. "github.com/siyuan-note/siyuan/kernel/task"
  31. "github.com/siyuan-note/siyuan/kernel/treenode"
  32. "github.com/siyuan-note/siyuan/kernel/util"
  33. )
  34. func resetTree(tree *parse.Tree, titleSuffix string) {
  35. tree.ID = ast.NewNodeID()
  36. tree.Root.ID = tree.ID
  37. if "" != titleSuffix {
  38. if t, parseErr := time.Parse("20060102150405", util.TimeFromID(tree.ID)); nil == parseErr {
  39. titleSuffix += " " + t.Format("2006-01-02 15:04:05")
  40. } else {
  41. titleSuffix = "Duplicated " + time.Now().Format("2006-01-02 15:04:05")
  42. }
  43. titleSuffix = "(" + titleSuffix + ")"
  44. titleSuffix = " " + titleSuffix
  45. }
  46. tree.Root.SetIALAttr("id", tree.ID)
  47. tree.Root.SetIALAttr("title", tree.Root.IALAttr("title")+titleSuffix)
  48. tree.Root.RemoveIALAttr("scroll")
  49. p := path.Join(path.Dir(tree.Path), tree.ID) + ".sy"
  50. tree.Path = p
  51. tree.HPath = tree.HPath + " " + titleSuffix
  52. // 收集所有引用
  53. refIDs := map[string]string{}
  54. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  55. if !entering || !treenode.IsBlockRef(n) {
  56. return ast.WalkContinue
  57. }
  58. defID, _, _ := treenode.GetBlockRef(n)
  59. if "" == defID {
  60. return ast.WalkContinue
  61. }
  62. refIDs[defID] = "1"
  63. return ast.WalkContinue
  64. })
  65. // 重置块 ID
  66. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  67. if !entering || ast.NodeDocument == n.Type {
  68. return ast.WalkContinue
  69. }
  70. if n.IsBlock() && "" != n.ID {
  71. newID := ast.NewNodeID()
  72. if "1" == refIDs[n.ID] {
  73. // 如果是文档自身的内部引用
  74. refIDs[n.ID] = newID
  75. }
  76. n.ID = newID
  77. n.SetIALAttr("id", n.ID)
  78. }
  79. return ast.WalkContinue
  80. })
  81. // 重置内部引用
  82. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  83. if !entering || !treenode.IsBlockRef(n) {
  84. return ast.WalkContinue
  85. }
  86. defID, _, _ := treenode.GetBlockRef(n)
  87. if "" == defID {
  88. return ast.WalkContinue
  89. }
  90. if "1" != refIDs[defID] {
  91. if ast.NodeTextMark == n.Type {
  92. n.TextMarkBlockRefID = refIDs[defID]
  93. }
  94. }
  95. return ast.WalkContinue
  96. })
  97. }
  98. func pagedPaths(localPath string, pageSize int) (ret map[int][]string) {
  99. ret = map[int][]string{}
  100. page := 1
  101. filepath.Walk(localPath, func(path string, info fs.FileInfo, err error) error {
  102. if info.IsDir() {
  103. if strings.HasPrefix(info.Name(), ".") {
  104. return filepath.SkipDir
  105. }
  106. return nil
  107. }
  108. if !strings.HasSuffix(info.Name(), ".sy") {
  109. return nil
  110. }
  111. ret[page] = append(ret[page], path)
  112. if pageSize <= len(ret[page]) {
  113. page++
  114. }
  115. return nil
  116. })
  117. return
  118. }
  119. func loadTree(localPath string, luteEngine *lute.Lute) (ret *parse.Tree, err error) {
  120. data, err := filelock.ReadFile(localPath)
  121. if nil != err {
  122. logging.LogErrorf("get data [path=%s] failed: %s", localPath, err)
  123. return
  124. }
  125. ret, err = filesys.ParseJSONWithoutFix(data, luteEngine.ParseOptions)
  126. if nil != err {
  127. logging.LogErrorf("parse json to tree [%s] failed: %s", localPath, err)
  128. return
  129. }
  130. return
  131. }
  132. var (
  133. ErrBoxNotFound = errors.New("notebook not found")
  134. ErrBlockNotFound = errors.New("block not found")
  135. ErrTreeNotFound = errors.New("tree not found")
  136. ErrIndexing = errors.New("indexing")
  137. )
  138. func LoadTreeByID(id string) (ret *parse.Tree, err error) {
  139. return loadTreeByBlockID(id)
  140. }
  141. func loadTreeByBlockID(id string) (ret *parse.Tree, err error) {
  142. if "" == id {
  143. return nil, ErrTreeNotFound
  144. }
  145. bt := treenode.GetBlockTree(id)
  146. if nil == bt {
  147. if task.Contain(task.DatabaseIndex, task.DatabaseIndexFull) {
  148. err = ErrIndexing
  149. return
  150. }
  151. return nil, ErrBlockNotFound
  152. }
  153. luteEngine := util.NewLute()
  154. ret, err = filesys.LoadTree(bt.BoxID, bt.Path, luteEngine)
  155. return
  156. }