feat(embedded HTML): changed markdown delivery to load it as external file

- enables embedding of HTML in Markdown
- requires a single file handler that delivers the markdown file content
- eased template handling
- added more reveal.js config option that should configurable in upcoming version
This commit is contained in:
Peter 2019-04-20 02:28:27 +02:00
parent 4539a9243b
commit 4b984de827
Signed by: prskr
GPG key ID: C1DB5D2E8DB512F9
6 changed files with 102 additions and 53 deletions

View file

@ -16,12 +16,11 @@
<div class="reveal"> <div class="reveal">
<div class="slides"> <div class="slides">
<!-- Slides are separated by newline + three dashes + newline, vertical slides identical but two dashes --> <section data-markdown="/markdown/content.md"
<section data-markdown data-separator="^\n{{ .Reveal.HorizontalSeparator }}\n" data-separator="^\n{{ .Reveal.HorizontalSeparator }}\n"
data-separator-vertical="^\n{{ .Reveal.VerticalSeparator }}\n"> data-separator-vertical="^\n{{ .Reveal.VerticalSeparator }}\n"
<script type="text/template"> data-separator-notes="^Note:"
{{ .MarkdownBody }} data-charset="iso-8859-15">
</script>
</section> </section>
</div> </div>
</div> </div>
@ -35,6 +34,13 @@
progress: true, progress: true,
history: true, history: true,
center: true, center: true,
slideNumber: true,
hash: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
markdown:{
smartypants: true,
smartLists: true,
},
// Optional libraries used to extend on reveal.js // Optional libraries used to extend on reveal.js
dependencies: [ dependencies: [
@ -46,6 +52,7 @@
}); });
</script> </script>
</body> </body>

View file

@ -4,8 +4,6 @@
Content 1.1 Content 1.1
asdf
Note: This will only appear in the speaker notes window. Note: This will only appear in the speaker notes window.
--- ---
@ -43,3 +41,9 @@ Content 3.2
## External 4.1 ## External 4.1
![Local image](/local/examples/gopher.jpg) ![Local image](/local/examples/gopher.jpg)
---
## External 4.2
<a href="https://www.google.com">Google</a>

View file

@ -16,7 +16,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/baez90/go-reveal-slides/internal/app/template" "github.com/baez90/go-reveal-slides/internal/app/rendering"
"github.com/gobuffalo/packr/v2" "github.com/gobuffalo/packr/v2"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"net/http" "net/http"
@ -35,26 +35,38 @@ var (
Long: ``, Long: ``,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
tmplRenderer, err := template.NewRevealRenderer(args[0], &params) tmplRenderer, err := rendering.NewRevealRenderer(&params)
if err != nil { if err != nil {
log.Errorf("Failed to initialize reveal renderer due to error: %v", err) log.Errorf("Failed to initialize reveal renderer due to error: %v", err)
os.Exit(1) os.Exit(1)
} }
markdownHandler, err := rendering.NewMarkdownHandler(args[0])
if err != nil {
log.Errorf("Failed to initialize reveal renderer due to error: %v", err)
os.Exit(1)
}
// Packr2 handler to serve Reveal.js assets
log.Info("Setup reveal assets under route /reveal/ route...") log.Info("Setup reveal assets under route /reveal/ route...")
revealBox := packr.New("reveal-assets", "./../../../assets/reveal") revealBox := packr.New("reveal-assets", "./../../../assets/reveal")
http.Handle("/reveal/", http.StripPrefix("/reveal/", http.FileServer(revealBox))) http.Handle("/reveal/", http.StripPrefix("/reveal/", http.FileServer(revealBox)))
// // Static file handler under subroute to serve static files e.g. images
log.Info("Setup static file serving under /local/ route...") log.Info("Setup static file serving under /local/ route...")
fs := http.FileServer(http.Dir(".")) fs := http.FileServer(http.Dir("."))
http.Handle("/local/", http.StripPrefix("/local/", fs)) http.Handle("/local/", http.StripPrefix("/local/", fs))
// single file handler that only delivers the single Markdown file containing the slides
log.Info("Setup markdown handler under /markdown/content.md route...")
http.Handle("/markdown/", markdownHandler)
// entrypoint that delivers the rendered reveal.js index HTML page
http.Handle("/", tmplRenderer) http.Handle("/", tmplRenderer)
// start HTTP server
hostPort := fmt.Sprintf("%s:%d", host, port) hostPort := fmt.Sprintf("%s:%d", host, port)
log.Infof("Running at addr http://%s/", hostPort) log.Infof("Running at addr http://%s/", hostPort)
if err := http.ListenAndServe(hostPort, nil); err != nil { if err := http.ListenAndServe(hostPort, nil); err != nil {
log.Error("Error while running serve command: %v", err) log.Error("Error while running serve command: %v", err)

View file

@ -0,0 +1,52 @@
// Copyright © 2019 Peter Kurfer peter.kurfer@googlemail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rendering
import (
"fmt"
"net/http"
"os"
"path"
)
type MarkdownHandler interface {
http.Handler
}
func NewMarkdownHandler(markdownPath string) (handler MarkdownHandler, err error) {
var info os.FileInfo
info, err = os.Stat(markdownPath)
if err != nil {
return
}
if info.IsDir() || path.Ext(info.Name()) != ".md" {
err = fmt.Errorf("path %s did not pass sanity checks for markdown files", markdownPath)
return
}
handler = &markdownHandler{
markdownPath: markdownPath,
}
return
}
type markdownHandler struct {
markdownPath string
}
func (handler *markdownHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
http.ServeFile(response, request, handler.markdownPath)
}

View file

@ -12,19 +12,14 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package template package rendering
import ( import (
"fmt"
"github.com/baez90/go-reveal-slides/internal/app/config" "github.com/baez90/go-reveal-slides/internal/app/config"
"github.com/gobuffalo/packr/v2" "github.com/gobuffalo/packr/v2"
"html/template"
"io/ioutil"
"net/http"
"os"
"path"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"html/template"
"net/http"
) )
type RevealRenderer interface { type RevealRenderer interface {
@ -32,20 +27,8 @@ type RevealRenderer interface {
init() error init() error
} }
func NewRevealRenderer(markdownPath string, params *config.RevealParams) (renderer RevealRenderer, err error) { func NewRevealRenderer(params *config.RevealParams) (renderer RevealRenderer, err error) {
var info os.FileInfo
info, err = os.Stat(markdownPath)
if err != nil {
return
}
if info.IsDir() || path.Ext(info.Name()) != ".md" {
err = fmt.Errorf("path %s did not pass sanity checks for markdown files", markdownPath)
return
}
renderer = &revealRenderer{ renderer = &revealRenderer{
markdownPath: markdownPath,
params: params, params: params,
} }
err = renderer.init() err = renderer.init()
@ -54,14 +37,13 @@ func NewRevealRenderer(markdownPath string, params *config.RevealParams) (render
} }
type revealRenderer struct { type revealRenderer struct {
markdownPath string
template *template.Template template *template.Template
renderedTemplate string renderedTemplate string
params *config.RevealParams params *config.RevealParams
} }
func (renderer *revealRenderer) init() (err error) { func (renderer *revealRenderer) init() (err error) {
templateBox := packr.New("template", "./../../../assets/template") templateBox := packr.New("rendering", "./../../../assets/template")
templateString, err := templateBox.FindString("reveal-markdown.tmpl") templateString, err := templateBox.FindString("reveal-markdown.tmpl")
if err != nil { if err != nil {
return return
@ -74,25 +56,17 @@ func (renderer *revealRenderer) init() (err error) {
func (renderer *revealRenderer) ServeHTTP(response http.ResponseWriter, request *http.Request) { func (renderer *revealRenderer) ServeHTTP(response http.ResponseWriter, request *http.Request) {
if renderer.template == nil { if renderer.template == nil {
writeErrorResponse(500, "template is not set - probably error during startup", response) writeErrorResponse(500, "rendering is not set - probably error during startup", response)
return return
} }
markdownContent, err := ioutil.ReadFile(renderer.markdownPath) err := renderer.template.Execute(response, struct {
if err != nil {
writeErrorResponse(500, "failed to read markdown content", response)
log.Errorf("Failed to read markdown content: %v", err)
return
}
err = renderer.template.Execute(response, struct {
Reveal config.RevealParams Reveal config.RevealParams
MarkdownBody string }{Reveal: *renderer.params})
}{Reveal: *renderer.params, MarkdownBody: string(markdownContent)})
if err != nil { if err != nil {
writeErrorResponse(500, "Failed to render Markdown to template", response) writeErrorResponse(500, "Failed to render Markdown to rendering", response)
log.Errorf("Failed to render Markdown template: %v", err) log.Errorf("Failed to render Markdown rendering: %v", err)
} }
} }

View file

@ -16,5 +16,5 @@ package go_reveal_slides
import ( import (
_ "github.com/baez90/go-reveal-slides/internal/app/cmd" _ "github.com/baez90/go-reveal-slides/internal/app/cmd"
_ "github.com/baez90/go-reveal-slides/internal/app/template" _ "github.com/baez90/go-reveal-slides/internal/app/rendering"
) )