Use same variable names as Hugo

This makes gmnhg use settings from Hugo by default, allowing
to override variables in a separate gmnhg section.

Currently the templates use baseUrl, title, languageCode and
copyright.

Fixes #29.
This commit is contained in:
mntn 2021-09-20 13:20:40 -04:00 committed by GitHub
parent 3110ac629c
commit 372646033c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 26 deletions

View file

@ -75,12 +75,26 @@ can be used as a standalone program as well.
## Site configuration
For RSS feeds to use correct URLs, you should define `geminiBaseURL` in
Hugo's configuration file (`config.toml`, `config.yaml`, or
`config.json`).
gmnhg will pick up some attributes such as site title, base URL, and
language code from your Hugo configuration file (`config.toml`,
`config.yaml`, or `config.json`). Presently these are used in the
default RSS template.
Other attributes from this file, such as site title, will also be used
during RSS feed generation if they are defined.
gmnhg provides a way to override these attributes by defining a
`gmnhg` section in the configuration file and nesting the attributes
to override underneath this section. Presently you can override both
`baseUrl` and `title` in this manner.
For example, you could add the following to your `config.toml` to
override your `baseUrl`:
```
[gmnhg]
baseUrl = "gemini://mysite.com"
```
This is recommended, as it will ensure that RSS links on your Gemini
site use the correct URL.
## License

View file

@ -83,10 +83,12 @@
// loaded from the Hugo configuration file (config.toml, config.yaml,
// or config.json).
//
// A new setting, geminiBaseURL, should be added to the Hugo
// configuration file to ensure that RSS paths are correct. This is
// more or less the same as Hugo's baseURL, but is separate in case
// your Gemini site is deployed to a different server.
// gmnhg provides a way to override these attributes by defining a
// "gmnhg" section in the configuration file and nesting the attributes
// to override underneath this section. Presently you can override both
// "baseUrl" and "title" in this manner. It is recommended to override
// at least "baseUrl" unless your site uses a protocol-relative base
// URL (beginning with a double slash instead of https://).
//
// RSS templates can be overriden by defining a template in one of
// several places:
@ -148,10 +150,16 @@ var (
var hugoConfigFiles = []string{"config.toml", "config.yaml", "config.json"}
type SiteConfig struct {
GeminiBaseURL string `yaml:"geminiBaseURL"`
Title string `yaml:"title"`
Copyright string `yaml:"copyright"`
LanguageCode string `yaml:"languageCode"`
BaseURL string `yaml:"baseURL"`
Title string `yaml:"title"`
Copyright string `yaml:"copyright"`
LanguageCode string `yaml:"languageCode"`
Gmnhg GmnhgConfig `yaml:"gmnhg"`
}
type GmnhgConfig struct {
BaseURL string `yaml:"baseURL"`
Title string `yaml:"title"`
}
func copyFile(dst, src string) error {
@ -476,10 +484,12 @@ func main() {
}
}
sc := map[string]interface{}{
"GeminiBaseURL": siteConf.GeminiBaseURL,
"Title": siteConf.Title,
"Copyright": siteConf.Copyright,
"LanguageCode": siteConf.LanguageCode,
"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,

View file

@ -52,26 +52,30 @@ Index of {{ trimPrefix "/" $dir }}:
`)
var defaultRssTemplate = mustParseTmpl("rss", `{{- $Site := .Site -}}
{{- $Dirname := trimPrefix "/" .Dirname -}}
{{- $DirLink := list (trimSuffix "/" $Site.GeminiBaseURL) $Dirname | join "/" | html -}}
{{- $RssLink := list (trimSuffix "/" $Site.GeminiBaseURL) (trimPrefix "/" .Link) | join "/" | html -}}
{{- $SiteTitle := or $Site.GmnhgTitle $Site.Title | html -}}
{{- $SiteBaseURL := or $Site.GmnhgBaseURL $Site.BaseURL | trimSuffix "/" | html -}}
{{- $Dirname := .Dirname | trimPrefix "/" | html -}}
{{- $DirURL := list $SiteBaseURL $Dirname | join "/" | html -}}
{{- $RssURL := list $SiteBaseURL (trimPrefix "/" .Link) | join "/" | html -}}
{{- $RssTitle := printf "%s%s" (or $SiteTitle "Site feed") (and $Dirname (printf " - %s" $Dirname)) | html -}}
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>{{ if $Site.Title }}{{ html $Site.Title }}{{ else }}Site feed{{ with $Dirname }} for {{ html . }}{{end}}{{end}}</title>
<link>{{ $DirLink }}</link>
<description>Recent content{{ with $Dirname }} in {{ html . }}{{end}}{{ with $Site.Title }} on {{ html . }}{{end}}</description>
<title>{{ $RssTitle }}</title>
<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 }}
<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\" />" $RssLink }}
{{ printf "<atom:link href=%q rel=\"self\" type=\"application/rss+xml\" />" $RssURL }}
{{ range $i, $p := .Posts | sortPosts }}{{ if lt $i 25 }}
{{- $AbsURL := list (trimSuffix "/" $Site.GeminiBaseURL) (trimPrefix "/" $p.Link) | join "/" | html }}
{{- $RelURL := trimPrefix "/" $p.Link | html -}}
{{- $AbsURL := list $SiteBaseURL $RelURL | join "/" }}
<item>
<title>{{ if $p.Metadata.Title }}{{ html $p.Metadata.Title }}{{ else }}{{ trimPrefix "/" $p.Link | html }}{{end}}</title>
<title>{{ if $p.Metadata.Title }}{{ html $p.Metadata.Title }}{{ else }}{{ $RelURL }}{{end}}</title>
<link>{{ $AbsURL }}</link>
<pubDate>{{ $p.Metadata.Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</pubDate>
<guid>{{ $AbsURL }}</guid>