nargo_document/theme/
default_theme.rs1use super::Theme;
5use crate::config::{Config, FooterConfig, NavItem as ConfigNavItem, SidebarItem as ConfigSidebarItem};
6use askama::Template;
7
8#[derive(Debug, Clone)]
10pub struct LocaleInfo {
11 pub code: String,
13 pub label: String,
15 pub is_current: bool,
17}
18
19#[derive(Debug, Clone)]
21pub struct SidebarGroup {
22 pub text: String,
24 pub items: Vec<SidebarLink>,
26}
27
28#[derive(Debug, Clone)]
30pub struct SidebarLink {
31 pub text: String,
33 pub link: String,
35}
36
37#[derive(Debug, Clone)]
39pub struct NavItem {
40 pub text: String,
42 pub link: String,
44}
45
46#[derive(Debug, Clone, Template)]
48#[template(path = "page.html")]
49pub struct PageContext {
50 pub page_title: String,
52 pub site_title: String,
54 pub content: String,
56 pub nav_items: Vec<NavItem>,
58 pub sidebar_groups: Vec<SidebarGroup>,
60 pub current_path: String,
62 pub has_footer: bool,
64 pub has_footer_message: bool,
66 pub footer_message: String,
68 pub has_footer_copyright: bool,
70 pub footer_copyright: String,
72 pub current_lang: String,
74 pub available_locales: Vec<LocaleInfo>,
76 pub root_path: String,
78}
79
80#[derive(Clone)]
82pub struct DefaultTheme {
83 pub config: Config,
85}
86
87impl Theme for DefaultTheme {
88 fn name(&self) -> &str {
89 "default"
90 }
91
92 fn render_page(&self, context: &PageContext) -> Result<String, Box<dyn std::error::Error>> {
93 <PageContext as Template>::render(context).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()).into())
94 }
95
96 fn config(&self) -> &Config {
97 &self.config
98 }
99}
100
101impl DefaultTheme {
102 pub fn new(config: Config) -> Result<Self, Box<dyn std::error::Error>> {
104 Ok(Self { config })
105 }
106
107 pub fn site_title(&self) -> &str {
109 self.config.title.as_deref().unwrap_or("Nargo Documentation")
110 }
111
112 pub fn convert_nav_items(items: &[ConfigNavItem]) -> Vec<NavItem> {
114 items.iter().filter_map(|item| item.link.as_ref().map(|link| NavItem { text: item.text.clone(), link: link.clone() })).collect()
115 }
116
117 pub fn convert_sidebar_groups(sidebar: &std::collections::HashMap<String, Vec<ConfigSidebarItem>>) -> Vec<SidebarGroup> {
119 sidebar
120 .iter()
121 .map(|(group_text, items)| {
122 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();
123
124 SidebarGroup { text: group_text.clone(), items: sidebar_links }
125 })
126 .collect()
127 }
128
129 pub fn footer_config(&self) -> &Option<FooterConfig> {
131 &self.config.theme.footer
132 }
133}