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!("{t}")),
Content::Item(l) | Content::List(l) => {
ret.push_str("
");
for item in l {
ret.push_str("- ");
ret.push_str(&print_content(item));
ret.push_str("
");
}
ret.push_str("
");
}
}
ret
}
fn print_paragraph(section: Section) -> String {
let mut ret = String::new();
ret.push_str("");
ret.push_str(&format!("{}", section.symb));
if let Some(par_header) = section.par_header {
ret.push_str(&format!("", par_header));
}
if let Some(note) = section.par_note {
ret.push_str(&format!("Beachte: {}", note));
}
ret.push_str(&print_content(section.content));
ret.push_str("
");
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!(
"\
{1}
\n\
",
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("
");
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() {
let configs = fs::read_dir("./laws").expect("No folder with config files");
let mut li_of_files = String::new();
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());
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!(
"{lawname}
\n"
));
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(&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");
let js = fs::read_to_string("templates/app.js").unwrap();
fs::write(&format!("output/app.js"), &js).expect("Unable to write file");
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");
}