diff --git a/templates.go b/templates.go index 413f76a..6af5b8f 100644 --- a/templates.go +++ b/templates.go @@ -1,185 +1,183 @@ package writefreely import ( "fmt" "github.com/dustin/go-humanize" "github.com/writeas/web-core/l10n" "github.com/writeas/web-core/log" "html/template" "io" "io/ioutil" "net/http" "os" "path/filepath" "strings" ) 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, } ) const ( templatesDir = "templates" pagesDir = "pages" ) func showUserPage(w http.ResponseWriter, name string, obj interface{}) { if obj == nil { log.Error("showUserPage: data is nil!") return } if err := userPages[filepath.Join("user", name+".tmpl")].ExecuteTemplate(w, name, obj); err != nil { log.Error("Error parsing %s: %v", name, err) } } func initTemplate(name string) { if debugging { log.Info(" %s%s%s.tmpl", templatesDir, string(filepath.Separator), name) } + files := []string{ + filepath.Join(templatesDir, name+".tmpl"), + filepath.Join(templatesDir, "include", "footer.tmpl"), + filepath.Join(templatesDir, "base.tmpl"), + } if name == "collection" || name == "collection-tags" { // These pages list out collection posts, so we also parse templatesDir + "include/posts.tmpl" - templates[name] = template.Must(template.New("").Funcs(funcMap).ParseFiles( - filepath.Join(templatesDir, name+".tmpl"), - filepath.Join(templatesDir, "include", "posts.tmpl"), - filepath.Join(templatesDir, "include", "footer.tmpl"), - filepath.Join(templatesDir, "base.tmpl"), - )) - } else { - templates[name] = template.Must(template.New("").Funcs(funcMap).ParseFiles( - filepath.Join(templatesDir, name+".tmpl"), - filepath.Join(templatesDir, "include", "footer.tmpl"), - filepath.Join(templatesDir, "base.tmpl"), - )) + files = append(files, filepath.Join(templatesDir, "include", "posts.tmpl")) + } + if name == "collection" || name == "collection-tags" || name == "collection-post" || name == "post" { + files = append(files, filepath.Join(templatesDir, "include", "post-render.tmpl")) } + templates[name] = template.Must(template.New("").Funcs(funcMap).ParseFiles(files...)) } func initPage(path, key string) { if debugging { log.Info(" %s", key) } pages[key] = template.Must(template.New("").Funcs(funcMap).ParseFiles( path, filepath.Join(templatesDir, "include", "footer.tmpl"), filepath.Join(templatesDir, "base.tmpl"), )) } func initUserPage(path, key string) { if debugging { log.Info(" %s", key) } userPages[key] = template.Must(template.New(key).Funcs(funcMap).ParseFiles( path, filepath.Join(templatesDir, "user", "include", "header.tmpl"), filepath.Join(templatesDir, "user", "include", "footer.tmpl"), )) } func initTemplates() error { log.Info("Loading templates...") tmplFiles, err := ioutil.ReadDir(templatesDir) if err != nil { return err } for _, f := range tmplFiles { if !f.IsDir() && !strings.HasPrefix(f.Name(), ".") { parts := strings.Split(f.Name(), ".") key := parts[0] initTemplate(key) } } log.Info("Loading pages...") // Initialize all static pages that use the base template filepath.Walk(pagesDir, func(path string, i os.FileInfo, err error) error { if !i.IsDir() && !strings.HasPrefix(i.Name(), ".") { parts := strings.Split(path, string(filepath.Separator)) key := i.Name() if len(parts) > 2 { key = fmt.Sprintf("%s%s%s", parts[1], string(filepath.Separator), i.Name()) } initPage(path, key) } return nil }) log.Info("Loading user pages...") // Initialize all user pages that use base templates filepath.Walk(filepath.Join(templatesDir, "user"), func(path string, f os.FileInfo, err error) error { if !f.IsDir() && !strings.HasPrefix(f.Name(), ".") { parts := strings.Split(path, string(filepath.Separator)) key := f.Name() if len(parts) > 2 { key = filepath.Join(parts[1], f.Name()) } initUserPage(path, key) } 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] } s = strings.Replace(s, "write.as", "write freely", 1) return template.HTML(s) } diff --git a/templates/collection-post.tmpl b/templates/collection-post.tmpl index 6f34863..818ec98 100644 --- a/templates/collection-post.tmpl +++ b/templates/collection-post.tmpl @@ -1,134 +1,128 @@ {{define "post"}} {{.PlainDisplayTitle}} — {{.Collection.DisplayTitle}} {{if gt .Views 1}} {{end}} {{if gt (len .Images) 0}}{{else}}{{end}} {{range .Images}}{{else}}{{end}} {{if .Collection.StyleSheet}}{{end}} + {{if .Collection.RenderMathJax}} - - {{end}} + + {{template "mathjax" . }} + {{end}} + + + {{template "highlighting" .}}

{{if .IsScheduled}}

Scheduled

{{end}}{{if .Title.String}}

{{.FormattedDisplayTitle}}

{{end}}
{{.HTMLContent}}
{{ if .Collection.ShowFooterBranding }} {{ end }} {{if .Collection.CanShowScript}} {{range .Collection.ExternalScripts}}{{end}} {{if .Collection.Script}}{{end}} {{end}} {{end}} diff --git a/templates/collection-tags.tmpl b/templates/collection-tags.tmpl index 021febc..fd37eed 100644 --- a/templates/collection-tags.tmpl +++ b/templates/collection-tags.tmpl @@ -1,200 +1,194 @@ {{define "collection-tags"}} {{.Tag}} — {{.Collection.DisplayTitle}} {{if not .Collection.IsPrivate}}{{end}} {{if gt .Views 1}} {{end}} {{if .Collection.StyleSheet}}{{end}} + {{if .Collection.RenderMathJax}} - - {{end}} + + {{template "mathjax" .}} + {{end}} + + + {{template "highlighting" . }}

{{.Collection.DisplayTitle}}

{{if .Posts}}
{{else}}
{{end}}

{{.Tag}}

{{template "posts" .}} {{if .Posts}}
{{else}}{{end}} {{ if .Collection.ShowFooterBranding }} {{ end }} {{if .CanShowScript}} {{range .ExternalScripts}}{{end}} {{if .Collection.Script}}{{end}} {{end}} {{if .IsOwner}} {{end}} {{end}} diff --git a/templates/collection.tmpl b/templates/collection.tmpl index ef2401d..a77ba5a 100644 --- a/templates/collection.tmpl +++ b/templates/collection.tmpl @@ -1,235 +1,229 @@ {{define "collection"}} {{.DisplayTitle}}{{if not .SingleUser}} — {{.SiteName}}{{end}} {{if gt .CurrentPage 1}}{{end}} {{if lt .CurrentPage .TotalPages}}{{end}} {{if not .IsPrivate}}{{end}} {{if .StyleSheet}}{{end}} + {{if .RenderMathJax}} - - {{end}} + + {{template "mathjax" .}} + {{end}} + + + {{template "highlighting" . }} {{if or .IsOwner .SingleUser}}{{end}}

{{if .Posts}}{{else}}write.as {{end}}{{.DisplayTitle}}

{{if .Description}}

{{.Description}}

{{end}} {{/*if not .Public/*}} {{/*end*/}} {{if .PinnedPosts}} {{end}}
{{if .Posts}}
{{else}}
{{end}} {{if .IsWelcome}}

Welcome, {{.Username}}!

This is your new blog.

Start writing, or customize your blog.

Check out our writing guide to see what else you can do, and get in touch anytime with questions or feedback.

{{end}} {{template "posts" .}} {{if gt .TotalPages 1}}{{end}} {{if .Posts}}
{{else}}{{end}} {{if .ShowFooterBranding }} {{ end }} - + {{if .CanShowScript}} {{range .ExternalScripts}}{{end}} {{if .Script}}{{end}} {{end}} {{end}} diff --git a/templates/include/post-render.tmpl b/templates/include/post-render.tmpl new file mode 100644 index 0000000..8ff2bbb --- /dev/null +++ b/templates/include/post-render.tmpl @@ -0,0 +1,50 @@ + +{{define "highlighting"}} + +{{end}} + + +{{define "mathjax"}} + + +{{end}} diff --git a/templates/post.tmpl b/templates/post.tmpl index 22fa294..5a187b4 100644 --- a/templates/post.tmpl +++ b/templates/post.tmpl @@ -1,97 +1,98 @@ {{define "post"}} {{if .Title}}{{.Title}}{{else}}{{.GenTitle}}{{end}} — {{.SiteName}} {{if .IsCode}} - + {{end}} {{if gt .Views 1}} {{end}} {{if .Author}}{{end}} - + + {{template "highlighting" .}}

{{.SiteName}}

{{if .Title}}

{{.Title}}

{{end}}{{ if .IsPlainText }}

{{.Content}}

{{ else }}
{{.HTMLContent}}
{{ end }}
{{if .IsCode}} - + {{else}} {{if .IsPlainText}}{{end}} {{end}} {{end}}