diff --git a/docx-core/src/documents/elements/bold.rs b/docx-core/src/documents/elements/bold.rs new file mode 100644 index 0000000..0bb3c68 --- /dev/null +++ b/docx-core/src/documents/elements/bold.rs @@ -0,0 +1,18 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct Bold {} + +impl Bold { + pub fn new() -> Bold { + Bold {} + } +} + +impl BuildXML for Bold { + fn build(&self) -> Vec { + let b = XMLBuilder::new(); + b.b().build() + } +} diff --git a/docx-core/src/documents/elements/bold_cs.rs b/docx-core/src/documents/elements/bold_cs.rs new file mode 100644 index 0000000..866a6c2 --- /dev/null +++ b/docx-core/src/documents/elements/bold_cs.rs @@ -0,0 +1,18 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct BoldCs {} + +impl BoldCs { + pub fn new() -> BoldCs { + BoldCs {} + } +} + +impl BuildXML for BoldCs { + fn build(&self) -> Vec { + let b = XMLBuilder::new(); + b.b_cs().build() + } +} diff --git a/docx-core/src/documents/elements/highlight.rs b/docx-core/src/documents/elements/highlight.rs new file mode 100644 index 0000000..a7746be --- /dev/null +++ b/docx-core/src/documents/elements/highlight.rs @@ -0,0 +1,38 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct Highlight { + val: String, +} + +impl Highlight { + pub fn new(val: impl Into) -> Highlight { + Highlight { val: val.into() } + } +} + +impl BuildXML for Highlight { + fn build(&self) -> Vec { + XMLBuilder::new().highlight(&self.val).build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_highlight() { + let c = Highlight::new("FFFFFF"); + let b = c.build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } +} diff --git a/docx-core/src/documents/elements/italic.rs b/docx-core/src/documents/elements/italic.rs new file mode 100644 index 0000000..68ecaff --- /dev/null +++ b/docx-core/src/documents/elements/italic.rs @@ -0,0 +1,18 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct Italic {} + +impl Italic { + pub fn new() -> Italic { + Italic {} + } +} + +impl BuildXML for Italic { + fn build(&self) -> Vec { + let b = XMLBuilder::new(); + b.i().build() + } +} diff --git a/docx-core/src/documents/elements/italic_cs.rs b/docx-core/src/documents/elements/italic_cs.rs new file mode 100644 index 0000000..1a62fff --- /dev/null +++ b/docx-core/src/documents/elements/italic_cs.rs @@ -0,0 +1,18 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct ItalicCs {} + +impl ItalicCs { + pub fn new() -> ItalicCs { + ItalicCs {} + } +} + +impl BuildXML for ItalicCs { + fn build(&self) -> Vec { + let b = XMLBuilder::new(); + b.i_cs().build() + } +} diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index df6a6d2..4fc42af 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -1,8 +1,13 @@ mod based_on; +mod bold; +mod bold_cs; mod color; mod doc_defaults; mod grid_span; +mod highlight; mod indent; +mod italic; +mod italic_cs; mod justification; mod name; mod next; @@ -33,10 +38,15 @@ mod text; mod vertical_merge; pub use based_on::*; +pub use bold::*; +pub use bold_cs::*; pub use color::*; pub use doc_defaults::*; pub use grid_span::*; +pub use highlight::*; pub use indent::*; +pub use italic::*; +pub use italic_cs::*; pub use justification::*; pub use name::*; pub use next::*; diff --git a/docx-core/src/documents/elements/run.rs b/docx-core/src/documents/elements/run.rs index 376c3bf..958e62d 100644 --- a/docx-core/src/documents/elements/run.rs +++ b/docx-core/src/documents/elements/run.rs @@ -20,6 +20,26 @@ impl Run { self.run_property = self.run_property.size(size); self } + + pub fn color(mut self, color: &str) -> Run { + self.run_property = self.run_property.color(color); + self + } + + pub fn highlight(mut self, color: &str) -> Run { + self.run_property = self.run_property.highlight(color); + self + } + + pub fn bold(mut self) -> Run { + self.run_property = self.run_property.bold(); + self + } + + pub fn italic(mut self) -> Run { + self.run_property = self.run_property.italic(); + self + } } impl Default for Run { diff --git a/docx-core/src/documents/elements/run_property.rs b/docx-core/src/documents/elements/run_property.rs index e7728b4..86c6101 100644 --- a/docx-core/src/documents/elements/run_property.rs +++ b/docx-core/src/documents/elements/run_property.rs @@ -1,4 +1,4 @@ -use super::{Color, Sz, SzCs}; +use super::{Bold, BoldCs, Color, Highlight, Italic, ItalicCs, Sz, SzCs}; use crate::documents::BuildXML; use crate::xml_builder::*; @@ -7,6 +7,11 @@ pub struct RunProperty { sz: Option, sz_cs: Option, color: Option, + highlight: Option, + bold: Option, + bold_cs: Option, + italic: Option, + italic_cs: Option, } impl RunProperty { @@ -24,6 +29,23 @@ impl RunProperty { self.color = Some(Color::new(color)); self } + + pub fn highlight(mut self, color: &str) -> RunProperty { + self.highlight = Some(Highlight::new(color)); + self + } + + pub fn bold(mut self) -> RunProperty { + self.bold = Some(Bold::new()); + self.bold_cs = Some(BoldCs::new()); + self + } + + pub fn italic(mut self) -> RunProperty { + self.italic = Some(Italic::new()); + self.italic_cs = Some(ItalicCs::new()); + self + } } impl Default for RunProperty { @@ -32,6 +54,11 @@ impl Default for RunProperty { color: None, sz: None, sz_cs: None, + highlight: None, + bold: None, + bold_cs: None, + italic: None, + italic_cs: None, } } } @@ -43,6 +70,11 @@ impl BuildXML for RunProperty { .add_optional_child(&self.sz) .add_optional_child(&self.sz_cs) .add_optional_child(&self.color) + .add_optional_child(&self.bold) + .add_optional_child(&self.bold_cs) + .add_optional_child(&self.italic) + .add_optional_child(&self.italic_cs) + .add_optional_child(&self.highlight) .close() .build() } @@ -57,7 +89,7 @@ mod tests { use std::str; #[test] - fn test_build() { + fn test_size() { let c = RunProperty::new().size(10).color("FFFFFF"); let b = c.build(); assert_eq!( @@ -65,4 +97,24 @@ mod tests { r#""# ); } + + #[test] + fn test_highlight() { + let c = RunProperty::new().highlight("FFFFFF"); + let b = c.build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } + + #[test] + fn test_bold() { + let c = RunProperty::new().bold(); + let b = c.build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } } diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index c024ccb..c212f2d 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -42,6 +42,12 @@ impl XMLBuilder { only_usize_val_el!(sz, "w:sz"); // i.e. only_usize_val_el!(sz_cs, "w:szCs"); + + closed_el!(b, "w:b"); + closed_el!(b_cs, "w:bCs"); + + closed_el!(i, "w:i"); + closed_el!(i_cs, "w:iCs"); // Build w:style element // i.e. pub(crate) fn open_style(mut self, style_type: StyleType, id: &str) -> Self { @@ -55,20 +61,13 @@ impl XMLBuilder { self } // i.e. - pub(crate) fn next(mut self, val: &str) -> Self { - self.writer - .write(XmlEvent::start_element("w:next").attr("w:val", val)) - .expect(EXPECT_MESSAGE); - self.close() - } + only_str_val_el!(next, "w:next"); // i.e. - pub(crate) fn color(mut self, val: &str) -> Self { - self.writer - .write(XmlEvent::start_element("w:color").attr("w:val", val)) - .expect(EXPECT_MESSAGE); - self.close() - } + only_str_val_el!(color, "w:color"); + + // i.e. + only_str_val_el!(highlight, "w:highlight"); // i.e. pub(crate) fn indent(mut self, left: usize, special_indent: Option) -> Self { diff --git a/docx-core/tests/lib.rs b/docx-core/tests/lib.rs index 84bfe30..a926382 100644 --- a/docx-core/tests/lib.rs +++ b/docx-core/tests/lib.rs @@ -135,3 +135,33 @@ pub fn table_merged() -> Result<(), DocxError> { Docx::new().add_table(table).build().pack(file)?; Ok(()) } + +#[test] +pub fn decoration() -> Result<(), DocxError> { + let path = std::path::Path::new("./tests/output/decoration.docx"); + let file = std::fs::File::create(&path).unwrap(); + Docx::new() + .add_paragraph( + Paragraph::new() + .add_run(Run::new("Hello")) + .add_run(Run::new(" World").bold()), + ) + .add_paragraph( + Paragraph::new() + .add_run(Run::new("Hello")) + .add_run(Run::new(" World").highlight("yellow")), + ) + .add_paragraph( + Paragraph::new() + .add_run(Run::new("Hello")) + .add_run(Run::new(" World").italic()), + ) + .add_paragraph( + Paragraph::new() + .add_run(Run::new("Hello")) + .add_run(Run::new(" World").color("FF0000")), + ) + .build() + .pack(file)?; + Ok(()) +} diff --git a/docx-core/tests/output/[Content_Types].xml b/docx-core/tests/output/[Content_Types].xml new file mode 100755 index 0000000..20999db --- /dev/null +++ b/docx-core/tests/output/[Content_Types].xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/docx-core/tests/output/_rels/.rels b/docx-core/tests/output/_rels/.rels new file mode 100755 index 0000000..2bee028 --- /dev/null +++ b/docx-core/tests/output/_rels/.rels @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/docx-core/tests/output/docProps/app.xml b/docx-core/tests/output/docProps/app.xml new file mode 100755 index 0000000..c577465 --- /dev/null +++ b/docx-core/tests/output/docProps/app.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/docx-core/tests/output/docProps/core.xml b/docx-core/tests/output/docProps/core.xml new file mode 100755 index 0000000..3ecca83 --- /dev/null +++ b/docx-core/tests/output/docProps/core.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/docx-core/tests/output/word/_rels/document.xml.rels b/docx-core/tests/output/word/_rels/document.xml.rels new file mode 100755 index 0000000..2bee028 --- /dev/null +++ b/docx-core/tests/output/word/_rels/document.xml.rels @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/docx-core/tests/output/word/document.xml b/docx-core/tests/output/word/document.xml new file mode 100755 index 0000000..1de8501 --- /dev/null +++ b/docx-core/tests/output/word/document.xml @@ -0,0 +1,81 @@ + + + + + + + + + + + Hello + + + + + + + World + + + + + + + + + + Hello + + + + + + World + + + + + + + + + + Hello + + + + + + + World + + + + + + + + + + Hello + + + + + + World + + + + \ No newline at end of file diff --git a/docx-core/tests/output/word/styles.xml b/docx-core/tests/output/word/styles.xml new file mode 100755 index 0000000..209712f --- /dev/null +++ b/docx-core/tests/output/word/styles.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/decoration/[Content_Types].xml b/fixtures/decoration/[Content_Types].xml new file mode 100644 index 0000000..dc111cb --- /dev/null +++ b/fixtures/decoration/[Content_Types].xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/fixtures/decoration/_rels/.rels b/fixtures/decoration/_rels/.rels new file mode 100644 index 0000000..f0b72e7 --- /dev/null +++ b/fixtures/decoration/_rels/.rels @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/fixtures/decoration/decoration.docx b/fixtures/decoration/decoration.docx new file mode 100644 index 0000000..77f87d3 Binary files /dev/null and b/fixtures/decoration/decoration.docx differ diff --git a/fixtures/decoration/docProps/app.xml b/fixtures/decoration/docProps/app.xml new file mode 100644 index 0000000..362b614 --- /dev/null +++ b/fixtures/decoration/docProps/app.xml @@ -0,0 +1,2 @@ + +2LibreOffice/6.0.7.3$Linux_X86_64 LibreOffice_project/00m0$Build-31848524 \ No newline at end of file diff --git a/fixtures/decoration/docProps/core.xml b/fixtures/decoration/docProps/core.xml new file mode 100644 index 0000000..366ab2c --- /dev/null +++ b/fixtures/decoration/docProps/core.xml @@ -0,0 +1,2 @@ + +2019-11-13T15:08:42Zja-JP2019-11-13T15:10:42Z1 \ No newline at end of file diff --git a/fixtures/decoration/word/_rels/document.xml.rels b/fixtures/decoration/word/_rels/document.xml.rels new file mode 100644 index 0000000..8a2db8a --- /dev/null +++ b/fixtures/decoration/word/_rels/document.xml.rels @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/fixtures/decoration/word/document.xml b/fixtures/decoration/word/document.xml new file mode 100644 index 0000000..369665a --- /dev/null +++ b/fixtures/decoration/word/document.xml @@ -0,0 +1,135 @@ + + + + + + + + + + + Hello + + + + + + + World + + + + !! + + + + + + + + + + Hello + + + + + + + + World + + + + !! + + + + + + + + + + + + + + Hello + + + + + + + + + + World + + + + + + !! + + + + + + + + + + + + + + + + + + + Hello + + + + + + + + + + + World + + + + + + + + !! + + + + + + + + + + + + + \ No newline at end of file diff --git a/fixtures/decoration/word/fontTable.xml b/fixtures/decoration/word/fontTable.xml new file mode 100644 index 0000000..94f56db --- /dev/null +++ b/fixtures/decoration/word/fontTable.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/fixtures/decoration/word/settings.xml b/fixtures/decoration/word/settings.xml new file mode 100644 index 0000000..97dba84 --- /dev/null +++ b/fixtures/decoration/word/settings.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/fixtures/decoration/word/styles.xml b/fixtures/decoration/word/styles.xml new file mode 100644 index 0000000..72ec81e --- /dev/null +++ b/fixtures/decoration/word/styles.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file