git.m455.casa

m455.casa

clone url: git://git.m455.casa/m455.casa


html/archive/2020/adding-date-support-to-my-awful-rss-feed-generator.html

1 <!DOCTYPE html>
2 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
3 <head>
4 <meta charset="utf-8" />
5 <meta name="generator" content="pandoc" />
6 <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
7 <title>Adding date support to my awful RSS feed generator</title>
8 <style>
9 code{white-space: pre-wrap;}
10 span.smallcaps{font-variant: small-caps;}
11 span.underline{text-decoration: underline;}
12 div.column{display: inline-block; vertical-align: top; width: 50%;}
13 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
14 ul.task-list{list-style: none;}
15 </style>
16 <style>
17 body {
18 line-height: 1.5;
19 font-family: sans-serif;
20 font-size: 18px;
21 margin: 20px auto;
22 max-width: 630px;
23 }
24
25 a {
26 color: blue;
27 }
28
29 code, pre {
30 background-color: #fddee3;
31 font-size: 14px;
32 }
33
34 pre {
35 padding: 25px 25px;
36 overflow: auto;
37 }
38
39 pre code {
40 white-space: pre;
41 }
42
43 img {
44 max-width: 100%;
45 }
46
47 table {
48 border-collapse: collapse;
49 }
50
51 table caption {
52 font-weight: bold;
53 margin: 10px 0px;
54 text-align: left;
55 }
56
57 th, td {
58 border: 1px solid #000;
59 padding: 4px;
60 }
61
62 blockquote {
63 border-left: 3px solid #000;
64 padding-left: 10px;
65 }
66
67 .border {
68 border: 1px solid #000;
69 margin: 25px 0px;
70 padding: 5px 25px;
71 }
72
73 @media only screen and (max-width: 700px) {
74 body {
75 margin: 10px;
76 }
77 }
78
79 @media (prefers-color-scheme: dark) {
80 body {
81 background-color: #111;
82 color: #eee;
83 }
84 a {
85 color: #009fff;
86 }
87 code, pre {
88 background-color: #111;
89 color: #fd6363;
90 }
91 pre {
92 padding: 15px 25px;
93 }
94 blockquote {
95 border-left: 3px solid #666;
96 }
97 .border, th, td {
98 border: 1px solid #666;
99 }
100 }
101 </style>
102 </head>
103 <body>
104 <main>
105 <h2 id="adding-date-support-to-my-awful-rss-feed-generator">Adding date support to my awful RSS feed generator</h2>
106 <p>2020-10-27 00:00</p>
107 <p>So, someone asked “when are you going to add dates to your RSS feed?”, and I replied “I’m not actually sure!”. I wasn’t sure, because my original goal was to create a bare-minimum, valid RSS feed, which <a href="https://www.rssboard.org/rss-specification#requiredChannelElements">doesn’t actually require dates</a>.</p>
108 <p>This went well and all, but it kind of messes with people’s RSS feed readers because it doesn’t know how to order them, so it adds dates in for them, which are all just the same date.</p>
109 <p>I created this generator so friends could keep up with my blog posts, so it kind of made sense to me that they should be able to view them in chronological order in their RSS feed reader haha.</p>
110 <p>Tonight I set out to do that, and, since my RSS-feed generator was already a mess, I had a lot of fun with this, because I didn’t need to worry about ruining nicely organized code.</p>
111 <p>I wasn’t sure how I was going to do it, because I was basically generating links and titles in the RSS feed based off the <code>posts.md</code> file, which consisted of a bunch of <code>* [I'm a title](im-a-link.md)</code>s, and just parsed those into RSS-style XML for the <code>&lt;title&gt;</code>, <code>&lt;link&gt;</code>, and <code>&lt;guid&gt;</code>. The problem was that there was no date for me to pull from there!</p>
112 <p>I realized I was keeping dates in all of my posts, and that I could just take the link my crappy parser found, remove the initial <code>/</code> from it, and it could open the file, split it into lines, grab the third line, which was the date, and plop it into the RSS!</p>
113 <p>It took a few tries, some referencing of <a href="https://%7B%7Bgit-domain%7D%7D/ruth">an IRC bot I made</a> to get the output of a shell command from racket, but in the end, I managed to do it!</p>
114 <p>I ended up using an awful nested list of functions, because, why not? The code is already awful, why not have more fun and keep it that way?</p>
115 <p>The result? This super awful function, and I love it!:</p>
116 <pre><code>(define (listof-rss-items-&gt;rss-items-string listof-rss-items)
117 (format &quot;~a&quot; (string-join listof-rss-items)))
118
119 (define (create-item l)
120 (let* ([post-title (car l)]
121 [link-fragment (car (cdr l))] ;; /posts/some-name.html
122 [date (car
123 (cdr
124 (cdr
125 (file-&gt;lines
126 (string-replace (string-replace link-fragment &quot;.html&quot; &quot;.md&quot;)
127 &quot;/posts/&quot;
128 &quot;posts/&quot;)))))] ;; creates &quot;yyyy-mm-dd&quot;
129 [date-converted (string-trim
130 (with-output-to-string
131 (lambda () (system (format &quot;date -Rd&#39;~a&#39;&quot; date)))))] ;; creates &quot;Day, Mon 31 00:00:00 EDT 2020&quot;
132 [link-full (string-append homepage-url link-fragment)])
133 (format
134 #&lt;&lt;string-block
135
136 &lt;item&gt;
137 &lt;title&gt;~a&lt;/title&gt;
138 &lt;link&gt;~a&lt;/link&gt;
139 &lt;guid&gt;~a&lt;/guid&gt;
140 &lt;pubDate&gt;~a&lt;/pubDate&gt;
141 &lt;/item&gt;
142
143 string-block
144 post-title
145 link-full
146 link-full
147 date-converted)))</code></pre>
148 <p>P.S. If you are following my RSS feed, to get it to regenerate with dates, you might need to delete my feed and reload it. I know I had to do this with Thunderbird while I was testing my feed.</p>
149 </main>
150 </body>
151 </html>