123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- // SiYuan - Refactor your thinking
- // 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/task"
- "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 "" != titleSuffix {
- 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 + ")"
- titleSuffix = " " + titleSuffix
- }
- tree.Root.SetIALAttr("id", tree.ID)
- tree.Root.SetIALAttr("title", tree.Root.IALAttr("title")+titleSuffix)
- tree.Root.RemoveIALAttr("scroll")
- 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] {
- if ast.NodeTextMark == n.Type {
- n.TextMarkBlockRefID = 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() {
- if strings.HasPrefix(info.Name(), ".") {
- return filepath.SkipDir
- }
- return nil
- }
- 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.ReadFile(localPath)
- if nil != err {
- logging.LogErrorf("get data [path=%s] failed: %s", localPath, err)
- return
- }
- ret, err = filesys.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")
- ErrBlockNotFound = errors.New("block not found")
- ErrTreeNotFound = errors.New("tree not found")
- ErrIndexing = errors.New("indexing")
- )
- func LoadTreeByID(id string) (ret *parse.Tree, err error) {
- return loadTreeByBlockID(id)
- }
- func loadTreeByBlockID(id string) (ret *parse.Tree, err error) {
- if "" == id {
- return nil, ErrTreeNotFound
- }
- bt := treenode.GetBlockTree(id)
- if nil == bt {
- if task.Contain(task.DatabaseIndex, task.DatabaseIndexFull) {
- err = ErrIndexing
- return
- }
- return nil, ErrBlockNotFound
- }
- luteEngine := util.NewLute()
- ret, err = filesys.LoadTree(bt.BoxID, bt.Path, luteEngine)
- return
- }
|