2020-07-31 13:48:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-07-31 17:46:12 +00:00
|
|
|
"bytes"
|
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"
|
2020-10-13 11:40:16 +00:00
|
|
|
"io/ioutil"
|
2020-07-31 13:48:01 +00:00
|
|
|
"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"
|
2020-10-12 17:46:14 +00:00
|
|
|
"github.com/goodsign/monday"
|
2020-07-31 13:48:01 +00:00
|
|
|
)
|
|
|
|
|
2020-09-20 15:47:54 +00:00
|
|
|
const templatesDir = "templates"
|
|
|
|
const templatesExt = ".gohtml"
|
|
|
|
|
|
|
|
const templateBase = "base"
|
2020-07-31 19:02:47 +00:00
|
|
|
const templatePost = "post"
|
|
|
|
const templateError = "error"
|
2020-08-05 17:14:10 +00:00
|
|
|
const templateIndex = "index"
|
2020-08-31 19:12:43 +00:00
|
|
|
const templateTaxonomy = "taxonomy"
|
2020-11-15 10:34:48 +00:00
|
|
|
const templateSearch = "search"
|
2020-11-16 17:34:29 +00:00
|
|
|
const templateSummary = "summary"
|
|
|
|
const templatePhotosSummary = "photosummary"
|
2020-12-12 22:44:03 +00:00
|
|
|
const templateEditor = "editor"
|
2020-12-15 16:40:14 +00:00
|
|
|
const templateLogin = "login"
|
2020-12-23 15:53:10 +00:00
|
|
|
const templateStaticHome = "statichome"
|
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)
|
|
|
|
},
|
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-11-06 17:45:31 +00:00
|
|
|
"postmentions": func(p *post) []*mention {
|
|
|
|
mentions, _ := getWebmentions(&webmentionsRequestConfig{
|
2020-11-16 17:34:29 +00:00
|
|
|
target: p.fullURL(),
|
2020-11-06 17:45:31 +00:00
|
|
|
status: webmentionStatusApproved,
|
2020-11-11 15:58:44 +00:00
|
|
|
asc: true,
|
2020-11-06 17:45:31 +00:00
|
|
|
})
|
|
|
|
return mentions
|
|
|
|
},
|
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
|
2020-09-18 10:34:52 +00:00
|
|
|
"dateformat": func(date string, format string) string {
|
2020-12-16 19:21:35 +00:00
|
|
|
d, err := dateparse.ParseLocal(date)
|
2020-09-18 10:34:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2020-12-16 20:24:53 +00:00
|
|
|
return d.Local().Format(format)
|
2020-09-18 10:34:52 +00:00
|
|
|
},
|
2020-11-03 17:00:03 +00:00
|
|
|
"longdate": func(date string, localeString string) string {
|
2020-12-16 19:21:35 +00:00
|
|
|
d, err := dateparse.ParseLocal(date)
|
2020-10-12 17:46:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
ml := monday.Locale(localeString)
|
2020-12-16 20:24:53 +00:00
|
|
|
return monday.Format(d.Local(), monday.LongFormatsByLocale[ml], ml)
|
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)
|
|
|
|
},
|
2020-09-20 17:10:01 +00:00
|
|
|
"asset": assetFile,
|
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{}{}
|
2020-10-13 11:40:16 +00:00
|
|
|
content, err := ioutil.ReadFile(filename)
|
|
|
|
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
|
|
|
},
|
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)
|
|
|
|
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
|
2020-07-31 19:02:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 16:47:23 +00:00
|
|
|
type renderData struct {
|
|
|
|
blogString string
|
2020-11-01 17:37:21 +00:00
|
|
|
Canonical string
|
2020-10-12 16:47:23 +00:00
|
|
|
Blog *configBlog
|
|
|
|
Data interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func render(w http.ResponseWriter, template string, data *renderData) {
|
|
|
|
// Check render data
|
|
|
|
if data.Blog == nil {
|
|
|
|
if len(data.blogString) == 0 {
|
|
|
|
data.blogString = appConfig.DefaultBlog
|
|
|
|
}
|
|
|
|
data.Blog = appConfig.Blogs[data.blogString]
|
|
|
|
}
|
2020-12-12 22:44:03 +00:00
|
|
|
if data.Data == nil {
|
|
|
|
data.Data = map[string]interface{}{}
|
|
|
|
}
|
2020-07-31 17:46:12 +00:00
|
|
|
// 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)
|
2020-11-22 19:30:02 +00:00
|
|
|
return
|
2020-07-31 17:46:12 +00:00
|
|
|
}
|
|
|
|
// Set content type (needed for minification middleware
|
2020-10-06 17:07:48 +00:00
|
|
|
w.Header().Set(contentType, contentTypeHTMLUTF8)
|
2020-07-31 17:46:12 +00:00
|
|
|
// Write buffered response
|
|
|
|
_, _ = w.Write(buffer.Bytes())
|
|
|
|
}
|