This commit is contained in:
2023-11-04 18:41:17 +01:00
parent ed109e1784
commit a78ba95775
5 changed files with 180 additions and 28 deletions

View File

@ -9,6 +9,7 @@ fn fetch_page(url: &str) -> Result<String, Error> {
pub(crate) fn parse(url: &str, builder: &mut LawBuilder) -> Result<(), Error> {
println!("{url}");
let xml = fetch_page(url)?;
let xml = xml.replace("<gdash />", "-"); // used e.g. in §11 Abs. 3 UrhG
let risdok = Risdok::from_str(&xml, builder)?;
println!("{builder:#?}");

View File

@ -144,12 +144,40 @@ impl Abschnitt {
}
}
// TODO: Continue here: We want to create a `Section`.
//
// We have 2 tasks
// 1) Get paragraph id
// 2) Get content
let mut absatze = Vec::new();
let absatz = AbsatzAbs::parse(c.next().expect("We need at least one 'Absatz'"));
let par_id = absatz
.gldsym
.clone()
.expect("First 'Absatz' needs to have § id");
absatze.push(absatz);
// If there's a "liste" after an "absatz", the "liste" should be part of the "absatz"
if let Some(child) = c.peek() {
if Liste::test(child) {
let liste = Liste::parse(c.next().unwrap());
//TODO do something with list
}
}
//There can be as many 'Absätze' as our lovely lawsetter wants
loop {
match c.peek() {
Some(child) => {
if AbsatzAbs::test(child) {
absatze.push(AbsatzAbs::parse(c.next().unwrap()));
// If there's a "liste" after an "absatz", the "liste" should be part of the "absatz"
if let Some(child) = c.peek() {
if Liste::test(child) {
let liste = Liste::parse(c.next().unwrap());
//TODO do something with list
}
}
continue;
}
break;
@ -159,24 +187,38 @@ impl Abschnitt {
}
if absatze.len() == 1 {
builder.new_par(Content::Text(format!(
"{} {}",
absatze[0].gldsym.clone().unwrap(),
absatze[0].content
)));
builder.new_par(par_id, Content::Text(absatze[0].content.clone()));
} else {
let mut content = Vec::new();
let mut contents = Vec::new();
for a in &absatze {
let mut txt = String::new();
if let Some(sym) = &a.gldsym {
txt.push_str(&format!("{sym} "));
}
txt.push_str(&a.content);
content.push(Box::new(Content::Text(txt)));
contents.push(Box::new(Content::Text(a.content.clone())));
}
builder.new_par(Content::Item(content));
builder.new_par(par_id, Content::Item(contents));
}
//if absatze.len() == 1 {
// builder.new_par(Content::Text(format!(
// "{} {}",
// absatze[0].gldsym.clone().unwrap(),
// absatze[0].content
// )));
//} else {
// let mut content = Vec::new();
// for a in &absatze {
// let mut txt = String::new();
// if let Some(sym) = &a.gldsym {
// if symb.is_some() {
// panic!("Two (or more) § symbols in single paragraph ?!?");
// } else {
// symb = Some(sym);
// }
// }
// txt.push_str(&a.content);
// content.push(Box::new(Content::Text(txt)));
// }
// builder.new_par(Content::Item(content));
//}
// Skip all UeberschriftTitle and Absatz
loop {
match c.peek() {
@ -195,12 +237,112 @@ impl Abschnitt {
}
}
println!("====");
println!("{c:#?}");
assert_eq!(c.next(), None);
Self { absatze }
}
}
#[derive(Debug, PartialEq)]
pub(crate) struct Symbol {
stellen: String,
content: String,
}
impl Symbol {
pub(crate) fn parse(n: Node) -> Self {
assert!(n.tag_name().name() == "symbol");
assert_eq!(n.children().count(), 1);
let stellen = n.attribute("stellen").unwrap().into();
let content = n.text().unwrap().into();
Self { stellen, content }
}
}
#[derive(Debug, PartialEq)]
pub(crate) struct Listelem {
symbol: Symbol,
text: String,
}
impl Listelem {
pub(crate) fn test(n: &Node) -> bool {
n.tag_name().name() == "listelem"
}
pub(crate) fn parse(n: Node) -> Self {
assert!(n.tag_name().name() == "listelem");
let mut c = n.children();
let symbol = Symbol::parse(c.next().unwrap());
let text = c.next().unwrap().text().unwrap().into();
assert_eq!(c.next(), None);
Self { symbol, text }
}
}
#[derive(Debug, PartialEq)]
pub(crate) struct Ziffernliste {
ebene: String,
listelems: Vec<Listelem>,
}
impl Ziffernliste {
pub(crate) fn parse(n: Node) -> Self {
assert!(n.tag_name().name() == "ziffernliste");
let ebene = n.attribute("ebene").unwrap().into();
let mut c = n.children().peekable();
let mut listelems = Vec::new();
loop {
match c.peek() {
Some(child) => {
if Listelem::test(child) {
listelems.push(Listelem::parse(c.next().unwrap()));
continue;
}
}
None => break,
}
break;
}
assert_eq!(c.next(), None);
Self { ebene, listelems }
}
}
#[derive(Debug, PartialEq)]
pub(crate) struct Liste {
ziffernliste: Ziffernliste,
}
impl Liste {
pub(crate) fn test(n: &Node) -> bool {
n.tag_name().name() == "liste"
&& n.children().count() == 1
&& n.children().next().unwrap().tag_name().name() == "ziffernliste"
}
pub(crate) fn parse(n: Node) -> Self {
assert!(Self::test(&n));
let mut c = n.children();
let ziffernliste = Ziffernliste::parse(c.next().unwrap());
assert_eq!(c.next(), None);
Self { ziffernliste }
}
}
#[derive(Debug, PartialEq)]
pub(crate) struct AbsatzAbs {
gldsym: Option<String>,