Add links to choose page language

This commit is contained in:
Miraty 2023-10-15 01:18:44 +02:00
parent af0d406144
commit 65735a5775
3 changed files with 62 additions and 9 deletions

View file

@ -42,11 +42,10 @@ Files starting with a dot (except for `.htaccess`) are ignored.
Files containing `draft` in their name (separated from other characters by `.`) are ignored.
Security note: as PHP code is executed, input files need to be trusted
Security note: any PHP code in input files is executed with the same permissions as `mkht.php`
## Output
* `/target/*.gmi` (if using `.gmi` extension in /src)
* `/target/*.md`
* `/target/*.html`
* `/target/*.html.gz`

View file

@ -51,6 +51,7 @@ function clearnetOrOnion($clearnet_url, $onion_url) {
$dirs_last_update = [];
$dirs_entries = [];
$pages_langs = [];
$nodes = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(SITE, RecursiveDirectoryIterator::SKIP_DOTS));
@ -73,6 +74,21 @@ foreach($nodes as $node) {
copy($src, $target);
if ($node_info->getExtension() !== 'md')
continue;
$pages[] = $node;
preg_match('/(?<pagename>[-\w]+)(?:\.(?<lang>[a-z]{2}))?\.md/', $path_parts_src['basename'], $matches);
$lang = $matches['lang'] ?? $config['default-lang'] ?? exit('no language found for ' . $src . LF);
$pages_langs[$path_parts_src['dirname'] . '/' . $matches['pagename']][] = $lang;
$page_names[$src] = $matches['pagename'];
}
foreach ($pages as $node) {
$node_info = new SplFileInfo($node->getPathName());
$src = $node_info->getPathname();
$target = str_replace(SITE, SITE . '/target', $src);
$path_parts_src = pathinfo($src);
$path_parts_target = pathinfo($target);
$base_filepath_src = $path_parts_src['dirname'] . '/' . $path_parts_src['filename'];
$base_filepath_target = $path_parts_target['dirname'] . '/' . $path_parts_target['filename'];
@ -82,9 +98,6 @@ foreach($nodes as $node) {
preg_match('/^# (?<title>.*)$/Dm', $content, $matches);
$title = $matches['title'] ?? NULL;
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;
@ -145,7 +158,9 @@ foreach($nodes as $node) {
<?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;
@ -162,6 +177,23 @@ foreach($nodes as $node) {
<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'];
echo '<aside><nav><ul>';
foreach ($page_langs as $lang) {
echo '<small><li lang="' . $lang . '">';
echo '<a href="' . $page_names[$src] . '.' . $lang . '" rel="alternate" hreflang="' . $lang . '">';
echo $lang_txt[$lang] ?? $lang;
echo '</a>';
echo '</small></li>';
}
echo '</ul></nav></aside>';
}
if (file_exists(SITE . '/header.inc.php'))
eval('?>' . file_get_contents(SITE . '/header.inc.php'));

View file

@ -36,6 +36,7 @@ body {
font-size: 1rem;
line-height: 1.5em;
margin: 1rem;
margin-top: 0;
padding: 0;
background-color: var(--background-color);
@ -112,6 +113,7 @@ pre, code, var, samp {
font-style: normal;
border-radius: 15px;
word-break: break-all;
white-space: pre;
}
address {
@ -172,6 +174,26 @@ p {
margin-bottom: 0.5rem;
}
aside {
text-align: center;
}
aside ul {
display: inline-block;
margin: 0;
padding: 0.3rem;
padding-top: 0;
border-bottom: 0.06rem solid black;
border-left: 0.06rem solid black;
border-right: 0.06rem solid black;
border-bottom-right-radius: 1rem;
border-bottom-left-radius: 1rem;
}
aside li {
display: inline;
padding-left: 0.5rem;
padding-right: 0.5rem;
}
header, footer, .centered {
text-align: center;
justify-content: center;
@ -186,14 +208,14 @@ footer {
text-decoration: dotted underline;
}
[hreflang=en]::after {
main [hreflang=en]::after {
content: ' (en)';
}
[hreflang=fr]::after {
main [hreflang=fr]::after {
content: ' (fr)';
}
[hreflang]::after {
main [hreflang]::after {
font-size: 0.8rem;
}