Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
Small tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ingalless committed May 3, 2024
1 parent 663fde8 commit d44218a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};
}
Expand Down
38 changes: 24 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -14,6 +17,7 @@ mod views;
#[derive(Clone, PartialEq, PartialOrd)]
pub struct Recipe {
_path: PathBuf,
slug: String,
title: String,
content: String,
}
Expand All @@ -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<Vec<Recipe>, 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::<Result<Vec<_>, std::io::Error>>();
return match entries {
Ok(recipes) => Ok(recipes),
Expand Down
6 changes: 3 additions & 3 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ pub fn index(recipes: Vec<Recipe>) -> 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." }
}
}
})
Expand Down

0 comments on commit d44218a

Please sign in to comment.