Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 515b638a authored by peterrabbit's avatar peterrabbit
Browse files

wip page body

parent 5d479ca1
No related branches found
No related tags found
No related merge requests found
......@@ -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": []
......
......@@ -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\"
}
]
}
......
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>")
}
}
......@@ -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 {
......
......@@ -11,13 +11,8 @@
},
"body": [
{
"layout": {},
"contents": [
{
"tag": "h1",
"text": "New website"
}
]
"tag": "h1",
"text": "New website"
}
]
},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment