parent
93b21cee78
commit
1ab91a6924
|
@ -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("./fixtures/paragraph/paragraph.docx").unwrap();
|
let mut file = File::open("./run.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());
|
||||||
|
|
|
@ -16,6 +16,8 @@ pub enum ReaderError {
|
||||||
DocumentRelsNotFoundError,
|
DocumentRelsNotFoundError,
|
||||||
#[error("Failed to find styles.")]
|
#[error("Failed to find styles.")]
|
||||||
DocumentStylesNotFoundError,
|
DocumentStylesNotFoundError,
|
||||||
|
#[error("Failed to find numberings.")]
|
||||||
|
DocumentNumberingsNotFoundError,
|
||||||
#[error("Unknown error")]
|
#[error("Unknown error")]
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,27 +54,25 @@ pub fn read_docx(buf: &[u8]) -> Result<Docx, ReaderError> {
|
||||||
.ok_or(ReaderError::DocumentNotFoundError)?;
|
.ok_or(ReaderError::DocumentNotFoundError)?;
|
||||||
let document_xml = archive.by_name(&main_rel.2)?;
|
let document_xml = archive.by_name(&main_rel.2)?;
|
||||||
let document = Document::from_xml(document_xml)?;
|
let document = Document::from_xml(document_xml)?;
|
||||||
|
let mut docx = Docx::new().document(document);
|
||||||
// Read document relationships
|
// Read document relationships
|
||||||
let rels = read_document_rels(&mut archive, &main_rel.2)?;
|
let rels = read_document_rels(&mut archive, &main_rel.2)?;
|
||||||
|
|
||||||
// Read styles
|
// Read styles
|
||||||
let style_path = rels
|
let style_path = rels.find_target_path(STYLE_RELATIONSHIP_TYPE);
|
||||||
.find_target_path(STYLE_RELATIONSHIP_TYPE)
|
if let Some(style_path) = style_path {
|
||||||
.ok_or(ReaderError::DocumentStylesNotFoundError)?;
|
let styles_xml = archive.by_name(style_path.to_str().expect("should have styles"))?;
|
||||||
let styles_xml = archive.by_name(style_path.to_str().expect("should have styles"))?;
|
let styles = Styles::from_xml(styles_xml)?;
|
||||||
let styles = Styles::from_xml(styles_xml)?;
|
docx = docx.styles(styles);
|
||||||
|
}
|
||||||
|
|
||||||
// Read numberings
|
// Read numberings
|
||||||
let num_path = rels
|
let num_path = rels.find_target_path(NUMBERING_RELATIONSHIP_TYPE);
|
||||||
.find_target_path(NUMBERING_RELATIONSHIP_TYPE)
|
if let Some(num_path) = num_path {
|
||||||
.ok_or(ReaderError::DocumentStylesNotFoundError)?;
|
let num_xml = archive.by_name(num_path.to_str().expect("should have numberings"))?;
|
||||||
let num_xml = archive.by_name(num_path.to_str().expect("should have numberings"))?;
|
let nums = Numberings::from_xml(num_xml)?;
|
||||||
let nums = Numberings::from_xml(num_xml)?;
|
docx = docx.numberings(nums);
|
||||||
|
}
|
||||||
|
|
||||||
let docx = Docx::new()
|
|
||||||
.document(document)
|
|
||||||
.styles(styles)
|
|
||||||
.numberings(nums);
|
|
||||||
Ok(docx)
|
Ok(docx)
|
||||||
}
|
}
|
||||||
|
|
|
@ -286,6 +286,10 @@ export class Docx {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const readDocx = (buf: Uint8Array) => {
|
||||||
|
return wasm.readDocx(buf);
|
||||||
|
};
|
||||||
|
|
||||||
export * from "./paragraph";
|
export * from "./paragraph";
|
||||||
export * from "./insert";
|
export * from "./insert";
|
||||||
export * from "./delete";
|
export * from "./delete";
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "docx-wasm",
|
"name": "docx-wasm",
|
||||||
"version": "0.0.22",
|
"version": "0.0.23",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"author": "bokuweb <bokuweb12@gmail.com>",
|
"author": "bokuweb <bokuweb12@gmail.com>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
use docx_rs;
|
use docx_rs;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
#[wasm_bindgen(js_name=readError)]
|
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
|
#[wasm_bindgen]
|
||||||
pub fn readDocx(buf: &[u8]) -> Result<String, JsValue> {
|
pub fn readDocx(buf: &[u8]) -> Result<String, JsValue> {
|
||||||
let mut d = docx_rs::read_docx(buf);
|
let mut d = docx_rs::read_docx(buf);
|
||||||
match d {
|
match d {
|
||||||
|
|
Loading…
Reference in New Issue