Clean up templates and unify template metadata

This exposes more site-specific information to templates. Top-level
_index(.gmi).md is now treated by the templating code the same
way per-directory indices are (which introduces a breaking change:
top-level posts now receive .Posts instead of .PostData).

Web-specific metadata is now removed from the RSS.

This makes gmnhg usable out of the box to generate a reasonable
site with zero configuration even with pages with no metadata.
This commit is contained in:
mntn 2021-09-21 17:29:14 -04:00 committed by GitHub
parent 372646033c
commit c80b72613d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 22 deletions

View file

@ -46,22 +46,29 @@
//
// 1. Single pages are given .Post, which contains the entire post
// rendered, .Metadata, which contains the metadata crawled from it (see
// HugoMetadata), and .Link, which contains the filename relative to
// content dir (with .md replaced with .gmi).
// Metadata in internal/gmnhg/post.go), and .Link, which contains the
// filename relative to content dir (with .md replaced with .gmi).
//
// 2. Directory index pages are passed .Posts, which is a slice over
// post metadata crawled (see HugoMetadata), .Dirname, which is
// directory name relative to content dir, and .Content, which is
// rendered from directory's _index.gmi.md.
// 2. Directory index pages, including the top-level index, are passed
// .Posts, which is a slice over post metadata crawled (see Metadata in
// internal/gmnhg/post.go), .Dirname, which is the directory name
// relative to the content dir, .Link, which contains the index filename
// relative to the content dir (with .md replaced with .gmi), .Content,
// which is rendered from directory's _index.gmi.md, and .Site, which
// contains site-level configuration data loaded from the Hugo site's
// config.toml, config.yaml, or config.json.
//
// The following keys are available in the .Site map, listed with their
// associated Hugo configuration key: .BaseURL (baseUrl), .GmnhgBaseURL,
// (gmnhg.baseUrl), .Title (title), .GmnhgTitle (gmnhg.title), .Copyright
// (copyright), and .LanguageCode (languageCode).
//
// Directory indices are passed all posts from subdirectories (branch
// and leaf bundles), with the exception of leaf resource pages. This
// allows for roll-up indices.
//
// 3. The top-level index.gmi is passed with the .PostData map whose
// keys are top-level content directories names and values are slices
// over the same post props as specified in 1, and .Content, which is
// rendered from top-level _index.gmi.md.
// 3. RSS templates receive the same data as directory index pages, but
// the filename provided by .Link is rss.xml instead of index.gmi.
//
// This program provides some extra template functions on top of sort:
//
@ -430,10 +437,20 @@ func main() {
if err != nil {
panic(err)
}
sc := map[string]interface{}{
"BaseURL": siteConf.BaseURL,
"GmnhgBaseURL": siteConf.Gmnhg.BaseURL,
"Title": siteConf.Title,
"GmnhgTitle": siteConf.Gmnhg.Title,
"Copyright": siteConf.Copyright,
"LanguageCode": siteConf.LanguageCode,
}
cnt := map[string]interface{}{
"Posts": posts,
"Dirname": dirname,
"Link": path.Join(dirname, indexFilename),
"Content": gemtext,
"Site": sc,
}
buf := bytes.Buffer{}
if err := tmpl.Execute(&buf, cnt); err != nil {
@ -457,8 +474,22 @@ func main() {
if err != nil {
panic(err)
}
sc := map[string]interface{}{
"BaseURL": siteConf.BaseURL,
"GmnhgBaseURL": siteConf.Gmnhg.BaseURL,
"Title": siteConf.Title,
"GmnhgTitle": siteConf.Gmnhg.Title,
"Copyright": siteConf.Copyright,
"LanguageCode": siteConf.LanguageCode,
}
cnt := map[string]interface{}{
"Posts": topLevelPosts,
"Dirname": "/",
"Link": path.Join("/", indexFilename),
"Content": gemtext,
"Site": sc,
}
buf := bytes.Buffer{}
cnt := map[string]interface{}{"PostData": topLevelPosts, "Content": gemtext}
if err := indexTmpl.Execute(&buf, cnt); err != nil {
panic(err)
}

View file

@ -35,20 +35,22 @@ func defineFuncMap() template.FuncMap {
return fm
}
var defaultSingleTemplate = mustParseTmpl("single", `# {{ .Metadata.Title }}
var defaultSingleTemplate = mustParseTmpl("single", `{{ with .Metadata.Title }}# {{.}}
{{ .Metadata.Date.Format "2006-01-02 15:04" }}
{{ end }}{{ if not .Metadata.Date.IsZero }}{{ .Metadata.Date.Format "2006-01-02 15:04" }}
{{ printf "%s" .Post }}`)
{{ end }}{{ printf "%s" .Post }}`)
var defaultIndexTemplate = mustParseTmpl("index", `# Site index
var defaultIndexTemplate = mustParseTmpl("index", `# {{ or .Site.GmnhgTitle (or .Site.Title "Site index") }}
{{ with .Content }}
{{ printf "%s" . }}{{- end }}
{{ with .Content }}{{ printf "%s" . -}}{{ end }}
{{- range $dir, $posts := .PostData }}{{ if and (ne $dir "/") (eq (dir $dir) "/") }}
{{- range $dir, $posts := .Posts }}{{ if and (ne $dir "/") (eq (dir $dir) "/") }}
Index of {{ trimPrefix "/" $dir }}:
{{ range $p := $posts | sortPosts }}=> {{ $p.Link }} {{ $p.Metadata.Date.Format "2006-01-02 15:04" }} - {{ if $p.Metadata.Title }}{{ $p.Metadata.Title }}{{else}}{{ $p.Link }}{{end}}
{{ end }}{{ end }}{{ end }}
{{ range $p := $posts | sortPosts }}=> {{ $p.Link }} {{ if not $p.Metadata.Date.IsZero }}
{{- $p.Metadata.Date.Format "2006-01-02 15:04" }} - {{end}}{{ if $p.Metadata.Title }}{{ $p.Metadata.Title }}{{else}}{{ $p.Link }}{{end}}
{{ end }}{{ end }}{{ end -}}
`)
var defaultRssTemplate = mustParseTmpl("rss", `{{- $Site := .Site -}}
@ -65,9 +67,7 @@ var defaultRssTemplate = mustParseTmpl("rss", `{{- $Site := .Site -}}
<link>{{ $DirURL }}</link>
<description>Recent content{{ with $Dirname }} in {{ . }}{{end}}{{ with $SiteTitle }} on {{ . }}{{end}}</description>
<generator>gmnhg</generator>{{ with $Site.LanguageCode }}
<language>{{ html .}}</language>{{end}}{{ with $Site.Author.email }}
<managingEditor>{{ html . }}{{ with $Site.Author.name }} ({{ html . }}){{end}}</managingEditor>
<webMaster>{{ html . }}{{ with $Site.Author.name }} ({{ html . }}){{end}}</webMaster>{{end}}{{ with $Site.Copyright }}
<language>{{ html .}}</language>{{end}}{{ with $Site.Copyright }}
<copyright>{{ html . }}</copyright>{{end}}
<lastBuildDate>{{ now.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</lastBuildDate>
{{ printf "<atom:link href=%q rel=\"self\" type=\"application/rss+xml\" />" $RssURL }}