From 3d60955e17328d2bb212a9dceb4825ac2f260681 Mon Sep 17 00:00:00 2001 From: Anthony Fok Date: Fri, 30 Jan 2015 06:24:44 -0700 Subject: [PATCH] Use .Date.IsZero to skip unset date in embedded templates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use `{{ if not .Date.IsZero }}` to print dates only when they are defined. This is to avoid things like Mon, 01 Jan 0001 00:00:00 +0000 and 0001-01-01T00:00:00+00:00 showing up in index.xml (RSS) and sitemap.xml. Pipe dates with ±hh:mm time zone through `safeHtml` to prevent the `+` sign from turning into `+`. Also make some shuffling to avoid blank lines in the output. --- tpl/template_embedded.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tpl/template_embedded.go b/tpl/template_embedded.go index d09ef8447..dbeace556 100644 --- a/tpl/template_embedded.go +++ b/tpl/template_embedded.go @@ -50,18 +50,18 @@ func (t *GoHtmlTemplate) EmbedTemplates() { {{ with .Title }}{{.}} on {{ end }}{{ .Site.Title }} {{ .Permalink }} Recent content {{ with .Title }}in {{.}} {{ end }}on {{ .Site.Title }} - Hugo -- gohugo.io - {{ with .Site.LanguageCode }}{{.}}{{end}} - {{ with .Site.Author.email }}{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}{{end}} - {{ with .Site.Author.email }}{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}{{end}} - {{ with .Site.Copyright }}{{.}}{{end}} - {{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" }} + Hugo -- gohugo.io{{ with .Site.LanguageCode }} + {{.}}{{end}}{{ with .Site.Author.email }} + {{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}{{end}}{{ with .Site.Author.email }} + {{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}{{end}}{{ with .Site.Copyright }} + {{.}}{{end}}{{ if not .Date.IsZero }} + {{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHtml }}{{ end }} {{ range first 15 .Data.Pages }} {{ .Title }} {{ .Permalink }} - {{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" }} + {{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHtml }} {{ with .Site.Author.email }}{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}{{end}} {{ .Permalink }} {{ .Content | html }} @@ -73,8 +73,8 @@ func (t *GoHtmlTemplate) EmbedTemplates() { t.AddInternalTemplate("_default", "sitemap.xml", ` {{ range .Data.Pages }} - {{ .Permalink }} - {{ safeHtml ( .Date.Format "2006-01-02T15:04:05-07:00" ) }}{{ with .Sitemap.ChangeFreq }} + {{ .Permalink }}{{ if not .Date.IsZero }} + {{ safeHtml ( .Date.Format "2006-01-02T15:04:05-07:00" ) }}{{ end }}{{ with .Sitemap.ChangeFreq }} {{ . }}{{ end }}{{ if ge .Sitemap.Priority 0.0 }} {{ .Sitemap.Priority }}{{ end }} @@ -134,7 +134,7 @@ func (t *GoHtmlTemplate) EmbedTemplates() { {{ end }}{{ end }} -{{ with .Params.audio }} +{{ with .Params.audio }} {{ end }}{{ with .Params.locale }} {{ end }}{{ with .Site.Params.title }} {{ end }}{{ with .Params.videos }} @@ -192,9 +192,9 @@ func (t *GoHtmlTemplate) EmbedTemplates() { -{{if .IsPage}}{{ $ISO8601 := "2006-01-02T15:04:05-07:00" }}{{ if ne (.PublishDate.Format $ISO8601) "0001-01-01T00:00:00+00:00" }} -{{ end }} - +{{if .IsPage}}{{ $ISO8601 := "2006-01-02T15:04:05-07:00" }}{{ if not .PublishDate.IsZero }} +{{ end }} + {{ with .Params.images }}{{ range first 6 . }}