Skip to content

Commit

Permalink
Support start chars (#51)
Browse files Browse the repository at this point in the history
* feat: suport start char for writer

* support start char and add option to indent start

* fix:lint error

* fix: lint
  • Loading branch information
bokuweb authored Mar 10, 2020
1 parent 4378de2 commit 6f54e5e
Show file tree
Hide file tree
Showing 27 changed files with 207 additions and 72 deletions.
17 changes: 10 additions & 7 deletions docx-core/examples/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ pub fn main() -> Result<(), DocxError> {
let path = std::path::Path::new("./output/indent.docx");
let file = std::fs::File::create(&path).unwrap();
Docx::new()
.add_paragraph(
Paragraph::new()
.add_run(Run::new().add_text(DUMMY))
.indent(840, None, None),
)
.add_paragraph(Paragraph::new().add_run(Run::new().add_text(DUMMY)).indent(
Some(840),
None,
None,
None,
))
.add_paragraph(Paragraph::new())
.add_paragraph(Paragraph::new().add_run(Run::new().add_text(DUMMY)).indent(
840,
Some(840),
Some(SpecialIndentType::FirstLine(720)),
None,
None,
))
.add_paragraph(Paragraph::new())
.add_paragraph(Paragraph::new().add_run(Run::new().add_text(DUMMY)).indent(
1560,
Some(1560),
Some(SpecialIndentType::Hanging(720)),
None,
None,
))
.build()
.pack(file)?;
Expand Down
7 changes: 6 additions & 1 deletion docx-core/examples/numbering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ pub fn main() -> Result<(), DocxError> {
LevelText::new("Section %1."),
LevelJc::new("left"),
)
.indent(1620, Some(SpecialIndentType::Hanging(320)), None),
.indent(
Some(1620),
Some(SpecialIndentType::Hanging(320)),
None,
None,
),
),
)
.add_numbering(Numbering::new(2, 2))
Expand Down
19 changes: 14 additions & 5 deletions docx-core/src/documents/elements/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ use crate::xml_builder::*;

#[derive(Debug, Clone, PartialEq)]
pub struct Indent {
start: i32,
start: Option<i32>,
end: Option<i32>,
special_indent: Option<SpecialIndentType>,
start_chars: Option<i32>,
}

impl Indent {
pub fn new(start: i32, special_indent: Option<SpecialIndentType>, end: Option<i32>) -> Indent {
pub fn new(
start: Option<i32>,
special_indent: Option<SpecialIndentType>,
end: Option<i32>,
start_chars: Option<i32>,
) -> Indent {
Indent {
start,
start_chars,
end,
special_indent,
}
Expand All @@ -34,6 +41,7 @@ impl BuildXML for Indent {
self.start,
self.special_indent,
self.end.unwrap_or_default(),
self.start_chars,
)
.build()
}
Expand All @@ -46,6 +54,7 @@ impl Serialize for Indent {
{
let mut t = serializer.serialize_struct("Indent", 3)?;
t.serialize_field("start", &self.start)?;
t.serialize_field("startChars", &self.start_chars)?;
t.serialize_field("end", &self.end)?;
t.serialize_field("specialIndent", &self.special_indent)?;
t.end()
Expand All @@ -62,7 +71,7 @@ mod tests {

#[test]
fn test_left() {
let b = Indent::new(20, None, None).build();
let b = Indent::new(Some(20), None, None, None).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:ind w:left="20" w:right="0" />"#
Expand All @@ -71,7 +80,7 @@ mod tests {

#[test]
fn test_first_line() {
let b = Indent::new(20, Some(SpecialIndentType::FirstLine(40)), None).build();
let b = Indent::new(Some(20), Some(SpecialIndentType::FirstLine(40)), None, None).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:ind w:left="20" w:right="0" w:firstLine="40" />"#
Expand All @@ -80,7 +89,7 @@ mod tests {

#[test]
fn test_hanging() {
let b = Indent::new(20, Some(SpecialIndentType::Hanging(50)), None).build();
let b = Indent::new(Some(20), Some(SpecialIndentType::Hanging(50)), None, None).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:ind w:left="20" w:right="0" w:hanging="50" />"#
Expand Down
9 changes: 6 additions & 3 deletions docx-core/src/documents/elements/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ impl Level {

pub fn indent(
mut self,
left: i32,
left: Option<i32>,
special_indent: Option<SpecialIndentType>,
end: Option<i32>,
start_chars: Option<i32>,
) -> Self {
self.paragraph_property = self.paragraph_property.indent(left, special_indent, end);
self.paragraph_property =
self.paragraph_property
.indent(left, special_indent, end, start_chars);
self
}

Expand Down Expand Up @@ -98,7 +101,7 @@ mod tests {
LevelText::new("%4."),
LevelJc::new("left"),
)
.indent(320, Some(SpecialIndentType::Hanging(200)), None)
.indent(Some(320), Some(SpecialIndentType::Hanging(200)), None, None)
.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
Expand Down
5 changes: 3 additions & 2 deletions docx-core/src/documents/elements/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,12 @@ impl Paragraph {

pub fn indent(
mut self,
left: i32,
left: Option<i32>,
special_indent: Option<SpecialIndentType>,
end: Option<i32>,
start_chars: Option<i32>,
) -> Paragraph {
self.property = self.property.indent(left, special_indent, end);
self.property = self.property.indent(left, special_indent, end, start_chars);
self
}

Expand Down
11 changes: 6 additions & 5 deletions docx-core/src/documents/elements/paragraph_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ impl ParagraphProperty {

pub fn indent(
mut self,
left: i32,
left: Option<i32>,
special_indent: Option<SpecialIndentType>,
end: Option<i32>,
start_chars: Option<i32>,
) -> Self {
self.indent = Some(Indent::new(left, special_indent, end));
self.indent = Some(Indent::new(left, special_indent, end, start_chars));
self
}

Expand Down Expand Up @@ -109,7 +110,7 @@ mod tests {
#[test]
fn test_indent() {
let c = ParagraphProperty::new();
let b = c.indent(20, None, None).build();
let b = c.indent(Some(20), None, None, None).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:pPr><w:pStyle w:val="Normal" /><w:rPr /><w:ind w:left="20" w:right="0" /></w:pPr>"#
Expand All @@ -119,10 +120,10 @@ mod tests {
#[test]
fn test_indent_json() {
let c = ParagraphProperty::new();
let b = c.indent(20, Some(SpecialIndentType::FirstLine(10)), None);
let b = c.indent(Some(20), Some(SpecialIndentType::FirstLine(10)), None, None);
assert_eq!(
serde_json::to_string(&b).unwrap(),
r#"{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null},"style":"Normal","numberingProperty":null,"alignment":null,"indent":{"start":20,"end":null,"specialIndent":{"type":"firstLine","val":10}}}"#
r#"{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null},"style":"Normal","numberingProperty":null,"alignment":null,"indent":{"start":20,"startChars":null,"end":null,"specialIndent":{"type":"firstLine","val":10}}}"#
);
}
}
7 changes: 5 additions & 2 deletions docx-core/src/documents/elements/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@ impl Style {

pub fn indent(
mut self,
left: i32,
left: Option<i32>,
special_indent: Option<SpecialIndentType>,
end: Option<i32>,
start_chars: Option<i32>,
) -> Self {
self.paragraph_property = self.paragraph_property.indent(left, special_indent, end);
self.paragraph_property =
self.paragraph_property
.indent(left, special_indent, end, start_chars);
self
}
}
Expand Down
53 changes: 44 additions & 9 deletions docx-core/src/documents/numberings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn create_default_numbering() -> AbstractNumbering {
LevelText::new("%1."),
LevelJc::new("left"),
)
.indent(420, Some(SpecialIndentType::Hanging(420)), None),
.indent(Some(420), Some(SpecialIndentType::Hanging(420)), None, None),
)
.add_level(
Level::new(
Expand All @@ -72,7 +72,7 @@ fn create_default_numbering() -> AbstractNumbering {
LevelText::new("(%2)"),
LevelJc::new("left"),
)
.indent(840, Some(SpecialIndentType::Hanging(420)), None),
.indent(Some(840), Some(SpecialIndentType::Hanging(420)), None, None),
)
.add_level(
Level::new(
Expand All @@ -82,7 +82,12 @@ fn create_default_numbering() -> AbstractNumbering {
LevelText::new("%3"),
LevelJc::new("left"),
)
.indent(1260, Some(SpecialIndentType::Hanging(420)), None),
.indent(
Some(1260),
Some(SpecialIndentType::Hanging(420)),
None,
None,
),
)
.add_level(
Level::new(
Expand All @@ -92,7 +97,12 @@ fn create_default_numbering() -> AbstractNumbering {
LevelText::new("%4."),
LevelJc::new("left"),
)
.indent(1680, Some(SpecialIndentType::Hanging(420)), None),
.indent(
Some(1680),
Some(SpecialIndentType::Hanging(420)),
None,
None,
),
)
.add_level(
Level::new(
Expand All @@ -102,7 +112,12 @@ fn create_default_numbering() -> AbstractNumbering {
LevelText::new("(%5)"),
LevelJc::new("left"),
)
.indent(2100, Some(SpecialIndentType::Hanging(420)), None),
.indent(
Some(2100),
Some(SpecialIndentType::Hanging(420)),
None,
None,
),
)
.add_level(
Level::new(
Expand All @@ -112,7 +127,12 @@ fn create_default_numbering() -> AbstractNumbering {
LevelText::new("%6"),
LevelJc::new("left"),
)
.indent(2520, Some(SpecialIndentType::Hanging(420)), None),
.indent(
Some(2520),
Some(SpecialIndentType::Hanging(420)),
None,
None,
),
)
.add_level(
Level::new(
Expand All @@ -122,7 +142,12 @@ fn create_default_numbering() -> AbstractNumbering {
LevelText::new("%7."),
LevelJc::new("left"),
)
.indent(2940, Some(SpecialIndentType::Hanging(420)), None),
.indent(
Some(2940),
Some(SpecialIndentType::Hanging(420)),
None,
None,
),
)
.add_level(
Level::new(
Expand All @@ -132,7 +157,12 @@ fn create_default_numbering() -> AbstractNumbering {
LevelText::new("(%8)"),
LevelJc::new("left"),
)
.indent(3360, Some(SpecialIndentType::Hanging(420)), None),
.indent(
Some(3360),
Some(SpecialIndentType::Hanging(420)),
None,
None,
),
)
.add_level(
Level::new(
Expand All @@ -142,6 +172,11 @@ fn create_default_numbering() -> AbstractNumbering {
LevelText::new("%9"),
LevelJc::new("left"),
)
.indent(3780, Some(SpecialIndentType::Hanging(420)), None),
.indent(
Some(3780),
Some(SpecialIndentType::Hanging(420)),
None,
None,
),
)
}
23 changes: 17 additions & 6 deletions docx-core/src/reader/attributes/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@ use crate::types::*;

use super::super::errors::*;

pub fn read_indent(
attrs: &[OwnedAttribute],
) -> Result<(i32, Option<i32>, Option<SpecialIndentType>), ReaderError> {
let mut start = 0;
pub type ReadIndentResult = Result<
(
Option<i32>,
Option<i32>,
Option<SpecialIndentType>,
Option<i32>,
),
ReaderError,
>;

pub fn read_indent(attrs: &[OwnedAttribute]) -> ReadIndentResult {
let mut start: Option<i32> = None;
let mut start_chars: Option<i32> = None;
let mut end: Option<i32> = None;
let mut special: Option<SpecialIndentType> = None;

for a in attrs {
let local_name = &a.name.local_name;
if local_name == "left" || local_name == "start" {
start = i32::from_str(&a.value)?;
start = Some(i32::from_str(&a.value)?);
} else if local_name == "leftChars" || local_name == "startChars" {
start_chars = Some(i32::from_str(&a.value)?);
} else if local_name == "end" || local_name == "right" {
end = Some(i32::from_str(&a.value)?);
} else if local_name == "hanging" {
Expand All @@ -26,5 +37,5 @@ pub fn read_indent(
}
}

Ok((start, end, special))
Ok((start, end, special, start_chars))
}
6 changes: 4 additions & 2 deletions docx-core/src/reader/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ impl ElementReader for Level {
let mut level_text = LevelText::new("");
let mut jc = LevelJc::new("left");

let mut indent_start = 0;
let mut indent_start = None;
let mut special_indent = None;
let mut indent_end = None;
let mut start_chars = None;
let mut has_indent = false;

loop {
Expand Down Expand Up @@ -52,6 +53,7 @@ impl ElementReader for Level {
indent_start = i.0;
indent_end = i.1;
special_indent = i.2;
start_chars = i.3;
has_indent = true;
}
_ => {}
Expand All @@ -65,7 +67,7 @@ impl ElementReader for Level {
l = l.paragraph_style(style_id);
}
if has_indent {
l = l.indent(indent_start, special_indent, indent_end);
l = l.indent(indent_start, special_indent, indent_end, start_chars);
}
return Ok(l);
}
Expand Down
Loading

0 comments on commit 6f54e5e

Please sign in to comment.