forked from siyuan-note/siyuan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
163 lines (147 loc) · 4.33 KB
/
tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// SiYuan - Build Your Eternal Digital Garden
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package model
import (
"errors"
"io/fs"
"path"
"path/filepath"
"strings"
"time"
"github.com/88250/lute"
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
func resetTree(tree *parse.Tree, titleSuffix string) {
tree.ID = ast.NewNodeID()
tree.Root.ID = tree.ID
if t, parseErr := time.Parse("20060102150405", util.TimeFromID(tree.ID)); nil == parseErr {
titleSuffix += " " + t.Format("2006-01-02 15:04:05")
} else {
titleSuffix = "Duplicated " + time.Now().Format("2006-01-02 15:04:05")
}
titleSuffix = "(" + titleSuffix + ")"
tree.Root.SetIALAttr("id", tree.ID)
tree.Root.SetIALAttr("title", tree.Root.IALAttr("title")+" "+titleSuffix)
p := path.Join(path.Dir(tree.Path), tree.ID) + ".sy"
tree.Path = p
tree.HPath = tree.HPath + " " + titleSuffix
// 收集所有引用
refIDs := map[string]string{}
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering || !treenode.IsBlockRef(n) {
return ast.WalkContinue
}
defID, _, _ := treenode.GetBlockRef(n)
if "" == defID {
return ast.WalkContinue
}
refIDs[defID] = "1"
return ast.WalkContinue
})
// 重置块 ID
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering || ast.NodeDocument == n.Type {
return ast.WalkContinue
}
if n.IsBlock() && "" != n.ID {
newID := ast.NewNodeID()
if "1" == refIDs[n.ID] {
// 如果是文档自身的内部引用
refIDs[n.ID] = newID
}
n.ID = newID
n.SetIALAttr("id", n.ID)
}
return ast.WalkContinue
})
// 重置内部引用
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering || !treenode.IsBlockRef(n) {
return ast.WalkContinue
}
defID, _, _ := treenode.GetBlockRef(n)
if "" == defID {
return ast.WalkContinue
}
if "1" != refIDs[defID] {
n.Tokens = []byte(refIDs[defID])
}
return ast.WalkContinue
})
}
func pagedPaths(localPath string, pageSize int) (ret map[int][]string) {
ret = map[int][]string{}
page := 1
filepath.Walk(localPath, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() && strings.HasPrefix(info.Name(), ".") {
return filepath.SkipDir
}
if !strings.HasSuffix(info.Name(), ".sy") {
return nil
}
ret[page] = append(ret[page], path)
if pageSize <= len(ret[page]) {
page++
}
return nil
})
return
}
func loadTree(localPath string, luteEngine *lute.Lute) (ret *parse.Tree, err error) {
data, err := filelock.NoLockFileRead(localPath)
if nil != err {
logging.LogErrorf("get data [path=%s] failed: %s", localPath, err)
return
}
ret, err = parse.ParseJSONWithoutFix(data, luteEngine.ParseOptions)
if nil != err {
logging.LogErrorf("parse json to tree [%s] failed: %s", localPath, err)
return
}
return
}
var ErrBoxNotFound = errors.New("notebook not found")
var ErrBlockNotFound = errors.New("block not found")
var ErrTreeNotFound = errors.New("tree not found")
func loadTreeByBlockID(id string) (ret *parse.Tree, err error) {
if "" == id {
return nil, ErrTreeNotFound
}
bt := treenode.GetBlockTree(id)
if nil == bt {
return nil, ErrBlockNotFound
}
ret, err = LoadTree(bt.BoxID, bt.Path)
if nil != err {
return
}
return
}
func LoadTree(boxID, p string) (*parse.Tree, error) {
luteEngine := NewLute()
tree, err := filesys.LoadTree(boxID, p, luteEngine)
if nil != err {
logging.LogErrorf("load tree [%s] failed: %s", boxID+p, err)
return nil, err
}
return tree, nil
}