From 1ab91a692481b3dc0b84676b8140855f552b3715 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Thu, 13 Feb 2020 02:57:48 +0900 Subject: [PATCH] Update npm pkg (#39) * fix: export read docx * fix: numberings --- docx-core/examples/reader.rs | 2 +- docx-core/src/reader/errors.rs | 2 ++ docx-core/src/reader/mod.rs | 28 +++++++++++++--------------- docx-wasm/js/index.ts | 4 ++++ docx-wasm/package.json | 2 +- docx-wasm/src/reader.rs | 2 +- 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/docx-core/examples/reader.rs b/docx-core/examples/reader.rs index ca28116..59cc831 100644 --- a/docx-core/examples/reader.rs +++ b/docx-core/examples/reader.rs @@ -3,7 +3,7 @@ use std::fs::*; use std::io::Read; 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![]; file.read_to_end(&mut buf).unwrap(); dbg!(read_docx(&buf).unwrap().json()); diff --git a/docx-core/src/reader/errors.rs b/docx-core/src/reader/errors.rs index d7387dd..62effd4 100644 --- a/docx-core/src/reader/errors.rs +++ b/docx-core/src/reader/errors.rs @@ -16,6 +16,8 @@ pub enum ReaderError { DocumentRelsNotFoundError, #[error("Failed to find styles.")] DocumentStylesNotFoundError, + #[error("Failed to find numberings.")] + DocumentNumberingsNotFoundError, #[error("Unknown error")] Unknown, } diff --git a/docx-core/src/reader/mod.rs b/docx-core/src/reader/mod.rs index 3565bb2..9a9bcd8 100644 --- a/docx-core/src/reader/mod.rs +++ b/docx-core/src/reader/mod.rs @@ -54,27 +54,25 @@ pub fn read_docx(buf: &[u8]) -> Result { .ok_or(ReaderError::DocumentNotFoundError)?; let document_xml = archive.by_name(&main_rel.2)?; let document = Document::from_xml(document_xml)?; - + let mut docx = Docx::new().document(document); // Read document relationships let rels = read_document_rels(&mut archive, &main_rel.2)?; // Read styles - let style_path = rels - .find_target_path(STYLE_RELATIONSHIP_TYPE) - .ok_or(ReaderError::DocumentStylesNotFoundError)?; - let styles_xml = archive.by_name(style_path.to_str().expect("should have styles"))?; - let styles = Styles::from_xml(styles_xml)?; + let style_path = rels.find_target_path(STYLE_RELATIONSHIP_TYPE); + if let Some(style_path) = style_path { + let styles_xml = archive.by_name(style_path.to_str().expect("should have styles"))?; + let styles = Styles::from_xml(styles_xml)?; + docx = docx.styles(styles); + } // Read numberings - let num_path = rels - .find_target_path(NUMBERING_RELATIONSHIP_TYPE) - .ok_or(ReaderError::DocumentStylesNotFoundError)?; - let num_xml = archive.by_name(num_path.to_str().expect("should have numberings"))?; - let nums = Numberings::from_xml(num_xml)?; + let num_path = rels.find_target_path(NUMBERING_RELATIONSHIP_TYPE); + if let Some(num_path) = num_path { + let num_xml = archive.by_name(num_path.to_str().expect("should have numberings"))?; + let nums = Numberings::from_xml(num_xml)?; + docx = docx.numberings(nums); + } - let docx = Docx::new() - .document(document) - .styles(styles) - .numberings(nums); Ok(docx) } diff --git a/docx-wasm/js/index.ts b/docx-wasm/js/index.ts index 4584c2d..365d50a 100644 --- a/docx-wasm/js/index.ts +++ b/docx-wasm/js/index.ts @@ -286,6 +286,10 @@ export class Docx { } } +export const readDocx = (buf: Uint8Array) => { + return wasm.readDocx(buf); +}; + export * from "./paragraph"; export * from "./insert"; export * from "./delete"; diff --git a/docx-wasm/package.json b/docx-wasm/package.json index b484ff6..b4887cd 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.0.22", + "version": "0.0.23", "main": "dist/index.js", "author": "bokuweb ", "license": "MIT", diff --git a/docx-wasm/src/reader.rs b/docx-wasm/src/reader.rs index 6e83793..5102b31 100644 --- a/docx-wasm/src/reader.rs +++ b/docx-wasm/src/reader.rs @@ -1,8 +1,8 @@ use docx_rs; use wasm_bindgen::prelude::*; -#[wasm_bindgen(js_name=readError)] #[allow(non_snake_case)] +#[wasm_bindgen] pub fn readDocx(buf: &[u8]) -> Result { let mut d = docx_rs::read_docx(buf); match d {