diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index 51e05cd..a62847f 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -42,6 +42,7 @@ mod table_row; mod table_row_property; mod table_width; mod text; +mod underline; mod vertical_merge; mod zoom; @@ -89,5 +90,6 @@ pub use table_row::*; pub use table_row_property::*; pub use table_width::*; pub use text::*; +pub use underline::*; pub use vertical_merge::*; pub use zoom::*; diff --git a/docx-core/src/documents/elements/run.rs b/docx-core/src/documents/elements/run.rs index dc7a58a..47b2b99 100644 --- a/docx-core/src/documents/elements/run.rs +++ b/docx-core/src/documents/elements/run.rs @@ -78,6 +78,11 @@ impl Run { self.run_property = self.run_property.italic(); self } + + pub fn underline(mut self, line_type: &str) -> Run { + self.run_property = self.run_property.underline(line_type); + self + } } impl BuildXML for Run { @@ -112,4 +117,13 @@ mod tests { r#"Hello"# ); } + + #[test] + fn test_underline() { + let b = Run::new().add_text("Hello").underline("single").build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#"Hello"# + ); + } } diff --git a/docx-core/src/documents/elements/run_property.rs b/docx-core/src/documents/elements/run_property.rs index 86c6101..0c01b8f 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::{Bold, BoldCs, Color, Highlight, Italic, ItalicCs, Sz, SzCs}; +use super::{Bold, BoldCs, Color, Highlight, Italic, ItalicCs, Sz, SzCs, Underline}; use crate::documents::BuildXML; use crate::xml_builder::*; @@ -8,6 +8,7 @@ pub struct RunProperty { sz_cs: Option, color: Option, highlight: Option, + underline: Option, bold: Option, bold_cs: Option, italic: Option, @@ -46,6 +47,11 @@ impl RunProperty { self.italic_cs = Some(ItalicCs::new()); self } + + pub fn underline(mut self, line_type: &str) -> RunProperty { + self.underline = Some(Underline::new(line_type)); + self + } } impl Default for RunProperty { @@ -55,6 +61,7 @@ impl Default for RunProperty { sz: None, sz_cs: None, highlight: None, + underline: None, bold: None, bold_cs: None, italic: None, @@ -75,6 +82,7 @@ impl BuildXML for RunProperty { .add_optional_child(&self.italic) .add_optional_child(&self.italic_cs) .add_optional_child(&self.highlight) + .add_optional_child(&self.underline) .close() .build() } @@ -117,4 +125,14 @@ mod tests { r#""# ); } + + #[test] + fn test_underline() { + let c = RunProperty::new().underline("single"); + let b = c.build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } } diff --git a/docx-core/src/documents/elements/underline.rs b/docx-core/src/documents/elements/underline.rs new file mode 100644 index 0000000..ba555f5 --- /dev/null +++ b/docx-core/src/documents/elements/underline.rs @@ -0,0 +1,35 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct Underline { + val: String, +} + +impl Underline { + pub fn new(val: impl Into) -> Underline { + Underline { val: val.into() } + } +} + +impl BuildXML for Underline { + fn build(&self) -> Vec { + XMLBuilder::new().underline(&self.val).build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_underline() { + let c = Underline::new("single"); + 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 74518a4..a5b552f 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -83,6 +83,9 @@ impl XMLBuilder { // i.e. only_str_val_el!(highlight, "w:highlight"); + // i.e. + only_str_val_el!(underline, "w:u"); + // i.e. pub(crate) fn indent(mut self, left: usize, special_indent: Option) -> Self { let left = &format!("{}", left); diff --git a/docx-core/tests/lib.rs b/docx-core/tests/lib.rs index 73eb01d..7a1cc50 100644 --- a/docx-core/tests/lib.rs +++ b/docx-core/tests/lib.rs @@ -230,3 +230,29 @@ pub fn history() -> Result<(), DocxError> { .pack(file)?; Ok(()) } + +#[test] +pub fn underline() -> Result<(), DocxError> { + let path = std::path::Path::new("./tests/output/underline.docx"); + let file = std::fs::File::create(&path).unwrap(); + Docx::new() + .add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello").underline("single"))) + .build() + .pack(file)?; + Ok(()) +} + +#[test] +pub fn highlight() -> Result<(), DocxError> { + let path = std::path::Path::new("./tests/output/highlight.docx"); + let file = std::fs::File::create(&path).unwrap(); + Docx::new() + .add_paragraph( + Paragraph::new() + .add_run(Run::new().add_text("Hello").highlight("cyan")) + .add_run(Run::new().add_text(" World!").highlight("yellow")), + ) + .build() + .pack(file)?; + Ok(()) +} diff --git a/fixtures/highlight_and_underline/[Content_Types].xml b/fixtures/highlight_and_underline/[Content_Types].xml new file mode 100644 index 0000000..dc111cb --- /dev/null +++ b/fixtures/highlight_and_underline/[Content_Types].xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/fixtures/highlight_and_underline/_rels/.rels b/fixtures/highlight_and_underline/_rels/.rels new file mode 100644 index 0000000..f0b72e7 --- /dev/null +++ b/fixtures/highlight_and_underline/_rels/.rels @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/fixtures/highlight_and_underline/docProps/app.xml b/fixtures/highlight_and_underline/docProps/app.xml new file mode 100644 index 0000000..f55b421 --- /dev/null +++ b/fixtures/highlight_and_underline/docProps/app.xml @@ -0,0 +1,2 @@ + +36LibreOffice/6.0.7.3$Linux_X86_64 LibreOffice_project/00m0$Build-316681 \ No newline at end of file diff --git a/fixtures/highlight_and_underline/docProps/core.xml b/fixtures/highlight_and_underline/docProps/core.xml new file mode 100644 index 0000000..8601f04 --- /dev/null +++ b/fixtures/highlight_and_underline/docProps/core.xml @@ -0,0 +1,2 @@ + +2019-11-22T13:53:22Zja-JP2019-11-22T15:50:33Z1 \ No newline at end of file diff --git a/fixtures/highlight_and_underline/highlight_and_underline.docx b/fixtures/highlight_and_underline/highlight_and_underline.docx new file mode 100644 index 0000000..5fe4312 Binary files /dev/null and b/fixtures/highlight_and_underline/highlight_and_underline.docx differ diff --git a/fixtures/highlight_and_underline/word/_rels/document.xml.rels b/fixtures/highlight_and_underline/word/_rels/document.xml.rels new file mode 100644 index 0000000..8a2db8a --- /dev/null +++ b/fixtures/highlight_and_underline/word/_rels/document.xml.rels @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/fixtures/highlight_and_underline/word/document.xml b/fixtures/highlight_and_underline/word/document.xml new file mode 100644 index 0000000..e7eb215 --- /dev/null +++ b/fixtures/highlight_and_underline/word/document.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + マーカー + + + + + + + + + + 下線 + + + + + + + + + + + + \ No newline at end of file diff --git a/fixtures/highlight_and_underline/word/fontTable.xml b/fixtures/highlight_and_underline/word/fontTable.xml new file mode 100644 index 0000000..94f56db --- /dev/null +++ b/fixtures/highlight_and_underline/word/fontTable.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/fixtures/highlight_and_underline/word/settings.xml b/fixtures/highlight_and_underline/word/settings.xml new file mode 100644 index 0000000..97dba84 --- /dev/null +++ b/fixtures/highlight_and_underline/word/settings.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/fixtures/highlight_and_underline/word/styles.xml b/fixtures/highlight_and_underline/word/styles.xml new file mode 100644 index 0000000..72e6f53 --- /dev/null +++ b/fixtures/highlight_and_underline/word/styles.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file