mirror of https://github.com/jlelse/GoBlog
Replace summary templates with new rendering method
This commit is contained in:
parent
7fba9bc0bc
commit
236437aba4
|
@ -253,7 +253,7 @@ func (a *goBlog) blogPhotosRouter(conf *configBlog) func(r chi.Router) {
|
||||||
parameter: a.cfg.Micropub.PhotoParam,
|
parameter: a.cfg.Micropub.PhotoParam,
|
||||||
title: pc.Title,
|
title: pc.Title,
|
||||||
description: pc.Description,
|
description: pc.Description,
|
||||||
summaryTemplate: templatePhotosSummary,
|
summaryTemplate: photoSummary,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
r.Get(photoPath, a.serveIndex)
|
r.Get(photoPath, a.serveIndex)
|
||||||
|
|
4
posts.go
4
posts.go
|
@ -238,7 +238,7 @@ type indexConfig struct {
|
||||||
year, month, day int
|
year, month, day int
|
||||||
title string
|
title string
|
||||||
description string
|
description string
|
||||||
summaryTemplate string
|
summaryTemplate summaryTyp
|
||||||
status postStatus
|
status postStatus
|
||||||
statusse []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)
|
nextPath = fmt.Sprintf("%s/page/%d", strings.TrimSuffix(path, "/"), nextPage)
|
||||||
summaryTemplate := ic.summaryTemplate
|
summaryTemplate := ic.summaryTemplate
|
||||||
if summaryTemplate == "" {
|
if summaryTemplate == "" {
|
||||||
summaryTemplate = templateSummary
|
summaryTemplate = defaultSummary
|
||||||
}
|
}
|
||||||
a.render(w, r, templateIndex, &renderData{
|
a.render(w, r, templateIndex, &renderData{
|
||||||
Canonical: a.getFullAddress(path),
|
Canonical: a.getFullAddress(path),
|
||||||
|
|
35
render.go
35
render.go
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -23,8 +22,6 @@ const (
|
||||||
templateIndex = "index"
|
templateIndex = "index"
|
||||||
templateTaxonomy = "taxonomy"
|
templateTaxonomy = "taxonomy"
|
||||||
templateSearch = "search"
|
templateSearch = "search"
|
||||||
templateSummary = "summary"
|
|
||||||
templatePhotosSummary = "photosummary"
|
|
||||||
templateEditor = "editor"
|
templateEditor = "editor"
|
||||||
templateEditorFiles = "editorfiles"
|
templateEditorFiles = "editorfiles"
|
||||||
templateLogin = "login"
|
templateLogin = "login"
|
||||||
|
@ -49,12 +46,9 @@ func (a *goBlog) initRendering() error {
|
||||||
"mdtitle": a.renderMdTitle,
|
"mdtitle": a.renderMdTitle,
|
||||||
"html": wrapStringAsHTML,
|
"html": wrapStringAsHTML,
|
||||||
// Post specific
|
// Post specific
|
||||||
"content": a.postHtml,
|
"content": a.postHtml,
|
||||||
"summary": a.postSummary,
|
"shorturl": a.shortPostURL,
|
||||||
"shorturl": a.shortPostURL,
|
"gettrack": a.getTrack,
|
||||||
"showfull": a.showFull,
|
|
||||||
"photolinks": a.photoLinks,
|
|
||||||
"gettrack": a.getTrack,
|
|
||||||
// Code based rendering
|
// Code based rendering
|
||||||
"posttax": func(p *post, b *configBlog) template.HTML {
|
"posttax": func(p *post, b *configBlog) template.HTML {
|
||||||
var hb htmlBuilder
|
var hb htmlBuilder
|
||||||
|
@ -91,6 +85,11 @@ func (a *goBlog) initRendering() error {
|
||||||
a.renderTorNotice(&hb, b, torUsed, torAddress)
|
a.renderTorNotice(&hb, b, torUsed, torAddress)
|
||||||
return hb.html()
|
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
|
// Others
|
||||||
"dateformat": dateFormat,
|
"dateformat": dateFormat,
|
||||||
"isodate": isoDateFormat,
|
"isodate": isoDateFormat,
|
||||||
|
@ -98,7 +97,6 @@ func (a *goBlog) initRendering() error {
|
||||||
"now": localNowString,
|
"now": localNowString,
|
||||||
"asset": a.assetFileName,
|
"asset": a.assetFileName,
|
||||||
"string": a.ts.GetTemplateStringVariantFunc(),
|
"string": a.ts.GetTemplateStringVariantFunc(),
|
||||||
"include": a.includeRenderedTemplate,
|
|
||||||
"urlize": urlize,
|
"urlize": urlize,
|
||||||
"absolute": a.getFullAddress,
|
"absolute": a.getFullAddress,
|
||||||
"opensearch": openSearchUrl,
|
"opensearch": openSearchUrl,
|
||||||
|
@ -216,20 +214,3 @@ func (a *goBlog) checkRenderData(r *http.Request, data *renderData) {
|
||||||
data.Data = map[string]interface{}{}
|
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")
|
|
||||||
}
|
|
||||||
|
|
|
@ -14,9 +14,9 @@
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ if .Data.Posts }}
|
{{ if .Data.Posts }}
|
||||||
{{ $summaryTemplate := .Data.SummaryTemplate }}
|
{{ $summaryTemplate := .Data.SummaryTemplate }}
|
||||||
{{ $rd := . }}
|
{{ $blog := .Blog }}
|
||||||
{{ range $i, $post := .Data.Posts }}
|
{{ range $i, $post := .Data.Posts }}
|
||||||
{{ include $summaryTemplate $rd $post }}
|
{{ summary $blog $post $summaryTemplate }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ else }}
|
{{ else }}
|
||||||
<p>{{ string .Blog.Lang "noposts" }}</p>
|
<p>{{ string .Blog.Lang "noposts" }}</p>
|
||||||
|
|
|
@ -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 }}
|
|
|
@ -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
ui.go
|
@ -66,6 +66,72 @@ func (a *goBlog) renderEditorPreview(hb *htmlBuilder, bc *configBlog, p *post) {
|
||||||
a.renderPostTax(hb, p, bc)
|
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.)
|
// Render the HTML to show the list of post taxonomy values (tags, series, etc.)
|
||||||
func (a *goBlog) renderPostTax(hb *htmlBuilder, p *post, b *configBlog) {
|
func (a *goBlog) renderPostTax(hb *htmlBuilder, p *post, b *configBlog) {
|
||||||
if b == nil || p == nil {
|
if b == nil || p == nil {
|
||||||
|
|
Loading…
Reference in New Issue