feat(tester): integrate pandoc
This commit is contained in:
parent
ffacc9e3ac
commit
94fc50e272
9 changed files with 315 additions and 105 deletions
|
@ -4,8 +4,11 @@ import (
|
|||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
|
@ -15,6 +18,7 @@ type Client struct {
|
|||
|
||||
type PandocClient interface {
|
||||
ConvertLatexToHtml5(ctx context.Context, text string) (string, error)
|
||||
BatchConvertLatexToHtml5(ctx context.Context, texts []string) ([]string, error)
|
||||
}
|
||||
|
||||
func NewPandocClient(client *http.Client, address string) *Client {
|
||||
|
@ -24,44 +28,126 @@ func NewPandocClient(client *http.Client, address string) *Client {
|
|||
}
|
||||
}
|
||||
|
||||
type convertRequest struct {
|
||||
type conversation struct {
|
||||
Text string `json:"text"`
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
Math string `json:"html-math-method"`
|
||||
}
|
||||
|
||||
func (client *Client) convert(ctx context.Context, text, from, to string) (string, error) {
|
||||
body, err := json.Marshal(convertRequest{
|
||||
Text: text,
|
||||
From: from,
|
||||
To: to,
|
||||
})
|
||||
type message struct {
|
||||
Message string `json:"message"`
|
||||
Verbosity string `json:"verbosity"`
|
||||
}
|
||||
|
||||
type output struct {
|
||||
Error string `json:"error"`
|
||||
Output string `json:"output"`
|
||||
Base64 bool `json:"base64"`
|
||||
Messages []message `json:"messages"`
|
||||
}
|
||||
|
||||
func (client *Client) sendRaw(ctx context.Context, path string, body []byte) ([]byte, error) {
|
||||
path, err := url.JoinPath(client.address, path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(body)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, client.address, buf)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path, buf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := client.client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func (client *Client) convert(ctx context.Context, text, from, to, math string) (string, error) {
|
||||
body, err := json.Marshal(conversation{
|
||||
Text: text,
|
||||
From: from,
|
||||
To: to,
|
||||
Math: math,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(body), nil
|
||||
resp, err := client.sendRaw(ctx, "/", body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(resp), nil
|
||||
}
|
||||
|
||||
func (client *Client) batchConvert(ctx context.Context, texts []string, from, to, math string) ([]string, error) {
|
||||
list := make([]conversation, len(texts))
|
||||
for i, text := range texts {
|
||||
list[i] = conversation{
|
||||
Text: text,
|
||||
From: from,
|
||||
To: to,
|
||||
Math: math,
|
||||
}
|
||||
}
|
||||
|
||||
body, err := json.Marshal(list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := client.sendRaw(ctx, "/batch", body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []output
|
||||
err = json.Unmarshal(resp, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(result) != len(texts) {
|
||||
return nil, fmt.Errorf("wrong number of fieilds returned: %d", len(result))
|
||||
}
|
||||
|
||||
err = nil
|
||||
for _, o := range result {
|
||||
if o.Error != "" {
|
||||
err = errors.Join(err, errors.New(o.Error))
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, Wrap(ErrBadInput, err, "BatchConvertLatexToHtml5", "invalid input")
|
||||
}
|
||||
|
||||
res := make([]string, len(result))
|
||||
for i, o := range result {
|
||||
res[i] = o.Output
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (client *Client) ConvertLatexToHtml5(ctx context.Context, text string) (string, error) {
|
||||
return client.convert(ctx, text, "latex", "html5")
|
||||
return client.convert(ctx, text, "latex", "html5", "katex")
|
||||
}
|
||||
|
||||
func (client *Client) BatchConvertLatexToHtml5(ctx context.Context, texts []string) ([]string, error) {
|
||||
return client.batchConvert(ctx, texts, "latex", "html5", "katex")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue