Skip to content

Commit

Permalink
feat(embedded HTML): changed markdown delivery to load it as external…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
prskr committed Apr 20, 2019
1 parent 4539a92 commit 4b984de
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 52 deletions.
21 changes: 14 additions & 7 deletions assets/template/reveal-markdown.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

<div class="reveal">
<div class="slides">
<!-- Slides are separated by newline + three dashes + newline, vertical slides identical but two dashes -->
<section data-markdown data-separator="^\n{{ .Reveal.HorizontalSeparator }}\n"
data-separator-vertical="^\n{{ .Reveal.VerticalSeparator }}\n">
<script type="text/template">
{{ .MarkdownBody }}
</script>
<section data-markdown="/markdown/content.md"
data-separator="^\n{{ .Reveal.HorizontalSeparator }}\n"
data-separator-vertical="^\n{{ .Reveal.VerticalSeparator }}\n"
data-separator-notes="^Note:"
data-charset="iso-8859-15">
</section>
</div>
</div>
Expand All @@ -35,6 +34,13 @@
progress: true,
history: 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
dependencies: [
Expand All @@ -46,7 +52,8 @@
});



</script>

</body>
</html>
</html>
10 changes: 7 additions & 3 deletions examples/slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

Content 1.1

asdf

Note: This will only appear in the speaker notes window.

---
Expand Down Expand Up @@ -42,4 +40,10 @@ Content 3.2

## 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>
20 changes: 16 additions & 4 deletions internal/app/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package cmd

import (
"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/spf13/cobra"
"net/http"
Expand All @@ -35,26 +35,38 @@ var (
Long: ``,
Run: func(cmd *cobra.Command, args []string) {

tmplRenderer, err := template.NewRevealRenderer(args[0], &params)
tmplRenderer, err := rendering.NewRevealRenderer(&params)

if err != nil {
log.Errorf("Failed to initialize reveal renderer due to error: %v", err)
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...")
revealBox := packr.New("reveal-assets", "./../../../assets/reveal")
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...")
fs := http.FileServer(http.Dir("."))
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)

// start HTTP server
hostPort := fmt.Sprintf("%s:%d", host, port)

log.Infof("Running at addr http://%s/", hostPort)
if err := http.ListenAndServe(hostPort, nil); err != nil {
log.Error("Error while running serve command: %v", err)
Expand Down
52 changes: 52 additions & 0 deletions internal/app/rendering/markdown.go
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,38 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package template
package rendering

import (
"fmt"
"github.com/baez90/go-reveal-slides/internal/app/config"
"github.com/gobuffalo/packr/v2"
log "github.com/sirupsen/logrus"
"html/template"
"io/ioutil"
"net/http"
"os"
"path"

log "github.com/sirupsen/logrus"
)

type RevealRenderer interface {
http.Handler
init() error
}

func NewRevealRenderer(markdownPath string, 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
}

func NewRevealRenderer(params *config.RevealParams) (renderer RevealRenderer, err error) {
renderer = &revealRenderer{
markdownPath: markdownPath,
params: params,
params: params,
}
err = renderer.init()

return
}

type revealRenderer struct {
markdownPath string
template *template.Template
renderedTemplate string
params *config.RevealParams
}

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")
if err != nil {
return
Expand All @@ -74,25 +56,17 @@ func (renderer *revealRenderer) init() (err error) {
func (renderer *revealRenderer) ServeHTTP(response http.ResponseWriter, request *http.Request) {

if renderer.template == nil {
writeErrorResponse(500, "template is not set - probably error during startup", response)
return
}

markdownContent, err := ioutil.ReadFile(renderer.markdownPath)
if err != nil {
writeErrorResponse(500, "failed to read markdown content", response)
log.Errorf("Failed to read markdown content: %v", err)
writeErrorResponse(500, "rendering is not set - probably error during startup", response)
return
}

err = renderer.template.Execute(response, struct {
Reveal config.RevealParams
MarkdownBody string
}{Reveal: *renderer.params, MarkdownBody: string(markdownContent)})
err := renderer.template.Execute(response, struct {
Reveal config.RevealParams
}{Reveal: *renderer.params})

if err != nil {
writeErrorResponse(500, "Failed to render Markdown to template", response)
log.Errorf("Failed to render Markdown template: %v", err)
writeErrorResponse(500, "Failed to render Markdown to rendering", response)
log.Errorf("Failed to render Markdown rendering: %v", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packr2_hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ package go_reveal_slides

import (
_ "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"
)

0 comments on commit 4b984de

Please sign in to comment.