diff --git a/httpRouters.go b/httpRouters.go index f0a4127..c1b28f1 100644 --- a/httpRouters.go +++ b/httpRouters.go @@ -253,7 +253,7 @@ func (a *goBlog) blogPhotosRouter(conf *configBlog) func(r chi.Router) { parameter: a.cfg.Micropub.PhotoParam, title: pc.Title, description: pc.Description, - summaryTemplate: templatePhotosSummary, + summaryTemplate: photoSummary, }), ) r.Get(photoPath, a.serveIndex) diff --git a/posts.go b/posts.go index a57541d..acadbf2 100644 --- a/posts.go +++ b/posts.go @@ -238,7 +238,7 @@ type indexConfig struct { year, month, day int title string description string - summaryTemplate string + summaryTemplate summaryTyp status postStatus statusse []postStatus } @@ -337,7 +337,7 @@ func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) { nextPath = fmt.Sprintf("%s/page/%d", strings.TrimSuffix(path, "/"), nextPage) summaryTemplate := ic.summaryTemplate if summaryTemplate == "" { - summaryTemplate = templateSummary + summaryTemplate = defaultSummary } a.render(w, r, templateIndex, &renderData{ Canonical: a.getFullAddress(path), diff --git a/render.go b/render.go index 7c8bcb4..c7d377d 100644 --- a/render.go +++ b/render.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "errors" "html/template" "net/http" "os" @@ -23,8 +22,6 @@ const ( templateIndex = "index" templateTaxonomy = "taxonomy" templateSearch = "search" - templateSummary = "summary" - templatePhotosSummary = "photosummary" templateEditor = "editor" templateEditorFiles = "editorfiles" templateLogin = "login" @@ -49,12 +46,9 @@ func (a *goBlog) initRendering() error { "mdtitle": a.renderMdTitle, "html": wrapStringAsHTML, // Post specific - "content": a.postHtml, - "summary": a.postSummary, - "shorturl": a.shortPostURL, - "showfull": a.showFull, - "photolinks": a.photoLinks, - "gettrack": a.getTrack, + "content": a.postHtml, + "shorturl": a.shortPostURL, + "gettrack": a.getTrack, // Code based rendering "posttax": func(p *post, b *configBlog) template.HTML { var hb htmlBuilder @@ -91,6 +85,11 @@ func (a *goBlog) initRendering() error { a.renderTorNotice(&hb, b, torUsed, torAddress) return hb.html() }, + "summary": func(bc *configBlog, p *post, typ summaryTyp) template.HTML { + var hb htmlBuilder + a.renderSummary(&hb, bc, p, typ) + return hb.html() + }, // Others "dateformat": dateFormat, "isodate": isoDateFormat, @@ -98,7 +97,6 @@ func (a *goBlog) initRendering() error { "now": localNowString, "asset": a.assetFileName, "string": a.ts.GetTemplateStringVariantFunc(), - "include": a.includeRenderedTemplate, "urlize": urlize, "absolute": a.getFullAddress, "opensearch": openSearchUrl, @@ -216,20 +214,3 @@ func (a *goBlog) checkRenderData(r *http.Request, data *renderData) { data.Data = map[string]interface{}{} } } - -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") -} diff --git a/templates/index.gohtml b/templates/index.gohtml index 576bf89..1e33c48 100644 --- a/templates/index.gohtml +++ b/templates/index.gohtml @@ -14,9 +14,9 @@ {{ end }} {{ if .Data.Posts }} {{ $summaryTemplate := .Data.SummaryTemplate }} - {{ $rd := . }} + {{ $blog := .Blog }} {{ range $i, $post := .Data.Posts }} - {{ include $summaryTemplate $rd $post }} + {{ summary $blog $post $summaryTemplate }} {{ end }} {{ else }}

{{ string .Blog.Lang "noposts" }}

diff --git a/templates/photosummary.gohtml b/templates/photosummary.gohtml deleted file mode 100644 index a412ca8..0000000 --- a/templates/photosummary.gohtml +++ /dev/null @@ -1,18 +0,0 @@ -{{ define "photosummary" }} -
- {{ if gt .Data.Priority 0 }}

📌 {{ string .Blog.Lang "pinned" }}

{{ end }} - {{ if .Data.RenderedTitle }} -

- - {{ .Data.RenderedTitle }} - -

- {{ end }} - {{ postmeta .Data .Blog "summary" }} - {{ range $i, $photo := (photolinks .Data) }} - {{ md ( printf "![](%s)" $photo ) }} - {{ end }} -

{{ summary .Data }}

-

{{ if (photolinks .Data) }}🖼️ {{ end }}{{ string .Blog.Lang "view" }}

-
-{{ end }} \ No newline at end of file diff --git a/templates/summary.gohtml b/templates/summary.gohtml deleted file mode 100644 index 2b030e3..0000000 --- a/templates/summary.gohtml +++ /dev/null @@ -1,19 +0,0 @@ -{{ define "summary" }} -
- {{ if gt .Data.Priority 0 }}

📌 {{ string .Blog.Lang "pinned" }}

{{ end }} - {{ if .Data.RenderedTitle }} -

- - {{ .Data.RenderedTitle }} - -

- {{ end }} - {{ postmeta .Data .Blog "summary" }} - {{ if showfull .Data }} -
{{ content .Data false }}
- {{ else }} -

{{ summary .Data }}

- {{ end }} -

{{ if (photolinks .Data) }}🖼️ {{ end }}{{ string .Blog.Lang "view" }}

-
-{{ end }} \ No newline at end of file diff --git a/ui.go b/ui.go index 7478525..235b16c 100644 --- a/ui.go +++ b/ui.go @@ -66,6 +66,72 @@ func (a *goBlog) renderEditorPreview(hb *htmlBuilder, bc *configBlog, p *post) { a.renderPostTax(hb, p, bc) } +type summaryTyp string + +const ( + defaultSummary summaryTyp = "summary" + photoSummary summaryTyp = "photosummary" +) + +// Render the HTML for the post summary on index pages +func (a *goBlog) renderSummary(hb *htmlBuilder, bc *configBlog, p *post, typ summaryTyp) { + if bc == nil || p == nil { + return + } + if typ == "" { + typ = defaultSummary + } + // Start article + hb.writeElementOpen("article", "class", "h-entry border-bottom") + if p.Priority > 0 { + // Is pinned post + hb.writeElementOpen("p") + hb.writeEscaped("📌 ") + hb.writeEscaped(a.ts.GetTemplateStringVariant(bc.Lang, "pinned")) + hb.writeElementClose("p") + } + if p.RenderedTitle != "" { + // Has title + hb.writeElementOpen("h2", "class", "p-name") + hb.writeElementOpen("a", "class", "u-url", "href", p.Path) + hb.writeEscaped(p.RenderedTitle) + hb.writeElementClose("a") + hb.writeElementClose("h2") + } + // Show photos in photo summary + photos := a.photoLinks(p) + if typ == photoSummary && len(photos) > 0 { + for _, photo := range photos { + hb.write(string(a.safeRenderMarkdownAsHTML(fmt.Sprintf("![](%s)", photo)))) + } + } + // Post meta + a.renderPostMeta(hb, p, bc, "summary") + if typ != photoSummary && a.showFull(p) { + // Show full content + hb.writeElementOpen("div", "class", "e-content") + hb.write(string(a.postHtml(p, false))) + hb.writeElementClose("div") + } else { + // Show summary + hb.writeElementOpen("p", "class", "p-summary") + hb.writeEscaped(a.postSummary(p)) + hb.writeElementClose("p") + } + // Show link to full post + hb.writeElementOpen("p") + if len(photos) > 0 { + // Contains photos + hb.writeEscaped("🖼️ ") + } + hb.writeElementOpen("a", "class", "u-url", "href", p.Path) + hb.writeEscaped(a.ts.GetTemplateStringVariant(bc.Lang, "view")) + hb.writeElementClose("a") + hb.writeElementClose("p") + // Finish article + hb.writeElementClose("article") +} + // Render the HTML to show the list of post taxonomy values (tags, series, etc.) func (a *goBlog) renderPostTax(hb *htmlBuilder, p *post, b *configBlog) { if b == nil || p == nil {