From 3e223118b2c6c481da6f26853e750497483d36b5 Mon Sep 17 00:00:00 2001 From: Maarten van Gompel Date: Thu, 3 Oct 2019 22:43:30 +0200 Subject: [PATCH] fixed text markup serialisation #3 --- Cargo.toml | 2 +- src/document.rs | 4 ++-- src/parser.rs | 1 - tests/main.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4f2842e..d06b871 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ maintenance = { status = "actively-developed" } [dependencies] -quick-xml = "0.16.0" +quick-xml = "0.16.1" matches = "0.1.8" strum = "0.16.0" strum_macros = "0.16.0" diff --git a/src/document.rs b/src/document.rs index 768f5a5..f144f15 100644 --- a/src/document.rs +++ b/src/document.rs @@ -106,7 +106,7 @@ impl Document { ///Load a FoliA document from file. Invokes the XML parser and loads it all into memory. pub fn from_file(filename: &str, properties: DocumentProperties) -> Result { let mut reader = Reader::from_file(Path::new(filename))?; - reader.trim_text(true); + reader.trim_text(false); let mut doc = Self::parse(&mut reader, properties)?; //associate the filename with the document doc.filename = Some(filename.to_string()); @@ -116,7 +116,7 @@ impl Document { ///Load a FoliA document from XML string representation, loading it all into memory. pub fn from_str(data: &str, properties: DocumentProperties) -> Result { let mut reader = Reader::from_str(data); - reader.trim_text(true); + reader.trim_text(false); Self::parse(&mut reader, properties) } diff --git a/src/parser.rs b/src/parser.rs index 529eadf..a667ba6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -405,7 +405,6 @@ impl Document { (None, Event::Text(s)) => { let text = s.unescape_and_decode(reader)?; if text.trim() != "" { - //eprintln!("TEXT: {}", text); if let Some(parent_key) = stack.last() { self.get_mut_elementdata(*parent_key).map( |parent| { parent.push(DataType::Text(text)); diff --git a/tests/main.rs b/tests/main.rs index 4cc803e..ddd8ecd 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -176,6 +176,37 @@ const EXAMPLE_DEP: &[u8] = br#" "#; +const EXAMPLE_MARKUP: &[u8] = br#" + + + + + + + + + + + + + + + + + + + + eng + + +

+ + Hello world! How are you today? + +

+
+
"#; + #[test] fn test001_instantiate() { match Document::new("example", DocumentProperties::default()) { @@ -677,3 +708,19 @@ fn test012_spanroles() { } } } + +#[test] +fn test013_textmarkup() { + match Document::from_str(str::from_utf8(EXAMPLE_MARKUP).expect("conversion from utf-8 of example"), DocumentProperties::default()) { + Ok(doc) => { + if let Some(sentence) = doc.get_element_by_id("example.p.1.s.1") { + assert_eq!(sentence.text(&TextParameters::default()).expect("unwrapping text of dep"), "Hello world! How are you today?"); + } else { + assert!(false, "dependency not found"); + } + }, + Err(err) => { + assert!(false, format!("Instantiation failed with error: {}",err)); + } + } +}