GoBlog/render.go

93 lines
2.5 KiB
Go
Raw Normal View History

2020-07-31 13:48:01 +00:00
package main
import (
"bytes"
"fmt"
"github.com/araddon/dateparse"
2020-07-31 13:48:01 +00:00
"html/template"
"log"
"net/http"
"time"
2020-07-31 13:48:01 +00:00
)
const templatePost = "post"
const templateError = "error"
const templateRedirect = "redirect"
2020-08-05 17:14:10 +00:00
const templateIndex = "index"
const templateSummary = "summary"
2020-08-31 19:12:43 +00:00
const templateTaxonomy = "taxonomy"
2020-09-19 11:37:58 +00:00
const templateMenu = "menu"
2020-07-31 13:48:01 +00:00
var templates map[string]*template.Template
var templateFunctions template.FuncMap
2020-07-31 13:48:01 +00:00
func initRendering() {
templateFunctions = template.FuncMap{
2020-07-31 13:48:01 +00:00
"blog": func() *configBlog {
2020-08-04 17:42:09 +00:00
return appConfig.Blog
2020-07-31 13:48:01 +00:00
},
2020-09-19 11:37:58 +00:00
"menu": func(id string) *menu {
2020-09-20 15:28:24 +00:00
return appConfig.Blog.Menus[id]
2020-09-19 11:37:58 +00:00
},
2020-07-31 13:48:01 +00:00
"md": func(content string) template.HTML {
htmlContent, err := renderMarkdown(content)
if err != nil {
log.Fatal(err)
return ""
}
return template.HTML(htmlContent)
},
2020-08-31 19:12:43 +00:00
// First parameter value
2020-09-02 15:48:37 +00:00
"p": func(post *Post, parameter string) string {
return post.firstParameter(parameter)
2020-08-31 19:12:43 +00:00
},
// All parameter values
2020-09-02 15:48:37 +00:00
"ps": func(post *Post, parameter string) []string {
return post.Parameters[parameter]
2020-07-31 13:48:01 +00:00
},
2020-09-02 15:48:37 +00:00
"title": func(post *Post) string {
return post.title()
},
"summary": func(post *Post) string {
return post.summary()
},
"dateformat": func(date string, format string) string {
d, err := dateparse.ParseIn(date, time.Local)
if err != nil {
return ""
}
return d.Format(format)
},
"asset": assetFile,
2020-08-05 17:14:10 +00:00
"include": func(templateName string, data interface{}) (template.HTML, error) {
buf := new(bytes.Buffer)
err := templates[templateName].ExecuteTemplate(buf, templateName, data)
return template.HTML(buf.String()), err
},
2020-09-01 16:53:21 +00:00
"urlize": urlize,
2020-09-19 12:56:31 +00:00
"sort": sortedStrings,
2020-07-31 13:48:01 +00:00
}
templates = make(map[string]*template.Template)
2020-09-19 11:37:58 +00:00
for _, name := range []string{templatePost, templateError, templateRedirect, templateIndex, templateSummary, templateTaxonomy, templateMenu} {
templates[name] = loadTemplate(name)
2020-07-31 13:48:01 +00:00
}
}
func loadTemplate(name string) *template.Template {
return template.Must(template.New(name).Funcs(templateFunctions).ParseFiles("templates/base.gohtml", fmt.Sprintf("templates/%s.gohtml", name)))
}
func render(w http.ResponseWriter, template string, data interface{}) {
// We need to use a buffer here to enable minification
var buffer bytes.Buffer
err := templates[template].ExecuteTemplate(&buffer, template, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
// Set content type (needed for minification middleware
w.Header().Set("Content-Type", contentTypeHTML)
// Write buffered response
_, _ = w.Write(buffer.Bytes())
}