mirror of https://github.com/jlelse/GoBlog
Improve taxonomy lists
This commit is contained in:
parent
93cdda07a1
commit
ec5b9a6868
2
go.mod
2
go.mod
|
@ -54,7 +54,7 @@ require (
|
||||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
|
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
|
||||||
golang.org/x/text v0.3.4 // 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/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
|
||||||
gopkg.in/ini.v1 v1.62.0 // indirect
|
gopkg.in/ini.v1 v1.62.0 // indirect
|
||||||
gopkg.in/yaml.v2 v2.3.0 // indirect
|
gopkg.in/yaml.v2 v2.3.0 // indirect
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -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-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-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-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-20201121010211-780cb80bd7fb h1:z5+u0pkAUPUWd3taoTialQ2JAMo4Wo1Z3L25U4ZV9r0=
|
||||||
golang.org/x/tools v0.0.0-20201120032337-6d151481565c/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
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-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-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
|
|
21
posts.go
21
posts.go
|
@ -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) {
|
func serveTaxonomyValue(blog string, path string, tax *taxonomy, value string) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return serveIndex(&indexConfig{
|
return serveIndex(&indexConfig{
|
||||||
blog: blog,
|
blog: blog,
|
||||||
|
|
|
@ -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),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,8 @@
|
||||||
{{ define "footer" }}
|
{{ define "footer" }}
|
||||||
<footer>
|
<footer>
|
||||||
{{ with menu .Blog "footer" }}
|
{{ with menu .Blog "footer" }}
|
||||||
{{ $first := true }}
|
|
||||||
{{ range $i, $item := .Items }}
|
{{ range $i, $item := .Items }}
|
||||||
{{ if ne $first true }} • {{ end }}<a
|
{{ if ne $i 0 }} • {{ end }}<a href="{{ $item.Link }}">{{ $item.Title }}</a>
|
||||||
href="{{ $item.Link }}">{{ $item.Title }}</a>{{ $first = false }}
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
<p>© {{ dateformat now "2006" }} {{ with user.Name }}{{ . }}{{ else }}{{ .Blog.Title }}{{ end }}</p>
|
<p>© {{ dateformat now "2006" }} {{ with user.Name }}{{ . }}{{ else }}{{ .Blog.Title }}{{ end }}</p>
|
||||||
|
|
|
@ -4,10 +4,8 @@
|
||||||
{{ with .Blog.Description }}<p><i>{{ . }}</i></p>{{ end }}
|
{{ with .Blog.Description }}<p><i>{{ . }}</i></p>{{ end }}
|
||||||
<nav>
|
<nav>
|
||||||
{{ with menu .Blog "main" }}
|
{{ with menu .Blog "main" }}
|
||||||
{{ $first := true }}
|
|
||||||
{{ range $i, $item := .Items }}
|
{{ range $i, $item := .Items }}
|
||||||
{{ if ne $first true }} • {{ end }}<a
|
{{ if ne $i 0 }} • {{ end }}<a href="{{ $item.Link }}">{{ $item.Title }}</a>
|
||||||
href="{{ $item.Link }}">{{ $item.Title }}</a>{{ $first = false }}
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</nav>
|
</nav>
|
||||||
|
|
|
@ -4,15 +4,18 @@
|
||||||
|
|
||||||
{{ define "main" }}
|
{{ define "main" }}
|
||||||
<main>
|
<main>
|
||||||
{{ with .Data.Taxonomy.Title }}<h1>{{ . }}</h1>{{ end }}
|
{{ with .Data.Taxonomy.Title }}<h1>{{ . }}</h1>{{ end }}
|
||||||
{{ with .Data.Taxonomy.Description }}{{ md . }}{{ end }}
|
{{ with .Data.Taxonomy.Description }}{{ md . }}{{ end }}
|
||||||
<ul>
|
{{ $blog := .Blog }}
|
||||||
{{ $blog := .Blog }}
|
{{ $taxonomy := .Data.Taxonomy.Name }}
|
||||||
{{ $taxonomy := .Data.Taxonomy.Name }}
|
{{ range $i, $valueGroup := .Data.ValueGroups }}
|
||||||
{{ range $i, $value := (sort .Data.TaxonomyValues) }}
|
<h2>{{ $valueGroup.Identifier }}</h2>
|
||||||
<li><a href="{{ blogrelative $blog ( printf "/%s/%s" $taxonomy (urlize .) ) }}">{{ . }}</a></li>
|
<p>
|
||||||
|
{{ range $i, $value := $valueGroup.Strings }}
|
||||||
|
{{ if ne $i 0 }} • {{ end }}<a href="{{ blogrelative $blog ( printf "/%s/%s" $taxonomy (urlize .) ) }}">{{ . }}</a>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</ul>
|
</p>
|
||||||
|
{{ end }}
|
||||||
</main>
|
</main>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
|
24
utils.go
24
utils.go
|
@ -115,3 +115,27 @@ func slashIfEmpty(s string) string {
|
||||||
}
|
}
|
||||||
return s
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue