Create page for Recipe
This commit is contained in:
parent
b4412c00ad
commit
9f8080ff21
|
@ -2,6 +2,7 @@ use uuid::Uuid;
|
|||
use chrono::prelude::*;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use build_html::{self, Html, HtmlContainer, ContainerType, Container, HtmlPage};
|
||||
use std::fmt;
|
||||
|
||||
use crate::io::write_html;
|
||||
|
||||
|
@ -18,6 +19,21 @@ pub enum TimeUnits {
|
|||
Seconds,
|
||||
}
|
||||
|
||||
impl fmt::Display for TimeUnits {
|
||||
// This trait requires `fmt` with this exact signature.
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// Write strictly the first element into the supplied output
|
||||
// stream: `f`. Returns `fmt::Result` which indicates whether the
|
||||
// operation succeeded or failed. Note that `write!` uses syntax which
|
||||
// is very similar to `println!`.
|
||||
match &self {
|
||||
TimeUnits::Hours => write!(f, "hr."),
|
||||
TimeUnits::Minutes => write!(f, "min."),
|
||||
TimeUnits::Seconds => write!(f, "sec."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub enum MeasurementUnits {
|
||||
Grams,
|
||||
|
@ -34,6 +50,32 @@ pub enum MeasurementUnits {
|
|||
Teaspoons,
|
||||
}
|
||||
|
||||
type mu = MeasurementUnits;
|
||||
|
||||
impl fmt::Display for MeasurementUnits {
|
||||
// This trait requires `fmt` with this exact signature.
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// Write strictly the first element into the supplied output
|
||||
// stream: `f`. Returns `fmt::Result` which indicates whether the
|
||||
// operation succeeded or failed. Note that `write!` uses syntax which
|
||||
// is very similar to `println!`.
|
||||
match &self {
|
||||
mu::Grams => write!(f, "g"),
|
||||
mu::Kilograms => write!(f, "kg"),
|
||||
mu::Pounds => write!(f, "lbs"),
|
||||
mu::Litres => write!(f, "L"),
|
||||
mu::Millilitres => write!(f, "mL"),
|
||||
mu::Gallons => write!(f, "gal"),
|
||||
mu::Ounces => write!(f, "oz"),
|
||||
mu::Pinches => write!(f, "pinch(es)"),
|
||||
mu::Drops => write!(f, "drop(s)"),
|
||||
mu::Cups => write!(f, "cup(s)"),
|
||||
mu::Tablespoons => write!(f, "tablespoon(s)"),
|
||||
mu::Teaspoons => write!(f, "teaspoon(s)"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
||||
pub struct Tag(pub char, pub String);
|
||||
|
||||
|
@ -129,7 +171,61 @@ impl Recipe {
|
|||
}
|
||||
|
||||
pub fn construct_page(&self) {
|
||||
let mut recipe_page = build_html::HtmlPage::new()
|
||||
// Metadata
|
||||
let meta = Container::new(ContainerType::UnorderedList)
|
||||
.with_attributes(vec![("class", "recipe")])
|
||||
.with_paragraph(format!("<b>⏲️ Preparation time: </b> {} {}", &self.prep_time.0, &self.prep_time.1))
|
||||
.with_paragraph(format!("<b>🍳 Cooking time:</b> {} {}", &self.cooking_time.0, &self.cooking_time.1))
|
||||
.with_paragraph(format!("<b>🍽️ Servings:</b> {}", &self.servings));
|
||||
|
||||
// Ingredients
|
||||
let mut ingredients_container = Container::new(ContainerType::UnorderedList)
|
||||
.with_attributes(vec![("class", "recipe")]);
|
||||
for i in &self.ingredients {
|
||||
let ingredient = format!("{} {} {}", &i.0, &i.1, &i.2);
|
||||
ingredients_container.add_paragraph(ingredient);
|
||||
}
|
||||
|
||||
// Directions
|
||||
let mut directions_container = Container::new(ContainerType::OrderedList)
|
||||
.with_attributes(vec![("class", "recipe")]);
|
||||
for n in &self.directions {
|
||||
directions_container.add_paragraph(&n.1);
|
||||
}
|
||||
|
||||
// Tags
|
||||
let mut tags_html = String::new();
|
||||
tags_html.push_str(r#"<p><b>Tags: </b><i>"#);
|
||||
match &self.tags {
|
||||
Some(list) => {
|
||||
for x in 0..list.len() {
|
||||
if x != list.len()-1 {
|
||||
tags_html.push_str(format!("<a href='/tags/{}'>#{}</a>,", list[x].1, list[x].1).as_str());
|
||||
} else {
|
||||
tags_html.push_str(format!("<a href='/tags/{}'>#{}</a>", list[x].1, list[x].1).as_str());
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
tags_html.push_str(r#"no tags!! :o"#);
|
||||
},
|
||||
}
|
||||
tags_html.push_str(r#"</i></p>"#);
|
||||
|
||||
// Date
|
||||
let edit_date = match &self.edited_date {
|
||||
Some(date) => {
|
||||
format!(
|
||||
"<p><b>Last edited on: </b><i>{}-{}-{}</i></p>",
|
||||
date.year(),
|
||||
date.month(),
|
||||
date.day())
|
||||
},
|
||||
None => "<p><i>No edits</o></p>".to_string(),
|
||||
};
|
||||
|
||||
// Construct Main Page
|
||||
let recipe_page = build_html::HtmlPage::new()
|
||||
.with_head_link("/favicon.ico", "icon") // Favicon
|
||||
.with_stylesheet("/style.css") // Link stylesheet
|
||||
.with_meta(vec![("charset", "UTF-8")])
|
||||
|
@ -137,52 +233,21 @@ impl Recipe {
|
|||
.with_meta(vec![("name", "description"), ("content", DESCRIPTION)]) // Add the description
|
||||
.with_title(TITLE)
|
||||
.with_header(1, &self.name)
|
||||
.with_raw(r#"<hr>"#);
|
||||
|
||||
// Metadata
|
||||
let meta = Container::new(ContainerType::UnorderedList)
|
||||
.with_attributes(vec![("class", "recipe")])
|
||||
.with_paragraph(format!("⏲️ Preparation time: {} {:?}", &self.prep_time.0, &self.prep_time.1))
|
||||
.with_paragraph(format!("🍳 Cooking time: {} {:?}", &self.cooking_time.0, &self.cooking_time.1))
|
||||
.with_paragraph(format!("🍽️ Servings: {}", &self.servings));
|
||||
recipe_page.add_container(meta);
|
||||
|
||||
// Append ingredients
|
||||
recipe_page.add_header(2, r#"Ingredients:"#);
|
||||
let mut ingredients_container = Container::new(ContainerType::UnorderedList)
|
||||
.with_attributes(vec![("class", "recipe")]);
|
||||
for i in &self.ingredients {
|
||||
let ingredient = format!("{:?} {:?} {}", &i.0, &i.1, &i.2);
|
||||
ingredients_container.add_paragraph(ingredient);
|
||||
}
|
||||
recipe_page.add_container(ingredients_container);
|
||||
|
||||
// Append directions
|
||||
recipe_page.add_header(2, r#"Directions:"#);
|
||||
let mut directions_container = Container::new(ContainerType::OrderedList)
|
||||
.with_attributes(vec![("class", "recipe")]);
|
||||
for n in &self.directions {
|
||||
directions_container.add_paragraph(&n.1);
|
||||
}
|
||||
recipe_page.add_container(directions_container);
|
||||
|
||||
recipe_page.add_header(2, r#"Author:"#);
|
||||
recipe_page.add_paragraph(&self.attribution);
|
||||
|
||||
// Append tags
|
||||
let mut tags_html = String::new();
|
||||
tags_html.push_str(r#"<i><p>Tags: "#);
|
||||
match &self.tags {
|
||||
Some(list) => {
|
||||
for x in list {
|
||||
tags_html.push_str(format!("<a href='/tags/{}'>#{}</a>,", x.1, x.1).as_str());
|
||||
}
|
||||
},
|
||||
None => {
|
||||
tags_html.push_str(r#"no tags!! :o"#);
|
||||
},
|
||||
}
|
||||
|
||||
.with_raw(r#"<hr>"#)
|
||||
.with_container(meta)
|
||||
.with_header(2, r#"Ingredients:"#)
|
||||
.with_container(ingredients_container)
|
||||
.with_header(2, r#"Directions:"#)
|
||||
.with_container(directions_container)
|
||||
.with_header(2, r#"Misc."#)
|
||||
.with_raw(format!("<p><b>Author: </b>{}</p>", &self.attribution).as_str())
|
||||
.with_raw(tags_html)
|
||||
.with_raw(format!(
|
||||
"<p><b>Recipe added on: </b><i>{}-{}-{}</i></p>",
|
||||
&self.posted_date.year(),
|
||||
&self.posted_date.month(),
|
||||
&self.posted_date.day()))
|
||||
.with_raw(edit_date);
|
||||
|
||||
write_html(recipe_page.to_html_string(), "en", Some(&self.shortcode));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue