🎨 桌面端内置 Pandoc 可执行文件 https://github.com/siyuan-note/siyuan/issues/5835

This commit is contained in:
Liang Ding 2022-09-07 10:30:25 +08:00
parent ffa53aa4f1
commit d134045a60
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
13 changed files with 91 additions and 19 deletions

View file

@ -17,6 +17,7 @@
package util
import (
"bytes"
"flag"
"log"
"math/rand"
@ -101,6 +102,7 @@ func Boot() {
initPathDir()
checkPort()
go initPandoc()
bootBanner := figure.NewColorFigure("SiYuan", "isometric3", "green", true)
logging.LogInfof("\n" + bootBanner.String())
@ -161,6 +163,7 @@ var (
DBPath string // SQLite 数据库文件路径
HistoryDBPath string // SQLite 历史数据库文件路径
BlockTreePath string // 区块树文件路径
PandocBinPath string // Pandoc 可执行文件路径
AppearancePath string // 配置目录下的外观目录 appearance/ 路径
ThemesPath string // 配置目录下的外观目录下的 themes/ 路径
IconsPath string // 配置目录下的外观目录下的 icons/ 路径
@ -454,3 +457,76 @@ func PidByPort(port string) (ret string) {
}
return
}
func initPandoc() {
if ContainerStd != Container {
return
}
pandocDir := filepath.Join(TempDir, "pandoc")
if gulu.OS.IsWindows() {
PandocBinPath = filepath.Join(pandocDir, "bin", "pandoc.exe")
} else if gulu.OS.IsDarwin() || gulu.OS.IsLinux() {
PandocBinPath = filepath.Join(pandocDir, "bin", "pandoc")
}
pandocVer := getPandocVer(PandocBinPath)
if "" != pandocVer {
logging.LogInfof("pandoc [ver=%s, bin=%s]", pandocVer, PandocBinPath)
return
}
pandocZip := filepath.Join(WorkingDir, "pandoc.zip")
if "dev" == Mode {
if gulu.OS.IsWindows() {
pandocZip = filepath.Join(WorkingDir, "pandoc/pandoc-windows-amd64.zip")
} else if gulu.OS.IsDarwin() {
pandocZip = filepath.Join(WorkingDir, "pandoc/pandoc-darwin-amd64.zip")
} else if gulu.OS.IsLinux() {
pandocZip = filepath.Join(WorkingDir, "pandoc/pandoc-linux-amd64.zip")
}
}
if err := gulu.Zip.Unzip(pandocZip, pandocDir); nil != err {
logging.LogErrorf("unzip pandoc failed: %s", err)
return
}
if gulu.OS.IsDarwin() || gulu.OS.IsLinux() {
exec.Command("chmod", "+x", PandocBinPath).CombinedOutput()
}
pandocVer = getPandocVer(PandocBinPath)
logging.LogInfof("initialized pandoc [ver=%s, bin=%s]", pandocVer, PandocBinPath)
}
func getPandocVer(binPath string) (ret string) {
if "" == binPath {
return
}
cmd := exec.Command(binPath, "--version")
CmdAttr(cmd)
data, err := cmd.CombinedOutput()
if nil == err && strings.HasPrefix(string(data), "pandoc") {
parts := bytes.Split(data, []byte("\n"))
if 0 < len(parts) {
ret = strings.TrimPrefix(string(parts[0]), "pandoc")
ret = strings.ReplaceAll(ret, ".exe", "")
ret = strings.TrimSpace(ret)
}
return
}
return
}
func IsValidPandocBin(binPath string) bool {
if "" == binPath {
return false
}
cmd := exec.Command(binPath, "--version")
CmdAttr(cmd)
data, err := cmd.CombinedOutput()
if nil == err && strings.HasPrefix(string(data), "pandoc") {
return true
}
return false
}