Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move to go:embed, housekeeping #16

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
34 changes: 0 additions & 34 deletions .drone.yml

This file was deleted.

26 changes: 26 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

name: Go

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'

- name: Build
run: go build -v .

- name: Test
run: go test -v .
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
module github.com/cdreier/slide-serve

go 1.12
go 1.22

require (
github.com/alecthomas/chroma v0.7.1
github.com/fsnotify/fsnotify v1.4.9
github.com/gorilla/websocket v1.4.2
github.com/markbates/pkger v0.15.0
github.com/urfave/cli v1.22.3
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
github.com/dlclark/regexp2 v1.1.6 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9 // indirect
)
17 changes: 0 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,13 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.1.6 h1:CqB4MjHw0MFCDj+PHHjiESmHX+N7t0tJzKvC6M97BRg=
github.com/dlclark/regexp2 v1.1.6/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/markbates/pkger v0.15.0 h1:rSXoKLBWBgYG7j/h6Be7kggju23ie1Gx3/va9xl5aUw=
github.com/markbates/pkger v0.15.0/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
Expand All @@ -45,18 +35,11 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5I
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/urfave/cli v1.22.3 h1:FpNT6zq26xNpHZy08emi755QwzLPs6Pukqjlc7RfOMU=
github.com/urfave/cli v1.22.3/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9 h1:L2auWcuQIvxz9xSEqzESnV/QN/gNRXNApHi3fYwl2w0=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
34 changes: 9 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"

"github.com/cdreier/slide-serve/www"
"github.com/gorilla/websocket"
"github.com/markbates/pkger"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -101,7 +100,7 @@ func main() {

}
func copyAllFileToFolderNotIncludeExtension(folder string, dest string, ext string) error {
files, err := ioutil.ReadDir(folder)
files, err := os.ReadDir(folder)
if err != nil {
return err
}
Expand All @@ -119,11 +118,11 @@ func copyAllFileToFolderNotIncludeExtension(folder string, dest string, ext stri
}

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

func export(c *cli.Context) error {
Expand Down Expand Up @@ -204,8 +203,7 @@ func run(c *cli.Context) error {
return http.ListenAndServe(":"+port, nil)
}
func (h *holder) handle(wr io.Writer, host string) {
slideFile, _ := pkger.Open("/www/slide.html")
t, _ := template.New("slide").Parse(mustFileToString(slideFile))
t, _ := template.New("slide").Parse(www.Slide)

slides := ""
styles := h.styles
Expand All @@ -226,15 +224,15 @@ func (h *holder) handle(wr io.Writer, host string) {

}

cssFile, _ := pkger.Open("/www/summaryStyle.css")
cssFile := www.StyleSummary
if h.pdfPrint {
cssFile, _ = pkger.Open("/www/pdfStyle.css")
cssFile = www.StylePDF
}

s := slideContent{
Slides: template.HTML(slides),
Styles: template.CSS(styles),
PrintStyle: template.CSS(mustFileToString(cssFile)),
PrintStyle: template.CSS(cssFile),
Title: h.title,
SlideRatio: h.slideRatio,
ClickListener: "",
Expand Down Expand Up @@ -271,8 +269,7 @@ func (h *holder) handle(wr io.Writer, host string) {
}

if h.dev {
devModeFile, _ := pkger.Open("/www/devMode.html")
js, _ := template.New("devmode").Parse(mustFileToString(devModeFile))
js, _ := template.New("devmode").Parse(www.DevMode)
var buf bytes.Buffer
data := make(map[string]string)
data["url"] = "ws://" + host + "/ws"
Expand All @@ -287,23 +284,10 @@ func (h *holder) handler(w http.ResponseWriter, r *http.Request) {
h.handle(w, r.Host)
}

func isDir(dir string) bool {
stat, _ := os.Stat(dir)
return stat.IsDir()
}

func dirExist(dir string) bool {
_, err := os.Stat(dir)
if err == nil {
return true
}
return !os.IsNotExist(err)
}

func mustFileToString(f http.File) string {
content, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal("must read file to string failed: ", err.Error())
}
return string(content)
}
35 changes: 17 additions & 18 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import (
"bufio"
"fmt"
"html/template"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/markbates/pkger"
)

type slide struct {
Expand Down Expand Up @@ -45,31 +42,33 @@ func (h *holder) parse() {
// example presentation
fmt.Println("serving example presentation")

pkger.Walk("/example", func(path string, info os.FileInfo, err error) error {
// TODO remove pkger in favor of embed

if filepath.Base(path) == "styles.css" {
f, _ := pkger.Open(path)
h.styles += mustFileToString(f)
}
// pkger.Walk("/example", func(path string, info os.FileInfo, err error) error {

if filepath.Ext(path) == ".md" {
f, _ := pkger.Open(path)
h.generateSlides(mustFileToString(f))
}
// if filepath.Base(path) == "styles.css" {
// f, _ := pkger.Open(path)
// h.styles += mustFileToString(f)
// }

return nil
})
// if filepath.Ext(path) == ".md" {
// f, _ := pkger.Open(path)
// h.generateSlides(mustFileToString(f))
// }

// return nil
// })

} else {
// user presentation
h.slides = make([]slide, 0)
h.styles = ""
filepath.Walk(h.dir, func(path string, info os.FileInfo, err error) error {
filepath.Walk(h.dir, func(path string, info os.FileInfo, _ error) error {
if info == nil || info.IsDir() {
return nil
}
// reading all the files, check file ext before reading?
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
fmt.Println("cannot read... skipping ", path)
return nil
Expand Down Expand Up @@ -127,14 +126,14 @@ func (h *holder) generateSlides(content string) {
s.image = strings.Replace(tmp, "@img", "", -1)
} else if strings.HasPrefix(tmp, "@css") {
filename := strings.Replace(tmp, "@css", "", -1)
data, err := ioutil.ReadFile(h.dir + filename)
data, err := os.ReadFile(h.dir + filename)
if err == nil {
s.styles = string(data)
}

} else if strings.HasPrefix(tmp, "@js") {
filename := strings.Replace(tmp, "@js", "", -1)
data, err := ioutil.ReadFile(h.dir + filename)
data, err := os.ReadFile(h.dir + filename)
if err == nil {
s.javascript = string(data)
}
Expand Down
11 changes: 3 additions & 8 deletions presenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strconv"
"strings"

"github.com/markbates/pkger"
"github.com/cdreier/slide-serve/www"
)

type presenterMsg struct {
Expand Down Expand Up @@ -45,18 +45,15 @@ func (h *holder) presenterSocket(w http.ResponseWriter, r *http.Request) {
} else {
log.Println("presenter socker is null")
}
break
case "prev":
if h.presenterCon != nil {
h.presenterCon.WriteJSON(presenterMsg{
Type: "requestPrev",
})
}
break
case "presentation:join":
h.presenterCon = connection
log.Println("presentation joined")
break
}
// err = connection.WriteMessage(mt, message)
// if err != nil {
Expand All @@ -67,8 +64,7 @@ func (h *holder) presenterSocket(w http.ResponseWriter, r *http.Request) {
}

func (h *holder) presenterHandler(w http.ResponseWriter, r *http.Request) {
slideFile, _ := pkger.Open("/www/presenter.html")
t, _ := template.New("slide").Parse(mustFileToString(slideFile))
t, _ := template.New("slide").Parse(www.Presenter)

slides := ""
notes := ""
Expand Down Expand Up @@ -101,8 +97,7 @@ func (h *holder) presenterHandler(w http.ResponseWriter, r *http.Request) {
}

if h.dev {
devModeFile, _ := pkger.Open("/www/devMode.html")
js, _ := template.New("devmode").Parse(mustFileToString(devModeFile))
js, _ := template.New("devmode").Parse(www.DevMode)
var buf bytes.Buffer
data := make(map[string]string)
data["url"] = "ws://" + r.Host + "/ws"
Expand Down
10 changes: 4 additions & 6 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ func renderSlide(s slide, index int, codeTheme string) string {
code += line + "\n"
slideMarkup += codeMarker
} else if strings.HasPrefix(line, "#") {
var headlinetype int
headlinetype = strings.Index(line, " ")
headlinetype := strings.Index(line, " ")
line = strings.TrimLeft(line, "#")
line = headline(line, headlinetype)
slideMarkup += line
Expand Down Expand Up @@ -74,8 +73,7 @@ func renderSlide(s slide, index int, codeTheme string) string {
}

if s.classes != "" {
var classes string
classes = strings.Replace(s.classes, ",", " ", -1)
classes := strings.Replace(s.classes, ",", " ", -1)
slideMarkup += fmt.Sprintf(`
<script>
window.addEventListener('slideEnter_%d', function(){
Expand Down Expand Up @@ -148,8 +146,8 @@ func getHighlightedMarkup(code string, lang string, codeTheme string) (string, s
highlightedCode := new(bytes.Buffer)
cssClasses := new(bytes.Buffer)

iterator, err := lexer.Tokenise(nil, code)
err = formatter.Format(highlightedCode, style, iterator)
iterator, _ := lexer.Tokenise(nil, code)
err := formatter.Format(highlightedCode, style, iterator)
if err != nil {
return fmt.Sprintf(`
<pre>%s</pre>
Expand Down
Loading
Loading