Skip to content

Commit

Permalink
feat: remark renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Mar 3, 2023
1 parent bcf1ce3 commit 6d809fb
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 78 deletions.
14 changes: 0 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,16 @@ GOTEST = $(GOCMD) test
MARP_VER = v2.4.0

build_linux:
wget https://github.com/marp-team/marp-cli/releases/download/${MARP_VER}/marp-cli-${MARP_VER}-linux.tar.gz -O ./assets/marp-cli.tar.gz
tar -xvf ./assets/marp-cli.tar.gz -C ./assets
rm ./assets/marp-cli.tar.gz
chmod +x ./assets/marp
GOOS=linux GOARCH=amd64 ${GOCMD} build -o cgp_linux ./cmd/cgp
zip cgp_linux.zip cgp_linux
rm ./assets/marp

build_windows:
wget https://github.com/marp-team/marp-cli/releases/download/${MARP_VER}/marp-cli-${MARP_VER}-win.zip -O ./assets/marp-cli.zip
unzip ./assets/marp-cli.zip -d ./assets
rm ./assets/marp-cli.zip
GOOS=windows GOARCH=amd64 ${GOCMD} build -o cgp_windows ./cmd/cgp
zip cgp_windows.zip cgp_windows
rm ./assets/marp.exe

build_macos:
wget https://github.com/marp-team/marp-cli/releases/download/${MARP_VER}/marp-cli-${MARP_VER}-mac.tar.gz -O ./assets/marp-cli.tar.gz
tar -xvf ./assets/marp-cli.tar.gz -C ./assets
rm ./assets/marp-cli.tar.gz
chmod +x ./assets/marp
GOOS=darwin GOARCH=amd64 ${GOCMD} build -o cgp_macos ./cmd/cgp
zip cgp_macos.zip cgp_macos
rm ./assets/marp

test:
$(GOTEST) ./...
6 changes: 0 additions & 6 deletions assets/embed.go

This file was deleted.

10 changes: 1 addition & 9 deletions cmd/cgp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"strings"

cgp "github.com/williamfzc/chat-gpt-ppt"
"github.com/williamfzc/chat-gpt-ppt/assets"
_ "github.com/williamfzc/chat-gpt-ppt/assets"
)

func main() {
Expand Down Expand Up @@ -40,13 +38,7 @@ func main() {
// renderer
logger.Println("start rendering")

renderer := cgp.NewMarpRenderer()
tmpF, err := os.CreateTemp(os.TempDir(), "tmpMarp*")
panicIfErr(err)
defer os.Remove(tmpF.Name())
_ = os.WriteFile(tmpF.Name(), assets.Static, 0755)
renderer.SetBinPath(tmpF.Name())

renderer := cgp.NewRemarkRenderer()
for _, eachTopic := range topics {
renderer.AddTopic(eachTopic)
}
Expand Down
49 changes: 0 additions & 49 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package chat_gpt_ppt

import (
"fmt"
"os"
"os/exec"
"strings"
)

type Topic struct {
Expand All @@ -25,49 +22,3 @@ type Renderer interface {
RenderFile(outputPath string) error
SetBinPath(path string)
}

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
}
53 changes: 53 additions & 0 deletions renderer_marp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package chat_gpt_ppt

import (
"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
}
75 changes: 75 additions & 0 deletions renderer_remark.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package chat_gpt_ppt

import (
"fmt"
"os"
"strings"
)

const RemarkTemplate = `
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta charset="utf-8">
<style>
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
body { font-family: 'Droid Serif'; }
h1, h2, h3 {
font-family: 'Yanone Kaffeesatz';
font-weight: normal;
}
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
</style>
</head>
<body>
<textarea id="source">
class: center, middle
%s
</textarea>
<script src="https://remarkjs.com/downloads/remark-latest.min.js">
</script>
<script>
var slideshow = remark.create();
</script>
</body>
</html>
`

type RemarkRenderer struct {
Topics []*Topic
}

func NewRemarkRenderer() Renderer {
return &RemarkRenderer{
Topics: make([]*Topic, 0),
}
}

func (r *RemarkRenderer) AddTopic(topic *Topic) {
r.Topics = append(r.Topics, topic)
}

func (r *RemarkRenderer) RenderFile(outputPath string) error {
parts := make([]string, 0)
for _, each := range r.Topics {
parts = append(parts, each.ToMarkdown())
}
final := strings.Join(parts, "\n---\n")

outputContent := fmt.Sprintf(RemarkTemplate, final)
err := os.WriteFile(outputPath, []byte(outputContent), 0644)
if err != nil {
return err
}
return nil
}

func (r *RemarkRenderer) SetBinPath(_ string) {
}

0 comments on commit 6d809fb

Please sign in to comment.