aboutsummaryrefslogtreecommitdiff
path: root/themes/default/layouts/partials/generate-feeds.html
blob: a6cd906cfecc9ff74e69cc2cc9e833e239f8515b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
{{- $author := partial "function-authors-data.html" . -}}

{{- $feeds := slice -}}

{{- if $author.feeds.rss -}}
  {{- range $key, $value := (seq 3) -}}
    {{- range $author.feeds.rss -}}

      {{- with resources.GetRemote . -}}
        {{- with .Err -}}
          {{- warnf "Feed fetch %s" . -}}
        {{- end -}}
      {{- end -}}

      {{- with resources.GetRemote . | transform.Unmarshal -}}

        {{- $content           := "" -}}
        {{- $date              := now.Format "2006-01-02T15:04:05Z" -}}
        {{- $description       := "No description found." -}}
        {{- $enclosure         := "" -}}
        {{- $imageLink         := "" -}}
        {{- $link              := "https://example.com/link/" -}}
        {{- $sourceDescription := "" -}}
        {{- $sourceHome        := "" -}}
        {{- $sourceLink        := "https://example.com/rss/" -}}
        {{- $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) -}}
              {{- $sourceLink = index (first 1 .link) 0 "-href" -}}
              {{- $sourceHome = index (last 1 .link) 0 -}}
            {{- else -}}
              {{- $sourceLink = index .link 0 -}}
            {{- end -}}
          {{- else -}}
            {{- $sourceLink = .link -}}
          {{- 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        = .link -}}
                {{- $date        = .pubDate -}}
                {{- with .enclosure -}}
                  {{- $enclosure = index . "-url" -}}
                {{- end -}}
              {{- end -}}
            {{- end -}}
          {{- end -}}

          {{- if not $date -}}
            {{- $date = .lastBuildDate -}}
          {{- end -}}

        {{- else -}}

          {{- with .link -}}
            {{- $sourceLink = index (index . 0) "-href" -}}
          {{- 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 | html | 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 -}}
        {{- end -}}

        {{-
          $feeds = $feeds | append (dict
            "FeedContent"           ($content | plainify | html | htmlUnescape)
            "FeedContentShort"      (delimit (first 2 (split ($content | plainify | html | htmlUnescape) "\n")) " " | truncate 250)
            "FeedDate"              ($date | plainify | html | htmlUnescape | time.Format "January 2, 2006")
            "FeedDateTime"          ($date | plainify | html | htmlUnescape | time.Format "2006-01-02T15:04:05Z")
            "FeedDateTitle"         ($date | plainify | html | htmlUnescape | time.Format "Monday, January 2 2006 at 15:04:05 MST")
            "FeedDescription"       ($description | plainify | html | htmlUnescape)
            "FeedDescriptionShort"  (delimit (first 2 (split ($description | plainify | html | htmlUnescape) "\n")) " " | truncate 250)
            "FeedEnclosure"         ($enclosure | plainify | html | htmlUnescape)
            "FeedImageLink"         ($imageLink | plainify | html | htmlUnescape)
            "FeedLink"              ($link | plainify | html | htmlUnescape)
            "FeedName"              (delimit ((delimit (first 2 (split ($sourceTitle | plainify | html | htmlUnescape) " ")) " ") | findRE "^([^.|?|!|\n|\"|]+)") " " | replaceRE "[^a-zA-Z ]" "")
            "FeedSourceDescription" ($sourceDescription | plainify | html | htmlUnescape)
            "FeedSourceDomain"      ($sourceDomain | plainify | html | htmlUnescape)
            "FeedSourceLink"        ($sourceLink | plainify | html | htmlUnescape)
            "FeedSourceHome"        ($sourceHome | plainify | html | htmlUnescape)
            "FeedSourceTitle"       ($sourceTitle | plainify | html | htmlUnescape)
            "FeedTitle"             ($title | plainify | html | htmlUnescape)
          )
        -}}

      {{- end -}}
    {{- end -}}
  {{- end -}}

  {{- range $feeds -}}

    {{- $name := .FeedName -}}
    {{- $domain := .FeedSourceDomain -}}

    {{- $faviconDomain :=
      (print
        "https://t1.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url="
        "http://"
        .FeedSourceDomain
        "&size=64"
      )
    -}}

    {{- with .FeedImageLink -}}
      {{- $faviconDomain = . -}}
      {{- $domain = $name | humanize | urlize -}}
    {{- end -}}

    {{- $imageSource := partial "function-caches-images.html"
      (dict
        "Source" $faviconDomain
        "Fit"    "160x160"
        "Target" (print (partial "function-paths.html" "media") "/")
        "Copy"   (print (partial "function-paths.html" "media") "/favicon." $domain ".png")
      )
    -}}

    {{- $template := resources.Get "templates/markdown-feed.yaml" -}}
    {{- $file := print (delimit (first 8 (split (.FeedTitle | urlize) "-")) "-" | humanize | urlize) "-" (.FeedLink | sha256 | truncate 8 "") -}}
    {{- $file = partial "function-strip-urlchars.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"     $imageSource.RelPermalink
        "home"        .FeedSourceHome
        "link"        .FeedLink
        "markdown"    (strings.TrimLeft "/" $path)
        "name"        .FeedName
        "self"        (print $author.user "/feeds/" $file "/")
        "tags"        "[rss]"
       )
       $template
    -}}
    {{- $writeToFile := $markdown.Permalink -}}
  {{ end }}

    <web-ring>
      <h1>Web Ring</h1>
        <aside>
          {{ range first 3 (uniq (sort $feeds "FeedDateTime" "desc")) }}
            <web-ring-item>
              <header>
                <a
                  title="{{ .FeedLink }}"
                  href="{{ .FeedLink }}">
                  {{ .FeedTitle }}
                </a>
              </header>
              <time
                title="{{ .FeedDateTitle }}"
                datetime="{{ .FeedDateTime }}">
                {{ .FeedDate }}
              </time>
              <p>
                {{ "--" | markdownify }}
                {{ if gt (len .FeedDescriptionShort) (len .FeedContentShort) -}}
                  {{ or .FeedDescriptionShort .FeedTitle }}
                {{- else -}}
                  {{ or .FeedContentShort .FeedTitle }}
                {{- end }}
                &mdash;
              </p>
              <a
                title="{{ .FeedSourceDescription }}"
                href="{{ or .FeedSourceHome .FeedSourceLink }}">
                {{ .FeedSourceTitle }}
              </a>
            </web-ring-item>
          {{ end }}
      </aside>
    </web-ring>
{{- end -}}