Merge branch 'md' into 'main'

add toc

See merge request PhilippHofer/risp!1
This commit is contained in:
PhilippHofer 2023-11-06 15:28:12 +00:00
commit d37e9490a7
3 changed files with 83 additions and 12 deletions

View File

@ -1,6 +1,9 @@
use log::debug; use log::debug;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::Arc; use std::{
fmt::{self, Display},
sync::Arc,
};
use crate::{overview, par}; use crate::{overview, par};
@ -17,6 +20,16 @@ struct Heading {
content: HeadingContent, // 1. Theil; 1. Subtheil; ... content: HeadingContent, // 1. Theil; 1. Subtheil; ...
} }
impl Display for Heading {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(desc) = &self.desc {
f.write_str(&format!("{} ({desc})", self.name))
} else {
f.write_str(&self.name)
}
}
}
#[derive(Debug, Serialize, Deserialize, PartialEq)] #[derive(Debug, Serialize, Deserialize, PartialEq)]
enum HeadingContent { enum HeadingContent {
Paragraph(Vec<Section>), Paragraph(Vec<Section>),
@ -67,6 +80,32 @@ impl From<LawBuilder> for Law {
} }
} }
impl Law {
pub(crate) fn to_md(&self) {
println!("# {}", self.name);
for header in &self.header {
Self::print_md(&header, 2);
}
}
fn print_md(header: &Heading, level: usize) {
println!("{} {}", "#".repeat(level), header);
match &header.content {
HeadingContent::Heading(h) => {
for child in h {
Self::print_md(&child, level + 1);
}
}
HeadingContent::Paragraph(p) => {
for par in p {
println!("{} {par}", "#".repeat(level + 1));
}
}
}
}
}
pub(crate) fn contains(classifier_name: &str, instance_name: &str) -> bool { pub(crate) fn contains(classifier_name: &str, instance_name: &str) -> bool {
instance_name instance_name
.to_lowercase() .to_lowercase()
@ -265,6 +304,7 @@ impl LawBuilder {
"New_par: {par};{}", "New_par: {par};{}",
serde_json::to_string(&content).unwrap() serde_json::to_string(&content).unwrap()
)); ));
debug!("new_par=par:{par};content:{content:#?}"); debug!("new_par=par:{par};content:{content:#?}");
if let Some(index) = self.last_header_index { if let Some(index) = self.last_header_index {
let section = Section { let section = Section {
@ -304,19 +344,29 @@ impl LawBuilder {
} }
} }
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, PartialEq, Serialize, Deserialize)]
pub(crate) struct Section { pub(crate) struct Section {
pub(crate) symb: String, // §"1", §"2", ... pub(crate) symb: String, // §"1", §"2", ...
pub(crate) par_header: Option<String>, pub(crate) par_header: Option<String>,
pub(crate) content: Content, pub(crate) content: Content,
} }
//impl fmt::Debug for Section { impl fmt::Debug for Section {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// let par_header = self.par_header.as_ref().map(String::as_str).unwrap_or(""); let par_header = self.par_header.as_ref().map(String::as_str).unwrap_or("");
// write!(f, "{} ({})", self.symb, par_header) write!(f, "{} ({})", self.symb, par_header)
// } }
//} }
impl fmt::Display for Section {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(header) = &self.par_header {
f.write_str(&format!("{} ({})\n{}", self.symb, header, self.content))
} else {
f.write_str(&format!("{} {}", self.symb, self.content))
}
}
}
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub(crate) struct ClassifierInstance { pub(crate) struct ClassifierInstance {
@ -415,6 +465,28 @@ pub(crate) enum Content {
List(Vec<Content>), List(Vec<Content>),
} }
impl Display for Content {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Text(a) => f.write_str(&format!("{a}\n")),
Self::Item(a) => {
let mut ret = String::new();
for aa in a {
ret.push_str(&format!("{aa}\n"));
}
f.write_str(&ret)
}
Self::List(a) => {
let mut ret = String::new();
for aa in a {
ret.push_str(&format!("{aa}\n"));
}
f.write_str(&ret)
}
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;

View File

@ -42,8 +42,8 @@ impl From<roxmltree::Error> for Error {
fn main() { fn main() {
env_logger::init(); env_logger::init();
let law = LawBuilder::new("MSchG"); let law = LawBuilder::new("ABGB");
println!("{:#?}", law); law.to_md();
//println!("{:#?}", builder.toc()); //println!("{:#?}", builder.toc());
} }

View File

@ -1,3 +1,4 @@
use log::{error, info};
use roxmltree::Node; use roxmltree::Node;
use crate::{ use crate::{
@ -159,8 +160,6 @@ impl Abschnitt {
absatze.push(Content::Text(absatz.content.replace('\u{a0}', " ").clone())); absatze.push(Content::Text(absatz.content.replace('\u{a0}', " ").clone()));
} }
//TODO: Continue here, (2) and (3) is somehow skipped
//There can be as many 'Absätze' as our lovely lawsetter wants //There can be as many 'Absätze' as our lovely lawsetter wants
while let Some(child) = c.peek() { while let Some(child) = c.peek() {
if AbsatzAbs::test(child) { if AbsatzAbs::test(child) {