Merge branch 'md' into 'main'
add toc See merge request PhilippHofer/risp!1
This commit is contained in:
commit
d37e9490a7
88
src/law.rs
88
src/law.rs
@ -1,6 +1,9 @@
|
||||
use log::debug;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
use std::{
|
||||
fmt::{self, Display},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use crate::{overview, par};
|
||||
|
||||
@ -17,6 +20,16 @@ struct Heading {
|
||||
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)]
|
||||
enum HeadingContent {
|
||||
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 {
|
||||
instance_name
|
||||
.to_lowercase()
|
||||
@ -265,6 +304,7 @@ impl LawBuilder {
|
||||
"New_par: {par};{}",
|
||||
serde_json::to_string(&content).unwrap()
|
||||
));
|
||||
|
||||
debug!("new_par=par:{par};content:{content:#?}");
|
||||
if let Some(index) = self.last_header_index {
|
||||
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) symb: String, // §"1", §"2", ...
|
||||
pub(crate) par_header: Option<String>,
|
||||
pub(crate) content: Content,
|
||||
}
|
||||
|
||||
//impl fmt::Debug for Section {
|
||||
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
// let par_header = self.par_header.as_ref().map(String::as_str).unwrap_or("");
|
||||
// write!(f, "{} ({})", self.symb, par_header)
|
||||
// }
|
||||
//}
|
||||
impl fmt::Debug for Section {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let par_header = self.par_header.as_ref().map(String::as_str).unwrap_or("");
|
||||
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)]
|
||||
pub(crate) struct ClassifierInstance {
|
||||
@ -415,6 +465,28 @@ pub(crate) enum 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)]
|
||||
mod tests {
|
||||
use pretty_assertions::assert_eq;
|
||||
|
@ -42,8 +42,8 @@ impl From<roxmltree::Error> for Error {
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
let law = LawBuilder::new("MSchG");
|
||||
let law = LawBuilder::new("ABGB");
|
||||
|
||||
println!("{:#?}", law);
|
||||
law.to_md();
|
||||
//println!("{:#?}", builder.toc());
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
use log::{error, info};
|
||||
use roxmltree::Node;
|
||||
|
||||
use crate::{
|
||||
@ -159,8 +160,6 @@ impl Abschnitt {
|
||||
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
|
||||
while let Some(child) = c.peek() {
|
||||
if AbsatzAbs::test(child) {
|
||||
|
Loading…
Reference in New Issue
Block a user