{{ $page := . }}

{{ $limit := site.Config.Services.RSS.Limit }}
{{ $pages := "" }}
{{ $pagesource := cond $page.IsHome site $page }}
{{ $title := partial "paige/title.html" $page }}

{{ if or $page.IsHome $page.IsSection }}
    {{ $pages = $pagesource.RegularPages }}
{{ else }}
    {{ $pages = $pagesource.Pages }}
{{ end }}

{{ if ge $limit 1 }}
    {{ $pages = $pages | first $limit }}
{{ end }}

{{ $pages = where $pages "Params.paige.rss.hide" "ne" true }}

{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        {{ with $page.OutputFormats.Get "RSS" }}
            {{ printf `<atom:link href="%s" rel="self" type="%s"/>` .Permalink .MediaType | safeHTML }}
        {{ end }}
        {{ with site.Copyright }}
            <copyright>{{ . }}</copyright>
        {{ end }}
        <description>{{ with $title -}} {{- . -}} {{- else -}} Recent content {{- end }}</description>
        {{ with site.LanguageCode | default site.Language.Lang }}
            <language>{{ . }}</language>
        {{ end }}
        {{ if and $page.PublishDate (not $page.PublishDate.IsZero) }}
            <lastBuildDate>{{ $page.PublishDate.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</lastBuildDate>
        {{ end }}
        <link>{{ $page.Permalink }}</link>
        {{ with $page.Param "paige.rss.managing_editor" }}
            <managingEditor>{{ . }}</managingEditor>
        {{ end }}
        {{ with $title }}
            <title>{{ . }}</title>
        {{ end }}
        {{ with $page.Param "paige.rss.web_master" }}
            <webMaster>{{ . }}</webMaster>
        {{ end }}
        {{ range $pages }}
            <item>
                {{ with partial "paige/authors.html" . }}
                    {{ $authors := slice }}

                    {{ range . }}
                        {{ $authors = $authors | append .name }}
                    {{ end }}

                    <author>{{ delimit $authors ", " }}</author>
                {{ end }}
                {{ if .Content }}
                    <description>{{ printf "<![CDATA[%s]]>" .Content | safeHTML }}</description>
                {{ else if .Description }}
                    <description>{{ .Description | markdownify | html }}</description>
                {{ end }}
                {{ with .Permalink }}
                    <guid>{{ . }}</guid>
                    <link>{{ . }}</link>
                {{ end }}
                {{ with .PublishDate }}
                    <pubDate>{{ .Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</pubDate>
                {{ end }}
                {{ with .Title }}
                    <title>{{ . | markdownify | plainify | htmlUnescape }}</title>
                {{ end }}
            </item>
        {{ end }}
    </channel>
</rss>
