forked from williamfzc/chat-gpt-ppt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer_marp.go
58 lines (49 loc) · 1.04 KB
/
renderer_marp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package chat_gpt_ppt
import (
"fmt"
"os"
"os/exec"
"strings"
)
type MarpRenderer struct {
BinPath string
Topics []*Topic
}
func NewMarpRenderer() Renderer {
return &MarpRenderer{
BinPath: "",
Topics: make([]*Topic, 0),
}
}
func (m *MarpRenderer) SetBinPath(path string) {
m.BinPath = path
}
func (m *MarpRenderer) AddTopic(topic *Topic) {
m.Topics = append(m.Topics, topic)
}
func (m *MarpRenderer) RenderFile(outputPath string) error {
parts := make([]string, 0)
for _, each := range m.Topics {
parts = append(parts, each.ToMarkdown())
}
final := strings.Join(parts, "\n---\n")
// call marp
file, err := os.CreateTemp(os.TempDir(), "cgp*.md")
if err != nil {
return err
}
defer os.Remove(file.Name())
err = os.WriteFile(file.Name(), []byte(final), 0644)
if err != nil {
return err
}
command := exec.Command(m.BinPath, file.Name(), "-o", outputPath)
err = command.Run()
if err != nil {
return err
}
return nil
}
func (m *MarpRenderer) RenderString() (string, error) {
return "", fmt.Errorf("not supported")
}