News feeds
I’ve always been an advocate of RSS 🗄️ and Atom feeds 🗄️.
The main advantage of web feeds is that you can handle them the way you like; you can choose the client for consuming them that you prefer, you can choose what to filter, what to ignore … similarly to email clients.
Unfortunately, most browsers stopped supporting them, but it is still possible to read feeds directly in the browser, either with plugins or through dedicated web pages.
It was not uncommon for every website to support either RSS or Atom feeds; even for social platforms, Twitter supported it, Facebook supported it, and most news websites too.
I guess the main reason why most websites do not offer a news feed anymore is that they can hardly collect any data about users. As of today, most of the time you have to register on some mailing list (thus giving your email address or other contact information), use dedicated programs (often closed-source applications for the phone), or log in continuously on their website.
All reasons why I like news feed so much, and because I like to be coherent, the main reason why I provide both a RSS and Atom feed
.
I generally do not like the idea of introducing another format for web feeds.
It looks like JSON Feed does not bring anything new to the table. It does not make something possible that it was not Atom and RSS, thus it increases friction between clients (which feed formats does a client support or not) and fragmentation in an already small community.
For the same reason, I do not like structured data 🗄️, Twitter Cards, The Open Graph protocol 🗄️, RDFa, Microdata 🗄️, and whatever other platforms want you to implement for getting more relevant.
Most, if not all, information is already there in the HTML file. And it can already be parsed systematically by machines.
There is little reason to create a new standard, containing the same information, to embed in the same document a second time.
Granted, some information might be missing or might not be that easy to parse, but when those formats are proposed, they are always presented like an all-or-nothing alternative.
And which variant should you implement?
Rant aside, and back to News feeds; I’m not particularly happy to find out there is a JSON-based feed, but I like feeds, so I decided to support it too.
Because of how I create my notes, creating the Atom and RSS feed is extremely simple:
<?php
require 'php/parse.php';
$rest_index = null;
$opts = getopt('',['rootdir:'],$rest_index);
$documents = array_slice($argv, $rest_index);
$array = fek\parse($documents);
usort($array, 'fek\cmp_meta_by_date');
echo '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>'
?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom">
<title>Fekir's Blog</title>
<link href="https://fekir.info/" />
<link href="https://fekir.info/feed.atom" rel="self" />
<id>https://fekir.info/feed.atom</id>
<author><name>Federico Kircheis</name></author>
<updated><?php echo date_format($array[0]->revdate, DateTime::ATOM) ?></updated>
<rights>This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.</rights>
<?php foreach ($array as $meta) : ?>
<entry>
<title><?php echo $meta->title ?></title>
<link href="https://fekir.info<?php echo $meta->dir->str() ?>"/>
<id>https://fekir.info<?php echo $meta->dir->str() ?></id>
<published><?php echo date_format($meta->revdate, DateTime::ATOM) ?></published>
<updated><?php echo date_format($meta->revupdate, DateTime::ATOM);?></updated>
<content><?php echo $meta->description ?></content>
</entry>
<?php endforeach; ?>
</feed> That’s pretty much it, slightly less than 30 lines of code. The "productive code" is slightly longer, with some additional options and error handling, but it is structurally the same.
It’s an XML document, with some embedded PHP for filling it. The RSS feed is done exactly the same way.
Creating the JSON feed is not really that much different:
<?php
require 'php/parse.php';
$rest_index = null;
$opts = getopt('',['rootdir:'],$rest_index);
$documents = array_slice($argv, $rest_index);
$array = fek\parse($documents);
usort($array, 'fek\cmp_meta_by_date');
$numItems = count($array);
$i = 0;
?>
{
"version": "https://jsonfeed.org/version/1.1",
"title": "Fekir's Blog",
"home_page_url": "https://fekir.info/",
"feed_url": "https://fekir.info/feed/feed.json",
"description": "Recent content on Fekir's Blog",
"language": "en-US",
"items": [
<?php foreach ($array as $meta) : ?>
{
"id": "https://fekir.info<?php echo $meta->dir->str() ?>",
"content_text": "<?php echo $meta->description ?>",
"url": "https://fekir.info<?php echo $meta->dir->str() ?>"
}
<?php if(++$i === $numItems) : ?>
<?php else : ?>,
<?php endif; ?>
<?php endforeach; ?>
]
} And since now, there it is , although I am not sure why it should be preferable over the others.
Granted, the heavy-lifting is done by the function fek\parse, but I think that those little PHP programs make my point: there is little reason to have three different formats for the same thing.
For "building" the feeds, I have a snippet similar to the following one in my makefile:
PHP := php --php-ini php.ini --no-header -f
MINIFY := minify
XMLTIDY := tidy -errors -quiet -xml
JSONLINT := jsonlint-php
$(OUT_DIR)/feed.rss: php/feed/rss.php $(ARTICLES) makefile | $(OUT_DIR)
$(PHP) $< -- --rootdir $(ROOT_DIR) -- $(ARTICLES) > $(TMP_DIR)/feed.rss
$(MINIFY) --type xml $(TMP_DIR)/feed.rss -o $@
$(XMLTIDY) $@
$(OUT_DIR)/feed.atom: php/feed/atom.php $(ARTICLES) makefile | $(OUT_DIR)
$(PHP) $< -- --rootdir $(ROOT_DIR) -- $(ARTICLES) > $(TMP_DIR)/feed.atom.xml
$(MINIFY) --type xml $(TMP_DIR)/feed.atom.xml -o $@
$(XMLTIDY) $@
$(OUT_DIR)/feed.json: php/feed/feed.json.php $(ARTICLES) makefile | $(OUT_DIR)
$(PHP) $< -- --rootdir $(ROOT_DIR) -- $(ARTICLES) > $(TMP_DIR)/feed.json
$(JSONLINT) $(TMP_DIR)/feed.json
$(MINIFY) $(TMP_DIR)/feed.json -o $@
If you have questions, comments, or found typos, the notes are not clear, or there are some errors; then just contact me.