Skip to content

Commit

Permalink
fixed text markup serialisation #3
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed Oct 3, 2019
1 parent 5e700cf commit 3e22311
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, FoliaError> {
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());
Expand All @@ -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<Self, FoliaError> {
let mut reader = Reader::from_str(data);
reader.trim_text(true);
reader.trim_text(false);
Self::parse(&mut reader, properties)
}

Expand Down
1 change: 0 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
47 changes: 47 additions & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,37 @@ const EXAMPLE_DEP: &[u8] = br#"<?xml version="1.0" encoding="utf-8"?>
</text>
</FoLiA>"#;

const EXAMPLE_MARKUP: &[u8] = br#"<?xml version="1.0" encoding="utf-8"?>
<FoLiA xmlns="http://ilk.uvt.nl/folia" version="2.0" xml:id="example">
<metadata type="native">
<annotations>
<text-annotation>
<annotator processor="p1" />
</text-annotation>
<sentence-annotation>
<annotator processor="p1" />
</sentence-annotation>
<paragraph-annotation>
<annotator processor="p1" />
</paragraph-annotation>
<style-annotation set="adhoc">
<annotator processor="p1" />
</style-annotation>
</annotations>
<provenance>
<processor xml:id="p1" name="proycon" type="manual" />
</provenance>
<meta id="language">eng</meta>
</metadata>
<text xml:id="example.text">
<p xml:id="example.p.1">
<s xml:id="example.p.1.s.1">
<t>Hello <t-style class="bold">world</t-style>! How are <t-style class="italics">you</t-style> today?</t>
</s>
</p>
</text>
</FoLiA>"#;

#[test]
fn test001_instantiate() {
match Document::new("example", DocumentProperties::default()) {
Expand Down Expand Up @@ -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));
}
}
}

0 comments on commit 3e22311

Please sign in to comment.