added categories

This commit is contained in:
2026-02-28 19:09:53 +01:00
parent be47059b32
commit e9855ec6a1
15 changed files with 169 additions and 129 deletions

View File

@@ -60,6 +60,9 @@ pub enum EntryCommands {
New {
/// The title of the new entry (e.g. "The Great Bernardo")
name: String,
/// Optional category/type for the entry
#[arg(short, long)]
category: Option<String>,
},
/// Remove an entry by its normalized name
Remove {

View File

@@ -9,7 +9,7 @@ use crate::cli::EntryCommands;
pub async fn handle(command: EntryCommands, root_dir: PathBuf) -> Result<()> {
match command {
EntryCommands::List => list_entries(&root_dir).await,
EntryCommands::New { name } => create_entry(&root_dir, &name).await,
EntryCommands::New { name, category } => create_entry(&root_dir, &name, category).await,
EntryCommands::Remove { name } => remove_entry(&root_dir, &name).await,
EntryCommands::Inspect { name } => inspect_entry(&root_dir, &name).await,
}
@@ -73,7 +73,7 @@ fn check_file_status(root: &PathBuf, filename: Option<String>) -> String {
}
}
async fn create_entry(root: &PathBuf, title: &str) -> Result<()> {
async fn create_entry(root: &PathBuf, title: &str, category: Option<String>) -> Result<()> {
let slug: String = title
.trim()
.to_lowercase()
@@ -102,7 +102,7 @@ async fn create_entry(root: &PathBuf, title: &str) -> Result<()> {
// Minimal default content
let toml_content = format!(
r#"title = "{}"
r#"title = "{}"{}
image = "{}"
content_file = "{}"
@@ -111,7 +111,14 @@ content_file = "{}"
"Status" = "WIP"
"Created" = "2026"
"Category" = "General""#,
title, img_filename, md_filename
title,
if let Some(cat) = category {
format!("\ncategory = \"{cat}\"")
} else {
String::new()
},
img_filename,
md_filename
);
fs::write(&toml_path, toml_content)

82
src/fs.rs Normal file
View File

@@ -0,0 +1,82 @@
use std::path::PathBuf;
use crate::{Page, WikiConfig};
pub async fn get_summary_data(docs_dir: &PathBuf, is_static: bool) -> Vec<Page> {
let mut pages = Vec::new();
if let Ok(mut entries) = tokio::fs::read_dir(docs_dir).await {
while let Ok(Some(entry)) = entries.next_entry().await {
let path = entry.path();
if path.extension().and_then(|s| s.to_str()) != Some("toml")
|| path.file_name().and_then(|s| s.to_str()) == Some("_changelog.toml")
{
continue;
}
let filename_str = if is_static {
entry.file_name().to_string_lossy().into_owned()
} else {
path.file_stem()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_else(|| entry.file_name().to_string_lossy().into_owned())
};
let (title, category) = if let Ok(content) = tokio::fs::read_to_string(&path).await {
if let Ok(config) = toml::from_str::<WikiConfig>(&content) {
(config.title, config.category)
} else {
(filename_str.clone(), None)
}
} else {
(filename_str.clone(), None)
};
pages.push(Page {
filename: filename_str,
title,
category,
datetime: "".to_string(),
});
}
}
pages.sort_by(|a, b| match (&a.category, &b.category) {
(None, Some(_)) => std::cmp::Ordering::Less,
(Some(_), None) => std::cmp::Ordering::Greater,
(Some(c1), Some(c2)) => match c1.cmp(c2) {
std::cmp::Ordering::Equal => a.title.cmp(&b.title),
ord => ord,
},
(None, None) => a.title.cmp(&b.title),
});
pages
}
pub fn get_nav_links(dir: &PathBuf, current_file: &str) -> (Option<String>, Option<String>) {
let mut files: Vec<String> = std::fs::read_dir(dir)
.unwrap()
.filter_map(|entry| {
let path = entry.ok()?.path();
if path.extension()? == "toml" {
Some(path.file_name()?.to_str()?.to_string())
} else {
None
}
})
.collect();
files.sort();
let pos = files.iter().position(|f| f == current_file);
match pos {
Some(i) => {
let prev = if i == 0 {
Some(".".to_string())
} else {
files.get(i - 1).cloned()
};
let next = files.get(i + 1).cloned();
(prev, next)
}
None => (None, None),
}
}

View File

@@ -15,10 +15,12 @@ mod analysis;
mod changelog;
mod cli;
mod entry;
mod fs;
mod rendering;
use crate::{
cli::{Cli, Commands},
fs::get_summary_data,
rendering::{
render_changelog_handler, render_page_handler, render_summary_handler, render_wiki_page,
},
@@ -64,11 +66,13 @@ pub struct Page {
pub filename: String,
pub title: String,
pub datetime: String,
pub category: Option<String>,
}
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct WikiConfig {
pub title: String,
pub category: Option<String>,
pub image: Option<String>,
pub infobox: Option<IndexMap<String, String>>,
pub content_file: Option<String>,
@@ -154,46 +158,6 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}
async fn get_summary_data(docs_dir: &PathBuf, is_static: bool) -> Vec<Page> {
let mut pages = Vec::new();
if let Ok(mut entries) = tokio::fs::read_dir(docs_dir).await {
while let Ok(Some(entry)) = entries.next_entry().await {
let path = entry.path();
if path.extension().and_then(|s| s.to_str()) != Some("toml")
|| path.file_name().and_then(|s| s.to_str()) == Some("_changelog.toml")
{
continue;
}
let filename_str = if is_static {
entry.file_name().to_string_lossy().into_owned()
} else {
path.file_stem()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_else(|| entry.file_name().to_string_lossy().into_owned())
};
let title = if let Ok(content) = tokio::fs::read_to_string(&path).await {
if let Ok(config) = toml::from_str::<WikiConfig>(&content) {
config.title
} else {
filename_str.clone()
}
} else {
filename_str.clone()
};
pages.push(Page {
filename: filename_str,
title,
datetime: "".to_string(),
});
}
}
pages.sort_by(|a, b| a.title.cmp(&b.title));
pages
}
async fn run_build(docs_dir: PathBuf, out_dir: PathBuf, no_navigation: bool) -> anyhow::Result<()> {
tracing::info!("Building static site to: {}", out_dir.display());
@@ -287,32 +251,3 @@ async fn serve_css() -> impl IntoResponse {
Err(_) => (StatusCode::NOT_FOUND, "CSS not found").into_response(),
}
}
fn get_nav_links(dir: &PathBuf, current_file: &str) -> (Option<String>, Option<String>) {
let mut files: Vec<String> = std::fs::read_dir(dir)
.unwrap()
.filter_map(|entry| {
let path = entry.ok()?.path();
if path.extension()? == "toml" {
Some(path.file_name()?.to_str()?.to_string())
} else {
None
}
})
.collect();
files.sort();
let pos = files.iter().position(|f| f == current_file);
match pos {
Some(i) => {
let prev = if i == 0 {
Some(".".to_string())
} else {
files.get(i - 1).cloned()
};
let next = files.get(i + 1).cloned();
(prev, next)
}
None => (None, None),
}
}

View File

@@ -13,8 +13,8 @@ use syntect::html::highlighted_html_for_string;
use tera::Context;
use crate::{
AppState, SYNTAX_SET, TEMPLATES, THEME_SET, WikiConfig, changelog, get_nav_links,
get_summary_data,
AppState, SYNTAX_SET, TEMPLATES, THEME_SET, WikiConfig, changelog,
fs::{get_nav_links, get_summary_data},
};
pub struct Renderer<'a> {
@@ -186,6 +186,7 @@ pub async fn render_wiki_page(
let mut context = Context::new();
context.insert("title", &config.title);
context.insert("category", &config.category);
context.insert("content", &html_output);
context.insert("infobox", &infobox_list);
context.insert("main_image", &main_image_path);