forked from agonzalezro/md2slides
/
main.go
70 lines (52 loc) · 1.79 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
package main
//go:generate go-bindata -pkg presentation -o presentation/bindata.go templates
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/agonzalezro/md2slides/presentation"
"github.com/gorilla/mux"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
availableThemes = []string{"reveal", "shower", "remark"}
theme *string
outputFile = kingpin.Flag("output", "output file where to write the HTML.").Default("/dev/stdout").Short('o').OpenFile(os.O_CREATE|os.O_WRONLY, 0644)
startDaemon = kingpin.Flag("daemon", "start a simple HTTP serving your slides.").Short('d').Bool()
port = kingpin.Flag("port", "port where to run the server.").Default("8080").Int()
config = kingpin.Flag("theme-config", "configuration for the theme (JS file)").Short('c').File()
source = kingpin.Arg("source", "Markdown source file.").Required().File()
)
func init() {
themesHelp := fmt.Sprintf("Choose one: [%s].", strings.Join(availableThemes, ", "))
theme = kingpin.Flag("theme", themesHelp).Default("reveal").String()
kingpin.CommandLine.HelpFlag.Short('h')
}
func main() {
kingpin.Parse()
if !contains(availableThemes, *theme) {
kingpin.Fatalf("theme: %s not found", *theme)
}
p, err := presentation.NewFromFileWithConfig(*source, *config)
ifErrFatal(err)
p.Theme = *theme
if *startDaemon {
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ifErrFatal(p.Write(w))
if err := p.Reload(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
})
r.PathPrefix("/").Handler(http.FileServer(http.Dir(".")))
port := ":" + strconv.Itoa(*port)
log.Println("Serving slides at", port)
log.Fatal(http.ListenAndServe(port, r))
return
}
// Write it just if we don't serve it
ifErrFatal(p.Write(*outputFile))
}