Refactor out the use of rice in favor of embed (#2)
Co-authored-by: James Mills <prologic@shortcircuit.net.au> Reviewed-on: https://git.mills.io/prologic/spyda/pulls/2
This commit is contained in:
@@ -10,11 +10,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
rice "github.com/GeertJohan/go.rice"
|
||||
"github.com/gomarkdown/markdown"
|
||||
"github.com/gomarkdown/markdown/html"
|
||||
"github.com/gomarkdown/markdown/parser"
|
||||
"github.com/james4k/fmatter"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/vcraescu/go-paginator"
|
||||
@@ -37,65 +32,6 @@ func (s *Server) NotFoundHandler(w http.ResponseWriter, r *http.Request) {
|
||||
s.render("404", w, ctx)
|
||||
}
|
||||
|
||||
type FrontMatter struct {
|
||||
Title string
|
||||
}
|
||||
|
||||
// PageHandler ...
|
||||
func (s *Server) PageHandler(name string) httprouter.Handle {
|
||||
box := rice.MustFindBox("pages")
|
||||
mdTpl := box.MustString(fmt.Sprintf("%s.md", name))
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
ctx := NewContext(s.config, s.db, r)
|
||||
|
||||
md, err := RenderString(mdTpl, ctx)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("error rendering page %s", name)
|
||||
ctx.Error = true
|
||||
ctx.Message = "Error loading help page! Please contact support."
|
||||
s.render("error", w, ctx)
|
||||
return
|
||||
}
|
||||
|
||||
var frontmatter FrontMatter
|
||||
content, err := fmatter.Read([]byte(md), &frontmatter)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("error parsing front matter")
|
||||
ctx.Error = true
|
||||
ctx.Message = "Error loading page! Please contact support."
|
||||
s.render("error", w, ctx)
|
||||
return
|
||||
}
|
||||
|
||||
extensions := parser.CommonExtensions | parser.AutoHeadingIDs
|
||||
p := parser.NewWithExtensions(extensions)
|
||||
|
||||
htmlFlags := html.CommonFlags
|
||||
opts := html.RendererOptions{
|
||||
Flags: htmlFlags,
|
||||
Generator: "",
|
||||
}
|
||||
renderer := html.NewRenderer(opts)
|
||||
|
||||
html := markdown.ToHTML(content, p, renderer)
|
||||
|
||||
var title string
|
||||
|
||||
if frontmatter.Title != "" {
|
||||
title = frontmatter.Title
|
||||
} else {
|
||||
title = strings.Title(name)
|
||||
}
|
||||
ctx.Title = title
|
||||
|
||||
ctx.Page = name
|
||||
ctx.Content = template.HTML(html)
|
||||
|
||||
s.render("page", w, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// IndexHandler ...
|
||||
func (s *Server) IndexHandler() httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
|
||||
Reference in New Issue
Block a user