Browse Source

Replace summary templates with new rendering method

master
Jan-Lukas Else 6 days ago
parent
commit
236437aba4
  1. 2
      httpRouters.go
  2. 4
      posts.go
  3. 35
      render.go
  4. 4
      templates/index.gohtml
  5. 18
      templates/photosummary.gohtml
  6. 19
      templates/summary.gohtml
  7. 66
      ui.go

2
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)

4
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),

35
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")
}

4
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 }}
<p>{{ string .Blog.Lang "noposts" }}</p>

18
templates/photosummary.gohtml

@ -1,18 +0,0 @@
{{ define "photosummary" }}
<article class="h-entry border-bottom">
{{ if gt .Data.Priority 0 }}<p>📌 {{ string .Blog.Lang "pinned" }}</p>{{ end }}
{{ if .Data.RenderedTitle }}
<h2 class="p-name">
<a class="u-url" href="{{ .Data.Path }}">
{{ .Data.RenderedTitle }}
</a>
</h2>
{{ end }}
{{ postmeta .Data .Blog "summary" }}
{{ range $i, $photo := (photolinks .Data) }}
{{ md ( printf "![](%s)" $photo ) }}
{{ end }}
<p class="p-summary">{{ summary .Data }}</p>
<p>{{ if (photolinks .Data) }}🖼️ {{ end }}<a class="u-url" href="{{ .Data.Path }}">{{ string .Blog.Lang "view" }}</a></p>
</article>
{{ end }}

19
templates/summary.gohtml

@ -1,19 +0,0 @@
{{ define "summary" }}
<article class="h-entry border-bottom">
{{ if gt .Data.Priority 0 }}<p>📌 {{ string .Blog.Lang "pinned" }}</p>{{ end }}
{{ if .Data.RenderedTitle }}
<h2 class="p-name">
<a class="u-url" href="{{ .Data.Path }}">
{{ .Data.RenderedTitle }}
</a>
</h2>
{{ end }}
{{ postmeta .Data .Blog "summary" }}
{{ if showfull .Data }}
<div class=e-content>{{ content .Data false }}</div>
{{ else }}
<p class=p-summary>{{ summary .Data }}</p>
{{ end }}
<p>{{ if (photolinks .Data) }}🖼️ {{ end }}<a class="u-url" href="{{ .Data.Path }}">{{ string .Blog.Lang "view" }}</a></p>
</article>
{{ end }}

66
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 {

Loading…
Cancel
Save