parent
7f65d18877
commit
df5eefff20
|
@ -1,41 +0,0 @@
|
||||||
use crate::documents::BuildXML;
|
|
||||||
use crate::xml_builder::*;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct DeleteText {
|
|
||||||
text: String,
|
|
||||||
preserve_space: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DeleteText {
|
|
||||||
pub fn new(text: impl Into<String>) -> DeleteText {
|
|
||||||
DeleteText {
|
|
||||||
text: text.into(),
|
|
||||||
preserve_space: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BuildXML for DeleteText {
|
|
||||||
fn build(&self) -> Vec<u8> {
|
|
||||||
XMLBuilder::new().delete_text(&self.text, true).build()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
#[cfg(test)]
|
|
||||||
use pretty_assertions::assert_eq;
|
|
||||||
use std::str;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_build() {
|
|
||||||
let b = DeleteText::new("Hello").build();
|
|
||||||
assert_eq!(
|
|
||||||
str::from_utf8(&b).unwrap(),
|
|
||||||
r#"<w:delText xml:space="preserve">Hello</w:delText>"#
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -10,7 +10,6 @@ mod comment_range_end;
|
||||||
mod comment_range_start;
|
mod comment_range_start;
|
||||||
mod default_tab_stop;
|
mod default_tab_stop;
|
||||||
mod delete;
|
mod delete;
|
||||||
mod delete_text;
|
|
||||||
mod doc_defaults;
|
mod doc_defaults;
|
||||||
mod font;
|
mod font;
|
||||||
mod grid_span;
|
mod grid_span;
|
||||||
|
@ -75,7 +74,6 @@ pub use comment_range_end::*;
|
||||||
pub use comment_range_start::*;
|
pub use comment_range_start::*;
|
||||||
pub use default_tab_stop::*;
|
pub use default_tab_stop::*;
|
||||||
pub use delete::*;
|
pub use delete::*;
|
||||||
pub use delete_text::*;
|
|
||||||
pub use doc_defaults::*;
|
pub use doc_defaults::*;
|
||||||
pub use font::*;
|
pub use font::*;
|
||||||
pub use grid_span::*;
|
pub use grid_span::*;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use super::{Break, DeleteText, RunProperty, Tab, Text};
|
use super::{Break, RunProperty, Tab, Text};
|
||||||
use crate::documents::BuildXML;
|
use crate::documents::BuildXML;
|
||||||
use crate::types::BreakType;
|
use crate::types::BreakType;
|
||||||
use crate::xml_builder::*;
|
use crate::xml_builder::*;
|
||||||
|
@ -22,7 +22,6 @@ impl Default for Run {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum RunChild {
|
pub enum RunChild {
|
||||||
Text(Text),
|
Text(Text),
|
||||||
DeleteText(DeleteText),
|
|
||||||
Tab(Tab),
|
Tab(Tab),
|
||||||
Break(Break),
|
Break(Break),
|
||||||
}
|
}
|
||||||
|
@ -34,12 +33,12 @@ impl Run {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_text(mut self, text: impl Into<String>) -> Run {
|
pub fn add_text(mut self, text: &str) -> Run {
|
||||||
self.children.push(RunChild::Text(Text::new(text)));
|
self.children.push(RunChild::Text(Text::new(text)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_delete_text(mut self, text: impl Into<String>) -> Run {
|
pub fn add_delete_text(mut self, text: &str) -> Run {
|
||||||
self.children.push(RunChild::Text(Text::new(text)));
|
self.children.push(RunChild::Text(Text::new(text)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -92,7 +91,6 @@ impl BuildXML for Run {
|
||||||
for c in &self.children {
|
for c in &self.children {
|
||||||
match c {
|
match c {
|
||||||
RunChild::Text(t) => b = b.add_child(t),
|
RunChild::Text(t) => b = b.add_child(t),
|
||||||
RunChild::DeleteText(t) => b = b.add_child(t),
|
|
||||||
RunChild::Tab(t) => b = b.add_child(t),
|
RunChild::Tab(t) => b = b.add_child(t),
|
||||||
RunChild::Break(t) => b = b.add_child(t),
|
RunChild::Break(t) => b = b.add_child(t),
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::documents::BuildXML;
|
use crate::documents::BuildXML;
|
||||||
|
use crate::escape::escape;
|
||||||
use crate::xml_builder::*;
|
use crate::xml_builder::*;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -8,9 +9,9 @@ pub struct Text {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Text {
|
impl Text {
|
||||||
pub fn new(text: impl Into<String>) -> Text {
|
pub fn new(text: &str) -> Text {
|
||||||
Text {
|
Text {
|
||||||
text: text.into(),
|
text: escape(text),
|
||||||
preserve_space: true,
|
preserve_space: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
pub(crate) fn escape(text: &str) -> String {
|
||||||
|
text.replace('&', "&")
|
||||||
|
.replace('<', "<")
|
||||||
|
.replace('>', ">")
|
||||||
|
.replace('"', """)
|
||||||
|
.replace('\'', "'")
|
||||||
|
.replace('\n', "
")
|
||||||
|
.replace('\r', "
")
|
||||||
|
}
|
|
@ -1,19 +1,11 @@
|
||||||
mod documents;
|
mod documents;
|
||||||
mod errors;
|
mod errors;
|
||||||
|
mod escape;
|
||||||
mod types;
|
mod types;
|
||||||
mod xml_builder;
|
mod xml_builder;
|
||||||
mod zipper;
|
mod zipper;
|
||||||
|
|
||||||
pub use documents::*;
|
pub use documents::*;
|
||||||
pub use errors::*;
|
pub use errors::*;
|
||||||
|
pub(crate) use escape::*;
|
||||||
pub use types::*;
|
pub use types::*;
|
||||||
|
|
||||||
pub fn simple() {
|
|
||||||
let path = std::path::Path::new("./test.docx");
|
|
||||||
let file = std::fs::File::create(&path).unwrap();
|
|
||||||
Docx::new()
|
|
||||||
.add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello")))
|
|
||||||
.add_paragraph(Paragraph::new().add_run(Run::new().add_text(" World")))
|
|
||||||
.build()
|
|
||||||
.pack(file);
|
|
||||||
}
|
|
||||||
|
|
|
@ -371,3 +371,18 @@ pub fn user_numbering() -> Result<(), DocxError> {
|
||||||
.pack(file)?;
|
.pack(file)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn escape() -> Result<(), DocxError> {
|
||||||
|
let path = std::path::Path::new("./tests/output/escape.docx");
|
||||||
|
let file = std::fs::File::create(&path).unwrap();
|
||||||
|
Docx::new()
|
||||||
|
.add_paragraph(
|
||||||
|
Paragraph::new()
|
||||||
|
.add_run(Run::new().add_text("&&&>>><<"))
|
||||||
|
.numbering(NumberingId::new(2), IndentLevel::new(0)),
|
||||||
|
)
|
||||||
|
.build()
|
||||||
|
.pack(file)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue