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-07-31 13:48:01 +00:00
|
|
|
"html/template"
|
2020-07-31 17:46:12 +00:00
|
|
|
"net/http"
|
2020-09-20 15:47:54 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-10-06 17:07:48 +00:00
|
|
|
|
2021-06-28 20:17:18 +00:00
|
|
|
"go.goblog.app/app/pkgs/contenttype"
|
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"
|
2021-06-28 20:17:18 +00:00
|
|
|
templateEditorFiles = "editorfiles"
|
2021-02-20 14:42:45 +00:00
|
|
|
templateLogin = "login"
|
|
|
|
templateStaticHome = "statichome"
|
|
|
|
templateBlogStats = "blogstats"
|
2021-05-09 07:08:31 +00:00
|
|
|
templateBlogStatsTable = "blogstatstable"
|
2021-02-20 14:42:45 +00:00
|
|
|
templateComment = "comment"
|
|
|
|
templateCaptcha = "captcha"
|
|
|
|
templateCommentsAdmin = "commentsadmin"
|
|
|
|
templateNotificationsAdmin = "notificationsadmin"
|
2021-02-20 22:35:16 +00:00
|
|
|
templateWebmentionAdmin = "webmentionadmin"
|
2021-05-08 19:22:48 +00:00
|
|
|
templateBlogroll = "blogroll"
|
2021-07-06 19:06:39 +00:00
|
|
|
templateGeoMap = "geomap"
|
2021-07-22 11:41:52 +00:00
|
|
|
templateContact = "contact"
|
2021-02-20 14:42:45 +00:00
|
|
|
)
|
2020-07-31 13:48:01 +00:00
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) initRendering() error {
|
|
|
|
a.templates = map[string]*template.Template{}
|
2021-03-11 08:16:19 +00:00
|
|
|
templateFunctions := template.FuncMap{
|
2021-08-04 21:26:38 +00:00
|
|
|
"md": a.safeRenderMarkdownAsHTML,
|
|
|
|
"mdtitle": a.renderMdTitle,
|
|
|
|
"html": wrapStringAsHTML,
|
2020-10-18 09:54:29 +00:00
|
|
|
// Post specific
|
2021-06-18 12:32:03 +00:00
|
|
|
"ps": postParameter,
|
|
|
|
"content": a.postHtml,
|
|
|
|
"summary": a.postSummary,
|
|
|
|
"translations": a.postTranslations,
|
|
|
|
"shorturl": a.shortPostURL,
|
2021-07-31 11:20:51 +00:00
|
|
|
"showfull": a.showFull,
|
|
|
|
"geouri": a.geoURI,
|
|
|
|
"replylink": a.replyLink,
|
|
|
|
"replytitle": a.replyTitle,
|
|
|
|
"likelink": a.likeLink,
|
|
|
|
"liketitle": a.likeTitle,
|
|
|
|
"photolinks": a.photoLinks,
|
2020-10-16 18:34:34 +00:00
|
|
|
// Others
|
2021-08-01 17:27:21 +00:00
|
|
|
"dateformat": dateFormat,
|
|
|
|
"isodate": isoDateFormat,
|
|
|
|
"unixtodate": unixToLocalDateString,
|
|
|
|
"now": localNowString,
|
|
|
|
"asset": a.assetFileName,
|
|
|
|
"string": a.ts.GetTemplateStringVariantFunc(),
|
|
|
|
"include": a.includeRenderedTemplate,
|
|
|
|
"urlize": urlize,
|
|
|
|
"sort": sortedStrings,
|
|
|
|
"absolute": a.getFullAddress,
|
|
|
|
"mentions": a.db.getWebmentionsByAddress,
|
|
|
|
"geotitle": a.geoTitle,
|
|
|
|
"geolink": geoOSMLink,
|
|
|
|
"opensearch": openSearchUrl,
|
|
|
|
"mbytes": mBytesString,
|
|
|
|
"editortemplate": a.editorPostTemplate,
|
|
|
|
"editorpostdesc": a.editorPostDesc,
|
2020-07-31 13:48:01 +00:00
|
|
|
}
|
2021-03-11 08:16:19 +00:00
|
|
|
baseTemplate, err := template.New("base").Funcs(templateFunctions).ParseFiles(path.Join(templatesDir, templateBase+templatesExt))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = filepath.Walk(templatesDir, func(p string, info os.FileInfo, err error) error {
|
2021-02-08 17:51:07 +00:00
|
|
|
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 {
|
2021-06-06 12:39:42 +00:00
|
|
|
if a.templates[name], err = template.Must(baseTemplate.Clone()).New(name).ParseFiles(p); err != nil {
|
2020-09-20 15:47:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-03-11 08:16:19 +00:00
|
|
|
})
|
|
|
|
if 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-04-23 17:36:57 +00:00
|
|
|
BlogString string
|
|
|
|
Canonical string
|
2021-05-09 12:42:53 +00:00
|
|
|
TorAddress string
|
2021-04-23 17:36:57 +00:00
|
|
|
Blog *configBlog
|
2021-06-18 12:32:03 +00:00
|
|
|
User *configUser
|
2021-04-23 17:36:57 +00:00
|
|
|
Data interface{}
|
|
|
|
CommentsEnabled bool
|
|
|
|
WebmentionReceivingEnabled bool
|
2021-05-09 12:42:53 +00:00
|
|
|
TorUsed bool
|
2021-07-23 10:59:24 +00:00
|
|
|
EasterEgg bool
|
2021-07-25 08:53:12 +00:00
|
|
|
// Not directly accessible
|
|
|
|
app *goBlog
|
|
|
|
req *http.Request
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *renderData) LoggedIn() bool {
|
|
|
|
return d.app.isLoggedIn(d.req)
|
2020-10-12 16:47:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) render(w http.ResponseWriter, r *http.Request, template string, data *renderData) {
|
2021-06-19 06:37:16 +00:00
|
|
|
a.renderWithStatusCode(w, r, http.StatusOK, template, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) renderWithStatusCode(w http.ResponseWriter, r *http.Request, statusCode int, template string, data *renderData) {
|
2020-10-12 16:47:23 +00:00
|
|
|
// Check render data
|
2021-06-23 17:20:50 +00:00
|
|
|
a.checkRenderData(r, data)
|
|
|
|
// Set content type
|
|
|
|
w.Header().Set(contentType, contenttype.HTMLUTF8)
|
|
|
|
// Minify and write response
|
|
|
|
var tw bytes.Buffer
|
|
|
|
err := a.templates[template].ExecuteTemplate(&tw, template, data)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
_, err = a.min.Write(w, contenttype.HTML, tw.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) checkRenderData(r *http.Request, data *renderData) {
|
2021-07-25 08:53:12 +00:00
|
|
|
if data.app == nil {
|
|
|
|
data.app = a
|
|
|
|
}
|
|
|
|
if data.req == nil {
|
|
|
|
data.req = r
|
|
|
|
}
|
2021-06-23 17:20:50 +00:00
|
|
|
// User
|
2021-06-18 12:32:03 +00:00
|
|
|
if data.User == nil {
|
|
|
|
data.User = a.cfg.User
|
|
|
|
}
|
2021-06-23 17:20:50 +00:00
|
|
|
// Blog
|
2020-10-12 16:47:23 +00:00
|
|
|
if data.Blog == nil {
|
2021-06-23 17:20:50 +00:00
|
|
|
if data.BlogString == "" {
|
2021-06-06 12:39:42 +00:00
|
|
|
data.BlogString = a.cfg.DefaultBlog
|
2021-01-17 11:53:07 +00:00
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
data.Blog = a.cfg.Blogs[data.BlogString]
|
2021-01-17 11:53:07 +00:00
|
|
|
}
|
|
|
|
if data.BlogString == "" {
|
2021-06-06 12:39:42 +00:00
|
|
|
for s, b := range a.cfg.Blogs {
|
2021-01-17 11:53:07 +00:00
|
|
|
if b == data.Blog {
|
|
|
|
data.BlogString = s
|
|
|
|
break
|
|
|
|
}
|
2020-10-12 16:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-23 17:20:50 +00:00
|
|
|
// Tor
|
2021-06-06 12:39:42 +00:00
|
|
|
if a.cfg.Server.Tor && a.torAddress != "" {
|
2021-08-02 18:43:24 +00:00
|
|
|
data.TorAddress = a.torAddress + r.RequestURI
|
2021-05-09 12:42:53 +00:00
|
|
|
}
|
2021-06-23 17:20:50 +00:00
|
|
|
if torUsed, ok := r.Context().Value(torUsedKey).(bool); ok && torUsed {
|
|
|
|
data.TorUsed = true
|
2020-12-12 22:44:03 +00:00
|
|
|
}
|
2021-04-21 18:17:26 +00:00
|
|
|
// Check if comments enabled
|
|
|
|
data.CommentsEnabled = data.Blog.Comments != nil && data.Blog.Comments.Enabled
|
2021-04-23 17:36:57 +00:00
|
|
|
// Check if able to receive webmentions
|
2021-06-06 12:39:42 +00:00
|
|
|
data.WebmentionReceivingEnabled = a.cfg.Webmention == nil || !a.cfg.Webmention.DisableReceiving
|
2021-07-23 10:59:24 +00:00
|
|
|
// Easter egg
|
|
|
|
if ee := a.cfg.EasterEgg; ee != nil && ee.Enabled {
|
|
|
|
data.EasterEgg = true
|
|
|
|
}
|
2021-06-23 17:20:50 +00:00
|
|
|
// Data
|
|
|
|
if data.Data == nil {
|
|
|
|
data.Data = map[string]interface{}{}
|
2020-07-31 17:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-18 12:32:03 +00:00
|
|
|
|
|
|
|
func (a *goBlog) includeRenderedTemplate(templateName string, data ...interface{}) (template.HTML, error) {
|
|
|
|
if l := len(data); l < 1 || l > 2 {
|
|
|
|
return "", errors.New("wrong argument count")
|
|
|
|
}
|
|
|
|
if rd, ok := data[0].(*renderData); ok {
|
|
|
|
if len(data) == 2 {
|
|
|
|
nrd := *rd
|
|
|
|
nrd.Data = data[1]
|
|
|
|
rd = &nrd
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := a.templates[templateName].ExecuteTemplate(&buf, templateName, rd)
|
|
|
|
return template.HTML(buf.String()), err
|
|
|
|
}
|
|
|
|
return "", errors.New("wrong arguments")
|
|
|
|
}
|