2020-04-07 04:24:56 +03:00
|
|
|
mod a_graphic;
|
|
|
|
mod a_graphic_data;
|
2020-02-11 10:01:39 +02:00
|
|
|
mod attributes;
|
|
|
|
mod delete;
|
|
|
|
mod document;
|
2020-02-12 09:03:11 +02:00
|
|
|
mod document_rels;
|
2020-04-07 04:24:56 +03:00
|
|
|
mod drawing;
|
2020-02-11 10:01:39 +02:00
|
|
|
mod errors;
|
|
|
|
mod from_xml;
|
2020-06-12 11:42:16 +03:00
|
|
|
mod ignore;
|
2020-02-11 10:01:39 +02:00
|
|
|
mod insert;
|
2020-02-12 12:19:38 +02:00
|
|
|
mod level;
|
2020-06-08 07:41:13 +03:00
|
|
|
mod level_override;
|
2020-04-07 04:24:56 +03:00
|
|
|
mod mc_fallback;
|
2020-02-11 10:01:39 +02:00
|
|
|
mod numbering_property;
|
2020-02-12 12:19:38 +02:00
|
|
|
mod numberings;
|
2020-02-11 10:01:39 +02:00
|
|
|
mod paragraph;
|
2020-02-13 09:14:06 +02:00
|
|
|
mod read_zip;
|
2020-02-11 10:01:39 +02:00
|
|
|
mod rels;
|
|
|
|
mod run;
|
2020-06-08 07:41:13 +03:00
|
|
|
mod run_property;
|
2020-02-12 08:44:53 +02:00
|
|
|
mod style;
|
|
|
|
mod styles;
|
2020-02-11 10:01:39 +02:00
|
|
|
mod table;
|
|
|
|
mod table_cell;
|
2020-04-27 06:06:19 +03:00
|
|
|
mod table_cell_borders;
|
2020-02-11 10:01:39 +02:00
|
|
|
mod table_row;
|
2020-04-07 04:24:56 +03:00
|
|
|
mod text_box_content;
|
|
|
|
mod wp_anchor;
|
|
|
|
mod wps_shape;
|
|
|
|
mod wps_text_box;
|
2020-02-11 10:01:39 +02:00
|
|
|
mod xml_element;
|
|
|
|
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
|
|
|
use crate::documents::*;
|
|
|
|
|
|
|
|
pub use attributes::*;
|
2020-02-12 09:03:11 +02:00
|
|
|
pub use document_rels::*;
|
2020-02-11 10:01:39 +02:00
|
|
|
pub use errors::ReaderError;
|
|
|
|
pub use from_xml::*;
|
2020-04-07 04:24:56 +03:00
|
|
|
pub use mc_fallback::*;
|
2020-02-13 09:14:06 +02:00
|
|
|
pub use read_zip::*;
|
2020-02-11 10:01:39 +02:00
|
|
|
pub use xml_element::*;
|
|
|
|
|
|
|
|
const DOC_RELATIONSHIP_TYPE: &str =
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
|
2020-02-12 09:03:11 +02:00
|
|
|
const STYLE_RELATIONSHIP_TYPE: &str =
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";
|
2020-02-12 12:19:38 +02:00
|
|
|
const NUMBERING_RELATIONSHIP_TYPE: &str =
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering";
|
2020-02-11 10:01:39 +02:00
|
|
|
|
|
|
|
pub fn read_docx(buf: &[u8]) -> Result<Docx, ReaderError> {
|
|
|
|
let cur = Cursor::new(buf);
|
|
|
|
let mut archive = zip::ZipArchive::new(cur)?;
|
|
|
|
// First, the content type for relationship parts and the Main Document part
|
|
|
|
// (the only required part) must be defined (physically located at /[Content_Types].xml in the package)
|
2020-02-13 09:14:06 +02:00
|
|
|
let _content_types = {
|
|
|
|
let data = read_zip(&mut archive, "[Content_Types].xml")?;
|
|
|
|
ContentTypes::from_xml(&data[..])?
|
|
|
|
};
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
// Next, the single required relationship (the package-level relationship to the Main Document part)
|
|
|
|
// must be defined (physically located at /_rels/.rels in the package)
|
2020-02-13 09:14:06 +02:00
|
|
|
let rels = {
|
|
|
|
let data = read_zip(&mut archive, "_rels/.rels")?;
|
|
|
|
Rels::from_xml(&data[..])?
|
|
|
|
};
|
2020-02-11 10:01:39 +02:00
|
|
|
// Finally, the minimum content for the Main Document part must be defined
|
|
|
|
// (physically located at /document.xml in the package):
|
|
|
|
let main_rel = rels
|
|
|
|
.find_target(DOC_RELATIONSHIP_TYPE)
|
|
|
|
.ok_or(ReaderError::DocumentNotFoundError)?;
|
2020-02-13 09:14:06 +02:00
|
|
|
let document = {
|
|
|
|
let data = read_zip(&mut archive, &main_rel.2)?;
|
|
|
|
Document::from_xml(&data[..])?
|
|
|
|
};
|
2020-02-12 19:57:48 +02:00
|
|
|
let mut docx = Docx::new().document(document);
|
2020-02-12 09:03:11 +02:00
|
|
|
// Read document relationships
|
|
|
|
let rels = read_document_rels(&mut archive, &main_rel.2)?;
|
2020-02-12 12:19:38 +02:00
|
|
|
|
|
|
|
// Read styles
|
2020-02-12 19:57:48 +02:00
|
|
|
let style_path = rels.find_target_path(STYLE_RELATIONSHIP_TYPE);
|
|
|
|
if let Some(style_path) = style_path {
|
2020-02-13 09:14:06 +02:00
|
|
|
let data = read_zip(
|
|
|
|
&mut archive,
|
|
|
|
style_path.to_str().expect("should have styles"),
|
|
|
|
)?;
|
|
|
|
let styles = Styles::from_xml(&data[..])?;
|
2020-02-12 19:57:48 +02:00
|
|
|
docx = docx.styles(styles);
|
|
|
|
}
|
2020-02-12 09:03:11 +02:00
|
|
|
|
2020-02-12 12:19:38 +02:00
|
|
|
// Read numberings
|
2020-02-12 19:57:48 +02:00
|
|
|
let num_path = rels.find_target_path(NUMBERING_RELATIONSHIP_TYPE);
|
|
|
|
if let Some(num_path) = num_path {
|
2020-02-13 09:14:06 +02:00
|
|
|
let data = read_zip(
|
|
|
|
&mut archive,
|
|
|
|
num_path.to_str().expect("should have numberings"),
|
|
|
|
)?;
|
|
|
|
let nums = Numberings::from_xml(&data[..])?;
|
2020-02-12 19:57:48 +02:00
|
|
|
docx = docx.numberings(nums);
|
|
|
|
}
|
2020-02-12 12:19:38 +02:00
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
Ok(docx)
|
|
|
|
}
|