feat: add pandoc
This commit is contained in:
parent
5832e83460
commit
b35fd29049
5 changed files with 227 additions and 93 deletions
61
internal/lib/pandoc.go
Normal file
61
internal/lib/pandoc.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type PandocClient struct {
|
||||
client *http.Client
|
||||
address string
|
||||
}
|
||||
|
||||
func NewPandocClient(client *http.Client, address string) *PandocClient {
|
||||
return &PandocClient{
|
||||
client: client,
|
||||
address: address,
|
||||
}
|
||||
}
|
||||
|
||||
type convertRequest struct {
|
||||
Text string `json:"text"`
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
}
|
||||
|
||||
func (client *PandocClient) convert(text, from, to string) (string, error) {
|
||||
body, err := json.Marshal(convertRequest{
|
||||
Text: text,
|
||||
From: from,
|
||||
To: to,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(body)
|
||||
|
||||
resp, err := client.client.Post(client.address, "application/json", buf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", err
|
||||
}
|
||||
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
func (client *PandocClient) ConvertLatexToHtml5(text string) (string, error) {
|
||||
return client.convert(text, "latex", "html5")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue