{{- $author := partial "function-authors-data.html" . -}}
{{- $feeds := slice -}}
{{- if $author.feeds.rss -}}
{{- range $key, $value := (seq 3) -}}
{{- range $author.feeds.rss -}}
{{- $data := split . " " -}}
{{- $url := delimit (first 1 $data) "" -}}
{{- $retitle := trim (delimit (after 1 $data) " ") " " -}}
{{- with $fetch := cond (default true $.Site.Params.site.offline) false (resources.GetRemote $url) -}}
{{- with $error := .Err -}}
{{- warnf "Feed fetch %s" $error -}}
{{- else -}}
{{- with $fetch | transform.Unmarshal -}}
{{- $content := "" -}}
{{- $date := now | time.Format "2006-01-02T15:04:05Z" -}}
{{- $description := "" -}}
{{- $enclosure := "" -}}
{{- $imageLink := "" -}}
{{- $link := "https://example.com/link/" -}}
{{- $sourceDescription := "" -}}
{{- $sourceHome := "" -}}
{{- $sourceLink := $url -}}
{{- $sourceTitle := "No source title found" -}}
{{- $title := "No Title Found" -}}
{{- with .channel -}}
{{- $sourceTitle = .title -}}
{{- $sourceDescription = .description -}}
{{- if reflect.IsSlice .link -}}
{{- if reflect.IsMap (index .link 0) -}}
{{- $sourceHome = index (last 1 .link) 0 -}}
{{- else -}}
{{- $sourceHome = index .link 0 -}}
{{- end -}}
{{- end -}}
{{- with .image -}}
{{- $imageLink = .url -}}
{{- end -}}
{{- with .item -}}
{{- if reflect.IsMap . -}}
{{- $title = index . "title" -}}
{{- $link = index . "link" -}}
{{- $date = index . "pubDate" -}}
{{- else -}}
{{- range first $value . -}}
{{- $description = .description -}}
{{- $content = .description -}}
{{- $title = .title -}}
{{- $link = or .link (index .guid "#text") -}}
{{- $date = .pubDate -}}
{{- with .enclosure -}}
{{- $enclosure = index . "-url" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- if not $date -}}
{{- $date = .lastBuildDate -}}
{{- end -}}
{{- else -}}
{{- with .author -}}
{{- if reflect.IsMap . -}}
{{- $sourceHome = .uri -}}
{{- end -}}
{{- end -}}
{{- with .subtitle -}}
{{- if reflect.IsMap . -}}
{{- $sourceDescription = index . "#text" -}}
{{- else -}}
{{- $sourceDescription = . -}}
{{- end -}}
{{- end -}}
{{- with .title -}}
{{- if reflect.IsMap . -}}
{{- $sourceTitle = index . "#text" -}}
{{- else -}}
{{- $sourceTitle = . -}}
{{- end -}}
{{- end -}}
{{- with .entry -}}
{{- range first $value . -}}
{{- $date = .updated -}}
{{- with .link -}}
{{- if reflect.IsMap . -}}
{{- $link = index . "-href" -}}
{{- else -}}
{{- if reflect.IsMap (index . 0) -}}
{{- $link = index (index . 0) "-href" -}}
{{- else -}}
{{- $link = . -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- with .title -}}
{{- if reflect.IsMap . -}}
{{- $title = index . "#text" -}}
{{- else -}}
{{- $title = . -}}
{{- end -}}
{{- end -}}
{{- with .content -}}
{{- $content = index . "#text" -}}
{{- end -}}
{{- with .summary -}}
{{- $description = index . "#text" -}}
{{- end -}}
{{- with .group -}}
{{- $description = .description -}}
{{- $content = index .content "#text" -}}
{{- $enclosure = index .thumbnail "-url" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $sourceDomain := ($sourceLink | plainify | htmlUnescape | urls.Parse).Host -}}
{{- /* Replace invalid time zones. */ -}}
{{- $date = replaceRE "G[0-9]+T" "UTC" $date -}}
{{- /* If missing seconds split and try again. */ -}}
{{- if lt (len (split $date ":")) 3 -}}
{{- $date = delimit (first 3 (after 1 (split $date " "))) " " -}}
{{- end -}}
{{- /* Try to complete enclosure relative links. */ -}}
{{- if and (not ($enclosure | urls.Parse).Host) (not ($link | urls.Parse).Host) -}}
{{- $enclosure = print "http://" $sourceDomain $link -}}
{{- end -}}
{{- /* Try to complete link relative links. */ -}}
{{- if not ($link | urls.Parse).Host -}}
{{- $link = print "http://" $sourceDomain $link -}}
{{- if not ($link | urls.Parse).Host -}}
{{- $link = print "http://" $sourceDomain -}}
{{- end -}}
{{- end -}}
{{-
$feeds = $feeds | append (dict
"FeedContent" ($content | plainify | htmlUnescape)
"FeedContentShort" (delimit (first 2 (split ($content | plainify | htmlUnescape) "\n")) " " | truncate 250 "...")
"FeedDate" ($date | plainify | htmlUnescape | time.Format "Mon Jan 2 2006")
"FeedDateTime" ($date | plainify | htmlUnescape | time.Format "2006-01-02T15:04:05Z")
"FeedDateTitle" ($date | plainify | htmlUnescape | time.Format "Monday January 2 2006 at 15:04:05 MST")
"FeedDescription" ($description | plainify | htmlUnescape)
"FeedDescriptionShort" (delimit (first 2 (split ($description | plainify | htmlUnescape) "\n")) " " | truncate 250 "...")
"FeedEnclosure" ($enclosure | plainify | htmlUnescape)
"FeedImageLink" ($imageLink | plainify | htmlUnescape)
"FeedLink" ($link | plainify | htmlUnescape)
"FeedName" (or $retitle (delimit (first 2 (split ($sourceTitle | plainify | htmlUnescape) " ")) " "))
"FeedSourceDescription" ($sourceDescription | plainify | htmlUnescape)
"FeedSourceDomain" ($sourceDomain | plainify | htmlUnescape)
"FeedSourceLink" ($sourceLink | plainify | htmlUnescape)
"FeedSourceHome" ($sourceHome | plainify | htmlUnescape)
"FeedSourceTitle" ($sourceTitle | plainify | htmlUnescape)
"FeedTitle" ($title | plainify | htmlUnescape)
)
-}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- range $feeds -}}
{{- $name := .FeedName -}}
{{- $domain := .FeedSourceDomain -}}
{{- $favicon := partial "function-favicon-domain.html" $domain -}}
{{- with .FeedImageLink -}}
{{- $favicon = . -}}
{{- end -}}
{{- $imageSource := partial "function-fetch-favicons.html"
(dict
"Context" $.Page
"Source" $favicon
"Infix" (.FeedSourceLink | anchorize)
)
-}}
{{- $template := resources.Get "templates/markdown-feed.yaml" -}}
{{- $alias := truncate 255 "" (delimit (first 8 (split (.FeedTitle | urlize) "-")) "-" | humanize | urlize) -}}
{{- $hash := .FeedLink | sha256 | truncate 8 "" -}}
{{- $file := print $alias "-" $hash -}}
{{- $file = partial "function-strip-filechars.html" $file -}}
{{- $path := print "/data/generates/content/" $author.user "/feeds/" $file ".md" -}}
{{- $content := .FeedContentShort -}}
{{- if (gt (len .FeedTitle) (len $content)) -}}
{{- $content = .FeedTitle -}}
{{- end -}}
{{- if (gt (len .FeedDescriptionShort) (len .FeedTitle)) -}}
{{- $content = .FeedDescriptionShort -}}
{{- end -}}
{{- $markdown := resources.ExecuteAsTemplate
$path
(dict
"author" $author.user
"content" (or $content "No content found.")
"date" .FeedDateTime
"description" (or .FeedSourceDescription .FeedSourceTitle "No source description found.")
"domain" .FeedSourceDomain
"enclosure" .FeedEnclosure
"favicon" (strings.TrimPrefix $.Site.BaseURL $imageSource.Permalink)
"home" .FeedSourceHome
"link" .FeedLink
"name" .FeedName
"source" .FeedSourceLink
"tags" "[feeds]"
)
$template
-}}
{{- $writeToFile := $markdown.Permalink -}}
{{- end -}}
{{- end -}}
{{- return $feeds -}}