Fix read error (#49)
* fix: use i32 instead of usize for indent * update * fix:insert json type * fix: insert and delete child * updatemain
parent
042cdc58f4
commit
4378de2ecc
|
@ -3,7 +3,7 @@ use std::fs::*;
|
|||
use std::io::Read;
|
||||
|
||||
pub fn main() {
|
||||
let mut file = File::open("./2.docx").unwrap();
|
||||
let mut file = File::open("./insert.docx").unwrap();
|
||||
let mut buf = vec![];
|
||||
file.read_to_end(&mut buf).unwrap();
|
||||
dbg!(read_docx(&buf).unwrap().json());
|
||||
|
|
|
@ -7,17 +7,13 @@ use crate::xml_builder::*;
|
|||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Indent {
|
||||
start: usize,
|
||||
end: Option<usize>,
|
||||
start: i32,
|
||||
end: Option<i32>,
|
||||
special_indent: Option<SpecialIndentType>,
|
||||
}
|
||||
|
||||
impl Indent {
|
||||
pub fn new(
|
||||
start: usize,
|
||||
special_indent: Option<SpecialIndentType>,
|
||||
end: Option<usize>,
|
||||
) -> Indent {
|
||||
pub fn new(start: i32, special_indent: Option<SpecialIndentType>, end: Option<i32>) -> Indent {
|
||||
Indent {
|
||||
start,
|
||||
end,
|
||||
|
@ -25,7 +21,7 @@ impl Indent {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn end(mut self, end: usize) -> Self {
|
||||
pub fn end(mut self, end: i32) -> Self {
|
||||
self.end = Some(end);
|
||||
self
|
||||
}
|
||||
|
|
|
@ -37,9 +37,9 @@ impl Level {
|
|||
|
||||
pub fn indent(
|
||||
mut self,
|
||||
left: usize,
|
||||
left: i32,
|
||||
special_indent: Option<SpecialIndentType>,
|
||||
end: Option<usize>,
|
||||
end: Option<i32>,
|
||||
) -> Self {
|
||||
self.paragraph_property = self.paragraph_property.indent(left, special_indent, end);
|
||||
self
|
||||
|
@ -84,9 +84,9 @@ mod tests {
|
|||
)
|
||||
.build();
|
||||
assert_eq!(
|
||||
str::from_utf8(&b).unwrap(),
|
||||
r#"<w:lvl w:ilvl="1"><w:start w:val="1" /><w:numFmt w:val="decimal" /><w:lvlText w:val="%4." /><w:lvlJc w:val="left" /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr></w:lvl>"#
|
||||
);
|
||||
str::from_utf8(&b).unwrap(),
|
||||
r#"<w:lvl w:ilvl="1"><w:start w:val="1" /><w:numFmt w:val="decimal" /><w:lvlText w:val="%4." /><w:lvlJc w:val="left" /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr></w:lvl>"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -101,8 +101,8 @@ mod tests {
|
|||
.indent(320, Some(SpecialIndentType::Hanging(200)), None)
|
||||
.build();
|
||||
assert_eq!(
|
||||
str::from_utf8(&b).unwrap(),
|
||||
r#"<w:lvl w:ilvl="1"><w:start w:val="1" /><w:numFmt w:val="decimal" /><w:lvlText w:val="%4." /><w:lvlJc w:val="left" /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /><w:ind w:left="320" w:right="0" w:hanging="200" /></w:pPr></w:lvl>"#
|
||||
);
|
||||
str::from_utf8(&b).unwrap(),
|
||||
r#"<w:lvl w:ilvl="1"><w:start w:val="1" /><w:numFmt w:val="decimal" /><w:lvlText w:val="%4." /><w:lvlJc w:val="left" /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /><w:ind w:left="320" w:right="0" w:hanging="200" /></w:pPr></w:lvl>"#
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -163,9 +163,9 @@ impl Paragraph {
|
|||
|
||||
pub fn indent(
|
||||
mut self,
|
||||
left: usize,
|
||||
left: i32,
|
||||
special_indent: Option<SpecialIndentType>,
|
||||
end: Option<usize>,
|
||||
end: Option<i32>,
|
||||
) -> Paragraph {
|
||||
self.property = self.property.indent(left, special_indent, end);
|
||||
self
|
||||
|
|
|
@ -50,9 +50,9 @@ impl ParagraphProperty {
|
|||
|
||||
pub fn indent(
|
||||
mut self,
|
||||
left: usize,
|
||||
left: i32,
|
||||
special_indent: Option<SpecialIndentType>,
|
||||
end: Option<usize>,
|
||||
end: Option<i32>,
|
||||
) -> Self {
|
||||
self.indent = Some(Indent::new(left, special_indent, end));
|
||||
self
|
||||
|
|
|
@ -89,9 +89,9 @@ impl Style {
|
|||
|
||||
pub fn indent(
|
||||
mut self,
|
||||
left: usize,
|
||||
left: i32,
|
||||
special_indent: Option<SpecialIndentType>,
|
||||
end: Option<usize>,
|
||||
end: Option<i32>,
|
||||
) -> Self {
|
||||
self.paragraph_property = self.paragraph_property.indent(left, special_indent, end);
|
||||
self
|
||||
|
|
|
@ -8,21 +8,21 @@ use super::super::errors::*;
|
|||
|
||||
pub fn read_indent(
|
||||
attrs: &[OwnedAttribute],
|
||||
) -> Result<(usize, Option<usize>, Option<SpecialIndentType>), ReaderError> {
|
||||
) -> Result<(i32, Option<i32>, Option<SpecialIndentType>), ReaderError> {
|
||||
let mut start = 0;
|
||||
let mut end: Option<usize> = 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 = usize::from_str(&a.value)?;
|
||||
start = i32::from_str(&a.value)?;
|
||||
} else if local_name == "end" || local_name == "right" {
|
||||
end = Some(usize::from_str(&a.value)?);
|
||||
end = Some(i32::from_str(&a.value)?);
|
||||
} else if local_name == "hanging" {
|
||||
special = Some(SpecialIndentType::Hanging(usize::from_str(&a.value)?))
|
||||
special = Some(SpecialIndentType::Hanging(i32::from_str(&a.value)?))
|
||||
} else if local_name == "firstLine" {
|
||||
special = Some(SpecialIndentType::FirstLine(usize::from_str(&a.value)?))
|
||||
special = Some(SpecialIndentType::FirstLine(i32::from_str(&a.value)?))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ impl ElementReader for Delete {
|
|||
r: &mut EventReader<R>,
|
||||
attrs: &[OwnedAttribute],
|
||||
) -> Result<Self, ReaderError> {
|
||||
let mut run: Option<Run> = None;
|
||||
let mut run = Run::new();
|
||||
loop {
|
||||
let e = r.next();
|
||||
match e {
|
||||
|
@ -19,26 +19,22 @@ impl ElementReader for Delete {
|
|||
let e = XMLElement::from_str(&name.local_name)
|
||||
.expect("should convert to XMLElement");
|
||||
if let XMLElement::Run = e {
|
||||
run = Some(Run::read(r, attrs)?);
|
||||
run = Run::read(r, attrs)?;
|
||||
}
|
||||
}
|
||||
Ok(XmlEvent::EndElement { name, .. }) => {
|
||||
let e = XMLElement::from_str(&name.local_name).unwrap();
|
||||
if e == XMLElement::Delete {
|
||||
if let Some(run) = run {
|
||||
let mut del = Delete::new(run);
|
||||
for attr in attrs {
|
||||
let local_name = &attr.name.local_name;
|
||||
if local_name == "author" {
|
||||
del = del.author(&attr.value);
|
||||
} else if local_name == "date" {
|
||||
del = del.date(&attr.value);
|
||||
}
|
||||
let mut del = Delete::new(run);
|
||||
for attr in attrs {
|
||||
let local_name = &attr.name.local_name;
|
||||
if local_name == "author" {
|
||||
del = del.author(&attr.value);
|
||||
} else if local_name == "date" {
|
||||
del = del.date(&attr.value);
|
||||
}
|
||||
return Ok(del);
|
||||
} else {
|
||||
return Err(ReaderError::XMLReadError);
|
||||
}
|
||||
return Ok(del);
|
||||
}
|
||||
}
|
||||
Err(_) => return Err(ReaderError::XMLReadError),
|
||||
|
|
|
@ -11,33 +11,29 @@ impl ElementReader for Insert {
|
|||
r: &mut EventReader<R>,
|
||||
attrs: &[OwnedAttribute],
|
||||
) -> Result<Self, ReaderError> {
|
||||
let mut run: Option<Run> = None;
|
||||
let mut run = Run::new();
|
||||
loop {
|
||||
let e = r.next();
|
||||
match e {
|
||||
Ok(XmlEvent::StartElement { name, .. }) => {
|
||||
let e = XMLElement::from_str(&name.local_name).unwrap();
|
||||
if let XMLElement::Run = e {
|
||||
run = Some(Run::read(r, attrs)?);
|
||||
run = Run::read(r, attrs)?;
|
||||
}
|
||||
}
|
||||
Ok(XmlEvent::EndElement { name, .. }) => {
|
||||
let e = XMLElement::from_str(&name.local_name).unwrap();
|
||||
if e == XMLElement::Insert {
|
||||
if let Some(run) = run {
|
||||
let mut ins = Insert::new(run);
|
||||
for attr in attrs {
|
||||
let local_name = &attr.name.local_name;
|
||||
if local_name == "author" {
|
||||
ins = ins.author(&attr.value);
|
||||
} else if local_name == "date" {
|
||||
ins = ins.date(&attr.value);
|
||||
}
|
||||
let mut ins = Insert::new(run);
|
||||
for attr in attrs {
|
||||
let local_name = &attr.name.local_name;
|
||||
if local_name == "author" {
|
||||
ins = ins.author(&attr.value);
|
||||
} else if local_name == "date" {
|
||||
ins = ins.date(&attr.value);
|
||||
}
|
||||
return Ok(ins);
|
||||
} else {
|
||||
return Err(ReaderError::XMLReadError);
|
||||
}
|
||||
return Ok(ins);
|
||||
}
|
||||
}
|
||||
Err(_) => return Err(ReaderError::XMLReadError),
|
||||
|
|
|
@ -7,8 +7,8 @@ use serde::Serialize;
|
|||
// Please convert typescript type to following type.
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub enum SpecialIndentType {
|
||||
FirstLine(usize),
|
||||
Hanging(usize),
|
||||
FirstLine(i32),
|
||||
Hanging(i32),
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
|
|
|
@ -89,9 +89,9 @@ impl XMLBuilder {
|
|||
// i.e. <w:ind ... >
|
||||
pub(crate) fn indent(
|
||||
mut self,
|
||||
start: usize,
|
||||
start: i32,
|
||||
special_indent: Option<SpecialIndentType>,
|
||||
end: usize,
|
||||
end: i32,
|
||||
) -> Self {
|
||||
let start = &format!("{}", start);
|
||||
let end = &format!("{}", end);
|
||||
|
|
|
@ -169,3 +169,18 @@ pub fn read_bookmark() {
|
|||
file.write_all(json.as_bytes()).unwrap();
|
||||
file.flush().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn read_insert_table() {
|
||||
let mut file = File::open("../fixtures/insert_table/insert_table.docx").unwrap();
|
||||
let mut buf = vec![];
|
||||
file.read_to_end(&mut buf).unwrap();
|
||||
let json = read_docx(&buf).unwrap().json();
|
||||
|
||||
assert_debug_snapshot!(&json);
|
||||
|
||||
let path = std::path::Path::new("./tests/output/insert_table.json");
|
||||
let mut file = std::fs::File::create(&path).unwrap();
|
||||
file.write_all(json.as_bytes()).unwrap();
|
||||
file.flush().unwrap();
|
||||
}
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,4 +1,4 @@
|
|||
import { RunJSON, RunChildJSON } from "./run";
|
||||
import { RunJSON, RunChildJSON, RunPropertyJSON } from "./run";
|
||||
import { IndentJSON } from "./indent";
|
||||
|
||||
export type ParagraphChildJSON =
|
||||
|
@ -32,7 +32,10 @@ export type ParagraphJSON = {
|
|||
export type InsertJSON = {
|
||||
type: "insert";
|
||||
data: {
|
||||
run: RunJSON;
|
||||
run: {
|
||||
runProperty: RunPropertyJSON;
|
||||
children: RunChildJSON[];
|
||||
};
|
||||
author: string;
|
||||
data: string;
|
||||
};
|
||||
|
@ -41,7 +44,10 @@ export type InsertJSON = {
|
|||
export type DeleteJSON = {
|
||||
type: "delete";
|
||||
data: {
|
||||
run: RunJSON;
|
||||
run: {
|
||||
runProperty: RunPropertyJSON;
|
||||
children: RunChildJSON[];
|
||||
};
|
||||
author: string;
|
||||
data: string;
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "docx-wasm",
|
||||
"version": "0.0.44",
|
||||
"version": "0.0.47",
|
||||
"main": "dist/node/index.js",
|
||||
"browser": "dist/web/index.js",
|
||||
"author": "bokuweb <bokuweb12@gmail.com>",
|
||||
|
|
|
@ -2,12 +2,14 @@ use docx_rs;
|
|||
|
||||
pub fn create_special_indent(
|
||||
special_indent_kind: Option<docx_rs::SpecialIndentKind>,
|
||||
special_indent_size: Option<usize>,
|
||||
special_indent_size: Option<i32>,
|
||||
) -> Option<docx_rs::SpecialIndentType> {
|
||||
if let Some(kind) = special_indent_kind {
|
||||
let size = special_indent_size.unwrap_or_else(|| 0);
|
||||
match kind {
|
||||
docx_rs::SpecialIndentKind::FirstLine => Some(docx_rs::SpecialIndentType::FirstLine(size)),
|
||||
docx_rs::SpecialIndentKind::FirstLine => {
|
||||
Some(docx_rs::SpecialIndentType::FirstLine(size))
|
||||
}
|
||||
docx_rs::SpecialIndentKind::Hanging => Some(docx_rs::SpecialIndentType::Hanging(size)),
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -25,9 +25,9 @@ impl Level {
|
|||
impl Level {
|
||||
pub fn indent(
|
||||
mut self,
|
||||
left: usize,
|
||||
left: i32,
|
||||
special_indent_kind: Option<docx_rs::SpecialIndentKind>,
|
||||
special_indent_size: Option<usize>,
|
||||
special_indent_size: Option<i32>,
|
||||
) -> Self {
|
||||
let special_indent = create_special_indent(special_indent_kind, special_indent_size);
|
||||
self.0.paragraph_property = self.0.paragraph_property.indent(left, special_indent, None);
|
||||
|
|
|
@ -74,9 +74,9 @@ impl Paragraph {
|
|||
|
||||
pub fn indent(
|
||||
mut self,
|
||||
left: usize,
|
||||
left: i32,
|
||||
special_indent_kind: Option<docx_rs::SpecialIndentKind>,
|
||||
special_indent_size: Option<usize>,
|
||||
special_indent_size: Option<i32>,
|
||||
) -> Paragraph {
|
||||
let special_indent = create_special_indent(special_indent_kind, special_indent_size);
|
||||
self.0.property = self.0.property.indent(left, special_indent, None);
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue