use std::collections::BTreeMap;
use std::path::Path;
use i18n_core::{LocaleTag, MessageModel};
use renderer_dom::escape_html;
use telar_project::{OgImage, PageLocation, ThemeColor, WebSection, is_absolute_url};
use super::page::HeadTag;
pub(crate) const SITEMAP_FILE: &str = "sitemap.xml";
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct SitePages {
pub(crate) pages: Vec<PageLocation>,
pub(crate) locales: Vec<LocaleTag>,
pub(crate) base_locale: Option<LocaleTag>,
pub(crate) not_found: bool,
pub(crate) page_dirs: Vec<String>,
}
impl SitePages {
pub(crate) fn root_only() -> Self {
Self {
pages: vec![PageLocation::root()],
..Self::default()
}
}
}
pub(crate) struct PageMeta<'a> {
pub(crate) title: &'a str,
pub(crate) lang: &'a LocaleTag,
pub(crate) location: Option<&'a PageLocation>,
pub(crate) pages: &'a SitePages,
}
#[derive(Clone, Debug)]
pub(crate) struct Site {
origin: Option<String>,
base: String,
description: Description,
og_image: Option<OgImage>,
theme_color: Option<ThemeColor>,
}
#[derive(Clone, Debug)]
enum Description {
Fixed(String),
Translated {
messages: BTreeMap<LocaleTag, String>,
fallback: String,
},
}
impl Site {
pub(crate) fn new(
web: &WebSection,
package_root: &Path,
app_name: &str,
) -> Result<Self, String> {
let description = match &web.description {
None => Description::Fixed(format!("{app_name}, a Telar application.")),
Some(key) => translated(key, package_root)?,
};
if let Some(image) = &web.og_image {
check_images(image, &web.public_dir(package_root).0)?;
}
Ok(Self {
origin: web.origin().map(str::to_string),
base: web.base_path(),
description,
og_image: web.og_image.clone(),
theme_color: web.theme_color.clone(),
})
}
pub(crate) fn base(&self) -> &str {
&self.base
}
pub(crate) fn has_origin(&self) -> bool {
self.origin.is_some()
}
pub(crate) fn path_of(&self, location: &PageLocation) -> String {
let mut path = self.base.clone();
let locale = location.locale.as_ref().map(ToString::to_string);
for segment in locale.iter().chain(&location.segments) {
path.push_str(&encode_segment(segment));
path.push('/');
}
path
}
pub(crate) fn url_of(&self, location: &PageLocation) -> Option<String> {
let origin = self.origin.as_ref()?;
Some(format!("{origin}{}", self.path_of(location)))
}
pub(crate) fn href_of(&self, location: &PageLocation) -> String {
self.url_of(location)
.unwrap_or_else(|| self.path_of(location))
}
pub(crate) fn description(&self, lang: &LocaleTag) -> &str {
match &self.description {
Description::Fixed(text) => text,
Description::Translated { messages, fallback } => {
let shipped: Vec<LocaleTag> = messages.keys().cloned().collect();
i18n_core::negotiate_locale(std::slice::from_ref(lang), &shipped)
.and_then(|locale| messages.get(locale))
.unwrap_or(fallback)
}
}
}
pub(crate) fn head_tags(&self, meta: &PageMeta) -> Vec<HeadTag> {
let mut tags = Vec::new();
if self.base != "/" {
tags.push(HeadTag::base(self.base.clone()));
}
let description = self.description(meta.lang);
tags.push(HeadTag::meta("description", description));
tags.extend(self.theme_color_tags());
let canonical = meta.location.and_then(|location| self.url_of(location));
if let Some(url) = &canonical {
tags.push(HeadTag::link("canonical", url.clone()));
}
let localized = meta
.location
.filter(|location| location.locale.is_some() && self.has_origin());
if let Some(location) = localized {
tags.extend(self.alternates(location, meta.pages));
}
tags.push(HeadTag::property("og:type", "website"));
tags.push(HeadTag::property("og:title", meta.title));
tags.push(HeadTag::property("og:description", description));
if let Some(url) = canonical {
tags.push(HeadTag::property("og:url", url));
}
tags.push(HeadTag::property("og:locale", og_locale(meta.lang)));
if let Some(location) = localized {
let own = location.locale.as_ref();
for locale in meta.pages.locales.iter().filter(|l| Some(*l) != own) {
tags.push(HeadTag::property("og:locale:alternate", og_locale(locale)));
}
}
let locale = meta
.location
.and_then(|location| location.locale.as_ref())
.unwrap_or(meta.lang);
let base_locale = meta.pages.base_locale.as_ref().unwrap_or(locale);
let image = self
.og_image
.as_ref()
.and_then(|image| image.for_locale(locale, base_locale))
.and_then(|source| self.image_url(source));
let card = match image {
Some(url) => {
tags.push(HeadTag::property("og:image", url));
"summary_large_image"
}
None => "summary",
};
tags.push(HeadTag::meta("twitter:card", card));
tags
}
pub(crate) fn alternates(&self, location: &PageLocation, pages: &SitePages) -> Vec<HeadTag> {
self.alternate_hrefs(location, pages)
.into_iter()
.map(|(hreflang, href)| HeadTag::link("alternate", href).attr("hreflang", hreflang))
.collect()
}
fn alternate_hrefs(&self, location: &PageLocation, pages: &SitePages) -> Vec<(String, String)> {
let mut hrefs: Vec<(String, String)> = pages
.locales
.iter()
.map(|locale| {
(
locale.to_string(),
self.href_of(&location.in_locale(locale)),
)
})
.collect();
if let Some(base) = &pages.base_locale {
hrefs.push((
"x-default".to_string(),
self.href_of(&location.in_locale(base)),
));
}
hrefs
}
pub(crate) fn sitemap(&self, pages: &SitePages) -> Option<String> {
self.origin.as_ref()?;
let mut xml = String::from(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">\n",
);
for page in &pages.pages {
let url = self.url_of(page)?;
xml.push_str(&format!(" <url>\n <loc>{}</loc>\n", escape_html(&url)));
if page.locale.is_some() {
for (hreflang, href) in self.alternate_hrefs(page, pages) {
xml.push_str(&format!(
" <xhtml:link rel=\"alternate\" hreflang=\"{}\" href=\"{}\"/>\n",
escape_html(&hreflang),
escape_html(&href)
));
}
}
xml.push_str(" </url>\n");
}
xml.push_str("</urlset>\n");
Some(xml)
}
fn theme_color_tags(&self) -> Vec<HeadTag> {
match &self.theme_color {
None => Vec::new(),
Some(ThemeColor::One(color)) => vec![HeadTag::meta("theme-color", color.clone())],
Some(ThemeColor::Schemes(colors)) => vec![
HeadTag::meta("theme-color", colors.light.clone())
.attr("media", "(prefers-color-scheme: light)"),
HeadTag::meta("theme-color", colors.dark.clone())
.attr("media", "(prefers-color-scheme: dark)"),
],
}
}
fn image_url(&self, source: &str) -> Option<String> {
if is_absolute_url(source) {
return Some(source.to_string());
}
let origin = self.origin.as_ref()?;
Some(format!(
"{origin}{}{}",
self.base,
source.trim_start_matches('/')
))
}
}
fn og_locale(tag: &LocaleTag) -> String {
tag.to_string().replace('-', "_")
}
fn translated(key: &str, package_root: &Path) -> Result<Description, String> {
let model = telar_baker::parse_catalog(package_root)
.map_err(|problem| crate::runner::bake::render(&problem, package_root))?
.ok_or_else(|| {
format!("`[telar.web] description = \"{key}\"` names a catalog key, but the project has no catalog")
})?;
let per_locale = model.entries.get(key).ok_or_else(|| {
format!("`[telar.web] description = \"{key}\"` is not a key of the project's catalog")
})?;
let mut messages = BTreeMap::new();
for (locale, message) in per_locale {
let MessageModel::Plain(text) = message else {
return Err(format!(
"`[telar.web] description = \"{key}\"` is a message with arguments or plural forms in `{locale}`; a description is plain text"
));
};
messages.insert(locale.clone(), text.clone());
}
let fallback = messages
.get(&model.default_locale)
.or_else(|| messages.values().next())
.cloned()
.unwrap_or_default();
Ok(Description::Translated { messages, fallback })
}
fn check_images(image: &OgImage, public: &Path) -> Result<(), String> {
let sources: Vec<&String> = match image {
OgImage::Shared(source) => vec![source],
OgImage::PerLocale(sources) => sources.values().collect(),
};
for source in sources
.into_iter()
.filter(|source| !is_absolute_url(source))
{
let file = public.join(source.trim_start_matches('/'));
if !file.is_file() {
return Err(format!(
"`[telar.web] og_image` names \"{source}\", which is not a file of the public directory ({})",
file.display()
));
}
}
Ok(())
}
fn encode_segment(segment: &str) -> String {
let mut encoded = String::with_capacity(segment.len());
for &byte in segment.as_bytes() {
if byte.is_ascii_alphanumeric() || b"-._~!$&'()*+,;=:@".contains(&byte) {
encoded.push(byte as char);
} else {
encoded.push_str(&format!("%{byte:02X}"));
}
}
encoded
}
#[cfg(test)]
#[path = "site.test.rs"]
mod tests;