articles/generate-feed.php

99 lines
2.7 KiB
PHP

#!/usr/bin/env php
<?php
function set_attr($xml, $name, $value) {
xmlwriter_start_attribute($xml, $name);
xmlwriter_text($xml, $value);
xmlwriter_end_attribute($xml);
}
function add_elem($xml, $tag, $content, $attributes = []) {
xmlwriter_start_element($xml, $tag);
foreach ($attributes as $name=>$value) {
set_attr($xml, $name, $value);
}
if ($content) xmlwriter_text($xml, $content);
xmlwriter_end_element($xml);
}
$xml = xmlwriter_open_uri(__DIR__ . "/static/atom.xml");
xmlwriter_set_indent($xml, true);
xmlwriter_set_indent_string($xml, "\t");
xmlwriter_start_document($xml, "1.0", "utf-8");
xmlwriter_start_element($xml, "feed");
set_attr($xml, "xmlns", "http://www.w3.org/2005/Atom");
add_elem($xml, "title", "Reimar's articles");
add_elem($xml, "id", "https://reim.ar/");
xmlwriter_start_element($xml, "author");
add_elem($xml, "name", "Reimar");
add_elem($xml, "email", "mail@reim.ar");
add_elem($xml, "uri", "https://reim.ar");
xmlwriter_end_element($xml);
add_elem($xml, "link", null, ["href" => "https://articles.reim.ar"]);
add_elem($xml, "link", null, ["rel" => "self", "href" => "https://articles.reim.ar/atom.xml"]);
$updated = 0;
$entries = [];
foreach (scandir(__DIR__ . "/static") as $file) {
$path = __DIR__ . "/static/" . $file;
if (is_dir($path) || !str_ends_with($path, ".html")) continue;
$doc = new DOMDocument();
$doc->load($path);
$xpath = new DOMXPath($doc);
$author = $xpath->query("//meta[@name='author']");
if (!count($author)) continue;
$published = $xpath->query("//*[@class='published']/time")[0]->attributes["datetime"]->value;
$published = date("c", strtotime($published));
$updated = max($updated, strtotime($published));
$title = $doc->getElementsByTagName("title")[0]->textContent;
$doc->getElementsByTagName("header")[0]->remove();
$doc->getElementsByTagName("footer")[0]->remove();
$content = $doc->saveXML($doc->getElementsByTagName("body")[0]);
$content = str_replace(["<body>", "</body>"], "", $content);
$entries[] = [
"file" => $file,
"title" => $title,
"published" => $published,
"content" => $content,
];
}
add_elem($xml, "updated", date("c", $updated));
foreach ($entries as $entry) {
xmlwriter_start_element($xml, "entry");
add_elem($xml, "id", "https://articles.reim.ar/" . $entry["file"]);
add_elem($xml, "link", null, [
"rel" => "alternate",
"href" => "https://articles.reim.ar/" . $entry["file"],
"type" => "text/html",
]);
add_elem($xml, "title", $entry["title"]);
add_elem($xml, "published", $entry["published"]);
add_elem($xml, "updated", $entry["published"]);
add_elem($xml, "content", $entry["content"], ["type" => "html"]);
xmlwriter_end_element($xml);
}
xmlwriter_end_document($xml);
xmlwriter_flush($xml);