355 lines
12 KiB
PHP
Executable file
355 lines
12 KiB
PHP
Executable file
#!/usr/bin/env php
|
|
<?php
|
|
if (php_sapi_name() !== 'cli')
|
|
exit('Must be run from CLI.' . PHP_EOL);
|
|
umask(0022);
|
|
const LF = "\n";
|
|
|
|
define('ROOT', dirname(realpath($_SERVER['SCRIPT_FILENAME'])));
|
|
|
|
if (!extension_loaded('tidy'))
|
|
echo 'PHP tidy extension unavailable. Feature disabled.' . PHP_EOL;
|
|
|
|
foreach ($argv as $arg) {
|
|
if ($arg === '-f')
|
|
$opt['force'] = true;
|
|
else
|
|
$args[] = $arg;
|
|
}
|
|
$opt['force'] ??= false;
|
|
|
|
$site = $args[1] ?? getcwd();
|
|
define('SITE', str_ends_with($site, '/') ? $site : $site . '/');
|
|
|
|
define('DESTINATION', $args[2] ?? 'dns');
|
|
|
|
if (file_exists(SITE . 'config.ini'))
|
|
$config = parse_ini_file(SITE . 'config.ini');
|
|
|
|
$config['header'] ??= false;
|
|
$config['author'] ??= NULL;
|
|
$config['base-url'] ??= [];
|
|
$config['center-index'] ??= false;
|
|
$config['default-lang'] ??= NULL;
|
|
$config['announce-css'] ??= false;
|
|
$config['announce-feed'] ??= false;
|
|
$config['gzip'] ??= false;
|
|
|
|
if (!isset($config['id'])) {
|
|
$config['id'] = bin2hex(random_bytes(32));
|
|
file_put_contents(SITE . 'config.ini', 'id = "' . $config['id'] . '"' . LF, FILE_APPEND);
|
|
}
|
|
|
|
foreach (['pandoc', 'gzip', 'po4a'] as $command) {
|
|
if ($command === 'po4a' && file_exists(SITE . 'po4a.cfg') === false)
|
|
continue;
|
|
if ($command === 'gzip' && !$config['gzip'])
|
|
continue;
|
|
exec('command -v ' . $command, result_code: $code);
|
|
if ($code !== 0)
|
|
exit($command . ' command not available.' . PHP_EOL);
|
|
}
|
|
|
|
// Determine whether links need to use Onion or DNS
|
|
function clearnetOrOnion($clearnet_url, $onion_url) {
|
|
return (DESTINATION === 'onion') ? $onion_url : $clearnet_url;
|
|
}
|
|
|
|
$dirs_last_update = [];
|
|
$pages_langs = [];
|
|
|
|
$nodes_src = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(SITE, RecursiveDirectoryIterator::SKIP_DOTS));
|
|
|
|
foreach($nodes_src as $node) {
|
|
$node_info = new SplFileInfo($node->getPathName());
|
|
$src = $node_info->getPathname();
|
|
if (str_starts_with($src, SITE . 'target/') OR str_starts_with($src, SITE . 'cache/'))
|
|
continue;
|
|
$target = str_replace(SITE, SITE . 'cache/', $src);
|
|
|
|
$path_parts_src = pathinfo($src);
|
|
$path_parts_target = pathinfo($target);
|
|
if (preg_match('#/\.(?!htaccess|well-known)#', $src) !== 0) // Skip hidden nodes other than .htaccess and .well-known
|
|
continue;
|
|
if ($node_info->getType() !== 'file')
|
|
continue;
|
|
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 (file_exists(SITE . 'po4a.cfg')) {
|
|
exec('po4a --destdir ' . SITE . ' --srcdir ' . SITE . ' ' . SITE . 'po4a.cfg');
|
|
foreach (glob('po/*.po*') as $gettext_file) // Remove annoying metadata generated by Gettext and translation tools
|
|
file_put_contents($gettext_file, preg_replace('/.*(POT-Creation-Date|PO-Revision-Date).*\n\n/sU', 'msgid ""' . LF . 'msgstr "Content-Type: text/plain; charset=UTF-8\n"' . LF . LF, file_get_contents($gettext_file)));
|
|
}
|
|
|
|
$nodes_cache = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(SITE . 'cache/', RecursiveDirectoryIterator::SKIP_DOTS));
|
|
|
|
foreach($nodes_cache as $node) {
|
|
$node_info = new SplFileInfo($node->getPathName());
|
|
$src = $node_info->getPathname();
|
|
if (str_starts_with($src, SITE . 'target/'))
|
|
continue;
|
|
$target = str_replace(SITE . 'cache/', SITE . 'target/', $src);
|
|
|
|
$path_parts_src = pathinfo($src);
|
|
$path_parts_target = pathinfo($target);
|
|
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;
|
|
|
|
preg_match('/(?<pagename>[-\w]+)(?:\.(?<lang>[a-z]{2}))?\.md/', $path_parts_src['basename'], $matches);
|
|
if ($matches['pagename'] === strtoupper($matches['pagename']) AND $matches['pagename'] !== strtolower($matches['pagename'])) // Skip uppercase-only filenames (like README.md), but not digit-only (like 404.md)
|
|
continue;
|
|
|
|
$pages[] = $node;
|
|
|
|
$lang = $matches['lang'] ?? $config['default-lang'] ?? exit('no language found for ' . $src . LF);
|
|
|
|
$pages_langs[$path_parts_src['dirname'] . '/' . $matches['pagename']][] = $lang;
|
|
$files_langs[$src] = $lang;
|
|
$page_names[$src] = $matches['pagename'];
|
|
}
|
|
|
|
foreach ($pages as $node) {
|
|
$node_info = new SplFileInfo($node->getPathName());
|
|
$src = $node_info->getPathname();
|
|
$target = str_replace(SITE . 'cache/', SITE . 'target/', $src);
|
|
$path_parts_src = pathinfo($src);
|
|
$path_parts_target = pathinfo($target);
|
|
$lang = $files_langs[$src];
|
|
|
|
$file_timestamped = str_replace(SITE . 'cache/', SITE, $src);
|
|
if (!file_exists($file_timestamped))
|
|
foreach ($pages_langs[$path_parts_src['dirname'] . '/' . $page_names[$src]] as $page_lang)
|
|
if (file_exists($file_timestamped = str_replace('.' . $lang . '.', '.' . $page_lang . '.', $file_timestamped)))
|
|
break;
|
|
$last_update = match (preg_match("/^(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})-/D", $path_parts_src['basename'], $date)) {
|
|
1 => (new DateTime())->setDate($date['year'], $date['month'], $date['day'])->format('U'),
|
|
0 => (new SplFileInfo($file_timestamped))->getMTime(),
|
|
};
|
|
|
|
$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_target . '.html') OR (filemtime($file_timestamped) > filemtime($base_filepath_target . '.html')) OR $opt['force']) {
|
|
echo 'Compiling ' . $src . ' ' . date("Y-m-d H:i:s", $last_update) . LF;
|
|
|
|
// Execute PHP code
|
|
ob_start();
|
|
eval('?>' . $content);
|
|
$content = ob_get_clean();
|
|
|
|
// Convert Gemtext to Markdown
|
|
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) {
|
|
if (!str_contains($matches['addr'], ':') AND str_ends_with($matches['addr'], '.gmi'))
|
|
$matches['addr'] = substr($matches['addr'], 0, -3) . 'md';
|
|
return '[' . ($matches['title'] ?? $matches['addr']) . '](' . $matches['addr'] . ')';
|
|
},
|
|
$content,
|
|
);
|
|
}
|
|
|
|
// Compile Markdown to HTML
|
|
$process = proc_open('pandoc --fail-if-warnings --section-divs -f markdown-citations-native_divs-native_spans+abbreviations+hard_line_breaks+lists_without_preceding_blankline+multiline_tables+fenced_divs+bracketed_spans+markdown_attribute -t html --wrap none', [
|
|
0 => ['pipe', 'r'],
|
|
1 => ['pipe', 'w'],
|
|
], $pipes);
|
|
if (is_resource($process) !== true)
|
|
exit('Can\'t spawn pandoc.' . PHP_EOL);
|
|
fwrite($pipes[0], $content);
|
|
fclose($pipes[0]);
|
|
$content = stream_get_contents($pipes[1]);
|
|
fclose($pipes[1]);
|
|
if (proc_close($process) !== 0)
|
|
exit('pandoc failed.' . PHP_EOL);
|
|
|
|
// .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_target['dirname']), '/'));
|
|
|
|
ob_start();
|
|
|
|
?>
|
|
<!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">
|
|
<?php
|
|
if (isset($config['author']))
|
|
echo ' <meta name="author" content="' . $config['author'] . '">';
|
|
?>
|
|
<link rel="canonical" href="<?= ($page_names[$src] === 'index') ? './' : $page_names[$src] ?>">
|
|
<?php
|
|
if ($config['announce-feed'])
|
|
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;
|
|
}
|
|
|
|
if (file_exists(SITE . 'head.inc.html'))
|
|
echo file_get_contents(SITE . 'head.inc.html');
|
|
?>
|
|
</head>
|
|
|
|
<body>
|
|
<?php
|
|
|
|
$page_langs = $pages_langs[$path_parts_src['dirname'] . '/' . $page_names[$src]];
|
|
if (count($page_langs) > 1) {
|
|
sort($page_langs);
|
|
$lang_txt = [
|
|
'fr' => 'français',
|
|
'en' => 'english',
|
|
];
|
|
?>
|
|
<aside>
|
|
<nav>
|
|
<ul>
|
|
<?php foreach ($page_langs as $page_lang) { ?>
|
|
<li lang="<?= $page_lang ?>">
|
|
<small>
|
|
<a href="<?= $page_names[$src] ?>.<?= $page_lang ?>" rel="alternate" hreflang="<?= $page_lang ?>">
|
|
<?= $lang_txt[$page_lang] ?? $page_lang ?>
|
|
</a>
|
|
</small>
|
|
</li>
|
|
<?php } ?>
|
|
</ul>
|
|
</nav>
|
|
</aside>
|
|
<?php
|
|
}
|
|
|
|
if (file_exists(SITE . 'header.inc.php'))
|
|
eval('?>' . file_get_contents(SITE . 'header.inc.php'));
|
|
|
|
if ($config['center-index'] AND $path_parts_target['filename'] === 'index')
|
|
echo '<div class="centered">' . $content . '</div>';
|
|
else
|
|
echo '<main>' . $content . '</main>';
|
|
if (file_exists(SITE . 'end.inc.html'))
|
|
require SITE . 'end.inc.html';
|
|
echo '</body></html>';
|
|
|
|
$content = ob_get_clean();
|
|
|
|
if (extension_loaded('tidy')) {
|
|
$content = tidy_repair_string($content, [
|
|
'indent' => true,
|
|
'indent-spaces' => 4,
|
|
'output-xhtml' => true,
|
|
'wrap' => 0,
|
|
'hide-comments' => true,
|
|
]);
|
|
$content = str_replace(' ', ' ', $content);
|
|
}
|
|
|
|
file_put_contents($base_filepath_target . '.html', $content);
|
|
|
|
if ($config['gzip'])
|
|
exec('gzip --keep --fast --force ' . $base_filepath_target . '.html');
|
|
}
|
|
|
|
if ($page_names[$src] === 'index')
|
|
continue;
|
|
|
|
$relative_addr = str_replace(SITE . 'cache/', '', $base_filepath_src . '.html');
|
|
|
|
// According to 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_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_target;
|
|
|
|
return ' href="' . ($config['base-url'][0] ?? '') . substr($path_parts_target['dirname'], strlen(SITE)) . '/' . $matches['relative_url'] . '"';
|
|
}, $atom_entry_content);
|
|
|
|
ob_start();
|
|
?>
|
|
<entry>
|
|
<title><?= $title ?></title>
|
|
<id><?= $public_id ?></id>
|
|
<updated><?= date('c', $last_update) ?></updated>
|
|
<?php
|
|
foreach ($config['base-url'] as $base_url)
|
|
echo ' <link rel="alternate" type="text/html" href="' . $base_url . $relative_addr . '"></link>' . LF;
|
|
?>
|
|
<content type="html"><?= htmlspecialchars($atom_entry_content) ?></content>
|
|
</entry>
|
|
<?php
|
|
$atom_entry = ob_get_clean();
|
|
|
|
foreach (explode('/', $relative_addr) as $level => $unused) {
|
|
$dir = implode('/', array_slice(explode('/', $relative_addr), 0, $level)) . '/';
|
|
|
|
$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;
|
|
}
|
|
}
|
|
|
|
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'] ?? '') . (($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 . $dir . 'feed.' . $lang . '.atom"></link>' . LF;
|
|
?>
|
|
<updated><?= date('c', $dirs_last_update[$lang][$dir]) ?></updated>
|
|
<author>
|
|
<name><?= $config['author'] ?? '' ?></name>
|
|
</author>
|
|
<?php
|
|
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');
|
|
if ($config['gzip'])
|
|
exec('gzip --keep --fast --force ' . SITE . 'target/mkht-php.css');
|
|
if (file_exists(SITE . 'style.css') && $config['gzip'])
|
|
exec('gzip --keep --fast --force ' . SITE . 'target/style.css');
|
|
}
|