2018-12-24 18:45:15 +01:00
|
|
|
/*
|
2022-11-11 05:49:16 +01:00
|
|
|
* Copyright © 2018-2021 Musing Studio LLC.
|
2018-12-24 18:45:15 +01:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
2018-12-31 07:05:26 +01:00
|
|
|
|
2018-11-08 05:50:50 +01:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
2020-08-19 22:24:37 +02:00
|
|
|
"errors"
|
2018-11-08 05:50:50 +01:00
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-10-25 21:04:24 +02:00
|
|
|
|
|
|
|
"github.com/dustin/go-humanize"
|
|
|
|
"github.com/writeas/web-core/l10n"
|
|
|
|
"github.com/writeas/web-core/log"
|
2021-04-06 23:24:07 +02:00
|
|
|
"github.com/writefreely/writefreely/config"
|
2018-11-08 05:50:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
templates = map[string]*template.Template{}
|
|
|
|
pages = map[string]*template.Template{}
|
|
|
|
userPages = map[string]*template.Template{}
|
|
|
|
funcMap = template.FuncMap{
|
|
|
|
"largeNumFmt": largeNumFmt,
|
|
|
|
"pluralize": pluralize,
|
|
|
|
"isRTL": isRTL,
|
|
|
|
"isLTR": isLTR,
|
|
|
|
"localstr": localStr,
|
|
|
|
"localhtml": localHTML,
|
|
|
|
"tolower": strings.ToLower,
|
2020-01-15 19:37:57 +01:00
|
|
|
"title": strings.Title,
|
2020-08-19 22:24:37 +02:00
|
|
|
"hasPrefix": strings.HasPrefix,
|
|
|
|
"hasSuffix": strings.HasSuffix,
|
|
|
|
"dict": dict,
|
2018-11-08 05:50:50 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-11-09 20:40:47 +01:00
|
|
|
templatesDir = "templates"
|
|
|
|
pagesDir = "pages"
|
2018-11-08 05:50:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func showUserPage(w http.ResponseWriter, name string, obj interface{}) {
|
|
|
|
if obj == nil {
|
|
|
|
log.Error("showUserPage: data is nil!")
|
|
|
|
return
|
|
|
|
}
|
2018-11-09 20:40:47 +01:00
|
|
|
if err := userPages[filepath.Join("user", name+".tmpl")].ExecuteTemplate(w, name, obj); err != nil {
|
2018-11-08 05:50:50 +01:00
|
|
|
log.Error("Error parsing %s: %v", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 00:57:04 +01:00
|
|
|
func initTemplate(parentDir, name string) {
|
2018-11-08 05:50:50 +01:00
|
|
|
if debugging {
|
2019-01-19 00:57:04 +01:00
|
|
|
log.Info(" " + filepath.Join(parentDir, templatesDir, name+".tmpl"))
|
2018-11-08 05:50:50 +01:00
|
|
|
}
|
|
|
|
|
2018-11-23 18:14:37 +01:00
|
|
|
files := []string{
|
2019-01-19 00:57:04 +01:00
|
|
|
filepath.Join(parentDir, templatesDir, name+".tmpl"),
|
|
|
|
filepath.Join(parentDir, templatesDir, "include", "footer.tmpl"),
|
|
|
|
filepath.Join(parentDir, templatesDir, "base.tmpl"),
|
2019-11-12 00:21:45 +01:00
|
|
|
filepath.Join(parentDir, templatesDir, "user", "include", "silenced.tmpl"),
|
2018-11-23 18:14:37 +01:00
|
|
|
}
|
2021-06-09 17:09:53 +02:00
|
|
|
if name == "collection" || name == "collection-tags" || name == "chorus-collection" || name == "read" {
|
2018-11-08 05:50:50 +01:00
|
|
|
// These pages list out collection posts, so we also parse templatesDir + "include/posts.tmpl"
|
2019-01-19 00:57:04 +01:00
|
|
|
files = append(files, filepath.Join(parentDir, templatesDir, "include", "posts.tmpl"))
|
2018-11-23 18:14:37 +01:00
|
|
|
}
|
2019-08-07 15:00:16 +02:00
|
|
|
if name == "chorus-collection" || name == "chorus-collection-post" {
|
|
|
|
files = append(files, filepath.Join(parentDir, templatesDir, "user", "include", "header.tmpl"))
|
|
|
|
}
|
|
|
|
if name == "collection" || name == "collection-tags" || name == "collection-post" || name == "post" || name == "chorus-collection" || name == "chorus-collection-post" {
|
2019-01-19 00:57:04 +01:00
|
|
|
files = append(files, filepath.Join(parentDir, templatesDir, "include", "post-render.tmpl"))
|
2018-11-08 05:50:50 +01:00
|
|
|
}
|
2018-11-23 18:14:37 +01:00
|
|
|
templates[name] = template.Must(template.New("").Funcs(funcMap).ParseFiles(files...))
|
2018-11-08 05:50:50 +01:00
|
|
|
}
|
|
|
|
|
2019-01-19 00:57:04 +01:00
|
|
|
func initPage(parentDir, path, key string) {
|
2018-11-08 05:50:50 +01:00
|
|
|
if debugging {
|
2019-01-19 00:57:04 +01:00
|
|
|
log.Info(" [%s] %s", key, path)
|
2018-11-08 05:50:50 +01:00
|
|
|
}
|
|
|
|
|
2020-08-19 19:31:07 +02:00
|
|
|
files := []string{
|
2018-11-08 05:50:50 +01:00
|
|
|
path,
|
2019-01-19 00:57:04 +01:00
|
|
|
filepath.Join(parentDir, templatesDir, "include", "footer.tmpl"),
|
|
|
|
filepath.Join(parentDir, templatesDir, "base.tmpl"),
|
2019-11-12 00:21:45 +01:00
|
|
|
filepath.Join(parentDir, templatesDir, "user", "include", "silenced.tmpl"),
|
2020-08-19 19:31:07 +02:00
|
|
|
}
|
|
|
|
|
2020-08-19 21:20:13 +02:00
|
|
|
if key == "login.tmpl" || key == "landing.tmpl" || key == "signup.tmpl" {
|
2020-08-19 19:31:07 +02:00
|
|
|
files = append(files, filepath.Join(parentDir, templatesDir, "include", "oauth.tmpl"))
|
|
|
|
}
|
|
|
|
|
|
|
|
pages[key] = template.Must(template.New("").Funcs(funcMap).ParseFiles(files...))
|
2018-11-08 05:50:50 +01:00
|
|
|
}
|
|
|
|
|
2019-01-19 00:57:04 +01:00
|
|
|
func initUserPage(parentDir, path, key string) {
|
2018-11-08 05:50:50 +01:00
|
|
|
if debugging {
|
2019-01-19 00:57:04 +01:00
|
|
|
log.Info(" [%s] %s", key, path)
|
2018-11-08 05:50:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
userPages[key] = template.Must(template.New(key).Funcs(funcMap).ParseFiles(
|
|
|
|
path,
|
2019-01-19 00:57:04 +01:00
|
|
|
filepath.Join(parentDir, templatesDir, "user", "include", "header.tmpl"),
|
|
|
|
filepath.Join(parentDir, templatesDir, "user", "include", "footer.tmpl"),
|
2019-11-12 00:21:45 +01:00
|
|
|
filepath.Join(parentDir, templatesDir, "user", "include", "silenced.tmpl"),
|
2020-08-19 22:24:37 +02:00
|
|
|
filepath.Join(parentDir, templatesDir, "user", "include", "nav.tmpl"),
|
2018-11-08 05:50:50 +01:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:50:23 +02:00
|
|
|
// InitTemplates loads all template files from the configured parent dir.
|
|
|
|
func InitTemplates(cfg *config.Config) error {
|
2018-11-08 05:50:50 +01:00
|
|
|
log.Info("Loading templates...")
|
2019-01-19 00:57:04 +01:00
|
|
|
tmplFiles, err := ioutil.ReadDir(filepath.Join(cfg.Server.TemplatesParentDir, templatesDir))
|
2018-11-08 05:50:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range tmplFiles {
|
|
|
|
if !f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
|
|
|
|
parts := strings.Split(f.Name(), ".")
|
|
|
|
key := parts[0]
|
2019-01-19 00:57:04 +01:00
|
|
|
initTemplate(cfg.Server.TemplatesParentDir, key)
|
2018-11-08 05:50:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Loading pages...")
|
|
|
|
// Initialize all static pages that use the base template
|
2019-01-19 00:57:04 +01:00
|
|
|
filepath.Walk(filepath.Join(cfg.Server.PagesParentDir, pagesDir), func(path string, i os.FileInfo, err error) error {
|
2018-11-08 05:50:50 +01:00
|
|
|
if !i.IsDir() && !strings.HasPrefix(i.Name(), ".") {
|
|
|
|
key := i.Name()
|
2019-01-19 00:57:04 +01:00
|
|
|
initPage(cfg.Server.PagesParentDir, path, key)
|
2018-11-08 05:50:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
log.Info("Loading user pages...")
|
|
|
|
// Initialize all user pages that use base templates
|
2019-01-19 00:57:04 +01:00
|
|
|
filepath.Walk(filepath.Join(cfg.Server.TemplatesParentDir, templatesDir, "user"), func(path string, f os.FileInfo, err error) error {
|
2018-11-08 05:50:50 +01:00
|
|
|
if !f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
|
2019-01-19 00:57:04 +01:00
|
|
|
corePath := path
|
|
|
|
if cfg.Server.TemplatesParentDir != "" {
|
|
|
|
corePath = corePath[len(cfg.Server.TemplatesParentDir)+1:]
|
|
|
|
}
|
|
|
|
parts := strings.Split(corePath, string(filepath.Separator))
|
2018-11-08 05:50:50 +01:00
|
|
|
key := f.Name()
|
|
|
|
if len(parts) > 2 {
|
2018-11-09 20:40:47 +01:00
|
|
|
key = filepath.Join(parts[1], f.Name())
|
2018-11-08 05:50:50 +01:00
|
|
|
}
|
2019-01-19 00:57:04 +01:00
|
|
|
initUserPage(cfg.Server.TemplatesParentDir, path, key)
|
2018-11-08 05:50:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// renderPage retrieves the given template and renders it to the given io.Writer.
|
|
|
|
// If something goes wrong, the error is logged and returned.
|
|
|
|
func renderPage(w io.Writer, tmpl string, data interface{}) error {
|
|
|
|
err := pages[tmpl].ExecuteTemplate(w, "base", data)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("%v", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func largeNumFmt(n int64) string {
|
|
|
|
return humanize.Comma(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func pluralize(singular, plural string, n int64) string {
|
|
|
|
if n == 1 {
|
|
|
|
return singular
|
|
|
|
}
|
|
|
|
return plural
|
|
|
|
}
|
|
|
|
|
|
|
|
func isRTL(d string) bool {
|
|
|
|
return d == "rtl"
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLTR(d string) bool {
|
|
|
|
return d == "ltr" || d == "auto"
|
|
|
|
}
|
|
|
|
|
|
|
|
func localStr(term, lang string) string {
|
|
|
|
s := l10n.Strings(lang)[term]
|
|
|
|
if s == "" {
|
|
|
|
s = l10n.Strings("")[term]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func localHTML(term, lang string) template.HTML {
|
|
|
|
s := l10n.Strings(lang)[term]
|
|
|
|
if s == "" {
|
|
|
|
s = l10n.Strings("")[term]
|
|
|
|
}
|
2019-04-12 03:33:33 +02:00
|
|
|
s = strings.Replace(s, "write.as", "<a href=\"https://writefreely.org\">writefreely</a>", 1)
|
2018-11-08 05:50:50 +01:00
|
|
|
return template.HTML(s)
|
|
|
|
}
|
2020-08-19 22:24:37 +02:00
|
|
|
|
|
|
|
// from: https://stackoverflow.com/a/18276968/1549194
|
|
|
|
func dict(values ...interface{}) (map[string]interface{}, error) {
|
|
|
|
if len(values)%2 != 0 {
|
|
|
|
return nil, errors.New("dict: invalid number of parameters")
|
|
|
|
}
|
|
|
|
dict := make(map[string]interface{}, len(values)/2)
|
|
|
|
for i := 0; i < len(values); i += 2 {
|
|
|
|
key, ok := values[i].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("dict: keys must be strings")
|
|
|
|
}
|
|
|
|
dict[key] = values[i+1]
|
|
|
|
}
|
|
|
|
return dict, nil
|
|
|
|
}
|