Improve theme patch: dynamic locale list, fix local file:// URLs

- Scan coreutils-l10n for available locales instead of hardcoding
- Fix language switcher to use relative path replacement so it works
  with both deployed and local file:// URLs
- Move links into the existing .additional div for better layout
This commit is contained in:
Sylvestre Ledru
2026-03-22 09:35:02 +01:00
parent 7f9102ed0c
commit f0c206ab48
+82 -42
View File
@@ -1,34 +1,66 @@
#!/bin/bash
# Patch mdbook theme to add language selector and utility links
# Usage: patch-mdbook-theme.sh <coreutils-docs-dir> [current-lang]
# Usage: patch-mdbook-theme.sh <coreutils-docs-dir> <coreutils-l10n-dir>
set -euo pipefail
DOCS_DIR="${1:?Usage: $0 <coreutils-docs-dir> [current-lang]}"
CURRENT_LANG="${2:-en}"
DOCS_DIR="${1:?Usage: $0 <coreutils-docs-dir> <coreutils-l10n-dir>}"
L10N_DIR="${2:?Usage: $0 <coreutils-docs-dir> <coreutils-l10n-dir>}"
HEAD_HBS="$DOCS_DIR/theme/head.hbs"
# Append language selector and utility links to head.hbs
# Language display names (ftl filename -> display name)
declare -A LANG_NAMES=(
[en-US]="English"
[ar]="العربية" [ast]="Asturianu" [ca]="Català" [cs]="Čeština"
[da]="Dansk" [de]="Deutsch" [eo]="Esperanto" [es-ES]="Español"
[fi]="Suomi" [fr-FR]="Français" [he]="עברית" [id]="Bahasa Indonesia"
[it]="Italiano" [ja]="日本語" [kab]="Taqbaylit" [ko]="한국어"
[nb-NO]="Norsk Bokmål" [ne]="नेपाली" [pl]="Polski" [pt]="Português"
[pt-BR]="Português (Brasil)" [ru]="Русский" [sv]="Svenska"
[tr]="Türkçe" [uk]="Українська" [vi]="Tiếng Việt"
[zh-Hans]="中文 (简体)" [zh-Hant]="中文 (繁體)"
)
# ftl filename -> URL lang code (used in /coreutils/docs-{code}/)
declare -A FTL_TO_URL=(
[en-US]="en" [fr-FR]="fr" [es-ES]="es" [zh-Hans]="zh" [zh-Hant]="zh-Hant"
[pt-BR]="pt-BR" [nb-NO]="nb-NO"
)
# For others, the ftl name IS the URL code (ar, de, it, ja, etc.)
# Scan l10n repo to find which locales have translations
# Use a representative utility (ls) to find available locales
LANGS_JSON="['en', 'English']"
for ftl in "$L10N_DIR"/src/uu/ls/locales/*.ftl; do
[ -f "$ftl" ] || continue
ftl_name=$(basename "$ftl" .ftl)
[ "$ftl_name" = "en-US" ] && continue
display="${LANG_NAMES[$ftl_name]:-$ftl_name}"
url_code="${FTL_TO_URL[$ftl_name]:-$ftl_name}"
LANGS_JSON="$LANGS_JSON, ['$url_code', '$display']"
done
# Append styles and JS to head.hbs
cat >> "$HEAD_HBS" << 'ENDSTYLE'
<style>
.util-bar {
.util-links {
display: flex;
justify-content: flex-end;
gap: 15px;
gap: 12px;
align-items: center;
font-size: 0.85em;
margin-bottom: 10px;
font-size: 0.75em;
}
.util-bar a {
.util-links a {
color: var(--links);
text-decoration: none;
}
.util-bar a:hover {
.util-links a:hover {
text-decoration: underline;
}
.lang-selector {
position: relative;
display: inline-block;
}
.lang-selector select {
appearance: none;
@@ -36,81 +68,89 @@ cat >> "$HEAD_HBS" << 'ENDSTYLE'
color: var(--fg);
border: 1px solid var(--table-border-color);
border-radius: 4px;
padding: 4px 24px 4px 8px;
font-size: 0.85em;
padding: 2px 20px 2px 6px;
font-size: 1em;
cursor: pointer;
}
.lang-selector::after {
content: "▾";
position: absolute;
right: 8px;
right: 6px;
top: 50%;
transform: translateY(-50%);
pointer-events: none;
color: var(--fg);
font-size: 0.8em;
}
</style>
ENDSTYLE
# Add JavaScript for language switching and utility bar injection
cat >> "$HEAD_HBS" << ENDSCRIPT
cat >> "$HEAD_HBS" << 'ENDSCRIPT'
<script>
document.addEventListener('DOMContentLoaded', function() {
// Only add util-bar on utility pages
var path = window.location.pathname;
var match = path.match(/\/coreutils\/docs(?:-([a-z]{2}(?:-[A-Z]{2})?))?\/utils\/(\w+)\.html/);
// Match both deployed (/coreutils/docs[-lang]/utils/X.html) and local (book[-lang]/utils/X.html)
var match = path.match(/\/(?:coreutils\/)?docs(?:-([a-z]{2}(?:-[A-Z]{2})?))?\/utils\/(\w+)\.html/)
|| path.match(/\/book(?:-([a-z]{2}(?:-[A-Z]{2})?))?\/utils\/(\w+)\.html/);
if (!match) return;
var currentLang = match[1] || 'en';
var utilName = match[2];
var bar = document.createElement('div');
bar.className = 'util-bar';
// Find the existing .additional div and append our links there
var additional = document.querySelector('.additional');
if (!additional) return;
var links = document.createElement('div');
links.className = 'util-links';
// Source code link
var srcLink = document.createElement('a');
srcLink.href = 'https://github.com/uutils/coreutils/tree/main/src/uu/' + utilName;
srcLink.textContent = 'Source code';
srcLink.textContent = 'Source';
srcLink.target = '_blank';
bar.appendChild(srcLink);
links.appendChild(srcLink);
// Report issue link
var issueLink = document.createElement('a');
issueLink.href = 'https://github.com/uutils/coreutils/issues/new?title=' + utilName + ':%20&labels=bug';
issueLink.textContent = 'Report an issue';
issueLink.textContent = 'Report a bug';
issueLink.target = '_blank';
bar.appendChild(issueLink);
links.appendChild(issueLink);
// Language selector
var langDiv = document.createElement('div');
langDiv.className = 'lang-selector';
var select = document.createElement('select');
var langs = {
'en': 'English', 'fr': 'Français', 'de': 'Deutsch', 'es': 'Español',
'it': 'Italiano', 'pt': 'Português', 'pt-BR': 'Português (BR)',
'ja': '日本語', 'ko': '한국어', 'ru': 'Русский', 'zh': '中文',
'uk': 'Українська', 'sv': 'Svenska', 'pl': 'Polski', 'tr': 'Türkçe'
};
for (var code in langs) {
var langs = LANGS_PLACEHOLDER;
langs.forEach(function(l) {
var opt = document.createElement('option');
opt.value = code;
opt.textContent = langs[code];
if (code === currentLang) opt.selected = true;
opt.value = l[0];
opt.textContent = l[1];
if (l[0] === currentLang) opt.selected = true;
select.appendChild(opt);
}
});
select.addEventListener('change', function() {
var newLang = this.value;
var suffix = newLang === 'en' ? '' : '-' + newLang;
window.location.href = '/coreutils/docs' + suffix + '/utils/' + utilName + '.html';
var curPath = window.location.pathname;
// Replace the directory segment right before /utils/:
// .../book[-lang]/utils/X.html or .../docs[-lang]/utils/X.html
var newPath = curPath.replace(
/\/(book|docs)(?:-[a-zA-Z]{2}(?:-[a-zA-Z]+)?)?\/utils\//,
function(m, base) {
return '/' + base + (newLang === 'en' ? '' : '-' + newLang) + '/utils/';
}
);
window.location.href = newPath;
});
langDiv.appendChild(select);
bar.appendChild(langDiv);
links.appendChild(langDiv);
// Insert bar at the top of the content
var main = document.querySelector('main');
if (main) main.insertBefore(bar, main.firstChild);
additional.appendChild(links);
});
</script>
ENDSCRIPT
# Inject the dynamically-built language list
sed -i "s|LANGS_PLACEHOLDER|[${LANGS_JSON}]|" "$HEAD_HBS"
echo "Patched $HEAD_HBS with language selector and utility links"