add sitemap to sidebar

This commit is contained in:
Penelope Gwen 2026-03-10 11:09:28 -07:00
parent 911db49475
commit 7bbe0b83e9
2 changed files with 85 additions and 2 deletions

View file

@ -1,6 +1,61 @@
use crate::{MarkdownModule, markdowner};
use ptree::{Style, TreeItem};
use rand::seq::IteratorRandom;
use std::borrow::Cow;
use std::path::PathBuf;
use std::{fs, io};
#[derive(Clone, Debug)]
pub struct PathItem(pub PathBuf);
impl TreeItem for PathItem {
type Child = Self;
fn write_self<W: io::Write>(&self, f: &mut W, style: &Style) -> io::Result<()> {
let cwd = std::env::current_dir()
.expect("unable to parse working directory")
.join("serve");
if let Some(n) = self.0.file_name() {
write!(
f,
"[{}](/{})\n",
style.paint(n.to_string_lossy()),
self.0
.clone()
.to_path_buf()
.strip_prefix(cwd)
.expect("could not strip prefix?")
.to_str()
.unwrap()
)
} else {
Ok(())
}
}
fn children(&self) -> Cow<[Self::Child]> {
let v = if let Ok(list) = fs::read_dir(&self.0) {
list.filter_map(|item| item.ok())
.map(|entry| entry.path())
.filter(|e| {
if e.is_dir() && !e.starts_with("assets") {
e.read_dir()
.unwrap()
.any(|x| x.unwrap().path().extension().unwrap_or_default().eq("md"))
} else {
false
}
})
.map(PathItem)
.collect()
} else {
Vec::new()
};
Cow::from(v)
}
}
fn random_image_module(module: &MarkdownModule, directory: &str) -> MarkdownModule {
let image = random_image(directory);
@ -32,13 +87,14 @@ fn random_image_module(module: &MarkdownModule, directory: &str) -> MarkdownModu
module.clone()
}
pub fn sidebar_content(target_path: &PathBuf) -> Vec<MarkdownModule> {
pub fn sidebar_content(target_path: &PathBuf, root_path: PathBuf) -> Vec<MarkdownModule> {
let sidebar_modules = markdowner::get_markdown_modules(target_path);
let sidebar_modules: Vec<MarkdownModule> = sidebar_modules
.iter()
.map(|f| match f.metadata.title.as_str() {
"rats" => random_image_module(f, "rats"),
"buttons" => random_image_module(f, "buttons"),
"sitemap" => sitemap(f, &root_path),
_ => MarkdownModule {
path: f.path.clone(),
content: f.content.clone(),
@ -49,6 +105,28 @@ pub fn sidebar_content(target_path: &PathBuf) -> Vec<MarkdownModule> {
sidebar_modules
}
fn sitemap(module: &MarkdownModule, root_path: &PathBuf) -> MarkdownModule {
let mut writer: Vec<u8> = Vec::new();
let dir = PathItem(root_path.clone());
ptree::write_tree(&dir, &mut writer).expect("could not write tree");
let template_content = module.content.clone();
let template = text_template::Template::from(template_content.as_str());
let mut values = std::collections::HashMap::new();
let tree_text = String::from_utf8(writer).expect("could not parse string from utf8");
values.insert("sitemap", tree_text.as_str());
let modu = MarkdownModule {
path: module.path.clone(),
content: template.fill_in(&values).to_string(),
metadata: module.metadata.clone(),
};
modu.clone()
}
fn random_image(directory: &str) -> PathBuf {
let rat_image = std::fs::read_dir(PathBuf::from("./serve/assets/img/random/").join(directory))
.expect("where the fuck are your rat pictures?")

View file

@ -47,7 +47,12 @@ fn renderer(path: FullPath, user_agent: String, query: WebQuery) -> Box<dyn warp
let page_contents = markdowner::get_markdown_modules(&target_path);
let sidebar_dir = PathBuf::from("assets/sidebar/");
let sidebar_contents = sidebar_content(&router(sidebar_dir));
let sidebar_contents = sidebar_content(
&router(sidebar_dir),
std::env::current_dir()
.expect("unable to determine current directory")
.join("serve"),
);
let response = if user_agent.starts_with("curl/") {
curl_response(page_contents, sidebar_contents, query.width)