Skip to content

Commit

Permalink
Merge pull request #15 from fasibio/export
Browse files Browse the repository at this point in the history
added export command
  • Loading branch information
cdreier authored Dec 29, 2021
2 parents 71bb72c + 7c4fa0f commit d4ea031
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
slide-serve
slide-serve.exe
pkged.go
pkged.go
export
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,21 @@ if something wont fit with your presentation styles, you can overwrite the print
}
```

# export

to export as static html file you can use the subcommand export ```slide-serve [global Flags] export [?dest="Destination folder"]```

```
NAME:
slide-serve export - export slides
USAGE:
slide-serve export [command options] [arguments...]
OPTIONS:
--dest value destination folder (default: "export")
```

# cli flags

-dev
Expand Down
119 changes: 114 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"bufio"
"bytes"
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
Expand All @@ -29,6 +32,8 @@ type holder struct {
slideRatio string
devCon *websocket.Conn
presenterCon *websocket.Conn
addWebsocket bool
imageRootUrl string
}

func main() {
Expand Down Expand Up @@ -75,16 +80,89 @@ func main() {
Usage: "on default you only navigate with arrow keys, this enabled 'next slide' on click",
},
}
app.Commands = []cli.Command{
{
Name: "export",
Usage: "export slides",
Action: export,
Flags: []cli.Flag{
cli.StringFlag{
Name: "dest",
Value: "export",
Usage: "destination folder",
},
},
},
}

if err := app.Run(os.Args); err != nil {
log.Fatal("cannot start slide-serve server! ", err.Error())
}

}
func copyAllFileToFolderNotIncludeExtension(folder string, dest string, ext string) error {
files, err := ioutil.ReadDir(folder)
if err != nil {
return err
}
for _, f := range files {
if strings.HasSuffix(f.Name(), ext) {
continue
}
src := folder + "/" + f.Name()
dst := dest + "/" + f.Name()
if err := copyFile(src, dst); err != nil {
return err
}
}
return nil
}

func copyFile(src, dest string) error {
data, err := ioutil.ReadFile(src)
if err != nil {
log.Fatal(err)
}
return ioutil.WriteFile(dest, data, 0644)
}

func export(c *cli.Context) error {
rootDir := c.GlobalString("dir")
dest := c.String("dest")
log.Println("start export")
if !dirExist(rootDir) {
return errors.New("cannot find root directory :(")
}
h := holder{
dir: rootDir,
title: c.GlobalString("title"),
dev: false,
demo: false,
codeTheme: c.GlobalString("syntaxhl"),
pdfPrint: c.GlobalBool("pdf"),
slideRatio: c.GlobalString("ratio"),
clickEnabled: c.GlobalBoolT("enableClick"),
addWebsocket: false,
imageRootUrl: "static",
}

h.parse()
os.Mkdir(dest, 0755)
os.Mkdir(fmt.Sprintf("%s/static", dest), 0755)
f, err := os.Create(fmt.Sprintf("%s/Side.html", dest))
copyAllFileToFolderNotIncludeExtension(rootDir, fmt.Sprintf("%s/static", dest), ".md")
if err != nil {
return err
}
w := bufio.NewWriter(f)
h.handle(w, "")
log.Printf("exported to %s", dest)
return nil
}
func run(c *cli.Context) error {
isDemo := false
rootDir := c.String("dir")

devMode := c.Bool("dev")
title := c.String("title")

Expand All @@ -105,6 +183,8 @@ func run(c *cli.Context) error {
pdfPrint: c.Bool("pdf"),
slideRatio: c.String("ratio"),
clickEnabled: c.Bool("enableClick"),
addWebsocket: true,
imageRootUrl: "/static",
}

h.parse()
Expand All @@ -123,8 +203,7 @@ func run(c *cli.Context) error {
log.Println("starting on port: " + port + " for directory " + rootDir)
return http.ListenAndServe(":"+port, nil)
}

func (h *holder) handler(w http.ResponseWriter, r *http.Request) {
func (h *holder) handle(wr io.Writer, host string) {
slideFile, _ := pkger.Open("/www/slide.html")
t, _ := template.New("slide").Parse(mustFileToString(slideFile))

Expand All @@ -136,7 +215,7 @@ func (h *holder) handler(w http.ResponseWriter, r *http.Request) {

if s.image != "" {
styles += "\n"
styles += addStyleRule(s.image, i)
styles += addStyleRule(s.image, i, h.imageRootUrl)
}

if s.styles != "" {
Expand All @@ -159,23 +238,53 @@ func (h *holder) handler(w http.ResponseWriter, r *http.Request) {
Title: h.title,
SlideRatio: h.slideRatio,
ClickListener: "",
SocketCode: "",
}

if h.clickEnabled {
s.ClickListener = "window.onclick = next;"
}

if h.addWebsocket {
s.SocketExecuter = "startSocket()"
s.SocketCode = `
function startSocket(){
ws = new WebSocket("ws://"+location.host+"/presenterws");
ws.onopen = function(){
ws.send(JSON.stringify({
type: "presentation:join",
}))
}
ws.onmessage = function(data){
var msg = JSON.parse(data.data)
switch(msg.type){
case "requestNext":
next();
break;
case "requestPrev":
prev();
break;
}
}
}
`
}

if h.dev {
devModeFile, _ := pkger.Open("/www/devMode.html")
js, _ := template.New("devmode").Parse(mustFileToString(devModeFile))
var buf bytes.Buffer
data := make(map[string]string)
data["url"] = "ws://" + r.Host + "/ws"
data["url"] = "ws://" + host + "/ws"
js.Execute(&buf, data)
s.DevMode = template.HTML(buf.String())
}

t.Execute(w, s)
t.Execute(wr, s)
}

func (h *holder) handler(w http.ResponseWriter, r *http.Request) {
h.handle(w, r.Host)
}

func isDir(dir string) bool {
Expand Down
22 changes: 12 additions & 10 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ func (s *slide) buildHash() {
}

type slideContent struct {
Slides template.HTML
Notes template.HTML
Title string
SlideRatio string
Styles template.CSS
PrintStyle template.CSS
DevMode template.HTML
ClickListener template.JS
Slides template.HTML
Notes template.HTML
Title string
SlideRatio string
Styles template.CSS
PrintStyle template.CSS
DevMode template.HTML
ClickListener template.JS
SocketCode template.JS
SocketExecuter template.JS
}

func (h *holder) parse() {
Expand Down Expand Up @@ -87,9 +89,9 @@ func (h *holder) parse() {

}

func addStyleRule(filename string, slideNumber int) string {
func addStyleRule(filename string, slideNumber int, imgRootUrl string) string {

imgURL := "/static" + filename
imgURL := imgRootUrl + filename

css := fmt.Sprintf(`.slide-%d {
background: url("%s");
Expand Down
2 changes: 1 addition & 1 deletion presenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (h *holder) presenterHandler(w http.ResponseWriter, r *http.Request) {

if s.image != "" {
styles += "\n"
styles += addStyleRule(s.image, i)
styles += addStyleRule(s.image, i, h.imageRootUrl)
}

if s.styles != "" {
Expand Down
22 changes: 2 additions & 20 deletions www/slide.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,7 @@
goTo(Math.max(currentSlide - 1, 0));
}

function startSocket(){
ws = new WebSocket("ws://"+location.host+"/presenterws");
ws.onopen = function(){
ws.send(JSON.stringify({
type: "presentation:join",
}))
}
ws.onmessage = function(data){
var msg = JSON.parse(data.data)
switch(msg.type){
case "requestNext":
next();
break;
case "requestPrev":
prev();
break;
}
}
}
{{.SocketCode}}

window.onload = function() {
resize();
Expand All @@ -88,7 +70,7 @@
prev();
}
};
startSocket()
{{.SocketExecuter}}
};
</script>
</head>
Expand Down

0 comments on commit d4ea031

Please sign in to comment.