Read hanging chars (#273)

* fix: reading hanging chars

* update snaps

* 0.0.183
main
bokuweb 2021-04-08 16:53:21 +09:00 committed by GitHub
parent 2a6e147a38
commit 153429a086
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 198 additions and 23 deletions

View File

@ -7,10 +7,13 @@ use crate::xml_builder::*;
#[derive(Debug, Clone, PartialEq)]
pub struct Indent {
start: Option<i32>,
end: Option<i32>,
special_indent: Option<SpecialIndentType>,
start_chars: Option<i32>,
pub start: Option<i32>,
pub end: Option<i32>,
pub special_indent: Option<SpecialIndentType>,
pub start_chars: Option<i32>,
// Internal, for reading
pub hanging_chars: Option<i32>,
pub first_line_chars: Option<i32>,
}
impl Indent {
@ -25,6 +28,9 @@ impl Indent {
start_chars,
end,
special_indent,
// Internal, for reading
hanging_chars: None,
first_line_chars: None,
}
}
@ -32,6 +38,16 @@ impl Indent {
self.end = Some(end);
self
}
pub fn hanging_chars(mut self, chars: i32) -> Self {
self.hanging_chars = Some(chars);
self
}
pub fn first_line_chars(mut self, chars: i32) -> Self {
self.first_line_chars = Some(chars);
self
}
}
impl BuildXML for Indent {
@ -57,6 +73,8 @@ impl Serialize for Indent {
t.serialize_field("startChars", &self.start_chars)?;
t.serialize_field("end", &self.end)?;
t.serialize_field("specialIndent", &self.special_indent)?;
t.serialize_field("hangingChars", &self.hanging_chars)?;
t.serialize_field("firstLineChars", &self.first_line_chars)?;
t.end()
}
}

View File

@ -178,6 +178,16 @@ impl Paragraph {
self
}
pub(crate) fn hanging_chars(mut self, chars: i32) -> Paragraph {
self.property = self.property.hanging_chars(chars);
self
}
pub(crate) fn first_line_chars(mut self, chars: i32) -> Paragraph {
self.property = self.property.first_line_chars(chars);
self
}
pub fn numbering(mut self, id: NumberingId, level: IndentLevel) -> Self {
self.property = self.property.numbering(id, level);
self.has_numbering = true;

View File

@ -69,6 +69,20 @@ impl ParagraphProperty {
self.line_height = Some(h);
self
}
pub(crate) fn hanging_chars(mut self, chars: i32) -> Self {
if let Some(indent) = self.indent {
self.indent = Some(indent.hanging_chars(chars));
}
self
}
pub(crate) fn first_line_chars(mut self, chars: i32) -> Self {
if let Some(indent) = self.indent {
self.indent = Some(indent.first_line_chars(chars));
}
self
}
}
impl BuildXML for ParagraphProperty {
@ -132,7 +146,7 @@ mod tests {
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,"spacing":null,"fonts":null,"textBorder":null},"style":null,"numberingProperty":null,"alignment":null,"indent":{"start":20,"startChars":null,"end":null,"specialIndent":{"type":"firstLine","val":10}},"lineHeight":null}"#
r#"{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"style":null,"numberingProperty":null,"alignment":null,"indent":{"start":20,"startChars":null,"end":null,"specialIndent":{"type":"firstLine","val":10},"hangingChars":null,"firstLineChars":null},"lineHeight":null}"#
);
}
}

View File

@ -111,6 +111,16 @@ impl Style {
self
}
pub(crate) fn hanging_chars(mut self, chars: i32) -> Self {
self.paragraph_property = self.paragraph_property.hanging_chars(chars);
self
}
pub(crate) fn first_line_chars(mut self, chars: i32) -> Self {
self.paragraph_property = self.paragraph_property.first_line_chars(chars);
self
}
pub fn table_property(mut self, p: TableProperty) -> Self {
self.table_property = p;
self

View File

@ -12,6 +12,8 @@ pub type ReadIndentResult = Result<
Option<i32>,
Option<SpecialIndentType>,
Option<i32>,
Option<i32>,
Option<i32>,
),
ReaderError,
>;
@ -19,6 +21,8 @@ pub type ReadIndentResult = Result<
pub fn read_indent(attrs: &[OwnedAttribute]) -> ReadIndentResult {
let mut start: Option<i32> = None;
let mut start_chars: Option<i32> = None;
let mut hanging_chars: Option<i32> = None;
let mut first_line_chars: Option<i32> = None;
let mut end: Option<i32> = None;
let mut special: Option<SpecialIndentType> = None;
for a in attrs {
@ -37,7 +41,22 @@ pub fn read_indent(attrs: &[OwnedAttribute]) -> ReadIndentResult {
} else if local_name == "firstLine" {
let v = super::value_to_dax(&a.value)?;
special = Some(SpecialIndentType::FirstLine(v))
} else if local_name == "firstLineChars" {
if let Ok(chars) = i32::from_str(&a.value) {
first_line_chars = Some(chars);
}
} else if local_name == "hangingChars" {
if let Ok(chars) = i32::from_str(&a.value) {
hanging_chars = Some(chars);
}
}
Ok((start, end, special, start_chars))
}
Ok((
start,
end,
special,
start_chars,
hanging_chars,
first_line_chars,
))
}

View File

@ -82,8 +82,16 @@ impl ElementReader for Paragraph {
continue;
}
XMLElement::Indent => {
let (start, end, special, start_chars) = read_indent(&attributes)?;
let (start, end, special, start_chars, hanging_chars, first_line_chars) =
read_indent(&attributes)?;
p = p.indent(start, special, end, start_chars);
if let Some(chars) = hanging_chars {
p = p.hanging_chars(chars);
}
if let Some(chars) = first_line_chars {
p = p.first_line_chars(chars);
}
continue;
}
XMLElement::Spacing => {

View File

@ -44,8 +44,17 @@ impl ElementReader for Style {
}
// pPr
XMLElement::Indent => {
let (start, end, special, start_chars) = read_indent(&attributes)?;
let (start, end, special, start_chars, hanging_chars, first_line_chars) =
read_indent(&attributes)?;
style = style.indent(start, special, end, start_chars);
if let Some(chars) = hanging_chars {
style = style.hanging_chars(chars);
}
if let Some(chars) = first_line_chars {
style = style.first_line_chars(chars);
}
continue;
}
XMLElement::Justification => {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -6,4 +6,7 @@ export type IndentJSON = {
val: number;
} | null;
startChars: number | null;
// Read only
hangingChars: number | null;
firstLineChars: number | null;
};

View File

@ -1,6 +1,6 @@
{
"name": "docx-wasm",
"version": "0.0.182",
"version": "0.0.183",
"main": "dist/node/index.js",
"browser": "dist/web/index.js",
"author": "bokuweb <bokuweb12@gmail.com>",

View File

@ -535,6 +535,8 @@ Object {
"alignment": "left",
"indent": Object {
"end": 0,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": null,
"start": 0,
"startChars": null,
@ -3467,6 +3469,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": null,
"start": null,
"startChars": 0,
@ -3564,6 +3568,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": null,
"start": null,
"startChars": 0,
@ -3692,6 +3698,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": null,
"start": null,
"startChars": 0,
@ -3774,6 +3782,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "firstLine",
"val": 147,
@ -3828,6 +3838,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -3882,6 +3894,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -3936,6 +3950,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -3990,6 +4006,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4044,6 +4062,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4098,6 +4118,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4152,6 +4174,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4206,6 +4230,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4267,6 +4293,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4321,6 +4349,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4375,6 +4405,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4429,6 +4461,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4483,6 +4517,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4537,6 +4573,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4591,6 +4629,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4645,6 +4685,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4699,6 +4741,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4760,6 +4804,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "firstLine",
"val": 147,
@ -4814,6 +4860,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4868,6 +4916,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4922,6 +4972,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -4976,6 +5028,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -5030,6 +5084,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -5084,6 +5140,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -5138,6 +5196,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -5192,6 +5252,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -5777,6 +5839,8 @@ Object {
"alignment": null,
"indent": Object {
"end": null,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": null,
"start": 840,
"startChars": 400,
@ -7496,6 +7560,8 @@ Object {
"alignment": null,
"indent": Object {
"end": 0,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -7550,6 +7616,8 @@ Object {
"alignment": null,
"indent": Object {
"end": 0,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -7604,6 +7672,8 @@ Object {
"alignment": null,
"indent": Object {
"end": 0,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -7658,6 +7728,8 @@ Object {
"alignment": null,
"indent": Object {
"end": 0,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -7712,6 +7784,8 @@ Object {
"alignment": null,
"indent": Object {
"end": 0,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -7766,6 +7840,8 @@ Object {
"alignment": null,
"indent": Object {
"end": 0,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -7820,6 +7896,8 @@ Object {
"alignment": null,
"indent": Object {
"end": 0,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -7874,6 +7952,8 @@ Object {
"alignment": null,
"indent": Object {
"end": 0,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -7928,6 +8008,8 @@ Object {
"alignment": null,
"indent": Object {
"end": 0,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 420,
@ -7989,6 +8071,8 @@ Object {
"alignment": null,
"indent": Object {
"end": 0,
"firstLineChars": null,
"hangingChars": null,
"specialIndent": Object {
"type": "hanging",
"val": 608,