use std::{
collections::HashMap,
fs::{self, read_dir},
io::Read,
};
use crate::{success, track_mount};
#[allow(clippy::too_many_lines)]
pub fn module(router: &mut windmark::Router) {
let paths = read_dir("content/pages/blog").unwrap();
let mut blogs: HashMap<String, HashMap<_, _>> = HashMap::new();
for path in paths {
let blog = path.unwrap().path().display().to_string();
let blog_paths = read_dir(&blog).unwrap();
let mut entries: HashMap<_, String> = HashMap::new();
blog_paths
.map(|e| e.unwrap().path().display().to_string())
.for_each(|file| {
let mut contents = String::new();
fs::File::open(&file)
.unwrap()
.read_to_string(&mut contents)
.unwrap();
entries.insert(
file.replace(&blog, "").replace(".gmi", "").replace(
{
#[cfg(windows)]
{
'\\'
}
#[cfg(unix)]
{
'/'
}
},
"",
),
contents,
);
});
blogs.insert(
blog
.replace(
{
#[cfg(windows)]
{
"content/pages/blog\\"
}
#[cfg(unix)]
{
"content/pages/blog/"
}
},
"",
)
.split('_')
.map(|s| {
let mut c = s.chars();
match c.next() {
None => String::new(),
Some(f) =>
f.to_uppercase()
.chain(c.flat_map(char::to_lowercase))
.collect(),
}
})
.collect::<Vec<_>>()
.join(" "),
entries,
);
}
let blog_clone = blogs.clone();
track_mount(
router,
"/blog",
"Fuwn's blogs",
Box::new(move |context| {
success!(
&format!(
"# BLOGS ({})\n\n{}",
blog_clone.len(),
blog_clone
.iter()
.map(|(title, _)| title.clone())
.collect::<Vec<_>>()
.into_iter()
.map(|i| {
format!(
"=> {} {}",
format_args!("/blog/{}", i.replace(' ', "_").to_lowercase(),),
i
)
})
.collect::<Vec<_>>()
.join("\n")
),
context
)
}),
);
for (blog, entries) in blogs {
let fixed_blog_name = blog.replace(' ', "_").to_lowercase();
let entries_clone = entries.clone();
let fixed_blog_name_clone = fixed_blog_name.clone();
let blog_clone = blog.clone();
track_mount(
router,
&format!("/blog/{}", fixed_blog_name),
&format!("{} ― One of Fuwn's blogs", &blog),
Box::new(move |context| {
success!(
&format!(
"# {} ({})\n\n{}",
blog.to_uppercase(),
entries_clone.len(),
entries_clone
.iter()
.map(|(title, _)| title.clone())
.collect::<Vec<_>>()
.into_iter()
.map(|i| {
format!(
"=> {} {}",
format_args!(
"/blog/{}/{}",
fixed_blog_name_clone,
i.to_lowercase()
),
i
)
})
.collect::<Vec<_>>()
.join("\n")
),
context
)
}),
);
for (title, contents) in entries {
track_mount(
router,
&format!("/blog/{}/{}", fixed_blog_name, title.to_lowercase()),
&format!(
"{}, {} ― An entry to one of Fuwn's blogs",
blog_clone, title
),
Box::new(move |context| success!(contents, context)),
);
}
}
}