From 36150b5f88b2e513c6791b62aaf5eb3401d1c8a2 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Sun, 22 May 2022 20:01:48 +0200 Subject: Rework feeds to provide support for section --- layouts/_default/home.atom.xml | 47 --------------------------- layouts/_default/home.feed.xml | 45 -------------------------- layouts/_default/home.json.json | 28 ----------------- layouts/_default/list.atom.xml | 70 +++++++++++++++++++++++++++++++++++++++++ layouts/_default/list.feed.xml | 65 ++++++++++++++++++++++++++++++++++++++ layouts/_default/list.json.json | 43 +++++++++++++++++++++++++ 6 files changed, 178 insertions(+), 120 deletions(-) delete mode 100644 layouts/_default/home.atom.xml delete mode 100644 layouts/_default/home.feed.xml delete mode 100644 layouts/_default/home.json.json create mode 100644 layouts/_default/list.atom.xml create mode 100644 layouts/_default/list.feed.xml create mode 100644 layouts/_default/list.json.json (limited to 'layouts') diff --git a/layouts/_default/home.atom.xml b/layouts/_default/home.atom.xml deleted file mode 100644 index 575b2df..0000000 --- a/layouts/_default/home.atom.xml +++ /dev/null @@ -1,47 +0,0 @@ -{{ safeHTML "" }} - - {{ printf `<![CDATA[%s]]>` site.Title | safeHTML }} - {{ with site.Params.description }}{{ printf `` . | safeHTML }}{{ end }} - - {{ with site.Home.OutputFormats.Get "ATOM" }}{{ end }} - {{ with site.Home.OutputFormats.Get "FEED"}}{{end}} - {{ with site.Home.OutputFormats.Get "JSON"}}{{end}} - {{ $logo := resources.Get (site.Params.assets.logo | default "/img/open-graph-logo.png" ) }}{{ $logo = $logo.Resize "96x96" }}{{ $logo.Permalink | absURL }} - {{ $logo.Permalink | absURL }} - {{ with site.Author.name }} - {{ printf `` . | safeHTML }} - {{ with site.Author.email }}{{ . | html }}{{ end }} - {{ end }} - {{ with site.Params.Copyright }}{{ printf `` . | safeHTML }}{{ end }} - Hugo - {{ if not site.LastChange.IsZero }}{{ site.LastChange.UTC.Format site.Params.dateFormatFeed }}{{ end }} - {{ $uuid := sha1 (site.BaseURL | absURL) }}urn:uuid:{{ substr $uuid 0 8 }}-{{ substr $uuid 8 4 }}-5{{ substr $uuid 13 3 }}-{{ substr $uuid 16 1 }}9{{ substr $uuid 17 2 }}-{{ substr $uuid 21 12 }} - {{ $feedsize := site.Params.feedSize | default 25 }} - {{- range first $feedsize (where site.RegularPages.ByDate.Reverse "Type" "in" site.Params.mainSections) }} - - {{- $url := .Permalink | absURL }}{{ $uuid := sha1 (.Permalink | absURL) }}{{ $page := . }} - - {{ range (.GetTerms "tags") }}{{ end }} - - {{ printf `<![CDATA[%s]]>` .Title | safeHTML }} - {{ (.PublishDate.Format site.Params.dateFormatFeed) | html }} - {{ if ne .Date .Lastmod }}{{ .Lastmod.UTC.Format site.Params.dateFormatFeed }}{{ end }} - urn:uuid:{{ substr $uuid 0 8 }}-{{ substr $uuid 8 4 }}-5{{ substr $uuid 13 3 }}-{{ substr $uuid 16 1 }}9{{ substr $uuid 17 2 }}-{{ substr $uuid 21 12 }} - {{ if .Params.author }} - {{- $author_type := (printf "%T" .Params.author) }} - {{- if eq $author_type "string" }} - - {{ .Params.author }} - - {{- else if eq $author_type "[]string" }} - {{ range .Params.author }} - - {{ .Name }} - - {{ end }} - {{- end }} - {{ end }} - {{ printf `` .Summary | safeHTML }} - - {{- end }} - diff --git a/layouts/_default/home.feed.xml b/layouts/_default/home.feed.xml deleted file mode 100644 index 714bc07..0000000 --- a/layouts/_default/home.feed.xml +++ /dev/null @@ -1,45 +0,0 @@ -{{ safeHTML "" }} - - - {{ printf `<![CDATA[%s RSS 2.0]]>` site.Title | safeHTML }} - - {{ site.BaseURL | absURL }} - {{ $logo := resources.Get (site.Params.assets.logo | default "/img/open-graph-logo.png" ) }}{{ $logo = $logo.Resize "96x96" }} - - {{ $logo.Permalink | absURL }} - {{ printf `<![CDATA[%s RSS 2.0]]>` site.Title | safeHTML }} - {{ site.BaseURL | absURL }} - {{ $logo.Width }} - {{ $logo.Height }} - - - {{ with site.Home.OutputFormats.Get "FEED"}}{{end}} - {{ with site.Home.OutputFormats.Get "ATOM"}}{{end}} - {{ with site.Home.OutputFormats.Get "JSON"}}{{end}} - {{ with site.Params.description }}{{ printf `` . | safeHTML }}{{ end }} - Hugo {{ hugo.Version }} - {{ with site.Author.name }}{{ printf `` . | safeHTML }}{{ end }} - {{ with site.LanguageCode }}{{ . }}{{ end }} - {{ with site.Params.Copyright }}{{ printf `` . | safeHTML }}{{ end }} - {{ if not site.LastChange.IsZero }}{{ site.LastChange.UTC.Format site.Params.dateFormatRFC822Z | html }}{{ end }} - {{ $feedsize := site.Params.feedSize | default 25 }} - {{- range first $feedsize (where site.RegularPages.ByDate.Reverse "Type" "in" site.Params.mainSections) }} - - {{ printf `<![CDATA[%s]]>` .Title | safeHTML }} - {{ .Permalink | absURL }} - {{ .PublishDate.UTC.Format site.Params.dateFormatRFC822Z | html }} - {{ .Section }} - {{ range (.GetTerms "tags") }}{{ .LinkTitle }}{{end}} - {{ md5 .Permalink }} - {{ printf `` .Summary | safeHTML }} - - {{ end }} - diff --git a/layouts/_default/home.json.json b/layouts/_default/home.json.json deleted file mode 100644 index 6df1514..0000000 --- a/layouts/_default/home.json.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "version": "https://jsonfeed.org/version/1.1", - "title": {{ (printf "%s JSON Feed" site.Title) | htmlUnescape | jsonify }}, - "home_page_url": {{ site.BaseURL | absURL | jsonify }}, - {{ with .OutputFormats.Get "JSON" }}"feed_url": {{.Permalink | absURL | jsonify }},{{ end }} - {{ with site.Params.description}}"description": {{site.Params.description | jsonify}},{{end}} - {{- $logo := resources.Get (site.Params.assets.logo | default "/img/open-graph-logo.png" ) }}{{ $logo = $logo.Resize "96x96" }} - "icon": {{ $logo.Permalink | jsonify }}, - "favicon": {{ $logo.Permalink | jsonify }}, - {{ with site.Author.name }}"authors": [ - { - "name": {{ . | jsonify }}{{ with site.Author.twitter }}, - "url": {{ (printf "https://twitter.com/%s" . ) | jsonify }}{{ end }} - } - ],{{ end }} - {{ with site.LanguageCode }}"language" : "{{ . }}",{{ end }}{{ $feedsize := site.Params.feedSize | default 25 }} - "items": [{{ $r := first $feedsize (where site.RegularPages.ByDate.Reverse "Type" "in" site.Params.mainSections) }} {{ range $i, $ := $r }}{{ if $i }},{{ end }} - { - "id": {{ $uuid := sha1 (.Permalink | absURL)}}"{{substr $uuid 0 8}}-{{substr $uuid 8 4}}-5{{substr $uuid 13 3}}-{{substr $uuid 16 1}}9{{substr $uuid 17 2}}-{{substr $uuid 21 12}}", - "url": {{ .Permalink | jsonify }}, - "title": {{ .Title | plainify | chomp | jsonify }}, - "summary": {{ .Summary | plainify | chomp | jsonify }}, - "date_published": {{ .PublishDate.UTC.Format site.Params.dateFormatFeed | jsonify }}{{ if ne .Date .Lastmod }}, - "date_modified" : {{ .Lastmod.UTC.Format site.Params.dateFormatFeed | jsonify }}{{ end }} - } - {{- end}} - ] -} diff --git a/layouts/_default/list.atom.xml b/layouts/_default/list.atom.xml new file mode 100644 index 0000000..f82bb8b --- /dev/null +++ b/layouts/_default/list.atom.xml @@ -0,0 +1,70 @@ +{{- $pctx := . -}} +{{- if .IsHome -}}{{ $pctx = .Site }}{{- end -}} + +{{- $pages := slice -}} +{{- if $.IsHome -}} + {{- $pages = where $pctx.RegularPages.ByDate.Reverse "Type" "in" site.Params.mainSections -}} +{{- else if $.IsSection -}} + {{- $pages = $pctx.RegularPages.ByDate.Reverse -}} +{{- else -}} + {{- $pages = $pctx.Pages -}} +{{- end -}} + +{{- $limit := site.Params.feedSize | default 25 -}} +{{- $pages = $pages | first $limit -}} + +{{- safeHTML "" }} + + {{ printf `<![CDATA[%s]]>` site.Title | safeHTML }} + {{ with site.Params.description }}{{ printf `` . | safeHTML }}{{ end }} + {{- $output_formats := .OutputFormats }} + {{- range $output_formats -}} + {{- $rel := (or (and (eq "atom" (.Name | lower)) "self") "alternate") -}} + {{- with $output_formats.Get .Name }} + {{ printf `` .Permalink $rel .MediaType.Type .Name | safeHTML }} + {{- end -}} + {{- end }} + {{ $logo := resources.Get (site.Params.assets.logo | default "/img/open-graph-logo.png") }}{{ $logo = $logo.Resize "96x96" }}{{ $logo.Permalink | absURL }} + {{ $logo.Permalink | absURL }} + {{ with site.Author.name }} + {{ printf `` . | safeHTML }} + {{ with site.Author.email }}{{ . | html }}{{ end }} + {{ end }} + {{ with site.Params.Copyright }} + {{- $copyright := replace . "{year}" now.Year -}} + {{- $copyright = replace $copyright "©" "©" }}{{ $copyright | plainify }}{{ end }} + Hugo + {{ if not site.LastChange.IsZero }}{{ site.LastChange.UTC.Format site.Params.dateFormatFeed }}{{ end }} + {{ $uuid := sha1 (site.BaseURL | absURL) }}urn:uuid:{{ substr $uuid 0 8 }}-{{ substr $uuid 8 4 }}-5{{ substr $uuid 13 3 }}-{{ substr $uuid 16 1 }}9{{ substr $uuid 17 2 }}-{{ substr $uuid 21 12 }} + {{- range $pages }} + + {{- $url := .Permalink | absURL }}{{ $uuid := sha1 (.Permalink | absURL) }}{{ $page := . }} + + {{ range (.GetTerms "tags") }}{{ end }} + + {{ printf `<![CDATA[%s]]>` .Title | safeHTML }} + {{/* rel=related: See https://validator.w3.org/feed/docs/atom.html#link */}} + {{- range first 5 (site.RegularPages.Related .) }} + + {{- end }} + {{ (.PublishDate.Format site.Params.dateFormatFeed) | html }} + {{ if ne .Date .Lastmod }}{{ .Lastmod.UTC.Format site.Params.dateFormatFeed }}{{ end }} + urn:uuid:{{ substr $uuid 0 8 }}-{{ substr $uuid 8 4 }}-5{{ substr $uuid 13 3 }}-{{ substr $uuid 16 1 }}9{{ substr $uuid 17 2 }}-{{ substr $uuid 21 12 }} + {{ if .Params.author }} + {{- $author_type := (printf "%T" .Params.author) }} + {{- if eq $author_type "string" }} + + {{ .Params.author }} + + {{- else if eq $author_type "[]string" }} + {{ range .Params.author }} + + {{ .Name }} + + {{ end }} + {{- end }} + {{ end }} + {{ printf `` .Summary | safeHTML }} + + {{- end }} + diff --git a/layouts/_default/list.feed.xml b/layouts/_default/list.feed.xml new file mode 100644 index 0000000..5a276ec --- /dev/null +++ b/layouts/_default/list.feed.xml @@ -0,0 +1,65 @@ +{{- $pctx := . -}} +{{- if .IsHome -}}{{ $pctx = .Site }}{{- end -}} + +{{- $pages := slice -}} +{{- if $.IsHome -}} + {{- $pages = where $pctx.RegularPages.ByDate.Reverse "Type" "in" site.Params.mainSections -}} +{{- else if $.IsSection -}} + {{- $pages = $pctx.RegularPages.ByDate.Reverse -}} +{{- else -}} + {{- $pages = $pctx.Pages -}} +{{- end -}} + +{{- $limit := site.Params.feedSize | default 25 -}} +{{- $pages = $pages | first $limit -}} + +{{- safeHTML "" }} + + + {{ printf `<![CDATA[%s RSS 2.0]]>` site.Title | safeHTML }} + + {{ site.BaseURL | absURL }} + {{ $logo := resources.Get (site.Params.assets.logo | default "/img/open-graph-logo.png") }}{{ $logo = $logo.Resize "96x96" }} + + {{ $logo.Permalink | absURL }} + {{ printf `<![CDATA[%s RSS 2.0]]>` site.Title | safeHTML }} + {{ site.BaseURL | absURL }} + {{ $logo.Width }} + {{ $logo.Height }} + + {{- $output_formats := .OutputFormats }} + {{- range $output_formats -}} + {{- $rel := (or (and (eq "feed" (.Name | lower)) "self") "alternate") -}} + {{- with $output_formats.Get .Name }} + {{ printf `` .Permalink $rel .MediaType.Type .Name | safeHTML }} + {{- end -}} + {{- end }} + {{ with site.Params.description }}{{ printf `` . | safeHTML }}{{ end }} + Hugo {{ hugo.Version }} + {{ with site.Author.name }}{{ printf `` . | safeHTML }}{{ end }} + {{ with site.LanguageCode }}{{ . }}{{ end }} + {{ with site.Params.Copyright }} + {{- $copyright := replace . "{year}" now.Year -}} + {{- $copyright = replace $copyright "©" "©" }}{{ $copyright | plainify }}{{ end }} + {{ if not site.LastChange.IsZero }}{{ site.LastChange.UTC.Format site.Params.dateFormatRFC822Z | html }}{{ end }} + {{ $feedsize := site.Params.feedSize | default 25 }} + {{- range $pages }} + + {{ printf `<![CDATA[%s]]>` .Title | safeHTML }} + {{ .Permalink | absURL }}?utm_source=rss_feed + {{ .PublishDate.UTC.Format site.Params.dateFormatRFC822Z | html }} + {{ .Section }} + {{ range (.GetTerms "tags") }}{{ .LinkTitle }}{{end}} + {{ md5 .Permalink }} + {{ printf `` .Summary | safeHTML }} + + {{ end }} + diff --git a/layouts/_default/list.json.json b/layouts/_default/list.json.json new file mode 100644 index 0000000..6151211 --- /dev/null +++ b/layouts/_default/list.json.json @@ -0,0 +1,43 @@ +{{- $pctx := . -}} +{{- if .IsHome -}}{{ $pctx = .Site }}{{- end -}} + +{{- $pages := slice -}} +{{- if $.IsHome -}} + {{- $pages = where $pctx.RegularPages.ByDate.Reverse "Type" "in" site.Params.mainSections -}} +{{- else if $.IsSection -}} + {{- $pages = $pctx.RegularPages.ByDate.Reverse -}} +{{- else -}} + {{- $pages = $pctx.Pages -}} +{{- end -}} + +{{- $limit := site.Params.feedSize | default 25 -}} +{{- $pages = $pages | first $limit -}} + +{ + "version": "https://jsonfeed.org/version/1.1", + "title": {{ (printf "%s JSON Feed" site.Title) | htmlUnescape | jsonify }}, + "home_page_url": {{ printf "%s?utm_source=json_feed" site.BaseURL | absURL | jsonify }}, + {{ with .OutputFormats.Get "JSON" }}"feed_url": {{ .Permalink | absURL | jsonify }},{{ end }} + {{ with site.Params.description}}"description": {{ site.Params.description | jsonify }},{{ end }} + {{- $logo := resources.Get (site.Params.assets.logo | default "/img/open-graph-logo.png") }}{{ $logo = $logo.Resize "96x96" }} + "icon": {{ $logo.Permalink | jsonify }}, + "favicon": {{ $logo.Permalink | jsonify }}, + {{ with site.Author.name }}"authors": [ + { + "name": {{ . | jsonify }}{{ with site.Author.twitter }}, + "url": {{ (printf "https://twitter.com/%s" . ) | jsonify }}{{ end }} + } + ],{{ end }} + {{ with site.LanguageCode }}"language" : "{{ . }}",{{ end }}{{ $feedsize := site.Params.feedSize | default 25 }} + "items": [{{ range $i, $ := $pages }}{{ if $i }},{{ end }} + { + "id": {{ $uuid := sha1 (.Permalink | absURL)}}"{{substr $uuid 0 8}}-{{substr $uuid 8 4}}-5{{substr $uuid 13 3}}-{{substr $uuid 16 1}}9{{substr $uuid 17 2}}-{{substr $uuid 21 12}}", + "url": {{ printf "%s?utm_source=json_feed" .Permalink | jsonify }}, + "title": {{ .Title | plainify | chomp | jsonify }}, + "summary": {{ .Summary | plainify | chomp | jsonify }}, + "date_published": {{ .PublishDate.UTC.Format site.Params.dateFormatFeed | jsonify }}{{ if ne .Date .Lastmod }}, + "date_modified" : {{ .Lastmod.UTC.Format site.Params.dateFormatFeed | jsonify }}{{ end }} + } + {{- end}} + ] +} -- cgit v1.2.3