Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docx-core/src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod numbering_property;
mod numberings;
mod page_num_type;
mod paragraph;
mod paragraph_borders;
mod paragraph_property;
mod paragraph_property_change;
mod pic;
Expand Down
37 changes: 37 additions & 0 deletions docx-core/src/reader/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,43 @@ mod tests {
);
}

#[test]
fn test_read_paragraph_borders() {
let c = r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:p>
<w:pPr>
<w:pBdr>
<w:top w:val="double" w:sz="24" w:space="1" w:color="622423"/>
<w:between w:val="dashed" w:sz="8" w:space="2" w:color="auto"/>
</w:pBdr>
</w:pPr>
</w:p>
</w:document>"#;
let mut parser = EventReader::new(c.as_bytes());
let p = Paragraph::read(&mut parser, &[]).unwrap();

assert_eq!(
p.property.borders,
Some(
ParagraphBorders::with_empty()
.set(
ParagraphBorder::new(ParagraphBorderPosition::Top)
.val(BorderType::Double)
.size(24)
.space(1)
.color("622423"),
)
.set(
ParagraphBorder::new(ParagraphBorderPosition::Between)
.val(BorderType::Dashed)
.size(8)
.space(2)
.color("auto"),
),
)
);
}

#[test]
fn test_read_numbering() {
let c = r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
Expand Down
51 changes: 51 additions & 0 deletions docx-core/src/reader/paragraph_borders.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::io::Read;
use std::str::FromStr;

use super::*;
use crate::types::*;

impl ElementReader for ParagraphBorders {
fn read<R: Read>(r: &mut EventReader<R>, _: &[OwnedAttribute]) -> Result<Self, ReaderError> {
let mut borders = ParagraphBorders::with_empty();
loop {
match r.next() {
Ok(XmlEvent::StartElement {
attributes, name, ..
}) => {
let position = match XMLElement::from_str(&name.local_name).unwrap() {
XMLElement::Top => Some(ParagraphBorderPosition::Top),
XMLElement::Left => Some(ParagraphBorderPosition::Left),
XMLElement::Bottom => Some(ParagraphBorderPosition::Bottom),
XMLElement::Right => Some(ParagraphBorderPosition::Right),
XMLElement::Between => Some(ParagraphBorderPosition::Between),
XMLElement::Bar => Some(ParagraphBorderPosition::Bar),
_ => None,
};

if let Some(position) = position {
let attributes = read_border(&attributes)?;
let mut border = ParagraphBorder::new(position)
.val(attributes.border_type)
.color(attributes.color);
if let Some(size) = attributes.size {
border = border.size(size as usize);
}
if let Some(space) = attributes.space {
border = border.space(space as usize);
}
borders = borders.set(border);
}
}
Ok(XmlEvent::EndElement { name, .. }) => {
if XMLElement::from_str(&name.local_name).unwrap()
== XMLElement::ParagraphBorders
{
return Ok(borders);
}
}
Err(_) => return Err(ReaderError::XMLReadError),
_ => {}
}
}
}
}
5 changes: 5 additions & 0 deletions docx-core/src/reader/paragraph_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ impl ElementReader for ParagraphProperty {
p.frame_property = Some(pr);
}
}
XMLElement::ParagraphBorders => {
if let Ok(borders) = ParagraphBorders::read(r, &attributes) {
p = p.set_borders(borders);
}
}
XMLElement::Tabs => {
if let Ok(tabs) = Tabs::read(r, &attributes) {
for t in tabs.tabs {
Expand Down
6 changes: 6 additions & 0 deletions docx-core/src/reader/xml_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum XMLElement {
Body,
Paragraph,
ParagraphProperty,
ParagraphBorders,
Run,
RunProperty,
Color,
Expand Down Expand Up @@ -112,6 +113,8 @@ pub enum XMLElement {
Bottom,
InsideH,
InsideV,
Between,
Bar,
Tl2br,
Tr2bl,
TableGrid,
Expand Down Expand Up @@ -265,6 +268,7 @@ impl FromStr for XMLElement {
"body" => Ok(XMLElement::Body),
"p" => Ok(XMLElement::Paragraph),
"pPr" => Ok(XMLElement::ParagraphProperty),
"pBdr" => Ok(XMLElement::ParagraphBorders),
"r" => Ok(XMLElement::Run),
"rPr" => Ok(XMLElement::RunProperty),
"rPrChange" => Ok(XMLElement::RunPropertyChange),
Expand Down Expand Up @@ -341,6 +345,8 @@ impl FromStr for XMLElement {
"bottom" => Ok(XMLElement::Bottom),
"insideH" => Ok(XMLElement::InsideH),
"insideV" => Ok(XMLElement::InsideV),
"between" => Ok(XMLElement::Between),
"bar" => Ok(XMLElement::Bar),
"tl2br" => Ok(XMLElement::Tl2br),
"tr2bl" => Ok(XMLElement::Tr2bl),
"tblGrid" => Ok(XMLElement::TableGrid),
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading