use super::Theme;
use crate::config::{Config, FooterConfig, NavItem as ConfigNavItem, SidebarItem as ConfigSidebarItem};
use askama::Template;
#[derive(Debug, Clone)]
pub struct LocaleInfo {
pub code: String,
pub label: String,
pub is_current: bool,
}
#[derive(Debug, Clone)]
pub struct SidebarGroup {
pub text: String,
pub items: Vec<SidebarLink>,
}
#[derive(Debug, Clone)]
pub struct SidebarLink {
pub text: String,
pub link: String,
}
#[derive(Debug, Clone)]
pub struct NavItem {
pub text: String,
pub link: String,
}
#[derive(Debug, Clone, Template)]
#[template(path = "page.html")]
pub struct PageContext {
pub page_title: String,
pub site_title: String,
pub content: String,
pub nav_items: Vec<NavItem>,
pub sidebar_groups: Vec<SidebarGroup>,
pub current_path: String,
pub has_footer: bool,
pub has_footer_message: bool,
pub footer_message: String,
pub has_footer_copyright: bool,
pub footer_copyright: String,
pub current_lang: String,
pub available_locales: Vec<LocaleInfo>,
pub root_path: String,
}
#[derive(Clone)]
pub struct DefaultTheme {
pub config: Config,
}
impl Theme for DefaultTheme {
fn name(&self) -> &str {
"default"
}
fn render_page(&self, context: &PageContext) -> Result<String, Box<dyn std::error::Error>> {
<PageContext as Template>::render(context).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()).into())
}
fn config(&self) -> &Config {
&self.config
}
}
impl DefaultTheme {
pub fn new(config: Config) -> Result<Self, Box<dyn std::error::Error>> {
Ok(Self { config })
}
pub fn site_title(&self) -> &str {
self.config.title.as_deref().unwrap_or("Nargo Documentation")
}
pub fn convert_nav_items(items: &[ConfigNavItem]) -> Vec<NavItem> {
items.iter().filter_map(|item| item.link.as_ref().map(|link| NavItem { text: item.text.clone(), link: link.clone() })).collect()
}
pub fn convert_sidebar_groups(sidebar: &std::collections::HashMap<String, Vec<ConfigSidebarItem>>) -> Vec<SidebarGroup> {
sidebar
.iter()
.map(|(group_text, items)| {
let sidebar_links: Vec<SidebarLink> = items.iter().filter_map(|item| item.link.as_ref().map(|link| SidebarLink { text: item.text.clone(), link: link.clone() })).collect();
SidebarGroup { text: group_text.clone(), items: sidebar_links }
})
.collect()
}
pub fn footer_config(&self) -> &Option<FooterConfig> {
&self.config.theme.footer
}
}