diff --git a/src/compiler.rs b/src/compiler.rs index f586872..e98eaa4 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -70,26 +70,26 @@ impl Compiler { for recipe in recipes { for tag in extract_tags(&self.options, &recipe.content) { match tag_map.get_mut(&tag) { - Some(v) => v.push(recipe.title.clone()), + Some(v) => v.push(recipe.slug.clone()), None => { - tag_map.insert(tag, vec![recipe.title.clone()]); + tag_map.insert(tag, vec![recipe.slug.clone()]); } } } - let target_path = Path::new(&self.path).join("recipes").join(&recipe.title); + let target_path = Path::new(&self.path).join("recipes").join(&recipe.slug); let write_result = match std::fs::create_dir_all(&target_path) { Ok(_) => std::fs::write( &target_path.join("index.html"), views::recipe(&recipe).into_string(), ), - Err(_) => return Err(format!("Failed to write {}", recipe.title)), + Err(_) => return Err(format!("Failed to write {}", recipe.slug)), }; match write_result { Ok(_) => println!("Wrote {}", target_path.to_str().unwrap()), Err(_) => { - println!("Failed to write {}", recipe.title); - return Err(format!("Failed to write {}", recipe.title)); + println!("Failed to write {}", recipe.slug); + return Err(format!("Failed to write {}", recipe.slug)); } }; } diff --git a/src/main.rs b/src/main.rs index 5584815..d52382c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,7 @@ -use std::{fs, path::PathBuf}; +use std::{ + fs::{self, DirEntry}, + path::PathBuf, +}; use compiler::Compiler; use comrak::{markdown_to_html, Options}; @@ -14,6 +17,7 @@ mod views; #[derive(Clone, PartialEq, PartialOrd)] pub struct Recipe { _path: PathBuf, + slug: String, title: String, content: String, } @@ -30,26 +34,32 @@ impl Recipe { impl Eq for Recipe {} impl Ord for Recipe { fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.title.cmp(&other.title) + self.slug.cmp(&other.slug) } } +fn map_to_recipe(entry: DirEntry) -> Recipe { + let slug = entry + .file_name() + .to_str() + .unwrap() + .to_string() + .replace(".md", ""); + let title = slug.replace("-", " "); + + Recipe { + _path: entry.path(), + slug, + title, + content: fs::read_to_string(entry.path()).unwrap_or(String::from("")), + } +} fn get_recipes(path: String) -> Result, String> { let dir = fs::read_dir(path); + let entries = dir .unwrap() - .map(|res| { - res.map(|e| Recipe { - _path: e.path(), - title: e - .file_name() - .to_str() - .unwrap() - .to_string() - .replace(".md", ""), - content: fs::read_to_string(e.path()).unwrap_or(String::from("")), - }) - }) + .map(|res| res.map(map_to_recipe)) .collect::, std::io::Error>>(); return match entries { Ok(recipes) => Ok(recipes), diff --git a/src/views.rs b/src/views.rs index 1c040fa..c65be4c 100644 --- a/src/views.rs +++ b/src/views.rs @@ -49,16 +49,16 @@ pub fn index(recipes: Vec) -> Markup { h2 class="text-xl mb-2 text-teal-900 font-bold" { "Our Recipes." } ul class="ml-1" { @for recipe in sorted(recipes) { - li { a class="text-teal-600 font-bold" href=(format!("/recipes/{}", recipe.title)) { (recipe.title) } } + li { a class="text-teal-600 font-bold" href=(format!("/recipes/{}", recipe.slug)) { (recipe.title) } } } } } } - section class="mt-24 p-24 w-full bg-teal-900" { + section class="mt-24 py-8 md:p-24 w-full bg-teal-900" { div class="mx-8 text-teal-50" { h2 class="text-teal-50 text-xl" { "Why?" } - p class="max-w-xl text-sm" { "Each week I'm asked \"What should we have for dinner this week?\". This question stumps me. \"But it's okay\" I think to myself, \"I can Google it!\" But doing so only results in disappointment as I fight ads, popups and woefully slow websites. This website is an attempt to capture the recipes we enjoy and share them without any of the nonsense that comes with mainstream recipe websites." } + p class="max-w-full text-justify md:max-w-xl text-sm" { "Each week I'm asked \"What should we have for dinner this week?\". This question stumps me. \"But it's okay\" I think to myself, \"I can Google it!\" But doing so only results in disappointment as I fight ads, popups and woefully slow websites. This website is an attempt to capture the recipes we enjoy and share them without any of the nonsense that comes with mainstream recipe websites." } } } })