GoBlog/render.go

111 lines
2.8 KiB
Go
Raw Normal View History

2020-07-31 13:48:01 +00:00
package main
import (
"bytes"
"github.com/araddon/dateparse"
2020-07-31 13:48:01 +00:00
"html/template"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"time"
2020-07-31 13:48:01 +00:00
)
const templatesDir = "templates"
const templatesExt = ".gohtml"
const templateBase = "base"
const templatePost = "post"
const templateError = "error"
const templateRedirect = "redirect"
2020-08-05 17:14:10 +00:00
const templateIndex = "index"
2020-08-31 19:12:43 +00:00
const templateTaxonomy = "taxonomy"
2020-09-21 16:03:05 +00:00
const templatePhotos = "photos"
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() error {
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,
"string": getDefaultTemplateString,
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)
baseTemplatePath := path.Join(templatesDir, templateBase+templatesExt)
err := filepath.Walk(templatesDir, func(p string, info os.FileInfo, err error) error {
if info.Mode().IsRegular() && path.Ext(p) == templatesExt {
name := strings.TrimSuffix(path.Base(p), templatesExt)
if name != templateBase {
templates[name], err = template.New(name).Funcs(templateFunctions).ParseFiles(baseTemplatePath, p)
if err != nil {
return err
}
}
}
return nil
})
if err != nil {
return err
}
return nil
}
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())
}