From 4a6c986a661eed98c684a3c93a22fefb555b2f26 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 20 Feb 2024 13:03:06 +0100 Subject: [PATCH] add part function --- src/law.rs | 4 +++ src/main.rs | 4 +++ src/part.rs | 33 +++++++++++++++++++++ templates/index.html | 58 ++----------------------------------- templates/law.html | 58 ++----------------------------------- templates/parts/footer.html | 31 ++++++++++++++++++++ templates/parts/head.html | 16 ++++++++++ templates/parts/header.html | 8 +++++ 8 files changed, 102 insertions(+), 110 deletions(-) create mode 100644 src/part.rs create mode 100644 templates/parts/footer.html create mode 100644 templates/parts/head.html create mode 100644 templates/parts/header.html diff --git a/src/law.rs b/src/law.rs index 2975700..2afa837 100644 --- a/src/law.rs +++ b/src/law.rs @@ -2,6 +2,8 @@ use std::fs; use risp::law::{Content, Heading, HeadingContent, Law, Section}; +use crate::part::Parts; + fn print_content(content: Content) -> String { let mut ret = String::new(); @@ -114,6 +116,8 @@ pub(crate) fn create_law_files() -> String { .replace("{{content}}", &content) .replace("{{title}}", &lawname); + let site = Parts::new().perform(site); + li_of_files.push_str(&format!( "
  • {lawname}
  • " )); diff --git a/src/main.rs b/src/main.rs index f80a46c..18a2106 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,9 @@ use std::{fs, path::Path}; +use part::Parts; + mod law; +mod part; fn main() { fs::create_dir_all("./output").unwrap(); @@ -15,6 +18,7 @@ fn main() { fn create_index(content: &str) { let mut index = fs::read_to_string("templates/index.html").unwrap(); index = index.replace("{{content}}", content); + let index = Parts::new().perform(index); fs::write("output/index.html", &index).expect("Unable to write file"); } diff --git a/src/part.rs b/src/part.rs new file mode 100644 index 0000000..5214d62 --- /dev/null +++ b/src/part.rs @@ -0,0 +1,33 @@ +use std::{collections::HashMap, fs, path::Path}; + +pub(crate) struct Parts { + parts: HashMap, +} + +impl Parts { + pub(crate) fn new() -> Self { + let mut parts = HashMap::new(); + let parts_path = Path::new("templates/parts"); + for part in fs::read_dir(parts_path).expect("No templates/parts folder") { + let part = part.unwrap(); + let filename = format!("{}", part.file_name().into_string().unwrap()); + let filename = filename.replace(".html", ""); + let path = format!("{}", part.path().display()); + + let template = fs::read_to_string(path).unwrap(); + + parts.insert(filename, template); + } + Self { parts } + } + + pub(crate) fn perform(&self, input: String) -> String { + let mut input = input; + for (key, value) in &self.parts { + let replace = format!("{{{{{}}}}}", key); + println!("{replace}"); + input = input.replace(&replace, value); + } + input + } +} diff --git a/templates/index.html b/templates/index.html index 70e8082..4027a86 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,65 +1,13 @@ - - - - RIS Parser - - - - - - - - - - - - + {{head}} -
    - -
    + {{header}}
      {{content}}
    - + {{footer}} diff --git a/templates/law.html b/templates/law.html index 0a4a8fe..9439a64 100644 --- a/templates/law.html +++ b/templates/law.html @@ -1,29 +1,7 @@ - - - - RIS Parser - - - - - - - - - - - - + {{head}} -
    - -
    + {{header}}
    @@ -32,37 +10,7 @@

    {{title}}

    {{content}}
    - + {{footer}} diff --git a/templates/parts/footer.html b/templates/parts/footer.html new file mode 100644 index 0000000..e2cb8a4 --- /dev/null +++ b/templates/parts/footer.html @@ -0,0 +1,31 @@ + diff --git a/templates/parts/head.html b/templates/parts/head.html new file mode 100644 index 0000000..cf573db --- /dev/null +++ b/templates/parts/head.html @@ -0,0 +1,16 @@ + + + + RIS Parser + + + + + + + + + + + + diff --git a/templates/parts/header.html b/templates/parts/header.html new file mode 100644 index 0000000..2b6e049 --- /dev/null +++ b/templates/parts/header.html @@ -0,0 +1,8 @@ +
    + +