clone url: git://


1 <!DOCTYPE html>
2 <html xmlns="" 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>All about 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 <link rel="stylesheet" href="/assets/archive.css">
17 </head>
18 <body>
19 <main>
20 <h2 id="all-about-my-awful-rss-feed-generator">All about my awful RSS feed generator</h2>
21 <p>2020-08-11 00:00</p>
22 <p>So, I joined the RSS bandwagon not too long ago. Right now, I’m just using Thunderbird’s built-in RSS feed manager to follow friends. I used to, and still do for some friends who don’t have an RSS feed, keep a text file called <code>~/Documents/links/friends-websites.txt</code>. This file contains line-separated links to friends’ homepages, which I would check most mornings while waking up with a cup of coffee.</p>
23 <p>Something about following friends’ personal homepages is way more appealing than feed-based social media sometimes. It’s relaxing, doesn’t require attention, it doesn’t have notifications, and I don’t even need an account to follow people. How great is that? I guess it’s like having a newspaper full of my friends’ beautiful discourse.</p>
24 <p>… Okay, RSS feeds are still feeds hahaha.</p>
25 <p>So, naturally, I wanted friends to have the same convenient access to my homepage activity as I did to theirs, so I began to research how RSS worked. I didn’t really know at all to be honest, and the XML examples on Wikipedia confused the hell out of me.</p>
26 <p>After staring at the examples for a while, I kind of got the gist of what they were and their structure.</p>
27 <p><a href="">The example on Wikipedia</a> indicated that I didn’t need much. In its example, it had a title, a description, a link, a build date, a publishing date, and a “ttl”–whatever the hell that is.</p>
28 <p>These elements seemed like they should exist at top of my RSS feed, inside a <code>&lt;channel&gt;</code> element, and each of them should only occur once.</p>
29 <p>Below the <code>&lt;channel&gt;</code> element, there were a few <code>&lt;item&gt;</code> elements, and inside each <code>&lt;item&gt;</code> element there was a <code>&lt;title&gt;</code>, <code>&lt;description&gt;</code>, <code>&lt;link&gt;</code>, <code>&lt;guid&gt;</code>, and a <code>&lt;pubDate&gt;</code> element.</p>
30 <p>At this point I was starting to understand more about how RSS worked.</p>
31 <p>After reading the Wikipedia page on RSS, I went to check <a href="">the official RSS standard</a> to see what it had to say. To my luck, it had listed “required channel elements”, which said you only need the following elements in the <code>&lt;channel&gt;</code> element:</p>
32 <ul>
33 <li><code>&lt;title&gt;</code>, which contains the name of the RSS feed</li>
34 <li><code>&lt;link&gt;</code>, which contains a link to your website, not the RSS feed file</li>
35 <li><code>&lt;description&gt;</code>, which contains a phrase describing your feed</li>
36 </ul>
37 <p>With this information I made a few definitions in Racket that would later populate the <code>&lt;title&gt;</code>, <code>&lt;link&gt;</code>, and <code>&lt;description&gt;</code> elements:</p>
38 <pre><code>#lang racket/base
40 (require racket/file
41 racket/string)
43 (define title &quot;m455&#39;s blog&quot;)
44 (define homepage-url &quot;;)
45 (define description &quot;A blog about programming, documentation, and anything that interests me.&quot;)</code></pre>
46 <p>Next, I had to figure out which element were required inside of the <code>&lt;item&gt;</code> element. There was no section that was called “Required …”, but I did manage to find this phrase from the <a href="">Elements of &lt;item&gt; section</a>:</p>
47 <blockquote>
48 <p>All elements of an item are optional, however at least one of title or description must be present.</p>
49 </blockquote>
50 <p>According to this, I only needed a <code>&lt;title&gt;</code> <strong>or</strong> a <code>&lt;description&gt;</code> element.</p>
51 <p>This was fine, because I could use the <code>&lt;title&gt;</code> element as the title of any given blog post.</p>
52 <p>Conveniently, the specification also mentioned that you could have a <code>&lt;link&gt;</code> element inside of the <code>&lt;item&gt;</code> element. This was great, because this would mean I could include a link for each blog post, so users could click a link in an RSS feed, instead of referencing the title text and searching for it manually.</p>
53 <p>After reading that, I had decided to make a test file based on the requirements I gathered:</p>
54 <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
55 &lt;rss version=&quot;2.0&quot;&gt;
57 &lt;channel&gt;
58 &lt;title&gt;m455&#39;s blog&lt;/title&gt;
59 &lt;link&gt;;/link&gt;
60 &lt;description&gt;A blog about programming, documentation, and anything that interests me.&lt;/description&gt;
62 &lt;item&gt;
63 &lt;title&gt;This is test1&lt;/title&gt;
64 &lt;link&gt;;/link&gt;
65 &lt;/item&gt;
67 &lt;item&gt;
68 &lt;title&gt;This is test2&lt;/title&gt;
69 &lt;link&gt;;/link&gt;
70 &lt;/item&gt;
72 &lt;/channel&gt;
73 &lt;/rss&gt;</code></pre>
74 <p>I took this over to the <a href="">w3schools RSS validator</a> and decided to test it.</p>
75 <p>Unsurprisingly, the validator returned errors:</p>
76 <ul>
77 <li>“item should contain a guid element”</li>
78 <li>“Missing atom:link with rel=”self"</li>
79 </ul>
80 <p>I clicked the help link beside the guid-related error, and the help documentation said that all I needed to do was add in a <code>&lt;guid&gt;</code> element. This is great, but I had no clue what I was supposed to be populating the <code>&lt;guid&gt;</code> element with.</p>
81 <p>After some research, which was basically a bunch of Wikipedia rabbit holes, I found out that the <code>&lt;guid&gt;</code> just needs to be a “unique identifier” for each <code>&lt;item&gt;</code> in a <code>&lt;channel&gt;</code>, so what better unique identifier for an item than the link to the item itself!</p>
82 <p>After modifying my already-modified RSS feed, I threw it at the RSS validator again:</p>
83 <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
84 &lt;rss version=&quot;2.0&quot;&gt;
86 &lt;channel&gt;
87 &lt;title&gt;m455&#39;s blog&lt;/title&gt;
88 &lt;link&gt;;/link&gt;
89 &lt;description&gt;A blog about programming, documentation, and anything that interests me.&lt;/description&gt;
91 &lt;item&gt;
92 &lt;title&gt;This is test1&lt;/title&gt;
93 &lt;link&gt;;/link&gt;
94 &lt;guid&gt;;/guid&gt;
95 &lt;/item&gt;
97 &lt;item&gt;
98 &lt;title&gt;This is test2&lt;/title&gt;
99 &lt;link&gt;;/link&gt;
100 &lt;guid&gt;;/guid&gt;
101 &lt;/item&gt;
103 &lt;/channel&gt;
104 &lt;/rss&gt;</code></pre>
105 <p>… and that seemed to get rid of the guid-related error! Did I do it right? Who knows!</p>
106 <p>Next up was that mysterious “Missing atom:link with rel=”self"" error.</p>
107 <p>I clicked the help link beside the error, and it gave me the following solution:</p>
108 <blockquote>
109 <p>If you haven’t already done so, declare the Atom namespace at the top of your feed, thus: <code>&lt;rss version="2.0" xmlns:atom=""&gt;</code></p>
110 <p>Then insert a atom:link to your feed in the channel section. Below is an example to get you started. Be sure to replace the value of the href attribute with the URL of your feed. <code>&lt;atom:link href="" rel="self" type="application/rss+xml" /&gt;</code></p>
111 </blockquote>
112 <p>The first suggestion just required you to add the <code>xmlns:atom=...</code> at the top of the RSS feed, but the second suggestion took a bit of fiddling around to figure out.</p>
113 <p>It turns out all I needed to do was provide a link to the RSS feed itself.</p>
114 <p>So, I, yet again, modified my RSS feed to test against the RSS validator:</p>
115 <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
116 &lt;rss version=&quot;2.0&quot; xmlns:atom=&quot;;&gt;
118 &lt;channel&gt;
119 &lt;title&gt;m455&#39;s blog&lt;/title&gt;
120 &lt;link&gt;;/link&gt;
121 &lt;description&gt;A blog about programming, documentation, and anything that interests me.&lt;/description&gt;
122 &lt;atom:link href=&quot;; rel=&quot;self&quot; type=&quot;application/rss+xml&quot; /&gt;
124 &lt;item&gt;
125 &lt;title&gt;This is test1&lt;/title&gt;
126 &lt;link&gt;;/link&gt;
127 &lt;guid&gt;;/guid&gt;
128 &lt;/item&gt;
130 &lt;item&gt;
131 &lt;title&gt;This is test2&lt;/title&gt;
132 &lt;link&gt;;/link&gt;
133 &lt;guid&gt;;/guid&gt;
134 &lt;/item&gt;
136 &lt;/channel&gt;
137 &lt;/rss&gt;</code></pre>
138 <p>… which turned out to be valid!</p>
139 <p>There was an error saying “Self reference doesn’t match document location”, but that was because it was trying to follow the link to the RSS feed when the RSS didn’t exist yet. To make sure it did work, I added the test feed to my live website to see if it validated using the URL validator, instead of the direct-input validator, and it did!</p>
140 <p>Now I knew what it took to create a bare-minimum, valid RSS feed.</p>
141 <p>This meant I could finally start the fun part: The programming of the RSS feed generator :D</p>
142 <p>For my definitions, I came up with</p>
143 <pre><code>(define title &quot;m455&#39;s blog&quot;)
144 (define homepage-url &quot;;)
145 (define description &quot;A blog about programming, documentation, and anything that interests me.&quot;)
146 (define reference-file &quot;pages/;)
147 (define feed-file &quot;feed.rss&quot;)
148 (define feed-file-output (string-append &quot;output/&quot; feed-file))
149 (define feed-file-url (string-append homepage-url &quot;/&quot; feed-file))</code></pre>
150 <p>The <code>feed-file-url</code> creates a <code></code> link, while the <code>feed-file-output</code> creates the location of where my <code>feed-file</code> is to be generated: <code>output/</code></p>
151 <p>The reason I needed the location of my <code>feed-file</code> is because the RSS-generator script exists in the same directory as the <code>output/</code> directory, along with <code>posts/</code>, <code>pages/</code>, <code>images/</code>, etc.</p>
152 <p>There is one special Markdown file I need to parse/transform, which is the <code></code> Markdown file, which exists inside of the <code>pages/</code> directory. This file contains a bulleted list of titles and links to all of my blog posts, which would soon be converted into an RSS feed.</p>
153 <p>I chose this file because it has the two pieces of information I need for each <code>&lt;item&gt;</code> element in my RSS feed:</p>
154 <ul>
155 <li>The title of the blog post</li>
156 <li>The link to the blog post</li>
157 <li>and because the <code>&lt;guid&gt;</code> element will be populated with the same information as the <code>&lt;link&gt;</code> element, I didn’t have to worry about finding data to populate the <code>&lt;guid&gt;</code> element with</li>
158 </ul>
159 <p>You can see what the whole <code></code> file looks like below:</p>
160 <pre><code># Posts
162 * [Thoughts on technical writing and accidentally gatekeeping communities](/posts/thoughts-on-technical-writing-and-accidentally-gatekeeping-communities.html)
163 * [Having fun with Lisp(s)](/posts/having-fun-with-lisps.html)
164 * [Public Unix server etiquette](/posts/public-unix-server-etiquette.html)
165 * [What I like about the Scheme community](/posts/what-i-like-about-the-scheme-community.html)
166 * [What are social Unix servers?](/posts/what-are-social-unix-servers.html)
167 * [Redirecting your GitHub Pages website to a Dat url](/posts/redirecting-your-github-pages-website-to-a-dat-url.html)
168 * [Setting up graphical applications in Windows Subsystem for Linux](/posts/setting-up-graphical-applications-in-windows-subsystem-for-linux.html)
169 * [Setting up a Chinese input method on GNU/Linux](/posts/setting-up-a-chinese-input-method-on-gnulinux.html)
170 * [A quick guide to pronouncing Chinese words](/posts/a-quick-guide-to-pronouncing-chinese-words.html)
171 * [Interpreting second language speakers](/posts/interpreting-second-language-speakers.html)
172 * [Learn to read and type Chinese: A primer for the people of the internet](/posts/learn-to-read-and-type-chinese.html)</code></pre>
173 <p>I decided that all I would need to do to convert this file into an RSS feed is:</p>
174 <ul>
175 <li>Remove the <code># Posts</code> title</li>
176 <li>Remove the <code>*</code> bullet points</li>
177 <li>Extract the title, which exists between the <code>[</code> and <code>]</code>, and store it in a local definition</li>
178 <li>Remove the <code>/posts/</code> bit from links</li>
179 <li>Extract the link, which exists between the <code>(</code> and <code>)</code>, and store it in a local definition</li>
180 <li>Remove the brackets and parentheses around the title and links</li>
181 </ul>
182 <p>The removal of items can be emulated by searching for a string and replacing it with <code>""</code>.</p>
183 <p>The extraction of the link and title information can be done with regex.</p>
184 <p>The rest of my script just needs to create string templates that are formatted, populated, and then stitched together.</p>
185 <p>One thing I really enjoyed about using Racket for this little project was that I could use string blocks, which allowed me to type in string values in a very free-form manner.</p>
186 <p>You can see what I mean by “free-form” below. Basically, everything between <code>#&lt;&lt;string-block</code> and <code>string-block</code> is treated as a string. New lines, tabs, etc. are all rendered as well, so it’s almost the same experience you would get if you were to type text into a plain-text file.</p>
187 <pre><code>(define rss-header
188 (format
189 #&lt;&lt;string-block
190 &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
191 &lt;rss version=&quot;2.0&quot; xmlns:atom=&quot;;&gt;
193 &lt;channel&gt;
194 &lt;title&gt;~a&lt;/title&gt;
195 &lt;link&gt;~a&lt;/link&gt;
196 &lt;description&gt;~a&lt;/description&gt;
197 &lt;atom:link href=&quot;~a&quot; rel=&quot;self&quot; type=&quot;application/rss+xml&quot; /&gt;
199 string-block
200 title
201 homepage-url
202 description
203 feed-file-url
204 ))</code></pre>
205 <p>The <code>~a</code>s are all populated with the <code>title</code>, <code>homepage-url</code>, <code>description</code>, and <code>feed-file-url</code> definitions, just the same as you would populate a string with <code>(format ... title homepage-url description feed-file-url)</code>.</p>
206 <p>Even though my RSS feed’s title, homepage link, and description aren’t directly connected to my website generator’s source files, it’s still fun to have a default RSS template that I can pass around for future websites I create, as long as I follow the same format as <code></code>. Yeah, it’s bad design, but that’s why this RSS-feed generator is beautifully awful haha.</p>
207 </main>
208 </body>
209 </html>