mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 22:50:13 +01:00
🎨 支持合并子文档导出 Word/PDF https://github.com/siyuan-note/siyuan/issues/3219
This commit is contained in:
parent
adc21c9520
commit
5b45ed0c67
1 changed files with 94 additions and 0 deletions
94
kernel/model/export_merge.go
Normal file
94
kernel/model/export_merge.go
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
// 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 (
|
||||
"github.com/88250/lute/ast"
|
||||
"github.com/88250/lute/parse"
|
||||
)
|
||||
|
||||
func mergeSubDocs(rootTree *parse.Tree) (ret *parse.Tree, err error) {
|
||||
ret = rootTree
|
||||
rootBlock := &Block{Box: rootTree.Box, ID: rootTree.ID, Path: rootTree.Path}
|
||||
if err = buildBlockChildren(rootBlock); nil != err {
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < 1024; i++ {
|
||||
if err = walkBlock(ret, rootBlock); nil != err {
|
||||
return
|
||||
}
|
||||
|
||||
if nil == rootBlock.Children {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func walkBlock(tree *parse.Tree, block *Block) (err error) {
|
||||
for _, c := range block.Children {
|
||||
if err = walkBlock(tree, c); nil != err {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for i := len(block.Children) - 1; -1 < i; i-- {
|
||||
c := block.Children[i]
|
||||
nodes, loadErr := loadTreeNodes(c.Box, c.Path)
|
||||
if nil != loadErr {
|
||||
return
|
||||
}
|
||||
|
||||
for j := len(nodes) - 1; -1 < j; j-- {
|
||||
tree.Root.LastChild.InsertAfter(nodes[j])
|
||||
}
|
||||
}
|
||||
block.Children = nil
|
||||
return
|
||||
}
|
||||
|
||||
func loadTreeNodes(box string, p string) (ret []*ast.Node, err error) {
|
||||
tree, err := LoadTree(box, p)
|
||||
if nil != err {
|
||||
return
|
||||
}
|
||||
|
||||
for c := tree.Root.FirstChild; nil != c; c = c.Next {
|
||||
ret = append(ret, c)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func buildBlockChildren(block *Block) (err error) {
|
||||
files, _, err := ListDocTree(block.Box, block.Path, Conf.FileTree.Sort)
|
||||
if nil != err {
|
||||
return
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
childBlock := &Block{Box: block.Box, ID: f.ID, Path: f.Path}
|
||||
block.Children = append(block.Children, childBlock)
|
||||
}
|
||||
|
||||
for _, c := range block.Children {
|
||||
if err = buildBlockChildren(c); nil != err {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue