Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add json definitions #40

Merged
merged 7 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions docs/1.index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docx-core/examples/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs::*;
use std::io::Read;

pub fn main() {
let mut file = File::open("./run.docx").unwrap();
let mut file = File::open("./10.docx").unwrap();
let mut buf = vec![];
file.read_to_end(&mut buf).unwrap();
dbg!(read_docx(&buf).unwrap().json());
Expand Down
2 changes: 1 addition & 1 deletion docx-core/src/documents/content_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl FromXML for ContentTypes {
Ok(XmlEvent::EndElement { .. }) => {
depth -= 1;
}
Err(_) => return Err(ReaderError::XMLReadError),
Err(_) => {}
_ => {}
}
}
Expand Down
19 changes: 19 additions & 0 deletions docx-core/src/documents/elements/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ impl Serialize for ParagraphChild {
t.serialize_field("data", r)?;
t.end()
}
ParagraphChild::Delete(ref r) => {
let mut t = serializer.serialize_struct("Delete", 2)?;
t.serialize_field("type", "delete")?;
t.serialize_field("data", r)?;
t.end()
}
ParagraphChild::BookmarkStart(ref r) => {
let mut t = serializer.serialize_struct("BookmarkStart", 2)?;
t.serialize_field("type", "bookmarkStart")?;
t.serialize_field("data", r)?;
t.end()
}
ParagraphChild::BookmarkEnd(ref r) => {
let mut t = serializer.serialize_struct("BookmarkEnd", 2)?;
t.serialize_field("type", "bookmarkEnd")?;
t.serialize_field("data", r)?;
t.end()
}
// TODO: Add comment later
_ => {
let mut t = serializer.serialize_struct("Unsupported", 2)?;
t.serialize_field("type", "unsupported")?;
Expand Down
4 changes: 2 additions & 2 deletions docx-core/src/reader/document_rels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub fn read_document_rels(
.ok_or(ReaderError::DocumentRelsNotFoundError)?;
let p = find_rels_filename(&main_path)?;
let p = p.to_str().ok_or(ReaderError::DocumentRelsNotFoundError)?;
let rels_xml = archive.by_name(&p)?;
let rels = read_rels_xml(rels_xml, dir)?;
let data = read_zip(archive, &p)?;
let rels = read_rels_xml(&data[..], dir)?;
Ok(rels)
}

Expand Down
35 changes: 25 additions & 10 deletions docx-core/src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod level;
mod numbering_property;
mod numberings;
mod paragraph;
mod read_zip;
mod rels;
mod run;
mod style;
Expand All @@ -27,6 +28,7 @@ pub use attributes::*;
pub use document_rels::*;
pub use errors::ReaderError;
pub use from_xml::*;
pub use read_zip::*;
pub use xml_element::*;

const DOC_RELATIONSHIP_TYPE: &str =
Expand All @@ -41,36 +43,49 @@ pub fn read_docx(buf: &[u8]) -> Result<Docx, ReaderError> {
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)
let content_types_xml = archive.by_name("[Content_Types].xml")?;
let _content_types = ContentTypes::from_xml(content_types_xml)?;
let _content_types = {
let data = read_zip(&mut archive, "[Content_Types].xml")?;
ContentTypes::from_xml(&data[..])?
};

// 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)
let rels_xml = archive.by_name("_rels/.rels")?;
let rels = Rels::from_xml(rels_xml)?;
let rels = {
let data = read_zip(&mut archive, "_rels/.rels")?;
Rels::from_xml(&data[..])?
};
// 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)?;
let document_xml = archive.by_name(&main_rel.2)?;
let document = Document::from_xml(document_xml)?;
let document = {
let data = read_zip(&mut archive, &main_rel.2)?;
Document::from_xml(&data[..])?
};
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);
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)?;
let data = read_zip(
&mut archive,
style_path.to_str().expect("should have styles"),
)?;
let styles = Styles::from_xml(&data[..])?;
docx = docx.styles(styles);
}

// Read numberings
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)?;
let data = read_zip(
&mut archive,
num_path.to_str().expect("should have numberings"),
)?;
let nums = Numberings::from_xml(&data[..])?;
docx = docx.numberings(nums);
}

Expand Down
18 changes: 6 additions & 12 deletions docx-core/src/reader/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ mod tests {

#[test]
fn test_read_indent() {
let c =
r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
let c = r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:p>
<w:pPr>
<w:ind w:left="1470" w:right="1270" w:hanging="0"/>
Expand Down Expand Up @@ -161,8 +160,7 @@ mod tests {

#[test]
fn test_read_jc() {
let c =
r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
let c = r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:p>
<w:pPr>
<w:jc w:val="left"/>
Expand Down Expand Up @@ -191,8 +189,7 @@ mod tests {

#[test]
fn test_read_numbering() {
let c =
r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
let c = r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:p>
<w:pPr>
<w:numPr>
Expand Down Expand Up @@ -227,8 +224,7 @@ mod tests {

#[test]
fn test_read_insert() {
let c =
r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
let c = r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:p>
<w:ins w:id="0" w:author="unknown" w:date="2019-11-15T14:19:04Z">
<w:r>
Expand Down Expand Up @@ -264,8 +260,7 @@ mod tests {

#[test]
fn test_read_delete() {
let c =
r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
let c = r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:p>
<w:del w:id="3" w:author="unknown" w:date="2019-11-15T14:19:04Z">
<w:r>
Expand Down Expand Up @@ -301,8 +296,7 @@ mod tests {

#[test]
fn test_read_bookmark() {
let c =
r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
let c = r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:p>
<w:bookmarkStart w:id="0" w:name="ABCD-1234"/>
<w:r>
Expand Down
24 changes: 24 additions & 0 deletions docx-core/src/reader/read_zip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::io::{Cursor, Read};
use zip;

use super::ReaderError;

pub fn read_zip(
archive: &mut zip::read::ZipArchive<Cursor<&[u8]>>,
name: &str,
) -> Result<Vec<u8>, ReaderError> {
let mut p = name.to_owned();
if p.starts_with('/') {
p.remove(0);
}
let mut xml = archive.by_name(&p)?;
let mut data = vec![];
xml.read_to_end(&mut data).unwrap();
// Remove BOM
if (data[0] == 0xef) && (data[1] == 0xbb) && (data[2] == 0xbf) {
data.remove(0);
data.remove(0);
data.remove(0);
}
Ok(data)
}
3 changes: 1 addition & 2 deletions docx-core/src/reader/styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ mod tests {

#[test]
fn test_from_xml() {
let xml =
r#"<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
let xml = r#"<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:style w:type="character" w:styleId="FootnoteTextChar">
<w:name w:val="Footnote Text Char"></w:name>
<w:rPr>
Expand Down
6 changes: 2 additions & 4 deletions docx-core/src/reader/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ mod tests {

#[test]
fn test_read_table_with_width_prop() {
let c =
r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
let c = r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:tbl>
<w:tblPr>
<w:tblW w:w="9638" w:type="dxa"/>
Expand All @@ -97,8 +96,7 @@ mod tests {

#[test]
fn test_read_table_with_layout() {
let c =
r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
let c = r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:tbl>
<w:tblPr>
<w:jc w:val="center"/>
Expand Down
3 changes: 1 addition & 2 deletions docx-core/src/reader/table_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ mod tests {

#[test]
fn test_read_cell_with_prop() {
let c =
r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
let c = r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:tc>
<w:tcPr>
<w:tcW w:w="6425" w:type="dxa"/>
Expand Down
7 changes: 6 additions & 1 deletion docx-core/src/types/alignment_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ pub enum AlignmentType {
Center,
Left,
Right,
Both,
Justified,
Unsupported,
}

impl fmt::Display for AlignmentType {
Expand All @@ -19,7 +21,9 @@ impl fmt::Display for AlignmentType {
AlignmentType::Center => write!(f, "center"),
AlignmentType::Left => write!(f, "left"),
AlignmentType::Right => write!(f, "right"),
AlignmentType::Both => write!(f, "both"),
AlignmentType::Justified => write!(f, "justified"),
_ => write!(f, "unsupported"),
}
}
}
Expand All @@ -31,8 +35,9 @@ impl FromStr for AlignmentType {
"left" => Ok(AlignmentType::Left),
"right" => Ok(AlignmentType::Right),
"center" => Ok(AlignmentType::Center),
"both" => Ok(AlignmentType::Both),
"justified" => Ok(AlignmentType::Justified),
_ => Err(errors::TypeError::FromStrError),
_ => Ok(AlignmentType::Unsupported),
}
}
}
4 changes: 3 additions & 1 deletion docx-core/src/types/break_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum BreakType {
Page,
Column,
TextWrapping,
Unsupported,
}

impl fmt::Display for BreakType {
Expand All @@ -24,6 +25,7 @@ impl fmt::Display for BreakType {
BreakType::Page => write!(f, "page"),
BreakType::Column => write!(f, "column"),
BreakType::TextWrapping => write!(f, "textWrapping"),
BreakType::Unsupported => write!(f, "unsupported"),
}
}
}
Expand All @@ -35,7 +37,7 @@ impl FromStr for BreakType {
"page" => Ok(BreakType::Page),
"column" => Ok(BreakType::Column),
"textWrapping" => Ok(BreakType::TextWrapping),
_ => Err(errors::TypeError::FromStrError),
_ => Ok(BreakType::Unsupported),
}
}
}
3 changes: 3 additions & 0 deletions docx-core/src/types/style_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub enum StyleType {
Paragraph,
Character,
Numbering,
Table,
Unsupported,
}

Expand All @@ -21,6 +22,7 @@ impl fmt::Display for StyleType {
StyleType::Paragraph => write!(f, "paragraph"),
StyleType::Character => write!(f, "character"),
StyleType::Numbering => write!(f, "numbering"),
StyleType::Table => write!(f, "table"),
StyleType::Unsupported => write!(f, "unsupported"),
}
}
Expand All @@ -33,6 +35,7 @@ impl FromStr for StyleType {
"paragraph" => Ok(StyleType::Paragraph),
"character" => Ok(StyleType::Character),
"numbering" => Ok(StyleType::Numbering),
"table" => Ok(StyleType::Table),
_ => Ok(StyleType::Unsupported),
}
}
Expand Down
4 changes: 3 additions & 1 deletion docx-core/src/types/vertical_merge_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ use std::str::FromStr;
pub enum VMergeType {
Continue,
Restart,
Unsupported,
}

impl fmt::Display for VMergeType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
VMergeType::Continue => write!(f, "continue"),
VMergeType::Restart => write!(f, "restart"),
VMergeType::Unsupported => write!(f, "unsupported"),
}
}
}
Expand All @@ -26,7 +28,7 @@ impl FromStr for VMergeType {
match s {
"continue" => Ok(VMergeType::Continue),
"restart" => Ok(VMergeType::Restart),
_ => Err(errors::TypeError::FromStrError),
_ => Ok(VMergeType::Unsupported),
}
}
}
4 changes: 3 additions & 1 deletion docx-core/src/types/width_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum WidthType {
DXA,
Auto,
Pct,
Unsupported,
}

impl fmt::Display for WidthType {
Expand All @@ -19,6 +20,7 @@ impl fmt::Display for WidthType {
WidthType::DXA => write!(f, "dxa"),
WidthType::Auto => write!(f, "auto"),
WidthType::Pct => write!(f, "pct"),
WidthType::Unsupported => write!(f, "unsupported"),
}
}
}
Expand All @@ -30,7 +32,7 @@ impl FromStr for WidthType {
"dxa" => Ok(WidthType::DXA),
"auto" => Ok(WidthType::Auto),
"pct" => Ok(WidthType::Pct),
_ => Err(errors::TypeError::FromStrError),
_ => Ok(WidthType::Unsupported),
}
}
}
Loading