{{- $pctx := . -}}
{{- if .IsHome -}}{{ $pctx = .Site }}{{- end -}}
{{- $pages := slice -}}
{{- if or $.IsHome $.IsSection -}}
{{- $pages = $pctx.RegularPages -}}
{{- else -}}
{{- $pages = $pctx.Pages -}}
{{- end -}}
{{- $limit := .Site.Config.Services.RSS.Limit -}}
{{- if ge $limit 1 -}}
{{- $pages = $pages | first $limit -}}
{{- end -}}
{{- printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
    <channel>
        <title>{{ if eq .Title .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} by {{ end }}{{ .Site.Title }}{{ end }}</title>
        <link>{{ .Permalink }}</link>
        <author>{{ .Site.Author.name }}</author>
        <email>{{ .Site.Author.email }}</email>
        {{ with .Site.Copyright }}<copyright>{{.}}</copyright>{{end}}
        <description>Recent content {{ if ne .Title .Site.Title }}{{ with .Title }}in {{.}} {{ end }}{{ end }}by {{ .Site.Title }}</description>
        <image>
            <url>{{ .Site.BaseURL }}img/website-avatar-large.png</url>
            <title>{{ if eq .Title .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} by {{ end }}{{ .Site.Title }}{{ end }}</title>
            <link>{{ .Permalink }}</link>
        </image>

        <!-- *** ITUNES TAGS *** -->
        <itunes:author>{{ .Site.Author.name }}</itunes:author>
        <itunes:category text="Society &amp; Culture">
            <itunes:category text="Philosophy"/>
        </itunes:category>
        <itunes:explicit>No</itunes:explicit>
        <itunes:type>episodic</itunes:type>
        <itunes:summary>Philosophy essays, debates, discussions, and other explorations.</itunes:summary>
        <itunes:image href="{{ .Site.BaseURL }}img/website-avatar-large.png"/>
        <itunes:owner>
            <itunes:name>{{ if eq .Title .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} by {{ end }}{{ .Site.Title }}{{ end }}</itunes:name>
            <itunes:email>{{ .Site.Params.email }}</itunes:email>
        </itunes:owner>
        <!-- /// END ITUNES TAGS /// -->

        <generator>Hugo -- gohugo.io</generator>
        {{ with .Site.LanguageCode }}<language>{{.}}</language>{{end}}
        {{ with .Site.Author.email }}<managingEditor>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</managingEditor>{{end}}
        {{ with .Site.Author.email }}<webMaster>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</webMaster>{{end}}
        {{ if not .Date.IsZero }}<lastBuildDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
        {{- with .OutputFormats.Get "RSS" -}}
        {{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
        {{- end -}}
        {{ range $pages }}
        <item>
            <title>{{ .Title }}</title>
            <category>{{ .Params.series }}</category>
            <pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
            <link>{{ .Permalink }}</link>
            <guid>{{ .Permalink }}</guid>

            <itunes:image href="{{.Site.Params.podcastCdnURL }}{{ with .Params.image }}{{.}}{{ end }}"/>

            {{ if isset .Params "enclosure" }}
                {{ $enclosure := .Params.enclosure }}
                {{ $scratch := newScratch }}
                {{ range $.Site.Data.podcasts.podcasts }}
                    {{ if eq .filename $enclosure }}
                        {{ $scratch.Set "filesize" .filesize }}
                    {{ end }}
                {{ end}}
                {{ $filesize := $scratch.Get "filesize" }}
                <enclosure url="{{ $.Site.Params.podcastCdnURL }}{{ $enclosure }}" type="audio/mpeg" length="{{ $filesize }}"/>
            {{ end }}

            <description>
                {{`<![CDATA[ ` | safeHTML }}
                {{ if isset .Params "image" }}<p><img src="{{ .Site.Params.podcastCdnURL }}{{ with .Params.image }}{{.}}{{ end }}" width="600" height="338" /></p>{{end}}
                {{ .Summary | safeHTML }}
                ]]>
            </description>
            <content:encoded>
                {{`<![CDATA[ ` | safeHTML }}
                  {{ if isset .Params "image" }}
                    {{ $cdnUrl := .Site.Params.podcastCdnUrl }}
                    <p><img src="{{ $cdnUrl }}{{ with .Params.image }}{{.}}{{ end }}"/></p>
                  {{end}}
                {{ .Content | safeHTML }}
                ]]>
            </content:encoded>
        </item>
        {{ end }}
    </channel>
</rss>