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 default_tab_stop;
|
||||
mod delete;
|
||||
mod delete_text;
|
||||
mod doc_defaults;
|
||||
mod font;
|
||||
mod grid_span;
|
||||
|
@ -75,7 +74,6 @@ pub use comment_range_end::*;
|
|||
pub use comment_range_start::*;
|
||||
pub use default_tab_stop::*;
|
||||
pub use delete::*;
|
||||
pub use delete_text::*;
|
||||
pub use doc_defaults::*;
|
||||
pub use font::*;
|
||||
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::types::BreakType;
|
||||
use crate::xml_builder::*;
|
||||
|
@ -22,7 +22,6 @@ impl Default for Run {
|
|||
#[derive(Debug, Clone)]
|
||||
pub enum RunChild {
|
||||
Text(Text),
|
||||
DeleteText(DeleteText),
|
||||
Tab(Tab),
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -92,7 +91,6 @@ impl BuildXML for Run {
|
|||
for c in &self.children {
|
||||
match c {
|
||||
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::Break(t) => b = b.add_child(t),
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use crate::documents::BuildXML;
|
||||
use crate::escape::escape;
|
||||
use crate::xml_builder::*;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -8,9 +9,9 @@ pub struct Text {
|
|||
}
|
||||
|
||||
impl Text {
|
||||
pub fn new(text: impl Into<String>) -> Text {
|
||||
pub fn new(text: &str) -> Text {
|
||||
Text {
|
||||
text: text.into(),
|
||||
text: escape(text),
|
||||
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 errors;
|
||||
mod escape;
|
||||
mod types;
|
||||
mod xml_builder;
|
||||
mod zipper;
|
||||
|
||||
pub use documents::*;
|
||||
pub use errors::*;
|
||||
pub(crate) use escape::*;
|
||||
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)?;
|
||||
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