Improve taxonomy lists

This commit is contained in:
Jan-Lukas Else 2020-11-22 09:11:57 +01:00
parent 93cdda07a1
commit ec5b9a6868
8 changed files with 61 additions and 38 deletions

2
go.mod
View File

@ -54,7 +54,7 @@ require (
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
golang.org/x/text v0.3.4 // indirect
golang.org/x/tools v0.0.0-20201120032337-6d151481565c // indirect
golang.org/x/tools v0.0.0-20201121010211-780cb80bd7fb // indirect
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect

4
go.sum
View File

@ -467,8 +467,8 @@ golang.org/x/tools v0.0.0-20191216052735-49a3e744a425 h1:VvQyQJN0tSuecqgcIxMWnnf
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200410194907-79a7a3126eef/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20201120032337-6d151481565c h1:IXtuZap6vTKIQ3jemmcwf2gY4BT+lwfZHBYwxMGe5/k=
golang.org/x/tools v0.0.0-20201120032337-6d151481565c/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20201121010211-780cb80bd7fb h1:z5+u0pkAUPUWd3taoTialQ2JAMo4Wo1Z3L25U4ZV9r0=
golang.org/x/tools v0.0.0-20201121010211-780cb80bd7fb/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=

View File

@ -105,27 +105,6 @@ func serveSection(blog string, path string, section *section) func(w http.Respon
})
}
func serveTaxonomy(blog string, tax *taxonomy) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
allValues, err := allTaxonomyValues(blog, tax.Name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
render(w, templateTaxonomy, &renderData{
blogString: blog,
Canonical: appConfig.Server.PublicAddress + r.URL.Path,
Data: struct {
Taxonomy *taxonomy
TaxonomyValues []string
}{
Taxonomy: tax,
TaxonomyValues: allValues,
},
})
}
}
func serveTaxonomyValue(blog string, path string, tax *taxonomy, value string) func(w http.ResponseWriter, r *http.Request) {
return serveIndex(&indexConfig{
blog: blog,

21
taxonomies.go Normal file
View File

@ -0,0 +1,21 @@
package main
import "net/http"
func serveTaxonomy(blog string, tax *taxonomy) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
allValues, err := allTaxonomyValues(blog, tax.Name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
render(w, templateTaxonomy, &renderData{
blogString: blog,
Canonical: appConfig.Server.PublicAddress + r.URL.Path,
Data: map[string]interface{}{
"Taxonomy": tax,
"ValueGroups": groupStrings(allValues),
},
})
}
}

View File

@ -1,10 +1,8 @@
{{ define "footer" }}
<footer>
{{ with menu .Blog "footer" }}
{{ $first := true }}
{{ range $i, $item := .Items }}
{{ if ne $first true }} &bull; {{ end }}<a
href="{{ $item.Link }}">{{ $item.Title }}</a>{{ $first = false }}
{{ if ne $i 0 }} &bull; {{ end }}<a href="{{ $item.Link }}">{{ $item.Title }}</a>
{{ end }}
{{ end }}
<p>&copy; {{ dateformat now "2006" }} {{ with user.Name }}{{ . }}{{ else }}{{ .Blog.Title }}{{ end }}</p>

View File

@ -4,10 +4,8 @@
{{ with .Blog.Description }}<p><i>{{ . }}</i></p>{{ end }}
<nav>
{{ with menu .Blog "main" }}
{{ $first := true }}
{{ range $i, $item := .Items }}
{{ if ne $first true }} &bull; {{ end }}<a
href="{{ $item.Link }}">{{ $item.Title }}</a>{{ $first = false }}
{{ if ne $i 0 }} &bull; {{ end }}<a href="{{ $item.Link }}">{{ $item.Title }}</a>
{{ end }}
{{ end }}
</nav>

View File

@ -4,15 +4,18 @@
{{ define "main" }}
<main>
{{ with .Data.Taxonomy.Title }}<h1>{{ . }}</h1>{{ end }}
{{ with .Data.Taxonomy.Description }}{{ md . }}{{ end }}
<ul>
{{ $blog := .Blog }}
{{ $taxonomy := .Data.Taxonomy.Name }}
{{ range $i, $value := (sort .Data.TaxonomyValues) }}
<li><a href="{{ blogrelative $blog ( printf "/%s/%s" $taxonomy (urlize .) ) }}">{{ . }}</a></li>
{{ with .Data.Taxonomy.Title }}<h1>{{ . }}</h1>{{ end }}
{{ with .Data.Taxonomy.Description }}{{ md . }}{{ end }}
{{ $blog := .Blog }}
{{ $taxonomy := .Data.Taxonomy.Name }}
{{ range $i, $valueGroup := .Data.ValueGroups }}
<h2>{{ $valueGroup.Identifier }}</h2>
<p>
{{ range $i, $value := $valueGroup.Strings }}
{{ if ne $i 0 }} &bull; {{ end }}<a href="{{ blogrelative $blog ( printf "/%s/%s" $taxonomy (urlize .) ) }}">{{ . }}</a>
{{ end }}
</ul>
</p>
{{ end }}
</main>
{{ end }}

View File

@ -115,3 +115,27 @@ func slashIfEmpty(s string) string {
}
return s
}
type stringGroup struct {
Identifier string
Strings []string
}
func groupStrings(toGroup []string) []stringGroup {
stringMap := map[string][]string{}
for _, s := range toGroup {
first := strings.ToUpper(s[:1])
stringMap[first] = append(stringMap[first], s)
}
stringGroups := []stringGroup{}
for key, sa := range stringMap {
stringGroups = append(stringGroups, stringGroup{
Identifier: key,
Strings: sortedStrings(sa),
})
}
sort.Slice(stringGroups, func(i, j int) bool {
return strings.ToLower(stringGroups[i].Identifier) < strings.ToLower(stringGroups[j].Identifier)
})
return stringGroups
}