Skip to main content

nargo_document/theme/
dark_theme.rs

1//! 暗色主题实现
2//! 提供现代美观的暗色文档主题
3
4use super::{
5    default_theme::{LocaleInfo, NavItem, PageContext, SidebarGroup, SidebarLink},
6    Theme,
7};
8use crate::config::Config;
9use askama::Template;
10
11/// 暗色主题页面模板上下文
12#[derive(Debug, Clone, Template)]
13#[template(path = "dark_page.html")]
14pub struct DarkPageContext {
15    pub page_title: String,
16    pub site_title: String,
17    pub content: String,
18    pub nav_items: Vec<NavItem>,
19    pub sidebar_groups: Vec<SidebarGroup>,
20    pub current_path: String,
21    pub has_footer: bool,
22    pub has_footer_message: bool,
23    pub footer_message: String,
24    pub has_footer_copyright: bool,
25    pub footer_copyright: String,
26    pub current_lang: String,
27    pub available_locales: Vec<LocaleInfo>,
28    pub root_path: String,
29}
30
31impl From<&PageContext> for DarkPageContext {
32    fn from(context: &PageContext) -> Self {
33        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() }
34    }
35}
36
37/// 暗色主题
38#[derive(Clone)]
39pub struct DarkTheme {
40    pub config: Config,
41}
42
43impl Theme for DarkTheme {
44    fn name(&self) -> &str {
45        "dark"
46    }
47
48    fn render_page(&self, context: &PageContext) -> Result<String, Box<dyn std::error::Error>> {
49        let dark_context: DarkPageContext = context.into();
50        <DarkPageContext as Template>::render(&dark_context).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()).into())
51    }
52
53    fn config(&self) -> &Config {
54        &self.config
55    }
56}
57
58impl DarkTheme {
59    pub fn new(config: Config) -> Result<Self, Box<dyn std::error::Error>> {
60        Ok(Self { config })
61    }
62
63    pub fn site_title(&self) -> &str {
64        self.config.title.as_deref().unwrap_or("")
65    }
66}