Generate Atom feeds for each directory and langage
This commit is contained in:
parent
2d23ff50f2
commit
af0d406144
2 changed files with 70 additions and 60 deletions
|
@ -21,7 +21,7 @@ If `site_path` is not set, it will default to current directory.
|
|||
|
||||
Source pages must end in `.md` and can use Markdown, HTML and PHP.
|
||||
|
||||
The following files have special meaning
|
||||
The following files have special meaning:
|
||||
|
||||
`/config.ini`
|
||||
: some default settings can be changed by this file
|
||||
|
@ -38,7 +38,7 @@ The following files have special meaning
|
|||
`/end.inc.html`
|
||||
: added just before `</body>`
|
||||
|
||||
Files starting with a dot or not ending in `.gmi`, `.md` or `.html` are ignored.
|
||||
Files starting with a dot (except for `.htaccess`) are ignored.
|
||||
|
||||
Files containing `draft` in their name (separated from other characters by `.`) are ignored.
|
||||
|
||||
|
|
126
mkht.php
126
mkht.php
|
@ -2,7 +2,7 @@
|
|||
<?php
|
||||
if (php_sapi_name() !== 'cli')
|
||||
exit('Must be run from CLI.' . PHP_EOL);
|
||||
|
||||
umask(0022);
|
||||
const LF = "\n";
|
||||
|
||||
define('ROOT', dirname($_SERVER['SCRIPT_FILENAME']));
|
||||
|
@ -49,7 +49,8 @@ function clearnetOrOnion($clearnet_url, $onion_url) {
|
|||
return (DESTINATION === 'onion') ? $onion_url : $clearnet_url;
|
||||
}
|
||||
|
||||
$feed = '';
|
||||
$dirs_last_update = [];
|
||||
$dirs_entries = [];
|
||||
|
||||
$nodes = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(SITE, RecursiveDirectoryIterator::SKIP_DOTS));
|
||||
|
||||
|
@ -59,30 +60,32 @@ foreach($nodes as $node) {
|
|||
if (str_starts_with($src, SITE . '/target'))
|
||||
continue;
|
||||
$target = str_replace(SITE, SITE . '/target', $src);
|
||||
$path_parts = pathinfo($target);
|
||||
if (strstr($src, '/.') !== false) // Skip hidden nodes
|
||||
$path_parts_src = pathinfo($src);
|
||||
$path_parts_target = pathinfo($target);
|
||||
if (strstr($src, '/.') !== false AND !str_ends_with($src, '/.htaccess')) // Skip hidden nodes (except for .htaccess)
|
||||
continue;
|
||||
if ($node_info->getType() !== 'file')
|
||||
continue;
|
||||
if (!file_exists($path_parts['dirname'])) // Create parent directory if needed
|
||||
mkdir($path_parts['dirname'], 0755, true);
|
||||
if (in_array('draft', explode('.', $path_parts_target['basename']), true))
|
||||
continue;
|
||||
if (!file_exists($path_parts_target['dirname'])) // Create parent directory if needed
|
||||
mkdir($path_parts_target['dirname'], 0755, true);
|
||||
copy($src, $target);
|
||||
if ($node_info->getExtension() !== 'md')
|
||||
continue;
|
||||
|
||||
$files_dates[$src] = $node_info->getMTime();
|
||||
|
||||
if (in_array('draft', explode('.', $path_parts['basename']), true))
|
||||
continue;
|
||||
|
||||
$base_filepath = $path_parts['dirname'] . '/' . $path_parts['filename'];
|
||||
$base_filepath_src = $path_parts_src['dirname'] . '/' . $path_parts_src['filename'];
|
||||
$base_filepath_target = $path_parts_target['dirname'] . '/' . $path_parts_target['filename'];
|
||||
|
||||
$content = file_get_contents($src);
|
||||
|
||||
preg_match('/^# (?<title>.*)$/Dm', $content, $matches);
|
||||
$title = $matches['title'] ?? NULL;
|
||||
|
||||
if (!file_exists($base_filepath . '.html') OR (filemtime($src) > filemtime($base_filepath . '.html')) OR $opt['force']) {
|
||||
preg_match('#\.(?<lang>[a-z]{2})\.md#', $path_parts_src['basename'], $matches);
|
||||
$lang = $matches['lang'] ?? $config['default-lang'] ?? exit('no language found for ' . $src . LF);
|
||||
|
||||
if (!file_exists($base_filepath_target . '.html') OR (filemtime($src) > filemtime($base_filepath_target . '.html')) OR $opt['force']) {
|
||||
echo 'Compiling ' . $src . ' ' . date("Y-m-d H:i:s", $node_info->getMTime()) . LF;
|
||||
|
||||
// Execute PHP code
|
||||
|
@ -91,7 +94,7 @@ foreach($nodes as $node) {
|
|||
$content = ob_get_clean();
|
||||
|
||||
// Convert Gemtext to Markdown
|
||||
if ($path_parts['extension'] === 'gmi' OR $path_parts['extension'] === 'md') {
|
||||
if ($path_parts_target['extension'] === 'gmi' OR $path_parts_target['extension'] === 'md') {
|
||||
$content = preg_replace_callback(
|
||||
'/^=>\h*(?<addr>\S+)(:?\h+(?<title>\V+))?$/m',
|
||||
function ($matches) {
|
||||
|
@ -120,60 +123,49 @@ foreach($nodes as $node) {
|
|||
// .md > .html for local links
|
||||
$content = preg_replace('/ href="([^:"]+)\.md"/', ' href="$1.html"', $content);
|
||||
|
||||
$relative_root_path = str_repeat('../', substr_count(str_replace(SITE . '/target', '', $path_parts['dirname']), '/'));
|
||||
$relative_root_path = str_repeat('../', substr_count(str_replace(SITE . '/target', '', $path_parts_target['dirname']), '/'));
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="<?php
|
||||
|
||||
preg_match('#\.([a-zA-Z-]{2,5})\.#', $path_parts['basename'], $file_lang);
|
||||
if (isset($file_lang[1])) {
|
||||
$lang = $file_lang[1];
|
||||
} else {
|
||||
preg_match('#/([a-z]{2})(/|$)#', $path_parts['dirname'], $dir_lang);
|
||||
$lang = $dir_lang[1] ?? $config['default-lang'];
|
||||
}
|
||||
echo $lang ?? '';
|
||||
|
||||
?>">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<?php
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="<?= $lang ?>">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<?php
|
||||
if (isset($title) AND isset($config['title']))
|
||||
echo '<title>' . $title . ' · ' . $config['title'] . '</title>';
|
||||
else if (isset($title))
|
||||
echo '<title>' . $title . '</title>';
|
||||
else if (isset($config['title']))
|
||||
echo '<title>' . $config['title'] . '</title>';
|
||||
?>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="referrer" content="no-referrer">
|
||||
?>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="referrer" content="no-referrer">
|
||||
<?php
|
||||
if (isset($config['author']))
|
||||
echo '<meta name="author" content="' . $config['author'] . '">';
|
||||
echo ' <meta name="author" content="' . $config['author'] . '">';
|
||||
|
||||
if ($config['announce-feed'])
|
||||
echo '<link rel="alternate" type="application/atom+xml" href="' . $relative_root_path . 'feed.atom">' . LF;
|
||||
echo ' <link rel="alternate" type="application/atom+xml" href="' . $relative_root_path . 'feed.atom">' . LF;
|
||||
|
||||
if ($config['announce-css']) {
|
||||
if (file_exists(SITE . '/style.css'))
|
||||
echo '<link rel="stylesheet" media="screen" href="' . $relative_root_path . 'style.css">' . LF;
|
||||
echo '<link rel="stylesheet" media="screen" href="' . $relative_root_path . 'mkht-php.css">' . LF;
|
||||
echo ' <link rel="stylesheet" media="screen" href="' . $relative_root_path . 'style.css">' . LF;
|
||||
echo ' <link rel="stylesheet" media="screen" href="' . $relative_root_path . 'mkht-php.css">' . LF;
|
||||
}
|
||||
|
||||
if (file_exists(SITE . '/head.inc.html'))
|
||||
echo file_get_contents(SITE . '/head.inc.html');
|
||||
?>
|
||||
</head>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<body>
|
||||
<?php
|
||||
if (file_exists(SITE . '/header.inc.php'))
|
||||
eval('?>' . file_get_contents(SITE . '/header.inc.php'));
|
||||
|
||||
if ($config['center-index'] AND $path_parts['filename'] === 'index')
|
||||
if ($config['center-index'] AND $path_parts_target['filename'] === 'index')
|
||||
echo '<div class="centered">' . $content . '</div>';
|
||||
else
|
||||
echo '<main>' . $content . '</main>';
|
||||
|
@ -193,26 +185,26 @@ foreach($nodes as $node) {
|
|||
$content = str_replace(' ', ' ', $content);
|
||||
}
|
||||
|
||||
file_put_contents($base_filepath . '.html', $content);
|
||||
file_put_contents($base_filepath_target . '.html', $content);
|
||||
|
||||
// Gzip compression
|
||||
exec('gzip --keep --fast --force ' . $base_filepath . '.html');
|
||||
exec('gzip --keep --fast --force ' . $base_filepath_target . '.html');
|
||||
}
|
||||
|
||||
$relative_addr = substr_replace($base_filepath . '.html', '', strpos($base_filepath, SITE), strlen(SITE));
|
||||
$relative_addr = substr_replace($base_filepath_src . '.html', '', strpos($base_filepath_src, SITE), strlen(SITE));
|
||||
|
||||
// As of RFC 3151: A URN Namespace for Public Identifiers
|
||||
$public_id = 'urn:publicid:' . $config['id'] . str_replace('/', '%2F', $relative_addr);
|
||||
|
||||
preg_match('#\<body\>(?<content>.*)\</body\>#s', file_get_contents($base_filepath . '.html'), $match);
|
||||
preg_match('#\<body\>(?<content>.*)\</body\>#s', file_get_contents($base_filepath_target . '.html'), $match);
|
||||
$atom_entry_content = $match['content'];
|
||||
|
||||
// Make relative links absolute
|
||||
$atom_entry_content = preg_replace_callback('# href=\"(?<relative_url>[^:"]+)\"#', function ($matches) {
|
||||
global $config;
|
||||
global $path_parts;
|
||||
global $path_parts_target;
|
||||
|
||||
return ' href="' . ($config['base-url'][0] ?? '') . substr($path_parts['dirname'], strlen(SITE)) . '/' . $matches['relative_url'] . '"';
|
||||
return ' href="' . ($config['base-url'][0] ?? '') . substr($path_parts_target['dirname'], strlen(SITE)) . '/' . $matches['relative_url'] . '"';
|
||||
}, $atom_entry_content);
|
||||
|
||||
ob_start();
|
||||
|
@ -228,27 +220,45 @@ foreach($nodes as $node) {
|
|||
<content type="html"><?= htmlspecialchars($atom_entry_content) ?></content>
|
||||
</entry>
|
||||
<?php
|
||||
$feed .= ob_get_clean();
|
||||
$atom_entry = ob_get_clean();
|
||||
|
||||
foreach (explode('/', $relative_addr) as $level => $unused) {
|
||||
if ($level === 0)
|
||||
continue;
|
||||
|
||||
$dir = implode('/', array_slice(explode('/', $relative_addr), 0, $level)) . '/';
|
||||
$last_update = $node_info->getMTime();
|
||||
|
||||
$dirs_entries_per_lang['all'][$dir] = ($dirs_entries_per_lang['all'][$dir] ?? '') . $atom_entry;
|
||||
$dirs_entries_per_lang[$lang][$dir] = ($dirs_entries_per_lang[$lang][$dir] ?? '') . $atom_entry;
|
||||
|
||||
if (!isset($dirs_last_update['all'][$dir]) OR $last_update > $dirs_last_update['all'][$dir])
|
||||
$dirs_last_update['all'][$dir] = $last_update;
|
||||
if (!isset($dirs_last_update[$lang][$dir]) OR $last_update > $dirs_last_update[$lang][$dir])
|
||||
$dirs_last_update[$lang][$dir] = $last_update;
|
||||
}
|
||||
}
|
||||
|
||||
asort($files_dates);
|
||||
|
||||
ob_start();
|
||||
foreach ($dirs_entries_per_lang as $lang => $dirs_entries) {
|
||||
foreach ($dirs_entries as $dir => $entries) {
|
||||
ob_start();
|
||||
?>
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<feed xmlns="http://www.w3.org/2005/Atom">
|
||||
<title><?= $config['title'] ?? '' ?></title>
|
||||
<id>urn:publicid:<?= $config['id'] ?></id>
|
||||
<title><?= ($config['title'] ?? '') . (($dir !== '/') ? ' ' . $dir : '') ?></title>
|
||||
<id>urn:publicid:<?= $config['id'] . str_replace('/', '%2F', $dir) ?></id>
|
||||
<?php
|
||||
foreach ($config['base-url'] as $url)
|
||||
echo ' <link rel="self" type="application/atom+xml" href="' . $url . '/feed.atom"></link>' . LF;
|
||||
foreach ($config['base-url'] as $url)
|
||||
echo ' <link rel="self" type="application/atom+xml" href="' . $url . $dir . 'feed.' . $lang . '.atom"></link>' . LF;
|
||||
?>
|
||||
<updated><?= date('c', $files_dates[array_key_last($files_dates)]) ?></updated>
|
||||
<updated><?= date('c', $dirs_last_update[$lang][$dir]) ?></updated>
|
||||
<author>
|
||||
<name><?= $config['author'] ?? '' ?></name>
|
||||
</author>
|
||||
<?php
|
||||
file_put_contents(SITE . '/target/feed.atom', ob_get_clean() . $feed . '</feed>' . LF);
|
||||
file_put_contents(SITE . '/target' . $dir . 'feed.' . $lang . '.atom', ob_get_clean() . $entries . '</feed>' . LF);
|
||||
}
|
||||
}
|
||||
|
||||
if ($config['announce-css'])
|
||||
copy(ROOT . '/style.css', SITE . '/target/mkht-php.css');
|
||||
|
|
Loading…
Reference in a new issue