From 1d31cbe20a4531dd60fbe31a652236c30b4835be Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Tue, 1 Sep 2020 18:14:49 +0200 Subject: [PATCH] Taxonomies, sections and blog with title and description --- config.go | 23 +++++++++--- example-config.yaml | 9 +++-- http.go | 15 ++++---- posts.go | 74 +++++++++++++++++++++++---------------- templates/index.gohtml | 3 ++ templates/taxonomy.gohtml | 13 ++++--- 6 files changed, 89 insertions(+), 48 deletions(-) diff --git a/config.go b/config.go index 8f95ee3..928c0d2 100644 --- a/config.go +++ b/config.go @@ -36,12 +36,26 @@ type configBlog struct { Lang string `mapstructure:"lang"` // Title of the blog, e.g. "My blog" Title string `mapstructure:"title"` + // Description of the blog + Description string `mapstructure:"description"` // Number of posts per page Pagination int `mapstructure:"pagination"` // Sections - Sections []string `mapstructure:"sections"` + Sections []*section `mapstructure:"sections"` // Taxonomies - Taxonomies []string `mapstructure:"taxonomies"` + Taxonomies []*taxonomy `mapstructure:"taxonomies"` +} + +type section struct { + Name string `mapstructure:"name"` + Title string `mapstructure:"title"` + Description string `mapstructure:"description"` +} + +type taxonomy struct { + Name string `mapstructure:"name"` + Title string `mapstructure:"title"` + Description string `mapstructure:"description"` } type configUser struct { @@ -71,9 +85,10 @@ func initConfig() error { viper.SetDefault("cache.expiration", 600) viper.SetDefault("blog.lang", "en") viper.SetDefault("blog.title", "My blog") + viper.SetDefault("blog.description", "This is my blog") viper.SetDefault("blog.pagination", 10) - viper.SetDefault("blog.sections", []string{"posts"}) - viper.SetDefault("blog.taxonomies", []string{"tags"}) + viper.SetDefault("blog.sections", []*section{{Name: "posts", Title: "Posts", Description: "**Posts** on this blog"}}) + viper.SetDefault("blog.taxonomies", []*taxonomy{{Name: "tags", Title: "Tags", Description: "**Tags** on this blog"}}) viper.SetDefault("user.nick", "admin") viper.SetDefault("user.name", "Admin") viper.SetDefault("user.password", "secret") diff --git a/example-config.yaml b/example-config.yaml index c0f64fe..3ed9370 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -13,10 +13,15 @@ cache: blog: lang: en title: My blog + description: This is my blog sections: - - posts + - name: posts + title: Posts + description: "**Posts** on this blog" taxonomies: - - tags + - name: tags + title: Tags + description: "**Tags** on this blog" user: nick: admin name: Admin diff --git a/http.go b/http.go index 06ad45e..9b64f41 100644 --- a/http.go +++ b/http.go @@ -88,21 +88,22 @@ func buildHandler() (http.Handler, error) { paginationPath := "/page/{page}" for _, section := range appConfig.Blog.Sections { - if section != "" { - r.With(cacheMiddleware, minifier.Middleware).Get("/"+section, serveSection("/"+section, section)) - r.With(cacheMiddleware, minifier.Middleware).Get("/"+section+paginationPath, serveSection("/"+section, section)) + if section.Name != "" { + path := "/"+section.Name + r.With(cacheMiddleware, minifier.Middleware).Get(path, serveSection(path, section)) + r.With(cacheMiddleware, minifier.Middleware).Get(path+paginationPath, serveSection(path, section)) } } for _, taxonomy := range appConfig.Blog.Taxonomies { - if taxonomy != "" { - r.With(cacheMiddleware, minifier.Middleware).Get("/"+taxonomy, serveTaxonomy(taxonomy)) - values, err := allTaxonomyValues(taxonomy) + if taxonomy.Name != "" { + r.With(cacheMiddleware, minifier.Middleware).Get("/"+taxonomy.Name, serveTaxonomy(taxonomy)) + values, err := allTaxonomyValues(taxonomy.Name) if err != nil { return nil, err } for _, tv := range values { - path := "/" + taxonomy + "/" + tv + path := "/" + taxonomy.Name + "/" + tv r.With(cacheMiddleware, minifier.Middleware).Get(path, serveTaxonomyValue(path, taxonomy, tv)) r.With(cacheMiddleware, minifier.Middleware).Get(path+paginationPath, serveTaxonomyValue(path, taxonomy, tv)) } diff --git a/posts.go b/posts.go index d66ef95..9f3cbc6 100644 --- a/posts.go +++ b/posts.go @@ -36,12 +36,14 @@ func servePost(w http.ResponseWriter, r *http.Request) { render(w, templatePost, post) } -type indexTemplateDate struct { - Posts []*Post - HasPrev bool - HasNext bool - Prev string - Next string +type indexTemplateData struct { + Title string + Description string + Posts []*Post + HasPrev bool + HasNext bool + Prev string + Next string } type postPaginationAdapter struct { @@ -72,45 +74,45 @@ func (p *postPaginationAdapter) Slice(offset, length int, data interface{}) erro } func serveHome(path string) func(w http.ResponseWriter, r *http.Request) { - return serveIndex(path, "", "", "") + return serveIndex(path, nil, nil, "") } -func serveSection(path, section string) func(w http.ResponseWriter, r *http.Request) { - return serveIndex(path, section, "", "") +func serveSection(path string, section *section) func(w http.ResponseWriter, r *http.Request) { + return serveIndex(path, section, nil, "") } -func serveTaxonomy(taxonomy string) func(w http.ResponseWriter, r *http.Request) { +func serveTaxonomy(tax *taxonomy) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - allValues, err := allTaxonomyValues(taxonomy) + allValues, err := allTaxonomyValues(tax.Name) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } render(w, templateTaxonomy, struct { - Taxonomy string + Taxonomy *taxonomy TaxonomyValues []string }{ - Taxonomy: taxonomy, + Taxonomy: tax, TaxonomyValues: allValues, }) } } -func serveTaxonomyValue(path, taxonomy, value string) func(w http.ResponseWriter, r *http.Request) { - return serveIndex(path, "", taxonomy, value) +func serveTaxonomyValue(path string, tax *taxonomy, value string) func(w http.ResponseWriter, r *http.Request) { + return serveIndex(path, nil, tax, value) } -func serveIndex(path, section, taxonomy, taxonomyValue string) func(w http.ResponseWriter, r *http.Request) { +func serveIndex(path string, sec *section, tax *taxonomy, taxonomyValue string) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { pageNoString := chi.URLParam(r, "page") pageNo, _ := strconv.Atoi(pageNoString) sections := appConfig.Blog.Sections - if len(section) > 0 { - sections = []string{section} + if sec != nil { + sections = []*section{sec} } p := paginator.New(&postPaginationAdapter{context: r.Context(), config: &postsRequestConfig{ sections: sections, - taxonomy: taxonomy, + taxonomy: tax, taxonomyValue: taxonomyValue, }}, appConfig.Blog.Pagination) p.SetPage(pageNo) @@ -128,12 +130,24 @@ func serveIndex(path, section, taxonomy, taxonomyValue string) func(w http.Respo if err == paginator.ErrNoNextPage { nextPage = p.Page() } - render(w, templateIndex, &indexTemplateDate{ - Posts: posts, - HasPrev: p.HasPrev(), - HasNext: p.HasNext(), - Prev: fmt.Sprintf("%s/page/%d", path, prevPage), - Next: fmt.Sprintf("%s/page/%d", path, nextPage), + var title, description string + if tax != nil { + title = fmt.Sprintf("%s: %s", tax.Title, taxonomyValue) + } else if sec != nil { + title = sec.Title + description = sec.Description + } else if tax == nil && sec == nil { + title = appConfig.Blog.Title + description = appConfig.Blog.Description + } + render(w, templateIndex, &indexTemplateData{ + Title: title, + Description: description, + Posts: posts, + HasPrev: p.HasPrev(), + HasNext: p.HasNext(), + Prev: fmt.Sprintf("%s/page/%d", path, prevPage), + Next: fmt.Sprintf("%s/page/%d", path, nextPage), }) } } @@ -152,8 +166,8 @@ type postsRequestConfig struct { path string limit int offset int - sections []string - taxonomy string + sections []*section + taxonomy *taxonomy taxonomyValue string } @@ -162,8 +176,8 @@ func getPosts(context context.Context, config *postsRequestConfig) (posts []*Pos var rows *sql.Rows defaultSelection := "select p.path, coalesce(content, ''), coalesce(published, ''), coalesce(updated, ''), coalesce(parameter, ''), coalesce(value, '') " postsTable := "posts" - if len(config.taxonomy) > 0 && 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 + "' and pp.value = '" + config.taxonomyValue + "')" + 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 pp.value = '" + config.taxonomyValue + "')" } if len(config.sections) > 0 { postsTable = "(select * from " + postsTable + " where" @@ -171,7 +185,7 @@ func getPosts(context context.Context, config *postsRequestConfig) (posts []*Pos if i > 0 { postsTable += " or" } - postsTable += " path like '/" + section + "/%'" + postsTable += " path like '/" + section.Name + "/%'" } postsTable += ")" } diff --git a/templates/index.gohtml b/templates/index.gohtml index 2d005bc..42b5d5d 100644 --- a/templates/index.gohtml +++ b/templates/index.gohtml @@ -4,6 +4,9 @@ {{ define "main" }}
+ {{ with .Title }}

{{ . }}

{{ end }} + {{ with .Description }}{{ md . }}{{ end }} + {{ if (or .Title .Description) }}
{{ end }} {{ range $i, $post := .Posts }} {{ include "summary" . }} {{ end }} diff --git a/templates/taxonomy.gohtml b/templates/taxonomy.gohtml index 294322f..360a49f 100644 --- a/templates/taxonomy.gohtml +++ b/templates/taxonomy.gohtml @@ -1,13 +1,16 @@ {{ define "title" }} - {{ blog.Title }} + {{ .Taxonomy.Title }} - {{ blog.Title }} {{ end }} {{ define "main" }}
-
    - {{ $taxonomy := .Taxonomy }} - {{ range $i, $value := .TaxonomyValues }}
  • {{ . }}
  • {{ end }} -
+ {{ with .Taxonomy.Title }}

{{ . }}

{{ end }} + {{ with .Taxonomy.Description }}{{ md . }}{{ end }} +
    + {{ $taxonomy := .Taxonomy.Name }} + {{ range $i, $value := .TaxonomyValues }} +
  • {{ . }}
  • {{ end }} +
{{ end }}