2020-07-31 13:48:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-07-31 17:46:12 +00:00
|
|
|
"bytes"
|
2020-07-31 19:02:47 +00:00
|
|
|
"fmt"
|
2020-09-18 10:34:52 +00:00
|
|
|
"github.com/araddon/dateparse"
|
2020-07-31 13:48:01 +00:00
|
|
|
"html/template"
|
|
|
|
"log"
|
2020-07-31 17:46:12 +00:00
|
|
|
"net/http"
|
2020-09-18 10:34:52 +00:00
|
|
|
"time"
|
2020-07-31 13:48:01 +00:00
|
|
|
)
|
|
|
|
|
2020-07-31 19:02:47 +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-07-31 13:48:01 +00:00
|
|
|
|
2020-07-31 19:02:47 +00:00
|
|
|
var templates map[string]*template.Template
|
|
|
|
var templateFunctions template.FuncMap
|
2020-07-31 13:48:01 +00:00
|
|
|
|
|
|
|
func initRendering() {
|
2020-07-31 19:02:47 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
"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 {
|
2020-07-31 19:02:47 +00:00
|
|
|
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()
|
|
|
|
},
|
2020-09-15 15:01:53 +00:00
|
|
|
"summary": func(post *Post) string {
|
|
|
|
return post.summary()
|
|
|
|
},
|
2020-09-18 10:34:52 +00:00
|
|
|
"dateformat": func(date string, format string) string {
|
|
|
|
d, err := dateparse.ParseIn(date, time.Local)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return d.Format(format)
|
|
|
|
},
|
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-07-31 13:48:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 19:02:47 +00:00
|
|
|
templates = make(map[string]*template.Template)
|
2020-08-31 19:12:43 +00:00
|
|
|
for _, name := range []string{templatePost, templateError, templateRedirect, templateIndex, templateSummary, templateTaxonomy} {
|
2020-07-31 19:02:47 +00:00
|
|
|
templates[name] = loadTemplate(name)
|
2020-07-31 13:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-31 17:46:12 +00:00
|
|
|
|
2020-07-31 19:02:47 +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)))
|
|
|
|
}
|
|
|
|
|
2020-07-31 17:46:12 +00:00
|
|
|
func render(w http.ResponseWriter, template string, data interface{}) {
|
|
|
|
// We need to use a buffer here to enable minification
|
|
|
|
var buffer bytes.Buffer
|
2020-07-31 19:02:47 +00:00
|
|
|
err := templates[template].ExecuteTemplate(&buffer, template, data)
|
2020-07-31 17:46:12 +00:00
|
|
|
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())
|
|
|
|
}
|