mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-02 11:50:06 +08:00
🎨 Provide API /api/convert/pandoc
https://github.com/siyuan-note/siyuan/issues/8235
This commit is contained in:
parent
5c06d60f05
commit
0bfc2b5c64
44
API.md
44
API.md
@ -44,6 +44,8 @@
|
|||||||
* [List files](#List-files)
|
* [List files](#List-files)
|
||||||
* [Export](#Export)
|
* [Export](#Export)
|
||||||
* [Export Markdown](#Export-Markdown)
|
* [Export Markdown](#Export-Markdown)
|
||||||
|
* [Conversion](#Conversion)
|
||||||
|
* [Pandoc](#Pandoc)
|
||||||
* [Notification](#Notification)
|
* [Notification](#Notification)
|
||||||
* [Push message](#Push-message)
|
* [Push message](#Push-message)
|
||||||
* [Push error message](#Push-error-message)
|
* [Push error message](#Push-error-message)
|
||||||
@ -710,9 +712,10 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
* `id`: Block ID to move
|
* `id`: Block ID to move
|
||||||
* `previousID`: The ID of the previous block, used to anchor the insertion position
|
* `previousID`: The ID of the previous block, used to anchor the insertion position
|
||||||
* `parentID`: The ID of the parent block, used to anchor the insertion position, `previousID` and `parentID` cannot be empty at the same time, if they exist at the same time, `previousID` will be used first
|
* `parentID`: The ID of the parent block, used to anchor the insertion position, `previousID` and `parentID` cannot
|
||||||
|
be empty at the same time, if they exist at the same time, `previousID` will be used first
|
||||||
* Return value
|
* Return value
|
||||||
|
|
||||||
```json
|
```json
|
||||||
@ -1014,6 +1017,41 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
|
|||||||
* `hPath`: human-readable path
|
* `hPath`: human-readable path
|
||||||
* `content`: Markdown content
|
* `content`: Markdown content
|
||||||
|
|
||||||
|
## Conversion
|
||||||
|
|
||||||
|
### Pandoc
|
||||||
|
|
||||||
|
* `/api/convert/pandoc`
|
||||||
|
* Working directory
|
||||||
|
* Executing the pandoc command will set the working directory to `workspace/temp/convert/pandoc/`
|
||||||
|
* API [`Put file`](#put-file) can be used to write the file to be converted to this directory first
|
||||||
|
* Then call the API for conversion, and the converted file will also be written to this directory
|
||||||
|
* Finally, call the API [`Get file`](#get-file) to get the converted file
|
||||||
|
* Or call the API [Create a document with Markdown](#Create-a-document-with-Markdown)
|
||||||
|
* Or call the internal API `importStdMd` to import the converted folder directly
|
||||||
|
* Parameters
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"args": [
|
||||||
|
"--to", "markdown_strict-raw_html",
|
||||||
|
"foo.epub",
|
||||||
|
"-o", "foo.md"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* `args`: Pandoc command line parameters
|
||||||
|
* Return value
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 0,
|
||||||
|
"msg": "",
|
||||||
|
"data": null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Notification
|
## Notification
|
||||||
|
|
||||||
### Push message
|
### Push message
|
||||||
|
37
API_zh_CN.md
37
API_zh_CN.md
@ -44,6 +44,8 @@
|
|||||||
* [列出文件](#列出文件)
|
* [列出文件](#列出文件)
|
||||||
* [导出](#导出)
|
* [导出](#导出)
|
||||||
* [导出 Markdown 文本](#导出-markdown-文本)
|
* [导出 Markdown 文本](#导出-markdown-文本)
|
||||||
|
* [转换](#转换)
|
||||||
|
* [Pandoc](#Pandoc)
|
||||||
* [通知](#通知)
|
* [通知](#通知)
|
||||||
* [推送消息](#推送消息)
|
* [推送消息](#推送消息)
|
||||||
* [推送报错消息](#推送报错消息)
|
* [推送报错消息](#推送报错消息)
|
||||||
@ -1008,6 +1010,41 @@
|
|||||||
* `hPath`:人类可读的路径
|
* `hPath`:人类可读的路径
|
||||||
* `content`:Markdown 内容
|
* `content`:Markdown 内容
|
||||||
|
|
||||||
|
## 转换
|
||||||
|
|
||||||
|
### Pandoc
|
||||||
|
|
||||||
|
* `/api/convert/pandoc`
|
||||||
|
* 工作目录
|
||||||
|
* 执行调用 pandoc 命令时工作目录会被设置在 `工作空间/temp/convert/pandoc/` 下
|
||||||
|
* 可先通过 API [`写入文件`](#写入文件) 将待转换文件写入该目录
|
||||||
|
* 然后再调用该 API 进行转换,转换后的文件也会被写入该目录
|
||||||
|
* 最后调用 API [`获取文件`](#获取文件) 获取转换后的文件内容
|
||||||
|
* 或者调用 API [`通过 Markdown 创建文档`](#通过-markdown-创建文档)
|
||||||
|
* 或者调用内部 API `importStdMd` 将转换后的文件夹直接导入
|
||||||
|
* 参数
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"args": [
|
||||||
|
"--to", "markdown_strict-raw_html",
|
||||||
|
"foo.epub",
|
||||||
|
"-o", "foo.md"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* `args`:Pandoc 命令行参数
|
||||||
|
* 返回值
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 0,
|
||||||
|
"msg": "",
|
||||||
|
"data": null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## 通知
|
## 通知
|
||||||
|
|
||||||
### 推送消息
|
### 推送消息
|
||||||
|
49
kernel/api/pandoc.go
Normal file
49
kernel/api/pandoc.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
// 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 api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/88250/gulu"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/siyuan-note/siyuan/kernel/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
func pandoc(c *gin.Context) {
|
||||||
|
ret := gulu.Ret.NewResult()
|
||||||
|
defer c.JSON(http.StatusOK, ret)
|
||||||
|
|
||||||
|
arg, ok := util.JsonArg(c, ret)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pandocArgs := arg["args"].([]interface{})
|
||||||
|
var args []string
|
||||||
|
for _, v := range pandocArgs {
|
||||||
|
args = append(args, v.(string))
|
||||||
|
}
|
||||||
|
|
||||||
|
err := util.ConvertPandoc(args...)
|
||||||
|
if nil != err {
|
||||||
|
ret.Code = -1
|
||||||
|
ret.Msg = err.Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
@ -259,6 +259,8 @@ func ServeAPI(ginServer *gin.Engine) {
|
|||||||
ginServer.Handle("POST", "/api/import/importData", model.CheckAuth, model.CheckReadonly, importData)
|
ginServer.Handle("POST", "/api/import/importData", model.CheckAuth, model.CheckReadonly, importData)
|
||||||
ginServer.Handle("POST", "/api/import/importSY", model.CheckAuth, model.CheckReadonly, importSY)
|
ginServer.Handle("POST", "/api/import/importSY", model.CheckAuth, model.CheckReadonly, importSY)
|
||||||
|
|
||||||
|
ginServer.Handle("POST", "/api/convert/pandoc", model.CheckAuth, model.CheckReadonly, pandoc)
|
||||||
|
|
||||||
ginServer.Handle("POST", "/api/template/render", model.CheckAuth, renderTemplate)
|
ginServer.Handle("POST", "/api/template/render", model.CheckAuth, renderTemplate)
|
||||||
ginServer.Handle("POST", "/api/template/docSaveAsTemplate", model.CheckAuth, model.CheckReadonly, docSaveAsTemplate)
|
ginServer.Handle("POST", "/api/template/docSaveAsTemplate", model.CheckAuth, model.CheckReadonly, docSaveAsTemplate)
|
||||||
ginServer.Handle("POST", "/api/template/renderSprig", model.CheckAuth, model.CheckReadonly, renderSprig)
|
ginServer.Handle("POST", "/api/template/renderSprig", model.CheckAuth, model.CheckReadonly, renderSprig)
|
||||||
|
@ -18,6 +18,8 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -26,6 +28,27 @@ import (
|
|||||||
"github.com/siyuan-note/logging"
|
"github.com/siyuan-note/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func ConvertPandoc(args ...string) (err error) {
|
||||||
|
if "" == PandocBinPath || ContainerStd != Container {
|
||||||
|
return errors.New("not found executable pandoc")
|
||||||
|
}
|
||||||
|
|
||||||
|
pandoc := exec.Command(PandocBinPath, args...)
|
||||||
|
gulu.CmdAttr(pandoc)
|
||||||
|
dir := filepath.Join(WorkspaceDir, "temp", "convert", "pandoc")
|
||||||
|
if err = os.MkdirAll(dir, 0755); nil != err {
|
||||||
|
logging.LogErrorf("mkdir [%s] failed: [%s]", dir, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pandoc.Dir = dir
|
||||||
|
output, err := pandoc.CombinedOutput()
|
||||||
|
if nil != err {
|
||||||
|
logging.LogErrorf("pandoc convert output [%s]", string(output))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func Pandoc(from, to, o, content string) (ret string, err error) {
|
func Pandoc(from, to, o, content string) (ret string, err error) {
|
||||||
if "" == from || "" == to || "md" == to {
|
if "" == from || "" == to || "md" == to {
|
||||||
ret = content
|
ret = content
|
||||||
|
Loading…
Reference in New Issue
Block a user