use std::collections::BTreeMap;
use telar_project::PageLocation;
use super::page::{HeadTag, escape, script_json};
use super::site::{PageMeta, Site, SitePages};
pub(crate) const NEGOTIATE_JS: &str = r#"function negotiateLocale(preferred, available, fallback) {
const lower = (tag) => String(tag).replace(/[A-Z]/g, (c) => c.toLowerCase());
const language = (tag) => lower(tag).split("-")[0];
for (const wanted of preferred) {
const exact = available.find((tag) => lower(tag) === lower(wanted));
if (exact !== undefined) return exact;
const primary = language(wanted);
if (primary === "") continue;
const same = available.find((tag) => language(tag) === primary);
if (same !== undefined) return same;
}
return fallback;
}"#;
pub(crate) fn root_page(
site: &Site,
pages: &SitePages,
base_locale: &str,
titles: &BTreeMap<String, String>,
) -> String {
let base_root = PageLocation::root().in_locale(base_locale);
let title = titles
.get(base_locale)
.map(String::as_str)
.unwrap_or(base_locale);
let mut head = site.head_tags(&PageMeta {
title,
lang: base_locale,
location: Some(&base_root),
pages,
});
if !site.has_origin() {
head.extend(site.alternates(&base_root, pages));
}
head.push(HeadTag::meta("color-scheme", "light dark"));
let head: Vec<String> = head
.iter()
.map(|tag| format!(" {}", tag.html()))
.collect();
let fallback = site.path_of(&base_root);
let links: Vec<String> = pages
.locales
.iter()
.map(|locale| {
let label = titles.get(locale).map(String::as_str).unwrap_or(locale);
format!(
" <li><a href=\"{}\" hreflang=\"{locale}\" lang=\"{locale}\">{}</a></li>",
escape(&site.path_of(&PageLocation::root().in_locale(locale))),
escape(label),
locale = escape(locale),
)
})
.collect();
let available = serde_json::Value::from(pages.locales.clone());
let dir = if layout_core::Direction::for_locale(base_locale).is_rtl() {
"rtl"
} else {
"ltr"
};
format!(
r#"<!doctype html>
<html lang="{lang}" dir="{dir}">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{title}</title>
{head}
<noscript><meta http-equiv="refresh" content="0; url={fallback}" /></noscript>
<script>
{negotiate}
const preferred = navigator.languages && navigator.languages.length ? navigator.languages : [navigator.language || ""];
const locale = negotiateLocale(preferred, {available}, {base_locale});
location.replace({base} + locale + "/" + location.search + location.hash);
</script>
</head>
<body>
<noscript>
<ul>
{links}
</ul>
</noscript>
</body>
</html>
"#,
lang = escape(base_locale),
title = escape(title),
head = head.join("\n"),
fallback = escape(&fallback),
negotiate = NEGOTIATE_JS,
available = script_json(&available),
base_locale = script_json(&serde_json::Value::from(base_locale)),
base = script_json(&serde_json::Value::from(site.base())),
links = links.join("\n"),
)
}
#[cfg(test)]
#[path = "negotiation_test.rs"]
mod tests;