// Create the example presentation slides. func createExampleSlides(presentationPath string) { // Create slides file. file, error := os.Create(path.Join(presentationPath, "slides.md")) if error != nil { log.Fatal(error) } // Write contents of example slides to slides file. writer := bufio.NewWriter(file) slides, _ := bindata.Asset("assets/slides.md") writer.Write(slides) writer.Flush() }
// Create the example presentation theme. func createExampleTheme(presentationPath string) { error := os.Mkdir(path.Join(presentationPath, "css"), 0777) if error != nil { log.Fatal(error) } // Create theme file. file, error := os.Create(path.Join(presentationPath, "css", "theme.css")) if error != nil { log.Fatal(error) } // Write contents of example theme to theme file. writer := bufio.NewWriter(file) theme, _ := bindata.Asset("assets/theme.css") writer.Write(theme) writer.Flush() }
// Serve the presentation. func Serve(c *cli.Context) { master := c.Bool("master") theme := c.String("theme") // Get the presentation path from the command line, or grab the current directory. presentationPath, _ := os.Getwd() if len(c.Args()) > 0 { presentationPath = c.Args()[0] } // Check if the path is relative. if !pathIsAbsolute(presentationPath) { presentationPath, _ = filepath.Abs(presentationPath) } // Check if there is a presentation file present. if fileExists(path.Join(presentationPath, "slides.md")) { log.Printf("slides.md does not exist at %s\n", presentationPath) os.Exit(1) } // Check if a theme was passed. if themeExists(theme) { log.Printf("Using build-in theme [%s]\n", theme) http.Handle("/css/", http.FileServer(&assetfs.AssetFS{bindata.Asset, bindata.AssetDir, bindata.AssetInfo, "themes/" + theme})) http.Handle("/fonts/", http.FileServer(&assetfs.AssetFS{bindata.Asset, bindata.AssetDir, bindata.AssetInfo, "themes/" + theme})) } else { if !fileExists(path.Join(presentationPath, "css", "theme.css")) { log.Println("Found a theme in the css directory, using it...") http.Handle("/css/", http.FileServer(http.Dir(presentationPath))) http.Handle("/fonts/", http.FileServer(http.Dir(presentationPath))) } else { log.Println("No theme found ...") } } // Handle the slides. http.HandleFunc("/slides.md", func(w http.ResponseWriter, r *http.Request) { t, _ := template.ParseFiles(path.Join(presentationPath, "slides.md")) t.Execute(w, "") }) // Handle images. http.HandleFunc("/img/", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, path.Join(presentationPath, r.URL.Path)) }) // Handle reveal.js files. http.Handle("/reveal.js/css/", http.FileServer(&assetfs.AssetFS{bindata.Asset, bindata.AssetDir, bindata.AssetInfo, ""})) http.Handle("/reveal.js/js/", http.FileServer(&assetfs.AssetFS{bindata.Asset, bindata.AssetDir, bindata.AssetInfo, ""})) http.Handle("/reveal.js/lib/", http.FileServer(&assetfs.AssetFS{bindata.Asset, bindata.AssetDir, bindata.AssetInfo, ""})) http.Handle("/reveal.js/plugin/", http.FileServer(&assetfs.AssetFS{bindata.Asset, bindata.AssetDir, bindata.AssetInfo, ""})) // Handle the website. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { opt := &Option{Markdown: "slides.md", Master: master} indexHTML, _ := bindata.Asset("assets/index.html") indexTemplate := template.Must(template.New("index").Parse(string(indexHTML))) indexTemplate.Execute(w, opt) }) log.Println("Serving presentation on http://localhost:8989") log.Println("Opening browser and redirecting to the presentation, press Ctrl-C to stop ...") browser.OpenURL("http://localhost:8989") err := http.ListenAndServe(":8989", nil) log.Fatalf("Error while serving slides: %s\n", err.Error()) }