From 8b8218a37e239c438e5b5f6579d64ecc742c8fcb Mon Sep 17 00:00:00 2001 From: Pierre Jarriges <pierre.jarriges@tutanota.com> Date: Fri, 2 Sep 2022 10:38:13 +0200 Subject: [PATCH] license&readme + template container element --- README.md | 129 +++++++++++++++++++++++++++++++++++++ default_static/style.css | 20 ++++++ example.json | 49 ++++++++------ src/website/page.rs | 95 +++++++++++++++++---------- templates/new_website.json | 34 ++++++---- 5 files changed, 259 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index e69de29..95a4ac6 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,129 @@ +# Krustacea +### *Create, serve and manage websites* ### + ------------------------------------------------ + |------- Developped by Kuadrado Software ------| + ------------------------------------------------ +_______ +Krustacea is a package that contains a web server written in Rust (Actix based) and a web application to build a static website. + +The web server loads a website as a single JSON document, the html contents is built in memory by the server at start, and the website is served without the need of any static html file. It also handles the necessary static files such as images, or additional source code like css or javascript. + +Modifications to the website object will be done through the use of a REST API and the web application on client side. + +_________________ + +## **/!\\** WIP ## +This package is a work in progress I have just begun. +More features and documentation should become visible soon. +_______ +## CLI Syntax +``` +krustacea [OPTIONS] +``` +### Options +- `-c` | `--ctx` : The execution context. Can be `debug` (DEFAULT) or `production`. +- `-d` | `--dir` : Any Linux directory path to hold the Krustacea static files. DEFAULT : In a `debug` context, the `$HOME` directory will be used (i.e. a krusteacea directory will be created in the HOME dir and krustacea will use it to store static assets, backups, config files, etc). In `production` context, the `/var` directory will be used. +- `--load` [OPTIONAL]: The path to the website to load as a JSON file. If no file is provided, a new website will be created from a blank template. +- `-h` | `--host` : The host name. DEFAULT: `localhost`. +- `-p` | `--port` : The HTTP port to use. DEFAULT: `8080` +- `--ptls` : The TLS port to use. DEFAULT: `8443` +- `--certs_dir`: The directory containing the SSL certificates. DEFAULT : `/etc/letsencrypt/live`. (The host name will be joined to this path to find the right certificates) + +___________ +## Website data +A website data is store as a JSON file with the following structure +```json +// example.json +{ + "root_page": { + "template_name": "A Custom Template", + "metadata": { + "title": "Hello Krustcea !", + "description": "An example website", + "image": "https://example.com/static/images/ex_pic.png", + "css": [], + "js": [], + "url_slug": "", + "lang": "en" + }, + "body": [ + { + "tag": "h1", + "text": "Example Story" + }, + { + "tag": "p", + "text": "Hello,<br />How are you?", + "attrs": { + "class": "p_class" + } + }, + { + "tag": "img", + "attrs": { + "src": "/img/url.png" + } + } + ], + "sub_pages": [] + }, + "templates": [ + { + "name": "Custom template", + "layout": { + "display": "grid" + }, + "contents": [ + { + "tag": "div", + "attrs": { + "id": "custom-template", + "class":"page-template" + }, + "contents": [ + { + "tag": "nav", + "contents": [ + { + "tag": "ul", + "contents": [ + { + "tag": "li", + "text": "menu item 1" + }, + { + "tag": "li", + "text": "menu item 2" + } + ] + } + ] + }, + { + "tag": "footer", + "contents": [] + } + ] + } + ] + } + ], + "assets_index": { + "images": [ + "/static/images/toto.jpg" + ], + "sounds": [ + "/static/sounds/toto.mp3" + ], + "video": [ + "/static/video/toto.mp4" + ], + "docs": [ + "/static/docs/toto.xcf" + ], + "source_code": [ + "/static/source_code/toto.js" + ] + } +} +``` \ No newline at end of file diff --git a/default_static/style.css b/default_static/style.css index f1924b0..dc3fbfe 100644 --- a/default_static/style.css +++ b/default_static/style.css @@ -5,4 +5,24 @@ body { margin: 0; +} + +#nav-content-footer-template { + display: flex; + flex-direction: column; + min-height: 100vh; +} + +#nav-content-footer-template>nav { + height: 100px; + background-color: #abc; +} + +#nav-content-footer-template>#page-body { + flex: 1; +} + +#nav-content-footer-template>footer { + height: 200px; + background-color: #679; } \ No newline at end of file diff --git a/example.json b/example.json index 933668a..d74a158 100644 --- a/example.json +++ b/example.json @@ -1,10 +1,10 @@ { "root_page": { - "template_name": "Pijar Custom Template", + "template_name": "A Custom Template", "metadata": { - "title": "Hello Pijar !", - "description": "A website for Pijar", - "image": "https://pijar.com/static/images/pijar_pic.png", + "title": "Hello Krustcea !", + "description": "An example website", + "image": "https://example.com/static/images/ex_pic.png", "css": [], "js": [], "url_slug": "", @@ -13,13 +13,13 @@ "body": [ { "tag": "h1", - "text": "Pijar Story" + "text": "Example Story" }, { "tag": "p", - "text": "Hello Pijar<br />Oui oui oui.", + "text": "Hello,<br />How are you?", "attrs": { - "class": "pijar_p_class" + "class": "p_class" } }, { @@ -33,32 +33,41 @@ }, "templates": [ { - "name": "Pijar Custom template", + "name": "Custom template", "layout": { "display": "grid" }, "contents": [ { - "tag": "nav", + "tag": "div", + "attrs": { + "id": "custom-template", + "class": "page-template" + }, "contents": [ { - "tag": "ul", + "tag": "nav", "contents": [ { - "tag": "li", - "text": "menu item 1" - }, - { - "tag": "li", - "text": "menu item 2" + "tag": "ul", + "contents": [ + { + "tag": "li", + "text": "menu item 1" + }, + { + "tag": "li", + "text": "menu item 2" + } + ] } ] + }, + { + "tag": "footer", + "contents": [] } ] - }, - { - "tag": "footer", - "contents": [] } ] } diff --git a/src/website/page.rs b/src/website/page.rs index 54771e1..95ff31e 100644 --- a/src/website/page.rs +++ b/src/website/page.rs @@ -25,6 +25,24 @@ pub struct PageTemplate { pub contents: Vec<HtmlElement>, } +impl PageTemplate { + pub fn get_page_body_placeholder<'a>( + elements: &'a mut Vec<HtmlElement>, + ) -> Result<&'a mut HtmlElement, ()> { + for el in elements.iter_mut() { + if el.attrs.get("id").unwrap_or(&String::new()).eq("page-body") { + return Ok(el); + } else if let Some(children) = &mut el.contents { + if let Ok(found) = Self::get_page_body_placeholder(children) { + return Ok(found); + } + } + } + + return Err(()); + } +} + #[derive(Debug, Serialize, Deserialize, Clone)] pub struct PageBody(Vec<HtmlElement>); @@ -176,16 +194,11 @@ impl Page { pub fn build_with_template(&mut self, template: PageTemplate) { let mut complete_body = template.clone(); self.template = Some(template); - let err = "Couldn't find page-body placeholder"; - - // Insert page body inside the template page-body container. - complete_body - .contents - .iter_mut() - .find(|el| el.attrs.get("id").unwrap_or(&String::new()).eq("page-body")) - .expect(err) + + PageTemplate::get_page_body_placeholder(&mut complete_body.contents) + .expect("Couldn't find page body container in template") .set_contents(self.body.0.clone()); - // Then replace the page body by the complete template + self.body.0 = complete_body.contents; } @@ -240,32 +253,46 @@ mod test_pages { PageTemplate { layout: StyleSheet(HashMap::new()), name: String::from("test template"), - contents: vec![ - HtmlElement { - tag: String::from("nav"), - attrs: HtmlAttributes::new(), - contents: None, - text: Some(String::from("NAV")), + contents: vec![HtmlElement { + tag: String::from("div"), + attrs: { + let mut attrs = HtmlAttributes::new(); + attrs + .0 + .insert(String::from("id"), String::from("test-template")); + attrs + .0 + .insert(String::from("class"), String::from("page-template")); + attrs }, - HtmlElement { - tag: String::from("div"), - attrs: { - let mut attrs = HtmlAttributes::new(); - attrs - .0 - .insert(String::from("id"), String::from("page-body")); - attrs + contents: Some(vec![ + HtmlElement { + tag: String::from("nav"), + attrs: HtmlAttributes::new(), + contents: None, + text: Some(String::from("NAV")), }, - contents: None, - text: None, - }, - HtmlElement { - tag: String::from("footer"), - attrs: HtmlAttributes::new(), - contents: None, - text: Some(String::from("FOOTER")), - }, - ], + HtmlElement { + tag: String::from("div"), + attrs: { + let mut attrs = HtmlAttributes::new(); + attrs + .0 + .insert(String::from("id"), String::from("page-body")); + attrs + }, + contents: None, + text: None, + }, + HtmlElement { + tag: String::from("footer"), + attrs: HtmlAttributes::new(), + contents: None, + text: Some(String::from("FOOTER")), + }, + ]), + text: None, + }], } } @@ -354,7 +381,7 @@ mod test_pages { page.build_with_template(test_page_template()); assert_eq!( page.body.to_string(), - "<nav>NAV</nav><div id=\"page-body\"><span>TEST</span></div><footer>FOOTER</footer>" + "<div id=\"test-template\" class=\"page-template\"><nav>NAV</nav><div id=\"page-body\"><span>TEST</span></div><footer>FOOTER</footer></div>" ) } } diff --git a/templates/new_website.json b/templates/new_website.json index 417fea9..392249b 100644 --- a/templates/new_website.json +++ b/templates/new_website.json @@ -1,14 +1,11 @@ { "root_page": { - "template_name": "basic", + "template_name": "Nav Content Footer", "metadata": { "title": "New Website", "description": "A new website", "url_slug": "", - "lang": "en", - "image": [ - "https://example.com/image.png" - ] + "lang": "en" }, "body": [ { @@ -19,20 +16,29 @@ }, "templates": [ { - "name": "basic", + "name": "Nav Content Footer", "layout": {}, "contents": [ - { - "tag": "nav" - }, { "tag": "div", "attrs": { - "id": "page-body" - } - }, - { - "tag": "footer" + "id": "nav-content-footer-template", + "class": "page-template" + }, + "contents": [ + { + "tag": "nav" + }, + { + "tag": "div", + "attrs": { + "id": "page-body" + } + }, + { + "tag": "footer" + } + ] } ] } -- GitLab