2020-07-31 13:48:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-07-31 17:46:12 +00:00
|
|
|
"bytes"
|
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"
|
|
|
|
"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"
|
|
|
|
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
|
|
|
|
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-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-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()
|
|
|
|
},
|
|
|
|
// Others
|
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-10-12 17:46:14 +00:00
|
|
|
"longDate": func(date string, localeString string) string {
|
|
|
|
d, err := dateparse.ParseIn(date, time.Local)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
ml := monday.Locale(localeString)
|
|
|
|
return monday.Format(d, monday.LongFormatsByLocale[ml], ml)
|
|
|
|
},
|
2020-09-20 17:10:01 +00:00
|
|
|
"asset": assetFile,
|
2020-10-12 17:12:43 +00:00
|
|
|
"string": getTemplateStringVariant,
|
2020-10-12 16:47:23 +00:00
|
|
|
"include": func(templateName string, blog *configBlog, data interface{}) (template.HTML, error) {
|
2020-08-05 17:14:10 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2020-10-12 16:47:23 +00:00
|
|
|
err := templates[templateName].ExecuteTemplate(buf, templateName, &renderData{
|
|
|
|
Blog: blog,
|
|
|
|
Data: data,
|
|
|
|
})
|
2020-08-05 17:14:10 +00:00
|
|
|
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-10-12 17:26:32 +00:00
|
|
|
"blogRelative": func(blog *configBlog, path string) string {
|
2020-10-16 18:34:34 +00:00
|
|
|
if !strings.HasPrefix(path, "/") {
|
|
|
|
path = "/" + path
|
|
|
|
}
|
2020-10-12 17:26:32 +00:00
|
|
|
if blog.Path != "/" {
|
|
|
|
return blog.Path + path
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
},
|
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-07-31 19:02:47 +00:00
|
|
|
templates = make(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
|
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
// 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())
|
|
|
|
}
|