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