diff --git a/docx-core/src/documents/elements/bookmark_end.rs b/docx-core/src/documents/elements/bookmark_end.rs new file mode 100644 index 0000000..d8ddeeb --- /dev/null +++ b/docx-core/src/documents/elements/bookmark_end.rs @@ -0,0 +1,39 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct BookmarkEnd<'a> { + id: &'a str, +} + +impl<'a> BookmarkEnd<'a> { + pub fn new(id: &'a str) -> BookmarkEnd<'a> { + BookmarkEnd { id } + } +} + +impl<'a> BuildXML for BookmarkEnd<'a> { + fn build(&self) -> Vec { + let b = XMLBuilder::new(); + b.bookmark_end(&self.id).build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_bookmark_end() { + let c = BookmarkEnd::new("mockid"); + let b = c.build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } +} diff --git a/docx-core/src/documents/elements/bookmark_start.rs b/docx-core/src/documents/elements/bookmark_start.rs new file mode 100644 index 0000000..dd14b0d --- /dev/null +++ b/docx-core/src/documents/elements/bookmark_start.rs @@ -0,0 +1,40 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct BookmarkStart<'a> { + id: &'a str, + name: &'a str, +} + +impl<'a> BookmarkStart<'a> { + pub fn new(id: &'a str, name: &'a str) -> BookmarkStart<'a> { + BookmarkStart { id, name } + } +} + +impl<'a> BuildXML for BookmarkStart<'a> { + fn build(&self) -> Vec { + let b = XMLBuilder::new(); + b.bookmark_start(&self.id, &self.name).build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_bookmark_start() { + let c = BookmarkStart::new("mockid", "mockname"); + let b = c.build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } +} diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index a62847f..b459a55 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -1,6 +1,8 @@ mod based_on; mod bold; mod bold_cs; +mod bookmark_end; +mod bookmark_start; mod br; mod color; mod default_tab_stop; @@ -49,6 +51,8 @@ mod zoom; pub use based_on::*; pub use bold::*; pub use bold_cs::*; +pub use bookmark_end::*; +pub use bookmark_start::*; pub use br::*; pub use color::*; pub use default_tab_stop::*; diff --git a/docx-core/src/documents/elements/paragraph.rs b/docx-core/src/documents/elements/paragraph.rs index 9fcf45c..243d056 100644 --- a/docx-core/src/documents/elements/paragraph.rs +++ b/docx-core/src/documents/elements/paragraph.rs @@ -1,4 +1,4 @@ -use super::{Delete, Insert, ParagraphProperty, Run}; +use super::{BookmarkEnd, BookmarkStart, Delete, Insert, ParagraphProperty, Run}; use crate::documents::BuildXML; use crate::types::*; use crate::xml_builder::*; @@ -25,6 +25,8 @@ pub enum ParagraphChild<'a> { Run(Run), Insert(Insert<'a>), Delete(Delete<'a>), + BookmarkStart(BookmarkStart<'a>), + BookmarkEnd(BookmarkEnd<'a>), } impl<'a> BuildXML for ParagraphChild<'a> { @@ -33,6 +35,8 @@ impl<'a> BuildXML for ParagraphChild<'a> { ParagraphChild::Run(v) => v.build(), ParagraphChild::Insert(v) => v.build(), ParagraphChild::Delete(v) => v.build(), + ParagraphChild::BookmarkStart(v) => v.build(), + ParagraphChild::BookmarkEnd(v) => v.build(), } } } @@ -62,6 +66,18 @@ impl<'a> Paragraph<'a> { self } + pub fn add_bookmark_start(mut self, id: &'a str, name: &'a str) -> Paragraph<'a> { + self.children + .push(ParagraphChild::BookmarkStart(BookmarkStart::new(id, name))); + self + } + + pub fn add_bookmark_end(mut self, id: &'a str) -> Paragraph<'a> { + self.children + .push(ParagraphChild::BookmarkEnd(BookmarkEnd::new(id))); + self + } + pub fn align(mut self, alignment_type: AlignmentType) -> Paragraph<'a> { self.property = self.property.align(alignment_type); self @@ -128,4 +144,17 @@ mod tests { r#"Hello"# ); } + + #[test] + fn test_bookmark() { + let b = Paragraph::new() + .add_bookmark_start("1234-5678", "article") + .add_run(Run::new().add_text("Hello")) + .add_bookmark_end("1234-5678") + .build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#"Hello"# + ); + } } diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index a5b552f..b43bf27 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -167,6 +167,9 @@ impl XMLBuilder { opened_el!(open_insert, "w:ins", "w:id", "w:author", "w:data"); opened_el!(open_delete, "w:del", "w:id", "w:author", "w:data"); + + closed_el!(bookmark_start, "w:bookmarkStart", "w:id", "w:name"); + closed_el!(bookmark_end, "w:bookmarkEnd", "w:id"); } #[cfg(test)] diff --git a/fixtures/bookmark/[Content_Types].xml b/fixtures/bookmark/[Content_Types].xml new file mode 100644 index 0000000..dc111cb --- /dev/null +++ b/fixtures/bookmark/[Content_Types].xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/fixtures/bookmark/_rels/.rels b/fixtures/bookmark/_rels/.rels new file mode 100644 index 0000000..f0b72e7 --- /dev/null +++ b/fixtures/bookmark/_rels/.rels @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/fixtures/bookmark/bookmark.docx b/fixtures/bookmark/bookmark.docx new file mode 100644 index 0000000..11e8ac6 Binary files /dev/null and b/fixtures/bookmark/bookmark.docx differ diff --git a/fixtures/bookmark/docProps/app.xml b/fixtures/bookmark/docProps/app.xml new file mode 100644 index 0000000..809ffb0 --- /dev/null +++ b/fixtures/bookmark/docProps/app.xml @@ -0,0 +1,2 @@ + +0LibreOffice/6.0.7.3$Linux_X86_64 LibreOffice_project/00m0$Build-31318192 \ No newline at end of file diff --git a/fixtures/bookmark/docProps/core.xml b/fixtures/bookmark/docProps/core.xml new file mode 100644 index 0000000..eddecae --- /dev/null +++ b/fixtures/bookmark/docProps/core.xml @@ -0,0 +1,2 @@ + +2019-12-04T16:33:05Zja-JP2019-12-04T16:33:39Z1 \ No newline at end of file diff --git a/fixtures/bookmark/word/_rels/document.xml.rels b/fixtures/bookmark/word/_rels/document.xml.rels new file mode 100644 index 0000000..8a2db8a --- /dev/null +++ b/fixtures/bookmark/word/_rels/document.xml.rels @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/fixtures/bookmark/word/document.xml b/fixtures/bookmark/word/document.xml new file mode 100644 index 0000000..53012ae --- /dev/null +++ b/fixtures/bookmark/word/document.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + Bookmark + + + + + Hello + + + + + + + + + + World + + + + + + + + + + + + \ No newline at end of file diff --git a/fixtures/bookmark/word/fontTable.xml b/fixtures/bookmark/word/fontTable.xml new file mode 100644 index 0000000..94f56db --- /dev/null +++ b/fixtures/bookmark/word/fontTable.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/fixtures/bookmark/word/settings.xml b/fixtures/bookmark/word/settings.xml new file mode 100644 index 0000000..97dba84 --- /dev/null +++ b/fixtures/bookmark/word/settings.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/fixtures/bookmark/word/styles.xml b/fixtures/bookmark/word/styles.xml new file mode 100644 index 0000000..72e6f53 --- /dev/null +++ b/fixtures/bookmark/word/styles.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file