better structure of code
All checks were successful
CI/CD Pipeline / deploy-main (push) Successful in 5m34s

This commit is contained in:
philipp 2024-02-20 12:34:36 +01:00
parent 97c75a0253
commit c93bd01b76
7 changed files with 155 additions and 135 deletions

14
Cargo.lock generated
View File

@ -2,13 +2,6 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "a"
version = "0.1.0"
dependencies = [
"risp",
]
[[package]] [[package]]
name = "adler" name = "adler"
version = "1.0.2" version = "1.0.2"
@ -891,6 +884,13 @@ dependencies = [
"rustls-pki-types", "rustls-pki-types",
] ]
[[package]]
name = "website-risg"
version = "0.1.0"
dependencies = [
"risp",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

124
src/law.rs Normal file
View File

@ -0,0 +1,124 @@
use std::fs;
use risp::law::{Content, Heading, HeadingContent, Law, Section};
fn print_content(content: Content) -> String {
let mut ret = String::new();
match content {
Content::Text(t) => ret.push_str(&format!("<span class='content'>{t}</span>")),
Content::List(l) => {
ret.push_str("<ul class='content list'>");
for item in l {
if let Content::List(_) = item {
ret.push_str(&print_content(item));
} else {
ret.push_str("<li>");
ret.push_str(&print_content(item));
ret.push_str("</li>");
}
}
ret.push_str("</ul>");
}
Content::Multi(l) => {
ret.push_str("<div class='multi'>");
for item in l {
ret.push_str(&print_content(item));
}
ret.push_str("</div>");
}
}
ret
}
fn print_paragraph(section: Section) -> String {
let mut ret = String::new();
ret.push_str("<div class='par'>");
ret.push_str(&format!("<span class='symb'>{}</span>", section.symb));
if let Some(par_header) = section.par_header {
ret.push_str(&format!("<span class='header'>{}</span>", par_header));
}
if let Some(note) = section.par_note {
ret.push_str(&format!("<span class='note'>Beachte: {}</span>", note));
}
ret.push_str(&print_content(section.content));
ret.push_str("</div>");
ret
}
fn print_header(header: Heading, level: usize) -> String {
let mut ret = String::new();
let mut header_title = header.name.clone();
if let Some(desc) = header.desc {
header_title.push_str(&format!(" ({desc})"))
}
ret.push_str(&format!(
"<details open='open'>\
<summary class='sticky'><h{0}>{1}</h{0}></summary>\n\
<div>",
level + 2,
header_title
));
match header.content {
HeadingContent::Paragraph(p) => {
for section in p {
ret.push_str(&print_paragraph(section));
}
}
HeadingContent::Heading(subheaders) => {
for subheader in subheaders {
ret.push_str(&print_header(subheader, level + 1));
}
}
}
ret.push_str("</div></details>");
ret
}
fn get_content(config_path: &str) -> (String, String) {
let law = Law::from_config(config_path).unwrap();
let lawname = law.name;
let mut ret = String::new();
for h in law.header {
ret.push_str(&print_header(h, 0));
}
(lawname, ret)
}
/// Reads configs in `./laws` and creates a `<lawname>.html` in the `output` folder.
pub(crate) fn create_law_files() -> String {
let mut configs: Vec<_> = fs::read_dir("./laws")
.unwrap()
.map(|r| r.unwrap())
.collect();
configs.sort_by_key(|dir| dir.path()); // Order by name
//
let mut li_of_files = String::new();
for config in configs {
let filename = config.file_name().into_string().unwrap();
//TODO: use proper logic...
let law_name = filename.replace(".toml", "");
let path = format!("{}", config.path().display());
let (lawname, content) = get_content(&path);
let template = fs::read_to_string("templates/law.html").unwrap();
let site = template
.replace("{{content}}", &content)
.replace("{{title}}", &lawname);
li_of_files.push_str(&format!(
"<li><a href='./{law_name}' title='{law_name}' class='contrast'>{lawname}</a></li>"
));
fs::write(&format!("output/{law_name}.html"), &site).expect("Unable to write file");
}
li_of_files
}

View File

@ -1,138 +1,34 @@
use std::fs; use std::{fs, path::Path};
use risp::law::{Content, Heading, HeadingContent, Law, Section}; mod law;
fn print_content(content: Content) -> String {
let mut ret = String::new();
match content {
Content::Text(t) => ret.push_str(&format!("<span class='content'>{t}</span>")),
Content::List(l) => {
ret.push_str("<ul class='content list'>");
for item in l {
if let Content::List(_) = item {
ret.push_str(&print_content(item));
} else {
ret.push_str("<li>");
ret.push_str(&print_content(item));
ret.push_str("</li>");
}
}
ret.push_str("</ul>");
}
Content::Multi(l) => {
ret.push_str("<div class='multi'>");
for item in l {
ret.push_str(&print_content(item));
}
ret.push_str("</div>");
}
}
ret
}
fn print_paragraph(section: Section) -> String {
let mut ret = String::new();
ret.push_str("<div class='par'>");
ret.push_str(&format!("<span class='symb'>{}</span>", section.symb));
if let Some(par_header) = section.par_header {
ret.push_str(&format!("<span class='header'>{}</span>", par_header));
}
if let Some(note) = section.par_note {
ret.push_str(&format!("<span class='note'>Beachte: {}</span>", note));
}
ret.push_str(&print_content(section.content));
ret.push_str("</div>");
ret
}
fn print_header(header: Heading, level: usize) -> String {
let mut ret = String::new();
let mut header_title = header.name.clone();
if let Some(desc) = header.desc {
header_title.push_str(&format!(" ({desc})"))
}
ret.push_str(&format!(
"<details open='open'>\
<summary class='sticky'><h{0}>{1}</h{0}></summary>\n\
<div>",
level + 2,
header_title
));
match header.content {
HeadingContent::Paragraph(p) => {
for section in p {
ret.push_str(&print_paragraph(section));
}
}
HeadingContent::Heading(subheaders) => {
for subheader in subheaders {
ret.push_str(&print_header(subheader, level + 1));
}
}
}
ret.push_str("</div></details>");
ret
}
fn get_content(config_path: &str) -> (String, String) {
let law = Law::from_config(config_path).unwrap();
let lawname = law.name;
let mut ret = String::new();
for h in law.header {
ret.push_str(&print_header(h, 0));
}
(lawname, ret)
}
fn main() { fn main() {
let mut configs: Vec<_> = fs::read_dir("./laws")
.unwrap()
.map(|r| r.unwrap())
.collect();
configs.sort_by_key(|dir| dir.path());
fs::create_dir_all("./output").unwrap(); fs::create_dir_all("./output").unwrap();
let mut li_of_files = String::new(); let overview = law::create_law_files();
for config in configs { create_static_files();
let filename = config.file_name().into_string().unwrap();
//TODO: use proper logic...
let law_name = filename.replace(".toml", "");
let path = format!("{}", config.path().display()); create_index(&overview);
let (lawname, content) = get_content(&path);
let template = fs::read_to_string("templates/law.html").unwrap();
let site = template
.replace("{{content}}", &content)
.replace("{{title}}", &lawname);
li_of_files.push_str(&format!(
"<li><a href='./{law_name}' title='{law_name}' class='contrast'>{lawname}</a></li>"
));
fs::write(&format!("output/{law_name}.html"), &site).expect("Unable to write file");
} }
let style = fs::read_to_string("templates/style.css").unwrap();
fs::write("output/style.css", style).expect("Unable to write file");
let style = fs::read_to_string("templates/pico.min.css").unwrap();
fs::write("output/pico.min.css", style).expect("Unable to write file");
let js = fs::read_to_string("templates/app.js").unwrap();
fs::write("output/app.js", js).expect("Unable to write file");
let js = fs::read_to_string("templates/toggle.js").unwrap();
fs::write("output/toggle.js", js).expect("Unable to write file");
/// Creates the `index.html` file.
fn create_index(content: &str) {
let mut index = fs::read_to_string("templates/index.html").unwrap(); let mut index = fs::read_to_string("templates/index.html").unwrap();
index = index.replace("{{content}}", &li_of_files); index = index.replace("{{content}}", content);
fs::write("output/index.html", &index).expect("Unable to write file"); fs::write("output/index.html", &index).expect("Unable to write file");
} }
/// Copies all files from `templates/static` to `output`
fn create_static_files() {
let from = Path::new("templates/static");
let to = Path::new("output");
for entry in fs::read_dir(from).expect("No templates/static folder") {
let entry = entry.unwrap();
let path = entry.path();
if path.is_file() {
let to_path = to.join(entry.file_name());
fs::copy(&path, &to_path).unwrap();
}
}
}