nargo-document 0.0.0

Nargo documentation tool
Documentation
//! 科技风主题实现
//! 提供现代科技感的文档主题

use super::{
    default_theme::{LocaleInfo, NavItem, PageContext, SidebarGroup, SidebarLink},
    Theme,
};
use crate::config::Config;
use askama::Template;

/// 科技风主题页面模板上下文
#[derive(Debug, Clone, Template)]
#[template(path = "tech_page.html")]
pub struct TechPageContext {
    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,
}

impl From<&PageContext> for TechPageContext {
    fn from(context: &PageContext) -> Self {
        Self { page_title: context.page_title.clone(), site_title: context.site_title.clone(), content: context.content.clone(), nav_items: context.nav_items.clone(), sidebar_groups: context.sidebar_groups.clone(), current_path: context.current_path.clone(), has_footer: context.has_footer, has_footer_message: context.has_footer_message, footer_message: context.footer_message.clone(), has_footer_copyright: context.has_footer_copyright, footer_copyright: context.footer_copyright.clone(), current_lang: context.current_lang.clone(), available_locales: context.available_locales.clone(), root_path: context.root_path.clone() }
    }
}

/// 科技风主题
#[derive(Clone)]
pub struct TechTheme {
    pub config: Config,
}

impl Theme for TechTheme {
    fn name(&self) -> &str {
        "tech"
    }

    fn render_page(&self, context: &PageContext) -> Result<String, Box<dyn std::error::Error>> {
        let tech_context: TechPageContext = context.into();
        <TechPageContext as Template>::render(&tech_context).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()).into())
    }

    fn config(&self) -> &Config {
        &self.config
    }
}

impl TechTheme {
    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("")
    }
}