Post translations, little progress on templates

This commit is contained in:
Jan-Lukas Else 2020-10-16 20:34:34 +02:00
parent 5652fab725
commit 4cb605c38b
6 changed files with 95 additions and 33 deletions

View File

@ -133,21 +133,21 @@ func serveTaxonomyValue(blog string, path string, tax *taxonomy, value string) f
func servePhotos(blog string) func(w http.ResponseWriter, r *http.Request) { func servePhotos(blog string) func(w http.ResponseWriter, r *http.Request) {
return serveIndex(&indexConfig{ return serveIndex(&indexConfig{
blog: blog, blog: blog,
path: appConfig.Blogs[blog].Photos.Path, path: appConfig.Blogs[blog].Photos.Path,
onlyWithParameter: appConfig.Blogs[blog].Photos.Parameter, parameter: appConfig.Blogs[blog].Photos.Parameter,
template: templatePhotos, template: templatePhotos,
}) })
} }
type indexConfig struct { type indexConfig struct {
blog string blog string
path string path string
section *section section *section
tax *taxonomy tax *taxonomy
taxValue string taxValue string
onlyWithParameter string parameter string
template string template string
} }
func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) { func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) {
@ -163,11 +163,11 @@ func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) {
} }
} }
p := paginator.New(&postPaginationAdapter{context: r.Context(), config: &postsRequestConfig{ p := paginator.New(&postPaginationAdapter{context: r.Context(), config: &postsRequestConfig{
blog: ic.blog, blog: ic.blog,
sections: sections, sections: sections,
taxonomy: ic.tax, taxonomy: ic.tax,
taxonomyValue: ic.taxValue, taxonomyValue: ic.taxValue,
onlyWithParameter: ic.onlyWithParameter, parameter: ic.parameter,
}}, appConfig.Blogs[ic.blog].Pagination) }}, appConfig.Blogs[ic.blog].Pagination)
p.SetPage(pageNo) p.SetPage(pageNo)
var posts []*post var posts []*post
@ -229,14 +229,15 @@ func getPost(context context.Context, path string) (*post, error) {
} }
type postsRequestConfig struct { type postsRequestConfig struct {
blog string blog string
path string path string
limit int limit int
offset int offset int
sections []string sections []string
taxonomy *taxonomy taxonomy *taxonomy
taxonomyValue string taxonomyValue string
onlyWithParameter string parameter string
parameterValue string
} }
func getPosts(context context.Context, config *postsRequestConfig) (posts []*post, err error) { func getPosts(context context.Context, config *postsRequestConfig) (posts []*post, err error) {
@ -247,8 +248,12 @@ func getPosts(context context.Context, config *postsRequestConfig) (posts []*pos
if config.blog != "" { if config.blog != "" {
postsTable = "(select * from " + postsTable + " where blog = '" + config.blog + "')" postsTable = "(select * from " + postsTable + " where blog = '" + config.blog + "')"
} }
if config.onlyWithParameter != "" { if config.parameter != "" {
postsTable = "(select distinct p.* from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path where pp.parameter = '" + config.onlyWithParameter + "' and length(coalesce(pp.value, '')) > 1)" if config.parameterValue != "" {
postsTable = "(select distinct p.* from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path where pp.parameter = '" + config.parameter + "' and pp.value = '" + config.parameterValue + "')"
} else {
postsTable = "(select distinct p.* from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path where pp.parameter = '" + config.parameter + "' and length(coalesce(pp.value, '')) > 1)"
}
} }
if config.taxonomy != nil && len(config.taxonomyValue) > 0 { if config.taxonomy != nil && len(config.taxonomyValue) > 0 {
postsTable = "(select distinct p.* from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path where pp.parameter = '" + config.taxonomy.Name + "' and lower(pp.value) = lower('" + config.taxonomyValue + "'))" postsTable = "(select distinct p.* from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path where pp.parameter = '" + config.taxonomy.Name + "' and lower(pp.value) = lower('" + config.taxonomyValue + "'))"

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"strings" "strings"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
@ -33,3 +34,27 @@ func (p *post) summary() (summary string) {
summary = firstSentences(doc.Text(), 3) summary = firstSentences(doc.Text(), 3)
return return
} }
func (p *post) translations() []*post {
translationkey := p.firstParameter("translationkey")
if translationkey == "" {
return nil
}
posts, err := getPosts(context.Background(), &postsRequestConfig{
parameter: "translationkey",
parameterValue: translationkey,
})
if err != nil || len(posts) == 0 {
return nil
}
translations := []*post{}
for _, t := range posts {
if p.Path != t.Path {
translations = append(translations, t)
}
}
if len(translations) == 0 {
return nil
}
return translations
}

View File

@ -33,6 +33,9 @@ var templateFunctions template.FuncMap
func initRendering() error { func initRendering() error {
templateFunctions = template.FuncMap{ templateFunctions = template.FuncMap{
"blog": func(blog string) *configBlog {
return appConfig.Blogs[blog]
},
"menu": func(blog *configBlog, id string) *menu { "menu": func(blog *configBlog, id string) *menu {
return blog.Menus[id] return blog.Menus[id]
}, },
@ -48,16 +51,23 @@ func initRendering() error {
"p": func(p *post, parameter string) string { "p": func(p *post, parameter string) string {
return p.firstParameter(parameter) return p.firstParameter(parameter)
}, },
// All parameter values // Post specific
"ps": func(p *post, parameter string) []string { "ps": func(p *post, parameter string) []string {
return p.Parameters[parameter] return p.Parameters[parameter]
}, },
"hasp": func(p *post, parameter string) bool {
return len(p.Parameters[parameter]) > 0
},
"title": func(p *post) string { "title": func(p *post) string {
return p.title() return p.title()
}, },
"summary": func(p *post) string { "summary": func(p *post) string {
return p.summary() return p.summary()
}, },
"translations": func(p *post) []*post {
return p.translations()
},
// Others
"dateformat": func(date string, format string) string { "dateformat": func(date string, format string) string {
d, err := dateparse.ParseIn(date, time.Local) d, err := dateparse.ParseIn(date, time.Local)
if err != nil { if err != nil {
@ -86,6 +96,9 @@ func initRendering() error {
"urlize": urlize, "urlize": urlize,
"sort": sortedStrings, "sort": sortedStrings,
"blogRelative": func(blog *configBlog, path string) string { "blogRelative": func(blog *configBlog, path string) string {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
if blog.Path != "/" { if blog.Path != "/" {
return blog.Path + path return blog.Path + path
} }

View File

@ -26,6 +26,9 @@
</p> </p>
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ range $i, $t := (translations .Data) }}
<p><a href="{{ $t.Path }}">{{ (blog $t.Blog).Title }}</a></p>
{{ end }}
</main> </main>
{{ end }} {{ end }}

View File

@ -1,9 +1,14 @@
{{ define "summary" }} {{ define "summary" }}
<article> <article class="h-entry border-bottom">
{{ with p .Data "title" }}<h2>{{ . }}</h2>{{ end }} {{ if p .Data "title" }}
{{ if .Data.Published }}<p>{{ longDate .Data.Published .Blog.Lang }}</p>{{ end }} <h2 class="p-name">
<p>{{ summary .Data }}</p> <a class="u-url" href="{{ .Data.Path }}">
<a href="{{ .Data.Path }}">{{ string .Blog.Lang "view" }}</a> {{ p .Data "title" }}
</a>
</h2>
{{ end }}
{{ include "summarymeta" .Blog .Data }}
<p class="p-summary">{{ summary .Data }}</p>
<p>{{ if (hasp .Data "images") }}🖼️ {{ end }}<a class="u-url" href="{{ .Data.Path }}">{{ string .Blog.Lang "view" }}</a></p>
</article> </article>
<hr>
{{ end }} {{ end }}

View File

@ -0,0 +1,11 @@
{{ define "summarymeta" }}
<div class="p">
{{ $section := (index .Blog.Sections .Data.Section) }}
{{ if .Data.Published }}
<div><time class="dt-published" datetime="{{ dateformat .Data.Published "2006-01-02T15:04:05Z07:00" }}">{{ longDate .Data.Published .Blog.Lang }}</time>{{ if $section }} in <a href="{{ blogRelative .Blog $section.Name }}">{{ $section.Title }}</a>{{ end }}</div>
{{ end }}
{{ if .Data.Updated }}
<div>{{ string .Blog.Lang "updatedon" }} <time class="dt-updated" datetime="{{ dateformat .Data.Updated "2006-01-02T15:04:05Z07:00" }}">{{ longDate .Data.Updated .Blog.Lang }}</time></div>
{{ end }}
</div>
{{ end }}