diff --git a/example.json b/example.json index c54e4b950866c4016a446f0f90c44996a2a42030..f115628d692c0c24c00f6ec39d4ea21e76bdc400 100644 --- a/example.json +++ b/example.json @@ -11,29 +11,21 @@ }, "body": [ { - "layout": { - "display": "flex", - "padding": "20px" - }, - "contents": [ - { - "tag": "h1", - "text": "Pijar Story" - }, - { - "tag": "p", - "text": "Hello Pijar<br />Oui oui oui.", - "attrs": { - "class": "pijar_p_class" - } - }, - { - "tag": "img", - "attrs": { - "src": "/img/url.png" - } - } - ] + "tag": "h1", + "text": "Pijar Story" + }, + { + "tag": "p", + "text": "Hello Pijar<br />Oui oui oui.", + "attrs": { + "class": "pijar_p_class" + } + }, + { + "tag": "img", + "attrs": { + "src": "/img/url.png" + } } ], "sub_pages": [] diff --git a/src/testing.rs b/src/testing.rs index 74a98598f267436d3a3e5cecc3eb2efba33415b4..c9b4bdb8ef46e48d326e9b43f1eb74326a3502a5 100644 --- a/src/testing.rs +++ b/src/testing.rs @@ -14,16 +14,8 @@ pub const TEST_JSON_WEBSITE: &'static str = " }, \"body\": [ { - \"layout\": { - \"display\": \"flex\", - \"padding\": \"20px\" - }, - \"contents\": [ - { - \"tag\": \"h1\", - \"text\": \"testing\" - } - ] + \"tag\": \"h1\", + \"text\": \"testing\" } ], \"sub_pages\": [ @@ -40,16 +32,8 @@ pub const TEST_JSON_WEBSITE: &'static str = " }, \"body\": [ { - \"layout\": { - \"display\": \"flex\", - \"padding\": \"20px\" - }, - \"contents\": [ - { - \"tag\": \"h1\", - \"text\": \"testing subpage\" - } - ] + \"tag\": \"h1\", + \"text\": \"testing subpage\" } ], \"sub_pages\": [ @@ -65,17 +49,9 @@ pub const TEST_JSON_WEBSITE: &'static str = " \"lang\":\"en\" }, \"body\": [ - { - \"layout\": { - \"display\": \"flex\", - \"padding\": \"20px\" - }, - \"contents\": [ - { - \"tag\": \"h1\", - \"text\": \"testing nested\" - } - ] + { + \"tag\": \"h1\", + \"text\": \"testing nested\" } ] } @@ -97,13 +73,17 @@ pub const TEST_JSON_WEBSITE: &'static str = " \"display\": \"grid\" }, \"contents\": [ + { + \"tag\": \"nav\" + }, { \"tag\": \"nav\", - \"contents\": [] + \"attrs\": { + \"id\": \"page-body\" + } }, { - \"tag\": \"footer\", - \"contents\": [] + \"tag\": \"footer\" } ] } diff --git a/src/website/item.rs b/src/website/item.rs index 69a80e48061d7c8ee6260abc5d4159fe364122d6..3a59435a056a5aef10791d2bfddd3841eaf83fac 100644 --- a/src/website/item.rs +++ b/src/website/item.rs @@ -1,27 +1,6 @@ -use super::css::StyleSheet; use serde::{Deserialize, Serialize}; use std::collections::HashMap; -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct Item { - pub contents: Vec<HtmlElement>, - pub layout: StyleSheet, -} - -impl std::fmt::Display for Item { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!( - f, - "{}", - self.contents - .iter() - .map(|item_content| item_content.to_string()) - .collect::<Vec<String>>() - .join("") - ) - } -} - #[derive(Debug, Serialize, Deserialize, Clone)] pub struct HtmlAttributes(pub HashMap<String, String>); @@ -58,6 +37,12 @@ pub struct HtmlElement { pub attrs: HtmlAttributes, } +impl HtmlElement { + pub fn set_contents(&mut self, contents: Vec<HtmlElement>) { + self.contents = Some(contents); + } +} + impl std::fmt::Display for HtmlElement { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { let body = match &self.contents { @@ -79,7 +64,6 @@ impl std::fmt::Display for HtmlElement { #[cfg(test)] mod test_items { use super::*; - use std::collections::HashMap; #[test] fn text_item_content_to_string() { @@ -98,7 +82,7 @@ mod test_items { let mut attrs = HtmlAttributes::new(); attrs.0.insert(String::from("id"), String::from("some-id")); - let mut item_content = HtmlElement { + let item_content = HtmlElement { tag: String::from("p"), text: Some(String::from("Hello")), contents: None, @@ -106,16 +90,6 @@ mod test_items { }; assert_eq!(item_content.to_string(), "<p id=\"some-id\">Hello</p>"); - - item_content - .attrs - .0 - .insert(String::from("class"), String::from("some-class")); - - assert_eq!( - item_content.to_string(), - "<p id=\"some-id\" class=\"some-class\">Hello</p>" - ); } #[test] @@ -145,27 +119,4 @@ mod test_items { "<p><span>Hello </span><b>World</b></p>" ) } - - #[test] - fn item_to_string() { - let item = Item { - layout: StyleSheet(HashMap::new()), - contents: vec![ - HtmlElement { - tag: String::from("span"), - text: Some(String::from("Hello ")), - contents: None, - attrs: HtmlAttributes::new(), - }, - HtmlElement { - tag: String::from("b"), - text: Some(String::from("World")), - contents: None, - attrs: HtmlAttributes::new(), - }, - ], - }; - - assert_eq!(item.to_string(), "<span>Hello </span><b>World</b>") - } } diff --git a/src/website/page.rs b/src/website/page.rs index 424f95420d22888fd536a32d7aaea3f9d195a507..68f47a9442659d7da201ea114041a766778a3ff5 100644 --- a/src/website/page.rs +++ b/src/website/page.rs @@ -23,7 +23,7 @@ pub struct PageTemplate { } #[derive(Debug, Serialize, Deserialize, Clone)] -pub struct PageBody(Vec<Item>); +pub struct PageBody(Vec<HtmlElement>); impl std::fmt::Display for PageBody { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { @@ -59,13 +59,13 @@ impl Page { let mut body = template.clone(); self.template = Some(template); let err = "Couldn't find page-body placeholder"; - let body_container = body - .contents - .iter() - .find(|el| el.attrs.get("id").expect(err).eq("page-body")) - .expect(err); - // HINT use HashMap::extend() to insert many values - // TODO concat template with page body (template should have a page body placeholder) + body.contents + .iter_mut() + .find(|el| el.attrs.get("id").unwrap_or(&String::new()).eq("page-body")) + .expect(err) + .set_contents(self.body.0.clone()); + + self.body.0 = body.contents; } pub fn text_from_key(&self, key: String) -> String { diff --git a/templates/new_website.json b/templates/new_website.json index 58d9d3d9890c5437d0e2b9d36028aaa0b64054da..29706c5138285bb9fb31780c74ed9d6fc571e41d 100644 --- a/templates/new_website.json +++ b/templates/new_website.json @@ -11,13 +11,8 @@ }, "body": [ { - "layout": {}, - "contents": [ - { - "tag": "h1", - "text": "New website" - } - ] + "tag": "h1", + "text": "New website" } ] },