website-risg/src/main.rs

117 lines
3.6 KiB
Rust
Raw Normal View History

2024-02-17 09:44:08 +01:00
use std::fs;
2024-02-16 12:13:24 +01:00
2024-02-17 09:44:08 +01:00
use risp::law::{Content, Heading, HeadingContent, Law, Section};
fn print_content(content: Content) -> String {
let mut ret = String::new();
match content {
2024-02-17 11:00:55 +01:00
Content::Text(t) => ret.push_str(&format!("<span class='content'>{t}</span>")),
2024-02-17 09:44:08 +01:00
Content::Item(l) | Content::List(l) => {
2024-02-17 10:55:38 +01:00
ret.push_str("<ul class='content list'>");
2024-02-17 09:44:08 +01:00
for item in l {
2024-02-17 10:59:18 +01:00
ret.push_str("<li>");
2024-02-17 09:44:08 +01:00
ret.push_str(&print_content(item));
2024-02-17 10:59:18 +01:00
ret.push_str("</li>");
2024-02-17 09:44:08 +01:00
}
2024-02-17 10:55:38 +01:00
ret.push_str("</ul>");
2024-02-17 09:44:08 +01:00
}
}
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();
2024-02-16 12:13:24 +01:00
let mut header_title = header.name.clone();
if let Some(desc) = header.desc {
header_title.push_str(&format!(" ({desc})"))
}
2024-02-17 09:44:08 +01:00
ret.push_str(&format!(
"<details>\
<summary><h{0}>{1}</h{0}></summary>\n\
2024-02-17 10:22:22 +01:00
<div>",
2024-02-17 09:44:08 +01:00
level + 2,
header_title
));
2024-02-16 12:13:24 +01:00
match header.content {
2024-02-17 09:44:08 +01:00
HeadingContent::Paragraph(p) => {
for section in p {
ret.push_str(&print_paragraph(section));
}
}
2024-02-16 12:13:24 +01:00
HeadingContent::Heading(subheaders) => {
for subheader in subheaders {
2024-02-17 09:44:08 +01:00
ret.push_str(&print_header(subheader, level + 1));
2024-02-16 12:13:24 +01:00
}
}
}
2024-02-17 09:44:08 +01:00
2024-02-17 10:22:22 +01:00
ret.push_str("</div></details>");
2024-02-17 09:44:08 +01:00
ret
2024-02-16 12:13:24 +01:00
}
2024-02-17 10:55:38 +01:00
fn get_content(config_path: &str) -> (String, String) {
2024-02-17 09:44:08 +01:00
let law = Law::from_config(config_path).unwrap();
2024-02-17 10:55:38 +01:00
let lawname = law.name;
2024-02-16 12:13:24 +01:00
2024-02-17 09:44:08 +01:00
let mut ret = String::new();
2024-02-16 12:13:24 +01:00
for h in law.header {
2024-02-17 09:44:08 +01:00
ret.push_str(&print_header(h, 0));
}
2024-02-17 10:55:38 +01:00
(lawname, ret)
2024-02-17 09:44:08 +01:00
}
fn main() {
let configs = fs::read_dir("./laws").expect("No folder with config files");
2024-02-17 16:11:16 +01:00
let mut li_of_files = String::new();
2024-02-17 09:44:08 +01:00
for config in configs {
let config = config.unwrap();
let filename = config.file_name().into_string().unwrap();
//TODO: use proper logic...
let law_name = filename.replace(".toml", "");
let path = format!("{}", config.path().display());
2024-02-17 10:55:38 +01:00
let (lawname, content) = get_content(&path);
2024-02-17 09:44:08 +01:00
2024-02-17 16:11:16 +01:00
let template = fs::read_to_string("templates/law.html").unwrap();
2024-02-17 10:15:15 +01:00
let site = template
.replace("{{content}}", &content)
2024-02-17 10:55:38 +01:00
.replace("{{title}}", &lawname);
2024-02-17 09:44:08 +01:00
2024-02-17 16:11:16 +01:00
li_of_files.push_str(&format!(
"<li><a href='./{law_name}'>{lawname}</a></li><br />\n"
));
2024-02-17 09:44:08 +01:00
fs::write(&format!("output/{law_name}.html"), &site).expect("Unable to write file");
2024-02-16 12:13:24 +01:00
}
2024-02-17 10:26:17 +01:00
let style = fs::read_to_string("templates/style.css").unwrap();
fs::write(&format!("output/style.css"), &style).expect("Unable to write file");
let style = fs::read_to_string("templates/pico.min.css").unwrap();
fs::write(&format!("output/pico.min.css"), &style).expect("Unable to write file");
2024-02-17 16:11:16 +01:00
let mut index = fs::read_to_string("templates/index.html").unwrap();
index = index.replace("{{content}}", &li_of_files);
fs::write(&format!("output/index.html"), &index).expect("Unable to write file");
2024-02-16 12:13:24 +01:00
}