This commit is contained in:
Liang Ding 2022-07-17 12:22:32 +08:00
parent c8ea858976
commit 505b973c2d
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
70 changed files with 671 additions and 942 deletions

View file

@ -41,6 +41,7 @@ import (
"github.com/emirpasic/gods/sets/hashset"
"github.com/emirpasic/gods/stacks/linkedliststack"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
@ -49,7 +50,7 @@ import (
func ExportSY(id string) (name, zipPath string) {
block := treenode.GetBlockTree(id)
if nil == block {
util.LogErrorf("not found block [%s]", id)
logging.LogErrorf("not found block [%s]", id)
return
}
@ -108,7 +109,7 @@ func ExportData() (zipPath string) {
func exportData(exportFolder string) (err error) {
baseFolderName := "data-" + util.CurrentTimeSecondsStr()
if err = os.MkdirAll(exportFolder, 0755); nil != err {
util.LogErrorf("create export temp folder failed: %s", err)
logging.LogErrorf("create export temp folder failed: %s", err)
return
}
@ -119,7 +120,7 @@ func exportData(exportFolder string) (err error) {
data := filepath.Join(util.WorkspaceDir, "data")
if err = stableCopy(data, exportFolder); nil != err {
util.LogErrorf("copy data dir from [%s] to [%s] failed: %s", data, baseFolderName, err)
logging.LogErrorf("copy data dir from [%s] to [%s] failed: %s", data, baseFolderName, err)
err = errors.New(fmt.Sprintf(Conf.Language(14), formatErrorMsg(err)))
return
}
@ -127,17 +128,17 @@ func exportData(exportFolder string) (err error) {
zipPath := exportFolder + ".zip"
zip, err := gulu.Zip.Create(zipPath)
if nil != err {
util.LogErrorf("create export data zip [%s] failed: %s", exportFolder, err)
logging.LogErrorf("create export data zip [%s] failed: %s", exportFolder, err)
return
}
if err = zip.AddDirectory(baseFolderName, exportFolder); nil != err {
util.LogErrorf("create export data zip [%s] failed: %s", exportFolder, err)
logging.LogErrorf("create export data zip [%s] failed: %s", exportFolder, err)
return
}
if err = zip.Close(); nil != err {
util.LogErrorf("close export data zip failed: %s", err)
logging.LogErrorf("close export data zip failed: %s", err)
}
os.RemoveAll(exportFolder)
@ -174,19 +175,19 @@ func ExportDocx(id, savePath string) (err error) {
pandoc.Stdin = bytes.NewBufferString(dom)
output, err := pandoc.CombinedOutput()
if nil != err {
util.LogErrorf("export docx failed: %s", gulu.Str.FromBytes(output))
logging.LogErrorf("export docx failed: %s", gulu.Str.FromBytes(output))
msg := fmt.Sprintf(Conf.Language(14), gulu.Str.FromBytes(output))
return errors.New(msg)
}
if err = gulu.File.Copy(tmpDocxPath, filepath.Join(savePath, name+".docx")); nil != err {
util.LogErrorf("export docx failed: %s", err)
logging.LogErrorf("export docx failed: %s", err)
return errors.New(fmt.Sprintf(Conf.Language(14), err))
}
tmpAssets := filepath.Join(tmpDir, "assets")
if gulu.File.IsDir(tmpAssets) {
if err = gulu.File.Copy(tmpAssets, filepath.Join(savePath, "assets")); nil != err {
util.LogErrorf("export docx failed: %s", err)
logging.LogErrorf("export docx failed: %s", err)
return errors.New(fmt.Sprintf(Conf.Language(14), err))
}
}
@ -200,7 +201,7 @@ func ExportMarkdownHTML(id, savePath string, docx bool) (name, dom string) {
name = path.Base(tree.HPath)
if err := os.MkdirAll(savePath, 0755); nil != err {
util.LogErrorf("mkdir [%s] failed: %s", savePath, err)
logging.LogErrorf("mkdir [%s] failed: %s", savePath, err)
return
}
@ -209,12 +210,12 @@ func ExportMarkdownHTML(id, savePath string, docx bool) (name, dom string) {
if strings.HasPrefix(asset, "assets/") {
srcAbsPath, err := GetAssetAbsPath(asset)
if nil != err {
util.LogWarnf("resolve path of asset [%s] failed: %s", asset, err)
logging.LogWarnf("resolve path of asset [%s] failed: %s", asset, err)
continue
}
targetAbsPath := filepath.Join(savePath, asset)
if err = gulu.File.Copy(srcAbsPath, targetAbsPath); nil != err {
util.LogWarnf("copy asset from [%s] to [%s] failed: %s", srcAbsPath, targetAbsPath, err)
logging.LogWarnf("copy asset from [%s] to [%s] failed: %s", srcAbsPath, targetAbsPath, err)
}
}
}
@ -224,7 +225,7 @@ func ExportMarkdownHTML(id, savePath string, docx bool) (name, dom string) {
from := filepath.Join(util.WorkingDir, src)
to := filepath.Join(savePath, src)
if err := gulu.File.Copy(from, to); nil != err {
util.LogWarnf("copy stage from [%s] to [%s] failed: %s", from, savePath, err)
logging.LogWarnf("copy stage from [%s] to [%s] failed: %s", from, savePath, err)
return
}
}
@ -238,7 +239,7 @@ func ExportMarkdownHTML(id, savePath string, docx bool) (name, dom string) {
from := filepath.Join(util.AppearancePath, src)
to := filepath.Join(savePath, "appearance", src)
if err := gulu.File.Copy(from, to); nil != err {
util.LogErrorf("copy appearance from [%s] to [%s] failed: %s", from, savePath, err)
logging.LogErrorf("copy appearance from [%s] to [%s] failed: %s", from, savePath, err)
return
}
}
@ -283,7 +284,7 @@ func ExportHTML(id, savePath string, pdf bool) (name, dom string) {
name = path.Base(tree.Path)
if err := os.MkdirAll(savePath, 0755); nil != err {
util.LogErrorf("mkdir [%s] failed: %s", savePath, err)
logging.LogErrorf("mkdir [%s] failed: %s", savePath, err)
return
}
@ -291,12 +292,12 @@ func ExportHTML(id, savePath string, pdf bool) (name, dom string) {
for _, asset := range assets {
srcAbsPath, err := GetAssetAbsPath(asset)
if nil != err {
util.LogWarnf("resolve path of asset [%s] failed: %s", asset, err)
logging.LogWarnf("resolve path of asset [%s] failed: %s", asset, err)
continue
}
targetAbsPath := filepath.Join(savePath, asset)
if err = gulu.File.Copy(srcAbsPath, targetAbsPath); nil != err {
util.LogWarnf("copy asset from [%s] to [%s] failed: %s", srcAbsPath, targetAbsPath, err)
logging.LogWarnf("copy asset from [%s] to [%s] failed: %s", srcAbsPath, targetAbsPath, err)
}
}
@ -307,7 +308,7 @@ func ExportHTML(id, savePath string, pdf bool) (name, dom string) {
from := filepath.Join(util.WorkingDir, src)
to := filepath.Join(savePath, src)
if err := gulu.File.Copy(from, to); nil != err {
util.LogErrorf("copy stage from [%s] to [%s] failed: %s", from, savePath, err)
logging.LogErrorf("copy stage from [%s] to [%s] failed: %s", from, savePath, err)
return
}
}
@ -321,7 +322,7 @@ func ExportHTML(id, savePath string, pdf bool) (name, dom string) {
from := filepath.Join(util.AppearancePath, src)
to := filepath.Join(savePath, "appearance", src)
if err := gulu.File.Copy(from, to); nil != err {
util.LogErrorf("copy appearance from [%s] to [%s] failed: %s", from, savePath, err)
logging.LogErrorf("copy appearance from [%s] to [%s] failed: %s", from, savePath, err)
return
}
}
@ -444,7 +445,7 @@ func AddPDFOutline(id, p string) (err error) {
outFile := inFile + ".tmp"
err = api.AddBookmarksFile(inFile, outFile, topBms, nil)
if nil != err {
util.LogErrorf("add bookmark failed: %s", err)
logging.LogErrorf("add bookmark failed: %s", err)
return
}
err = os.Rename(outFile, inFile)
@ -467,7 +468,7 @@ func CopyStdMarkdown(id string) string {
func ExportMarkdown(id string) (name, zipPath string) {
block := treenode.GetBlockTree(id)
if nil == block {
util.LogErrorf("not found block [%s]", id)
logging.LogErrorf("not found block [%s]", id)
return
}
@ -496,7 +497,7 @@ func BatchExportMarkdown(boxID, folderPath string) (zipPath string) {
} else {
block := treenode.GetBlockTreeRootByHPath(box.ID, folderPath)
if nil == block {
util.LogErrorf("not found block")
logging.LogErrorf("not found block")
return
}
baseFolderName = path.Base(block.HPath)
@ -527,7 +528,7 @@ func exportMarkdownZip(boxID, baseFolderName string, docPaths []string) (zipPath
exportFolder := filepath.Join(util.TempDir, "export", baseFolderName)
if err := os.MkdirAll(exportFolder, 0755); nil != err {
util.LogErrorf("create export temp folder failed: %s", err)
logging.LogErrorf("create export temp folder failed: %s", err)
return
}
@ -553,11 +554,11 @@ func exportMarkdownZip(boxID, baseFolderName string, docPaths []string) (zipPath
}
writeFolder := filepath.Dir(writePath)
if err := os.MkdirAll(writeFolder, 0755); nil != err {
util.LogErrorf("create export temp folder [%s] failed: %s", writeFolder, err)
logging.LogErrorf("create export temp folder [%s] failed: %s", writeFolder, err)
continue
}
if err := gulu.File.WriteFileSafer(writePath, gulu.Str.ToBytes(md), 0644); nil != err {
util.LogErrorf("write export markdown file [%s] failed: %s", writePath, err)
logging.LogErrorf("write export markdown file [%s] failed: %s", writePath, err)
continue
}
@ -573,7 +574,7 @@ func exportMarkdownZip(boxID, baseFolderName string, docPaths []string) (zipPath
srcPath, err := GetAssetAbsPath(asset)
if nil != err {
util.LogWarnf("get asset [%s] abs path failed: %s", asset, err)
logging.LogWarnf("get asset [%s] abs path failed: %s", asset, err)
continue
}
@ -584,7 +585,7 @@ func exportMarkdownZip(boxID, baseFolderName string, docPaths []string) (zipPath
err = gulu.File.CopyFile(srcPath, destPath)
}
if nil != err {
util.LogErrorf("copy asset from [%s] to [%s] failed: %s", srcPath, destPath, err)
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", srcPath, destPath, err)
continue
}
}
@ -593,17 +594,17 @@ func exportMarkdownZip(boxID, baseFolderName string, docPaths []string) (zipPath
zipPath = exportFolder + ".zip"
zip, err := gulu.Zip.Create(zipPath)
if nil != err {
util.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
logging.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
return ""
}
if err = zip.AddDirectory(baseFolderName, exportFolder); nil != err {
util.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
logging.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
return ""
}
if err = zip.Close(); nil != err {
util.LogErrorf("close export markdown zip failed: %s", err)
logging.LogErrorf("close export markdown zip failed: %s", err)
}
os.RemoveAll(exportFolder)
@ -624,7 +625,7 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (
exportFolder := filepath.Join(util.TempDir, "export", baseFolderName)
if err := os.MkdirAll(exportFolder, 0755); nil != err {
util.LogErrorf("create export temp folder failed: %s", err)
logging.LogErrorf("create export temp folder failed: %s", err)
return
}
@ -657,7 +658,7 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (
readPath := filepath.Join(util.DataDir, tree.Box, tree.Path)
data, readErr := filelock.NoLockFileRead(readPath)
if nil != readErr {
util.LogErrorf("read file [%s] failed: %s", readPath, readErr)
logging.LogErrorf("read file [%s] failed: %s", readPath, readErr)
continue
}
@ -665,11 +666,11 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (
writePath = filepath.Join(exportFolder, writePath)
writeFolder := filepath.Dir(writePath)
if mkdirErr := os.MkdirAll(writeFolder, 0755); nil != mkdirErr {
util.LogErrorf("create export temp folder [%s] failed: %s", writeFolder, mkdirErr)
logging.LogErrorf("create export temp folder [%s] failed: %s", writeFolder, mkdirErr)
continue
}
if writeErr := os.WriteFile(writePath, data, 0644); nil != writeErr {
util.LogErrorf("write export file [%s] failed: %s", writePath, writeErr)
logging.LogErrorf("write export file [%s] failed: %s", writePath, writeErr)
continue
}
}
@ -679,14 +680,14 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (
readPath := filepath.Join(util.DataDir, tree.Box, tree.Path)
data, readErr := filelock.NoLockFileRead(readPath)
if nil != readErr {
util.LogErrorf("read file [%s] failed: %s", readPath, readErr)
logging.LogErrorf("read file [%s] failed: %s", readPath, readErr)
continue
}
writePath := strings.TrimPrefix(tree.Path, rootDirPath)
writePath = filepath.Join(exportFolder, treeID+".sy")
if writeErr := os.WriteFile(writePath, data, 0644); nil != writeErr {
util.LogErrorf("write export file [%s] failed: %s", writePath, writeErr)
logging.LogErrorf("write export file [%s] failed: %s", writePath, writeErr)
continue
}
}
@ -713,7 +714,7 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (
srcPath, assetErr := GetAssetAbsPath(asset)
if nil != assetErr {
util.LogWarnf("get asset [%s] abs path failed: %s", asset, assetErr)
logging.LogWarnf("get asset [%s] abs path failed: %s", asset, assetErr)
continue
}
@ -724,7 +725,7 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (
assetErr = gulu.File.CopyFile(srcPath, destPath)
}
if nil != assetErr {
util.LogErrorf("copy asset from [%s] to [%s] failed: %s", srcPath, destPath, assetErr)
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", srcPath, destPath, assetErr)
continue
}
@ -735,17 +736,17 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (
zipPath = exportFolder + ".sy.zip"
zip, err := gulu.Zip.Create(zipPath)
if nil != err {
util.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
logging.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
return ""
}
if err = zip.AddDirectory(baseFolderName, exportFolder); nil != err {
util.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
logging.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
return ""
}
if err = zip.Close(); nil != err {
util.LogErrorf("close export markdown zip failed: %s", err)
logging.LogErrorf("close export markdown zip failed: %s", err)
}
os.RemoveAll(exportFolder)
@ -987,24 +988,24 @@ func exportTree(tree *parse.Tree, wysiwyg bool) (ret *parse.Tree) {
p := refID[:strings.LastIndex(refID, "/")]
absPath, err := GetAssetAbsPath(p)
if nil != err {
util.LogWarnf("get assets abs path by rel path [%s] failed: %s", p, err)
logging.LogWarnf("get assets abs path by rel path [%s] failed: %s", p, err)
return ast.WalkSkipChildren
}
sya := absPath + ".sya"
syaData, err := os.ReadFile(sya)
if nil != err {
util.LogErrorf("read file [%s] failed: %s", sya, err)
logging.LogErrorf("read file [%s] failed: %s", sya, err)
return ast.WalkSkipChildren
}
syaJSON := map[string]interface{}{}
if err = gulu.JSON.UnmarshalJSON(syaData, &syaJSON); nil != err {
util.LogErrorf("unmarshal file [%s] failed: %s", sya, err)
logging.LogErrorf("unmarshal file [%s] failed: %s", sya, err)
return ast.WalkSkipChildren
}
annotationID := refID[strings.LastIndex(refID, "/")+1:]
annotationData := syaJSON[annotationID]
if nil == annotationData {
util.LogErrorf("not found annotation [%s] in .sya", annotationID)
logging.LogErrorf("not found annotation [%s] in .sya", annotationID)
return ast.WalkSkipChildren
}
pages := annotationData.(map[string]interface{})["pages"].([]interface{})
@ -1293,7 +1294,7 @@ func collectFootnotesDefs(id string, refFootnotes *[]*refAsFootnotes, treeCache
}
node := treenode.GetNodeInTree(t, b.ID)
if nil == node {
util.LogErrorf("not found node [%s] in tree [%s]", b.ID, t.Root.ID)
logging.LogErrorf("not found node [%s] in tree [%s]", b.ID, t.Root.ID)
return
}
collectFootnotesDefs0(node, refFootnotes, treeCache, depth)