Skip to content

Commit

Permalink
🐛 Fix pandoc export
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Jun 19, 2023
1 parent ca01112 commit b2c0f3e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions kernel/model/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ func clearCorruptedNotebooks() {
func clearWorkspaceTemp() {
os.RemoveAll(filepath.Join(util.TempDir, "bazaar"))
os.RemoveAll(filepath.Join(util.TempDir, "export"))
os.RemoveAll(filepath.Join(util.TempDir, "convert"))
os.RemoveAll(filepath.Join(util.TempDir, "import"))
os.RemoveAll(filepath.Join(util.TempDir, "repo"))
os.RemoveAll(filepath.Join(util.TempDir, "os"))
Expand Down
9 changes: 1 addition & 8 deletions kernel/model/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2071,18 +2071,11 @@ func exportPandocConvertZip(boxID, baseFolderName string, docPaths []string,
}

// 调用 Pandoc 进行格式转换
output, err := util.Pandoc(pandocFrom, pandocTo, writePath, md)
err := util.Pandoc(pandocFrom, pandocTo, writePath, md)
if nil != err {
logging.LogErrorf("pandoc failed: %s", err)
continue
}

if "odt" != pandocTo && "epub" != pandocTo && "rtf" != pandocTo {
if err := gulu.File.WriteFileSafer(writePath, gulu.Str.ToBytes(output), 0644); nil != err {
logging.LogErrorf("write export markdown file [%s] failed: %s", writePath, err)
continue
}
}
}

zipPath = exportFolder + ".zip"
Expand Down
25 changes: 16 additions & 9 deletions kernel/util/pandoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func ConvertPandoc(args ...string) (err error) {

pandoc := exec.Command(PandocBinPath, args...)
gulu.CmdAttr(pandoc)
dir := filepath.Join(WorkspaceDir, "temp", "convert", "pandoc")
dir := filepath.Join(WorkspaceDir, "temp", "convert", "pandoc", gulu.Rand.String(7))

This comment has been minimized.

Copy link
@Zuoqiu-Yingyi

Zuoqiu-Yingyi Jun 26, 2023

Contributor

这里是 /api/convert/pandoc 的处理函数, 此处新建了一个目录作为 pandoc 的工作目录, 但是没有任何该目录的相关信息, 导致 API 调用后无法对其进行后续处理

优化方案一

/api/convert/pandoc 请求体新增一个可选参数 pathName, 该参数指定目录名称, 若未设置则使用随机的目录名, pandoc 使用 workspace/temp/convert/pandoc/pathName 作为其工作目录, 同时将该目录相对于思源工作空间根目录的相对路径作为响应体 data.path 返回

优化方案二

/api/convert/pandoc 请求体新增一个可选参数 path, 该参数指定相对于思源工作空间目录的相对目录, 若未设置则使用 workspace/temp/convert/pandoc/随机目录名 目录, pandoc 使用该目录作为其工作目录, 同时将该目录相对于思源工作空间根目录的相对路径作为响应体 data.path 返回

This comment has been minimized.

Copy link
@terwer

terwer Jun 26, 2023

Contributor

@88250 我暂时用临时方案兼容了。siyuan-plugin-importer
但是还是建议采纳 @Zuoqiu-Yingyi 的建议。
我个人倾向于方案一,改动小。

This comment has been minimized.

Copy link
@88250

88250 Jun 26, 2023

Author Member

收到,稍后改进。

if err = os.MkdirAll(dir, 0755); nil != err {
logging.LogErrorf("mkdir [%s] failed: [%s]", dir, err)
return
Expand All @@ -50,31 +50,38 @@ func ConvertPandoc(args ...string) (err error) {
return
}

func Pandoc(from, to, o, content string) (ret string, err error) {
func Pandoc(from, to, o, content string) (err error) {
if "" == from || "" == to || "md" == to {
ret = content
return
}

dir := filepath.Join(WorkspaceDir, "temp", "convert", "pandoc", gulu.Rand.String(7))
if err = os.MkdirAll(dir, 0755); nil != err {
logging.LogErrorf("mkdir [%s] failed: [%s]", dir, err)
return
}
tmpPath := filepath.Join(dir, gulu.Rand.String(7))
if err = os.WriteFile(tmpPath, []byte(content), 0644); nil != err {
logging.LogErrorf("write file failed: [%s]", err)
return
}

args := []string{
tmpPath,
"--from", from,
"--to", to,
"--resource-path", filepath.Dir(o),
"-s",
}

if "" != o {
args = append(args, "-o", o)
"-o", o,
}

pandoc := exec.Command(PandocBinPath, args...)
gulu.CmdAttr(pandoc)
pandoc.Stdin = bytes.NewBufferString(content)
output, err := pandoc.CombinedOutput()
if nil != err {
logging.LogErrorf("pandoc convert output [%s], error [%s]", string(output), err)
return
}
ret = string(output)
return
}

Expand Down

0 comments on commit b2c0f3e

Please sign in to comment.