diff --git a/errors.go b/errors.go index ff8fec6..9d47f32 100644 --- a/errors.go +++ b/errors.go @@ -6,16 +6,16 @@ import ( ) type errorData struct { - Blog string Title string Message string } func serve404(w http.ResponseWriter, r *http.Request) { - render(w, templateError, &errorData{ - Blog: appConfig.DefaultBlog, - Title: "404 Not Found", - Message: fmt.Sprintf("`%s` was not found", r.RequestURI), + render(w, templateError, &renderData{ + Data: &errorData{ + Title: "404 Not Found", + Message: fmt.Sprintf("`%s` was not found", r.RequestURI), + }, }) w.WriteHeader(http.StatusNotFound) } diff --git a/posts.go b/posts.go index 9779c01..28e0a3d 100644 --- a/posts.go +++ b/posts.go @@ -42,7 +42,10 @@ func servePost(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } - render(w, templatePost, post) + render(w, templatePost, &renderData{ + blogString: post.Blog, + Data: post, + }) } type indexTemplateData struct { @@ -108,14 +111,15 @@ func serveTaxonomy(blog string, tax *taxonomy) func(w http.ResponseWriter, r *ht http.Error(w, err.Error(), http.StatusInternalServerError) return } - render(w, templateTaxonomy, struct { - Taxonomy *taxonomy - TaxonomyValues []string - Blog string - }{ - Taxonomy: tax, - TaxonomyValues: allValues, - Blog: blog, + render(w, templateTaxonomy, &renderData{ + blogString: blog, + Data: struct { + Taxonomy *taxonomy + TaxonomyValues []string + }{ + Taxonomy: tax, + TaxonomyValues: allValues, + }, }) } } @@ -202,16 +206,18 @@ func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) { if len(template) == 0 { template = templateIndex } - render(w, template, &indexTemplateData{ - Blog: ic.blog, - Title: title, - Description: description, - Posts: posts, - HasPrev: p.HasPrev(), - HasNext: p.HasNext(), - First: ic.path, - Prev: fmt.Sprintf("%s/page/%d", ic.path, prevPage), - Next: fmt.Sprintf("%s/page/%d", ic.path, nextPage), + render(w, template, &renderData{ + blogString: ic.blog, + Data: &indexTemplateData{ + Title: title, + Description: description, + Posts: posts, + HasPrev: p.HasPrev(), + HasNext: p.HasNext(), + First: ic.path, + Prev: fmt.Sprintf("%s/page/%d", ic.path, prevPage), + Next: fmt.Sprintf("%s/page/%d", ic.path, nextPage), + }, }) } } diff --git a/redirects.go b/redirects.go index 2908382..e828026 100644 --- a/redirects.go +++ b/redirects.go @@ -21,12 +21,8 @@ func serveRedirect(w http.ResponseWriter, r *http.Request) { } // Send redirect w.Header().Set("Location", redirect) - render(w, templateRedirect, struct { - Blog string - Permalink string - }{ - Blog: appConfig.DefaultBlog, - Permalink: redirect, + render(w, templateRedirect, &renderData{ + Data: redirect, }) w.WriteHeader(http.StatusFound) } diff --git a/render.go b/render.go index f54b631..5e2507a 100644 --- a/render.go +++ b/render.go @@ -30,17 +30,11 @@ var templateFunctions template.FuncMap func initRendering() error { templateFunctions = template.FuncMap{ - "blogs": func() map[string]*configBlog { - return appConfig.Blogs - }, - "blog": func(blog string) *configBlog { - return appConfig.Blogs[blog] - }, "micropub": func() *configMicropub { return appConfig.Micropub }, - "menu": func(blog, id string) *menu { - return appConfig.Blogs[blog].Menus[id] + "menu": func(blog *configBlog, id string) *menu { + return blog.Menus[id] }, "md": func(content string) template.HTML { htmlContent, err := renderMarkdown(content) @@ -73,9 +67,12 @@ func initRendering() error { }, "asset": assetFile, "string": getDefaultTemplateString, - "include": func(templateName string, data interface{}) (template.HTML, error) { + "include": func(templateName string, blog *configBlog, data interface{}) (template.HTML, error) { buf := new(bytes.Buffer) - err := templates[templateName].ExecuteTemplate(buf, templateName, data) + err := templates[templateName].ExecuteTemplate(buf, templateName, &renderData{ + Blog: blog, + Data: data, + }) return template.HTML(buf.String()), err }, "urlize": urlize, @@ -103,7 +100,20 @@ func initRendering() error { return nil } -func render(w http.ResponseWriter, template string, data interface{}) { +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] + } // We need to use a buffer here to enable minification var buffer bytes.Buffer err := templates[template].ExecuteTemplate(&buffer, template, data) diff --git a/templates/base.gohtml b/templates/base.gohtml index 5dd60f9..f3b561f 100644 --- a/templates/base.gohtml +++ b/templates/base.gohtml @@ -1,14 +1,14 @@ {{ define "base" }} - + {{ template "title" . }} {{ if micropub.Enabled }} - {{ include "micropub" . }} + {{ include "micropub" .Blog .Data }} {{ end }} - {{ include "header" . }} + {{ include "header" .Blog .Data }} {{ template "main" . }} {{ end }} \ No newline at end of file diff --git a/templates/error.gohtml b/templates/error.gohtml index 9f7698d..e95273a 100644 --- a/templates/error.gohtml +++ b/templates/error.gohtml @@ -1,11 +1,11 @@ {{ define "title" }} - {{ with .Title }}{{ . }}{{end}} + {{ with .Data.Title }}{{ . }}{{end}} {{ end }} {{ define "main" }}
- {{ with .Title }}

{{ . }}

{{ end }} - {{ with .Message }}{{ md . }}{{ end }} + {{ with .Data.Title }}

{{ . }}

{{ end }} + {{ with .Data.Message }}{{ md . }}{{ end }}
{{ end }} diff --git a/templates/header.gohtml b/templates/header.gohtml index 24b3572..ba9d9a5 100644 --- a/templates/header.gohtml +++ b/templates/header.gohtml @@ -1,8 +1,7 @@ {{ define "header" }}
- {{ $blog := (blog .Blog) }} -

{{ $blog.Title }}

- {{ with $blog.Description }}

{{ . }}

{{ end }} - {{ include "menu" . }} +

{{ .Blog.Title }}

+ {{ with .Blog.Description }}

{{ . }}

{{ end }} + {{ include "menu" .Blog .Data }}
{{ end }} \ No newline at end of file diff --git a/templates/index.gohtml b/templates/index.gohtml index da07c32..775db46 100644 --- a/templates/index.gohtml +++ b/templates/index.gohtml @@ -1,38 +1,39 @@ {{ define "title" }} - {{ if .Title }} - {{ .Title }} - {{ (blog .Blog).Title }} + {{ if .Data.Title }} + {{ .Data.Title }} - {{ .Blog.Title }} {{ else }} - {{ (blog .Blog).Title }} + {{ .Blog.Title }} {{ end }} + href="{{ .Data.First }}.rss"/> + href="{{ .Data.First }}.atom"/> + href="{{ .Data.First }}.json"/> {{ end }} {{ define "main" }}
- {{ with .Title }}

{{ . }}

{{ end }} - {{ with .Description }}{{ md . }}{{ end }} - {{ if (or .Title .Description) }} + {{ with .Data.Title }}

{{ . }}

{{ end }} + {{ with .Data.Description }}{{ md . }}{{ end }} + {{ if (or .Data.Title .Data.Description) }}
{{ end }} - {{ range $i, $post := .Posts }} - {{ include "summary" . }} + {{ $blog := .Blog }} + {{ range $i, $post := .Data.Posts }} + {{ include "summary" $blog $post }} {{ end }} - {{ if .HasPrev }} -

Prev

+ {{ if .Data.HasPrev }} +

Prev

{{ end }} - {{ if .HasNext }} -

Next

+ {{ if .Data.HasNext }} +

Next

{{ end }}
{{ end }} diff --git a/templates/photos.gohtml b/templates/photos.gohtml index 9f6d156..f3a99f7 100644 --- a/templates/photos.gohtml +++ b/templates/photos.gohtml @@ -1,28 +1,27 @@ {{ define "title" }} - {{ $blog := (blog .Blog) }} - {{ if $blog.Photos.Title }} - {{ $blog.Photos.Title }} - {{ $blog.Title }} + {{ if .Blog.Photos.Title }} + {{ .Blog.Photos.Title }} - {{ .Blog.Title }} {{ else }} - {{ $blog.Title }} + {{ .Blog.Title }} {{ end }} {{ end }} {{ define "main" }}
- {{ $blog := (blog .Blog) }} - {{ with $blog.Photos.Title }}

{{ . }}

{{ end }} - {{ with $blog.Photos.Description }}{{ md . }}{{ end }} - {{ if (or $blog.Photos.Title $blog.Photos.Description) }} + {{ with .Blog.Photos.Title }}

{{ . }}

{{ end }} + {{ with .Blog.Photos.Description }}{{ md . }}{{ end }} + {{ if (or .Blog.Photos.Title .Blog.Photos.Description) }}
{{ end }} - {{ range $i, $post := .Posts }} - {{ include "photosummary" . }} + {{ $blog := .Blog }} + {{ range $i, $post := .Data.Posts }} + {{ include "photosummary" $blog $post }} {{ end }} - {{ if .HasPrev }} -

Prev

+ {{ if .Data.HasPrev }} +

Prev

{{ end }} - {{ if .HasNext }} -

Next

+ {{ if .Data.HasNext }} +

Next

{{ end }}
{{ end }} diff --git a/templates/photosummary.gohtml b/templates/photosummary.gohtml index dee12b4..2b2d1c3 100644 --- a/templates/photosummary.gohtml +++ b/templates/photosummary.gohtml @@ -1,12 +1,12 @@ {{ define "photosummary" }}
- {{ with p . "title" }}

{{ . }}

{{ end }} - {{ with .Published }}

{{ dateformat . "02. Jan 2006" }}

{{ end }} - {{ range $i, $photo := ( ps . (blog .Blog).Photos.Parameter ) }} + {{ with p .Data "title" }}

{{ . }}

{{ end }} + {{ with .Data.Published }}

{{ dateformat . "02. Jan 2006" }}

{{ end }} + {{ range $i, $photo := ( ps .Data .Blog.Photos.Parameter ) }} {{ md ( printf "![](%s)" $photo ) }} {{ end }} -

{{ summary . }}

- View +

{{ summary .Data }}

+ View

{{ end }} \ No newline at end of file diff --git a/templates/post.gohtml b/templates/post.gohtml index 5f2176c..bb05226 100644 --- a/templates/post.gohtml +++ b/templates/post.gohtml @@ -1,22 +1,21 @@ {{ define "title" }} - {{ with p . "title" }}{{ . }} - {{end}}{{ (blog .Blog).Title }} + {{ with p .Data "title" }}{{ . }} - {{end}}{{ .Blog.Title }} {{ end }} {{ define "main" }}
- {{ with title . }}

{{ . }}

{{ end }} - {{ with .Published }} + {{ with title .Data }}

{{ . }}

{{ end }} + {{ with .Data.Published }}

{{ string "publishedon" }} {{ dateformat . "02. Jan 2006" }}

{{ end }} - {{ with .Updated }} + {{ with .Data.Updated }}

{{ string "updatedon" }} {{ dateformat . "02. Jan 2006" }}

{{ end }} - {{ with .Content }} + {{ with .Data.Content }}
{{ md . }}
{{ end }}
- {{ $taxonomies := (blog .Blog).Taxonomies }} - {{ $post := . }} - {{ range $i, $tax := $taxonomies }} + {{ $post := .Data }} + {{ range $i, $tax := .Blog.Taxonomies }} {{ $tvs := ps $post $tax.Name }} {{ if gt (len $tvs) 0 }}

In {{ $tax.Title }}: diff --git a/templates/redirect.gohtml b/templates/redirect.gohtml index c8c7c39..6a0402f 100644 --- a/templates/redirect.gohtml +++ b/templates/redirect.gohtml @@ -1,9 +1,9 @@ {{ define "redirect" }} - + - {{ .Permalink }} - - + {{ .Data }} + + {{ end }} \ No newline at end of file diff --git a/templates/summary.gohtml b/templates/summary.gohtml index 466fb22..fbc854d 100644 --- a/templates/summary.gohtml +++ b/templates/summary.gohtml @@ -1,9 +1,9 @@ {{ define "summary" }}

- {{ with p . "title" }}

{{ . }}

{{ end }} - {{ with .Published }}

{{ dateformat . "02. Jan 2006" }}

{{ end }} -

{{ summary . }}

- View + {{ with p .Data "title" }}

{{ . }}

{{ end }} + {{ with .Data.Published }}

{{ dateformat . "02. Jan 2006" }}

{{ end }} +

{{ summary .Data }}

+ View

{{ end }} \ No newline at end of file diff --git a/templates/taxonomy.gohtml b/templates/taxonomy.gohtml index 6c055e4..db2aebf 100644 --- a/templates/taxonomy.gohtml +++ b/templates/taxonomy.gohtml @@ -1,14 +1,14 @@ {{ define "title" }} - {{ .Taxonomy.Title }} - {{ (blog .Blog).Title }} + {{ .Data.Taxonomy.Title }} - {{ .Blog.Title }} {{ end }} {{ define "main" }}
- {{ with .Taxonomy.Title }}

{{ . }}

{{ end }} - {{ with .Taxonomy.Description }}{{ md . }}{{ end }} + {{ with .Data.Taxonomy.Title }}

{{ . }}

{{ end }} + {{ with .Data.Taxonomy.Description }}{{ md . }}{{ end }}