2020-07-31 13:48:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-07-31 17:46:12 +00:00
|
|
|
"bytes"
|
2021-01-21 16:59:47 +00:00
|
|
|
"encoding/json"
|
2020-11-01 17:37:21 +00:00
|
|
|
"errors"
|
2020-10-13 11:40:16 +00:00
|
|
|
"fmt"
|
2020-07-31 13:48:01 +00:00
|
|
|
"html/template"
|
|
|
|
"log"
|
2020-07-31 17:46:12 +00:00
|
|
|
"net/http"
|
2020-09-20 15:47:54 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2020-11-01 17:37:21 +00:00
|
|
|
"reflect"
|
2020-09-20 15:47:54 +00:00
|
|
|
"strings"
|
2020-09-18 10:34:52 +00:00
|
|
|
"time"
|
2020-10-06 17:07:48 +00:00
|
|
|
|
|
|
|
"github.com/araddon/dateparse"
|
2021-03-10 17:47:56 +00:00
|
|
|
servertiming "github.com/mitchellh/go-server-timing"
|
2020-07-31 13:48:01 +00:00
|
|
|
)
|
|
|
|
|
2021-02-20 14:42:45 +00:00
|
|
|
const (
|
|
|
|
templatesDir = "templates"
|
|
|
|
templatesExt = ".gohtml"
|
2020-09-20 15:47:54 +00:00
|
|
|
|
2021-02-20 14:42:45 +00:00
|
|
|
templateBase = "base"
|
|
|
|
templatePost = "post"
|
|
|
|
templateError = "error"
|
|
|
|
templateIndex = "index"
|
|
|
|
templateTaxonomy = "taxonomy"
|
|
|
|
templateSearch = "search"
|
|
|
|
templateSummary = "summary"
|
|
|
|
templatePhotosSummary = "photosummary"
|
|
|
|
templateEditor = "editor"
|
|
|
|
templateLogin = "login"
|
|
|
|
templateStaticHome = "statichome"
|
|
|
|
templateBlogStats = "blogstats"
|
|
|
|
templateComment = "comment"
|
|
|
|
templateCaptcha = "captcha"
|
|
|
|
templateCommentsAdmin = "commentsadmin"
|
|
|
|
templateNotificationsAdmin = "notificationsadmin"
|
2021-02-20 22:35:16 +00:00
|
|
|
templateWebmentionAdmin = "webmentionadmin"
|
2021-02-20 14:42:45 +00:00
|
|
|
)
|
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
|
|
|
|
2020-09-20 15:47:54 +00:00
|
|
|
func initRendering() error {
|
2020-07-31 19:02:47 +00:00
|
|
|
templateFunctions = template.FuncMap{
|
2020-10-16 18:34:34 +00:00
|
|
|
"blog": func(blog string) *configBlog {
|
|
|
|
return appConfig.Blogs[blog]
|
|
|
|
},
|
2020-10-12 16:47:23 +00:00
|
|
|
"menu": func(blog *configBlog, id string) *menu {
|
|
|
|
return blog.Menus[id]
|
2020-09-19 11:37:58 +00:00
|
|
|
},
|
2020-11-01 17:37:21 +00:00
|
|
|
"user": func() *configUser {
|
|
|
|
return appConfig.User
|
|
|
|
},
|
2020-07-31 13:48:01 +00:00
|
|
|
"md": func(content string) template.HTML {
|
2020-12-23 13:36:27 +00:00
|
|
|
htmlContent, err := renderMarkdown(content, false)
|
2020-07-31 13:48:01 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return template.HTML(htmlContent)
|
|
|
|
},
|
2021-02-03 09:44:39 +00:00
|
|
|
"html": func(s string) template.HTML {
|
|
|
|
return template.HTML(s)
|
|
|
|
},
|
2020-10-18 09:54:29 +00:00
|
|
|
// Post specific
|
2020-10-15 15:32:46 +00:00
|
|
|
"p": func(p *post, parameter string) string {
|
|
|
|
return p.firstParameter(parameter)
|
2020-08-31 19:12:43 +00:00
|
|
|
},
|
2020-10-15 15:32:46 +00:00
|
|
|
"ps": func(p *post, parameter string) []string {
|
|
|
|
return p.Parameters[parameter]
|
2020-07-31 13:48:01 +00:00
|
|
|
},
|
2020-10-16 18:34:34 +00:00
|
|
|
"hasp": func(p *post, parameter string) bool {
|
|
|
|
return len(p.Parameters[parameter]) > 0
|
|
|
|
},
|
2020-10-15 15:32:46 +00:00
|
|
|
"title": func(p *post) string {
|
|
|
|
return p.title()
|
2020-09-02 15:48:37 +00:00
|
|
|
},
|
2020-10-18 09:54:29 +00:00
|
|
|
"content": func(p *post) template.HTML {
|
|
|
|
return p.html()
|
|
|
|
},
|
2020-10-15 15:32:46 +00:00
|
|
|
"summary": func(p *post) string {
|
|
|
|
return p.summary()
|
2020-09-15 15:01:53 +00:00
|
|
|
},
|
2020-10-16 18:34:34 +00:00
|
|
|
"translations": func(p *post) []*post {
|
|
|
|
return p.translations()
|
|
|
|
},
|
2020-12-22 21:15:29 +00:00
|
|
|
"shorturl": func(p *post) string {
|
|
|
|
return p.shortURL()
|
|
|
|
},
|
2020-10-16 18:34:34 +00:00
|
|
|
// Others
|
2021-02-26 09:31:38 +00:00
|
|
|
"dateformat": dateFormat,
|
|
|
|
"isodate": func(date string) string {
|
|
|
|
return dateFormat(date, "2006-01-02")
|
2020-10-12 17:46:14 +00:00
|
|
|
},
|
2020-11-06 17:45:31 +00:00
|
|
|
"unixtodate": func(unix int64) string {
|
2020-12-16 20:24:53 +00:00
|
|
|
return time.Unix(unix, 0).Local().String()
|
2020-11-06 17:45:31 +00:00
|
|
|
},
|
2020-11-01 17:37:21 +00:00
|
|
|
"now": func() string {
|
2020-12-16 20:24:53 +00:00
|
|
|
return time.Now().Local().String()
|
2020-11-01 17:37:21 +00:00
|
|
|
},
|
2020-11-03 17:00:03 +00:00
|
|
|
"dateadd": func(date string, years, months, days int) string {
|
2020-12-16 19:21:35 +00:00
|
|
|
d, err := dateparse.ParseLocal(date)
|
2020-11-03 17:00:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2020-12-16 20:24:53 +00:00
|
|
|
return d.AddDate(years, months, days).Local().String()
|
2020-11-03 17:00:03 +00:00
|
|
|
},
|
|
|
|
"datebefore": func(date string, before string) bool {
|
2020-12-16 19:21:35 +00:00
|
|
|
d, err := dateparse.ParseLocal(date)
|
2020-11-03 17:00:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2020-12-16 19:21:35 +00:00
|
|
|
b, err := dateparse.ParseLocal(before)
|
2020-11-03 17:00:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return d.Before(b)
|
|
|
|
},
|
2021-01-10 16:19:08 +00:00
|
|
|
"asset": assetFileName,
|
2020-10-12 17:12:43 +00:00
|
|
|
"string": getTemplateStringVariant,
|
2020-11-01 17:37:21 +00:00
|
|
|
"include": func(templateName string, data ...interface{}) (template.HTML, error) {
|
|
|
|
if len(data) == 1 {
|
|
|
|
if rd, ok := data[0].(*renderData); ok {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err := templates[templateName].ExecuteTemplate(buf, templateName, rd)
|
|
|
|
return template.HTML(buf.String()), err
|
|
|
|
}
|
|
|
|
return "", errors.New("wrong argument")
|
|
|
|
} else if len(data) == 2 {
|
|
|
|
if blog, ok := data[0].(*configBlog); ok {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err := templates[templateName].ExecuteTemplate(buf, templateName, &renderData{
|
|
|
|
Blog: blog,
|
|
|
|
Data: data[1],
|
|
|
|
})
|
|
|
|
return template.HTML(buf.String()), err
|
|
|
|
}
|
|
|
|
return "", errors.New("wrong arguments")
|
|
|
|
}
|
|
|
|
return "", errors.New("wrong argument count")
|
|
|
|
},
|
|
|
|
"default": func(dflt interface{}, given ...interface{}) interface{} {
|
|
|
|
if len(given) == 0 {
|
|
|
|
return dflt
|
|
|
|
}
|
|
|
|
g := reflect.ValueOf(given[0])
|
|
|
|
if !g.IsValid() {
|
|
|
|
return dflt
|
|
|
|
}
|
|
|
|
set := false
|
|
|
|
switch g.Kind() {
|
|
|
|
case reflect.Bool:
|
|
|
|
set = true
|
|
|
|
case reflect.String, reflect.Array, reflect.Slice, reflect.Map:
|
|
|
|
set = g.Len() != 0
|
|
|
|
case reflect.Int:
|
|
|
|
set = g.Int() != 0
|
|
|
|
default:
|
|
|
|
set = !g.IsNil()
|
|
|
|
}
|
|
|
|
if set {
|
|
|
|
return given[0]
|
|
|
|
}
|
|
|
|
return dflt
|
2020-08-05 17:14:10 +00:00
|
|
|
},
|
2020-09-01 16:53:21 +00:00
|
|
|
"urlize": urlize,
|
2020-09-19 12:56:31 +00:00
|
|
|
"sort": sortedStrings,
|
2020-11-01 17:37:21 +00:00
|
|
|
"absolute": func(path string) string {
|
|
|
|
return appConfig.Server.PublicAddress + path
|
|
|
|
},
|
2020-11-03 17:00:03 +00:00
|
|
|
"blogrelative": func(blog *configBlog, path string) string {
|
2020-10-26 16:37:31 +00:00
|
|
|
return blog.getRelativePath(path)
|
2020-10-12 17:26:32 +00:00
|
|
|
},
|
2020-10-14 16:23:56 +00:00
|
|
|
"jsonFile": func(filename string) *map[string]interface{} {
|
|
|
|
parsed := &map[string]interface{}{}
|
2021-02-17 07:23:03 +00:00
|
|
|
content, err := os.ReadFile(filename)
|
2020-10-13 11:40:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-14 16:23:56 +00:00
|
|
|
err = json.Unmarshal(content, parsed)
|
2020-10-13 11:40:16 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-14 16:23:56 +00:00
|
|
|
return parsed
|
2020-10-13 11:40:16 +00:00
|
|
|
},
|
2021-01-23 16:24:47 +00:00
|
|
|
"commentsenabled": func(blog *configBlog) bool {
|
|
|
|
return blog.Comments != nil && blog.Comments.Enabled
|
|
|
|
},
|
2021-02-14 13:15:01 +00:00
|
|
|
"mentions": func(absolute string) []*mention {
|
|
|
|
mentions, _ := getWebmentions(&webmentionsRequestConfig{
|
|
|
|
target: absolute,
|
|
|
|
status: webmentionStatusApproved,
|
|
|
|
asc: true,
|
|
|
|
})
|
|
|
|
return mentions
|
|
|
|
},
|
2020-07-31 13:48:01 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 18:36:19 +00:00
|
|
|
templates = map[string]*template.Template{}
|
2020-07-31 17:46:12 +00:00
|
|
|
|
2020-09-20 15:47:54 +00:00
|
|
|
baseTemplatePath := path.Join(templatesDir, templateBase+templatesExt)
|
2021-02-08 17:51:07 +00:00
|
|
|
if err := filepath.Walk(templatesDir, func(p string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-20 15:47:54 +00:00
|
|
|
if info.Mode().IsRegular() && path.Ext(p) == templatesExt {
|
2021-02-08 17:51:07 +00:00
|
|
|
if name := strings.TrimSuffix(path.Base(p), templatesExt); name != templateBase {
|
|
|
|
if templates[name], err = template.New(name).Funcs(templateFunctions).ParseFiles(baseTemplatePath, p); err != nil {
|
2020-09-20 15:47:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-02-08 17:51:07 +00:00
|
|
|
}); err != nil {
|
2020-09-20 15:47:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-31 19:02:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 16:47:23 +00:00
|
|
|
type renderData struct {
|
2021-01-17 11:53:07 +00:00
|
|
|
BlogString string
|
2020-11-01 17:37:21 +00:00
|
|
|
Canonical string
|
2020-10-12 16:47:23 +00:00
|
|
|
Blog *configBlog
|
|
|
|
Data interface{}
|
2021-02-20 22:35:16 +00:00
|
|
|
LoggedIn bool
|
2020-10-12 16:47:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-20 22:35:16 +00:00
|
|
|
func render(w http.ResponseWriter, r *http.Request, template string, data *renderData) {
|
2021-03-10 17:47:56 +00:00
|
|
|
// Server timing
|
|
|
|
t := servertiming.FromContext(r.Context()).NewMetric("r").Start()
|
2020-10-12 16:47:23 +00:00
|
|
|
// Check render data
|
|
|
|
if data.Blog == nil {
|
2021-01-17 11:53:07 +00:00
|
|
|
if len(data.BlogString) == 0 {
|
|
|
|
data.BlogString = appConfig.DefaultBlog
|
|
|
|
}
|
|
|
|
data.Blog = appConfig.Blogs[data.BlogString]
|
|
|
|
}
|
|
|
|
if data.BlogString == "" {
|
|
|
|
for s, b := range appConfig.Blogs {
|
|
|
|
if b == data.Blog {
|
|
|
|
data.BlogString = s
|
|
|
|
break
|
|
|
|
}
|
2020-10-12 16:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 22:44:03 +00:00
|
|
|
if data.Data == nil {
|
|
|
|
data.Data = map[string]interface{}{}
|
|
|
|
}
|
2021-02-20 22:35:16 +00:00
|
|
|
// Check login
|
|
|
|
if loggedIn, ok := r.Context().Value(loggedInKey).(bool); ok && loggedIn {
|
|
|
|
data.LoggedIn = true
|
|
|
|
}
|
|
|
|
// Minify and write response
|
|
|
|
mw := minifier.Writer(contentTypeHTML, w)
|
|
|
|
defer func() {
|
|
|
|
_ = mw.Close()
|
|
|
|
}()
|
|
|
|
err := templates[template].ExecuteTemplate(mw, template, data)
|
2020-07-31 17:46:12 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2020-11-22 19:30:02 +00:00
|
|
|
return
|
2020-07-31 17:46:12 +00:00
|
|
|
}
|
2021-02-16 15:26:21 +00:00
|
|
|
// Set content type
|
2020-10-06 17:07:48 +00:00
|
|
|
w.Header().Set(contentType, contentTypeHTMLUTF8)
|
2021-03-10 17:47:56 +00:00
|
|
|
// Server timing
|
|
|
|
t.Stop()
|
2020-07-31 17:46:12 +00:00
|
|
|
}
|