2020-02-11 10:01:39 +02:00
|
|
|
use serde::ser::{SerializeStruct, Serializer};
|
|
|
|
use serde::Serialize;
|
|
|
|
|
2019-12-05 08:44:18 +02:00
|
|
|
use super::*;
|
2019-11-07 09:08:59 +02:00
|
|
|
use crate::documents::BuildXML;
|
2019-11-11 06:05:07 +02:00
|
|
|
use crate::types::*;
|
2019-11-07 09:08:59 +02:00
|
|
|
use crate::xml_builder::*;
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
#[derive(Serialize, Debug, Clone, PartialEq)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-12-08 21:14:27 +02:00
|
|
|
pub struct Paragraph {
|
2020-08-13 19:57:59 +03:00
|
|
|
pub id: String,
|
2019-12-11 07:12:22 +02:00
|
|
|
pub children: Vec<ParagraphChild>,
|
|
|
|
pub property: ParagraphProperty,
|
2019-12-16 04:36:04 +02:00
|
|
|
pub has_numbering: bool,
|
2019-11-07 09:08:59 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl Default for Paragraph {
|
2019-11-07 09:08:59 +02:00
|
|
|
fn default() -> Self {
|
2019-11-11 06:05:07 +02:00
|
|
|
Self {
|
2020-08-13 19:57:59 +03:00
|
|
|
id: crate::generate_para_id(),
|
2019-11-15 11:15:43 +02:00
|
|
|
children: Vec::new(),
|
2019-11-11 06:05:07 +02:00
|
|
|
property: ParagraphProperty::new(),
|
2019-12-16 04:36:04 +02:00
|
|
|
has_numbering: false,
|
2019-11-11 06:05:07 +02:00
|
|
|
}
|
2019-11-07 09:08:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2019-12-08 21:14:27 +02:00
|
|
|
pub enum ParagraphChild {
|
2020-06-08 07:41:13 +03:00
|
|
|
Run(Box<Run>),
|
2019-12-08 21:14:27 +02:00
|
|
|
Insert(Insert),
|
|
|
|
Delete(Delete),
|
|
|
|
BookmarkStart(BookmarkStart),
|
2021-12-17 18:03:02 +02:00
|
|
|
Hyperlink(Hyperlink),
|
2019-12-08 21:14:27 +02:00
|
|
|
BookmarkEnd(BookmarkEnd),
|
2020-01-24 10:57:14 +02:00
|
|
|
CommentStart(Box<CommentRangeStart>),
|
2019-12-08 21:14:27 +02:00
|
|
|
CommentEnd(CommentRangeEnd),
|
2021-12-09 12:37:54 +02:00
|
|
|
StructuredDataTag(Box<StructuredDataTag>),
|
2019-11-15 11:15:43 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl BuildXML for ParagraphChild {
|
2019-11-15 11:15:43 +02:00
|
|
|
fn build(&self) -> Vec<u8> {
|
|
|
|
match self {
|
|
|
|
ParagraphChild::Run(v) => v.build(),
|
|
|
|
ParagraphChild::Insert(v) => v.build(),
|
|
|
|
ParagraphChild::Delete(v) => v.build(),
|
2021-12-17 18:03:02 +02:00
|
|
|
ParagraphChild::Hyperlink(v) => v.build(),
|
2019-12-04 09:55:03 +02:00
|
|
|
ParagraphChild::BookmarkStart(v) => v.build(),
|
|
|
|
ParagraphChild::BookmarkEnd(v) => v.build(),
|
2019-12-05 08:44:18 +02:00
|
|
|
ParagraphChild::CommentStart(v) => v.build(),
|
|
|
|
ParagraphChild::CommentEnd(v) => v.build(),
|
2021-12-09 12:37:54 +02:00
|
|
|
ParagraphChild::StructuredDataTag(v) => v.build(),
|
2019-11-15 11:15:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
impl Serialize for ParagraphChild {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
ParagraphChild::Run(ref r) => {
|
|
|
|
let mut t = serializer.serialize_struct("Run", 2)?;
|
|
|
|
t.serialize_field("type", "run")?;
|
|
|
|
t.serialize_field("data", r)?;
|
|
|
|
t.end()
|
|
|
|
}
|
|
|
|
ParagraphChild::Insert(ref r) => {
|
|
|
|
let mut t = serializer.serialize_struct("Insert", 2)?;
|
|
|
|
t.serialize_field("type", "insert")?;
|
|
|
|
t.serialize_field("data", r)?;
|
|
|
|
t.end()
|
|
|
|
}
|
2020-02-13 09:14:06 +02:00
|
|
|
ParagraphChild::Delete(ref r) => {
|
|
|
|
let mut t = serializer.serialize_struct("Delete", 2)?;
|
|
|
|
t.serialize_field("type", "delete")?;
|
|
|
|
t.serialize_field("data", r)?;
|
|
|
|
t.end()
|
|
|
|
}
|
2021-12-17 18:03:02 +02:00
|
|
|
ParagraphChild::Hyperlink(ref r) => {
|
|
|
|
let mut t = serializer.serialize_struct("hyperlink", 2)?;
|
|
|
|
t.serialize_field("type", "hyperlink")?;
|
|
|
|
t.serialize_field("data", r)?;
|
|
|
|
t.end()
|
|
|
|
}
|
2020-02-13 09:14:06 +02:00
|
|
|
ParagraphChild::BookmarkStart(ref r) => {
|
|
|
|
let mut t = serializer.serialize_struct("BookmarkStart", 2)?;
|
|
|
|
t.serialize_field("type", "bookmarkStart")?;
|
|
|
|
t.serialize_field("data", r)?;
|
|
|
|
t.end()
|
|
|
|
}
|
|
|
|
ParagraphChild::BookmarkEnd(ref r) => {
|
|
|
|
let mut t = serializer.serialize_struct("BookmarkEnd", 2)?;
|
|
|
|
t.serialize_field("type", "bookmarkEnd")?;
|
|
|
|
t.serialize_field("data", r)?;
|
|
|
|
t.end()
|
|
|
|
}
|
2020-12-15 15:33:01 +02:00
|
|
|
ParagraphChild::CommentStart(ref r) => {
|
|
|
|
let mut t = serializer.serialize_struct("CommentRangeStart", 2)?;
|
|
|
|
t.serialize_field("type", "commentRangeStart")?;
|
|
|
|
t.serialize_field("data", r)?;
|
|
|
|
t.end()
|
|
|
|
}
|
|
|
|
ParagraphChild::CommentEnd(ref r) => {
|
|
|
|
let mut t = serializer.serialize_struct("CommentRangeEnd", 2)?;
|
|
|
|
t.serialize_field("type", "commentRangeEnd")?;
|
|
|
|
t.serialize_field("data", r)?;
|
2020-02-11 10:01:39 +02:00
|
|
|
t.end()
|
|
|
|
}
|
2021-12-09 12:37:54 +02:00
|
|
|
ParagraphChild::StructuredDataTag(ref r) => {
|
|
|
|
let mut t = serializer.serialize_struct("StructuredDataTag", 2)?;
|
|
|
|
t.serialize_field("type", "structuredDataTag")?;
|
|
|
|
t.serialize_field("data", r)?;
|
|
|
|
t.end()
|
|
|
|
}
|
2020-02-11 10:01:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl Paragraph {
|
|
|
|
pub fn new() -> Paragraph {
|
2019-11-07 09:08:59 +02:00
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:33:01 +02:00
|
|
|
pub fn id(mut self, id: impl Into<String>) -> Self {
|
|
|
|
self.id = id.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:44:18 +02:00
|
|
|
pub fn children(&self) -> &Vec<ParagraphChild> {
|
|
|
|
&self.children
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn add_run(mut self, run: Run) -> Paragraph {
|
2020-06-08 07:41:13 +03:00
|
|
|
self.children.push(ParagraphChild::Run(Box::new(run)));
|
2019-11-07 09:08:59 +02:00
|
|
|
self
|
|
|
|
}
|
2019-11-11 06:05:07 +02:00
|
|
|
|
2022-01-02 19:18:04 +02:00
|
|
|
pub(crate) fn unshift_run(mut self, run: Run) -> Paragraph {
|
|
|
|
self.children.insert(0, ParagraphChild::Run(Box::new(run)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn wrap_by_bookmark(mut self, id: usize, name: impl Into<String>) -> Paragraph {
|
|
|
|
self.children.insert(
|
|
|
|
0,
|
|
|
|
ParagraphChild::BookmarkStart(BookmarkStart::new(id, name)),
|
|
|
|
);
|
|
|
|
self.children
|
|
|
|
.push(ParagraphChild::BookmarkEnd(BookmarkEnd::new(id)));
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-12-17 18:03:02 +02:00
|
|
|
pub fn add_hyperlink(mut self, link: Hyperlink) -> Self {
|
|
|
|
self.children.push(ParagraphChild::Hyperlink(link));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-12-09 12:37:54 +02:00
|
|
|
pub fn add_structured_data_tag(mut self, t: StructuredDataTag) -> Self {
|
|
|
|
self.children
|
|
|
|
.push(ParagraphChild::StructuredDataTag(Box::new(t)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn add_insert(mut self, insert: Insert) -> Paragraph {
|
2019-11-15 11:15:43 +02:00
|
|
|
self.children.push(ParagraphChild::Insert(insert));
|
2019-11-13 10:52:02 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn add_delete(mut self, delete: Delete) -> Paragraph {
|
2019-11-15 11:15:43 +02:00
|
|
|
self.children.push(ParagraphChild::Delete(delete));
|
2019-11-11 06:05:07 +02:00
|
|
|
self
|
|
|
|
}
|
2019-11-11 08:36:26 +02:00
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
pub fn add_bookmark_start(mut self, id: usize, name: impl Into<String>) -> Paragraph {
|
2019-12-04 09:55:03 +02:00
|
|
|
self.children
|
|
|
|
.push(ParagraphChild::BookmarkStart(BookmarkStart::new(id, name)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
pub fn add_bookmark_end(mut self, id: usize) -> Paragraph {
|
2019-12-04 09:55:03 +02:00
|
|
|
self.children
|
|
|
|
.push(ParagraphChild::BookmarkEnd(BookmarkEnd::new(id)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn add_comment_start(mut self, comment: Comment) -> Paragraph {
|
2020-01-24 10:57:14 +02:00
|
|
|
self.children.push(ParagraphChild::CommentStart(Box::new(
|
|
|
|
CommentRangeStart::new(comment),
|
|
|
|
)));
|
2019-12-05 08:44:18 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-13 17:47:47 +02:00
|
|
|
pub fn add_comment_end(mut self, id: usize) -> Paragraph {
|
2019-12-05 08:44:18 +02:00
|
|
|
self.children
|
|
|
|
.push(ParagraphChild::CommentEnd(CommentRangeEnd::new(id)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn align(mut self, alignment_type: AlignmentType) -> Paragraph {
|
2019-11-15 11:15:43 +02:00
|
|
|
self.property = self.property.align(alignment_type);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn style(mut self, style_id: &str) -> Paragraph {
|
2019-11-11 08:36:26 +02:00
|
|
|
self.property = self.property.style(style_id);
|
|
|
|
self
|
|
|
|
}
|
2019-11-11 09:48:28 +02:00
|
|
|
|
2021-04-14 06:01:38 +03:00
|
|
|
pub fn keep_next(mut self, v: bool) -> Self {
|
|
|
|
self.property = self.property.keep_next(v);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn keep_lines(mut self, v: bool) -> Self {
|
|
|
|
self.property = self.property.keep_lines(v);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-09-10 03:42:08 +03:00
|
|
|
pub fn outline_lvl(mut self, v: usize) -> Self {
|
|
|
|
self.property = self.property.outline_lvl(v);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-04-14 06:01:38 +03:00
|
|
|
pub fn page_break_before(mut self, v: bool) -> Self {
|
|
|
|
self.property = self.property.page_break_before(v);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn window_control(mut self, v: bool) -> Self {
|
|
|
|
self.property = self.property.window_control(v);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-01-02 19:18:04 +02:00
|
|
|
pub fn add_tab(mut self, t: Tab) -> Self {
|
|
|
|
self.property = self.property.add_tab(t);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
pub fn indent(
|
|
|
|
mut self,
|
2020-03-10 04:56:12 +02:00
|
|
|
left: Option<i32>,
|
2020-02-11 10:01:39 +02:00
|
|
|
special_indent: Option<SpecialIndentType>,
|
2020-02-28 12:52:41 +02:00
|
|
|
end: Option<i32>,
|
2020-03-10 04:56:12 +02:00
|
|
|
start_chars: Option<i32>,
|
2020-02-11 10:01:39 +02:00
|
|
|
) -> Paragraph {
|
2020-03-10 04:56:12 +02:00
|
|
|
self.property = self.property.indent(left, special_indent, end, start_chars);
|
2019-11-11 09:48:28 +02:00
|
|
|
self
|
|
|
|
}
|
2019-12-06 12:18:48 +02:00
|
|
|
|
2021-10-20 10:37:51 +03:00
|
|
|
pub fn hanging_chars(mut self, chars: i32) -> Paragraph {
|
2021-04-08 10:53:21 +03:00
|
|
|
self.property = self.property.hanging_chars(chars);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-10-20 10:37:51 +03:00
|
|
|
pub fn first_line_chars(mut self, chars: i32) -> Paragraph {
|
2021-04-08 10:53:21 +03:00
|
|
|
self.property = self.property.first_line_chars(chars);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-06 19:15:21 +02:00
|
|
|
pub fn numbering(mut self, id: NumberingId, level: IndentLevel) -> Self {
|
2019-12-06 12:18:48 +02:00
|
|
|
self.property = self.property.numbering(id, level);
|
2019-12-16 04:36:04 +02:00
|
|
|
self.has_numbering = true;
|
2019-12-06 12:18:48 +02:00
|
|
|
self
|
|
|
|
}
|
2020-06-08 07:41:13 +03:00
|
|
|
|
|
|
|
pub fn size(mut self, size: usize) -> Self {
|
|
|
|
self.property.run_property = self.property.run_property.size(size);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bold(mut self) -> Self {
|
|
|
|
self.property.run_property = self.property.run_property.bold();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn italic(mut self) -> Self {
|
|
|
|
self.property.run_property = self.property.run_property.italic();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fonts(mut self, f: RunFonts) -> Self {
|
|
|
|
self.property.run_property = self.property.run_property.fonts(f);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-10-20 10:37:51 +03:00
|
|
|
pub fn run_property(mut self, p: RunProperty) -> Self {
|
2020-06-08 07:41:13 +03:00
|
|
|
self.property.run_property = p;
|
|
|
|
self
|
|
|
|
}
|
2020-10-14 04:16:13 +03:00
|
|
|
|
2021-11-25 12:42:06 +02:00
|
|
|
pub fn line_spacing(mut self, spacing: LineSpacing) -> Self {
|
|
|
|
self.property = self.property.line_spacing(spacing);
|
2020-10-14 04:16:13 +03:00
|
|
|
self
|
|
|
|
}
|
2022-01-02 19:18:04 +02:00
|
|
|
|
|
|
|
pub fn raw_text(&self) -> String {
|
|
|
|
let mut s = "".to_string();
|
|
|
|
// For now support only run and ins.
|
|
|
|
for c in self.children.iter() {
|
|
|
|
match c {
|
|
|
|
ParagraphChild::Insert(i) => {
|
|
|
|
for c in i.children.iter() {
|
|
|
|
if let InsertChild::Run(r) = c {
|
|
|
|
for c in r.children.iter() {
|
|
|
|
if let RunChild::Text(t) = c {
|
|
|
|
s.push_str(&t.text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ParagraphChild::Run(run) => {
|
|
|
|
for c in run.children.iter() {
|
|
|
|
if let RunChild::Text(t) = c {
|
|
|
|
s.push_str(&t.text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
2019-11-07 09:08:59 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl BuildXML for Paragraph {
|
2019-11-07 09:08:59 +02:00
|
|
|
fn build(&self) -> Vec<u8> {
|
2019-11-07 10:31:04 +02:00
|
|
|
XMLBuilder::new()
|
2020-08-13 19:57:59 +03:00
|
|
|
.open_paragraph(&self.id)
|
2019-11-11 06:05:07 +02:00
|
|
|
.add_child(&self.property)
|
2019-11-15 11:15:43 +02:00
|
|
|
.add_children(&self.children)
|
2019-11-07 10:31:04 +02:00
|
|
|
.close()
|
|
|
|
.build()
|
2019-11-07 09:08:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
#[cfg(test)]
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_paragraph() {
|
2019-11-13 09:51:58 +02:00
|
|
|
let b = Paragraph::new()
|
|
|
|
.add_run(Run::new().add_text("Hello"))
|
|
|
|
.build();
|
2019-11-07 09:08:59 +02:00
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2020-08-13 19:57:59 +03:00
|
|
|
r#"<w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p>"#
|
2019-11-13 10:52:02 +02:00
|
|
|
);
|
|
|
|
}
|
2019-12-04 09:55:03 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bookmark() {
|
|
|
|
let b = Paragraph::new()
|
2020-02-11 10:01:39 +02:00
|
|
|
.add_bookmark_start(0, "article")
|
2019-12-04 09:55:03 +02:00
|
|
|
.add_run(Run::new().add_text("Hello"))
|
2020-02-11 10:01:39 +02:00
|
|
|
.add_bookmark_end(0)
|
2019-12-04 09:55:03 +02:00
|
|
|
.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2020-08-13 19:57:59 +03:00
|
|
|
r#"<w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:bookmarkStart w:id="0" w:name="article" /><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r><w:bookmarkEnd w:id="0" /></w:p>"#
|
2019-12-04 09:55:03 +02:00
|
|
|
);
|
|
|
|
}
|
2019-12-05 08:44:18 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_comment() {
|
|
|
|
let b = Paragraph::new()
|
2019-12-13 17:47:47 +02:00
|
|
|
.add_comment_start(Comment::new(1))
|
2019-12-05 08:44:18 +02:00
|
|
|
.add_run(Run::new().add_text("Hello"))
|
2019-12-13 17:47:47 +02:00
|
|
|
.add_comment_end(1)
|
2019-12-05 08:44:18 +02:00
|
|
|
.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2020-08-13 19:57:59 +03:00
|
|
|
r#"<w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:commentRangeStart w:id="1" /><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r><w:r>
|
2019-12-06 12:18:48 +02:00
|
|
|
<w:rPr />
|
|
|
|
</w:r>
|
2019-12-13 17:47:47 +02:00
|
|
|
<w:commentRangeEnd w:id="1" />
|
2019-12-05 08:44:18 +02:00
|
|
|
<w:r>
|
2019-12-13 17:47:47 +02:00
|
|
|
<w:commentReference w:id="1" />
|
2019-12-05 08:44:18 +02:00
|
|
|
</w:r></w:p>"#
|
|
|
|
);
|
|
|
|
}
|
2019-12-06 12:18:48 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_numbering() {
|
|
|
|
let b = Paragraph::new()
|
|
|
|
.add_run(Run::new().add_text("Hello"))
|
2019-12-06 19:15:21 +02:00
|
|
|
.numbering(NumberingId::new(0), IndentLevel::new(1))
|
2019-12-06 12:18:48 +02:00
|
|
|
.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2020-08-13 19:57:59 +03:00
|
|
|
r#"<w:p w14:paraId="12345678"><w:pPr><w:rPr /><w:numPr><w:numId w:val="0" /><w:ilvl w:val="1" /></w:numPr></w:pPr><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p>"#
|
2019-12-06 12:18:48 +02:00
|
|
|
);
|
|
|
|
}
|
2020-02-11 10:01:39 +02:00
|
|
|
|
2021-09-29 03:03:39 +03:00
|
|
|
#[test]
|
|
|
|
fn test_line_spacing_and_character_spacing() {
|
2021-11-25 12:42:06 +02:00
|
|
|
let spacing = LineSpacing::new()
|
|
|
|
.line_rule(LineSpacingType::Auto)
|
|
|
|
.before(20)
|
|
|
|
.after(30)
|
|
|
|
.line(200);
|
2021-09-29 03:03:39 +03:00
|
|
|
let b = Paragraph::new()
|
2021-11-25 12:42:06 +02:00
|
|
|
.line_spacing(spacing)
|
2021-09-29 03:03:39 +03:00
|
|
|
.add_run(Run::new().add_text("Hello"))
|
|
|
|
.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
|
|
|
r#"<w:p w14:paraId="12345678"><w:pPr><w:rPr /><w:spacing w:before="20" w:after="30" w:line="200" w:lineRule="auto" /></w:pPr><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p>"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
#[test]
|
|
|
|
fn test_paragraph_run_json() {
|
|
|
|
let run = Run::new().add_text("Hello");
|
|
|
|
let p = Paragraph::new().add_run(run);
|
|
|
|
assert_eq!(
|
|
|
|
serde_json::to_string(&p).unwrap(),
|
2022-01-02 19:18:04 +02:00
|
|
|
r#"{"id":"12345678","children":[{"type":"run","data":{"runProperty":{},"children":[{"type":"text","data":{"preserveSpace":true,"text":"Hello"}}]}}],"property":{"runProperty":{},"style":null,"numberingProperty":null,"alignment":null,"indent":null,"lineSpacing":null,"keepNext":false,"keepLines":false,"pageBreakBefore":false,"windowControl":false,"outlineLvl":null,"tabs":[],"divId":null},"hasNumbering":false}"#,
|
2020-02-11 10:01:39 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_paragraph_insert_json() {
|
|
|
|
let run = Run::new().add_text("Hello");
|
|
|
|
let ins = Insert::new(run);
|
|
|
|
let p = Paragraph::new().add_insert(ins);
|
|
|
|
assert_eq!(
|
|
|
|
serde_json::to_string(&p).unwrap(),
|
2022-01-02 19:18:04 +02:00
|
|
|
r#"{"id":"12345678","children":[{"type":"insert","data":{"children":[{"type":"run","data":{"runProperty":{},"children":[{"type":"text","data":{"preserveSpace":true,"text":"Hello"}}]}}],"author":"unnamed","date":"1970-01-01T00:00:00Z"}}],"property":{"runProperty":{},"style":null,"numberingProperty":null,"alignment":null,"indent":null,"lineSpacing":null,"keepNext":false,"keepLines":false,"pageBreakBefore":false,"windowControl":false,"outlineLvl":null,"tabs":[],"divId":null},"hasNumbering":false}"#
|
2020-02-11 10:01:39 +02:00
|
|
|
);
|
|
|
|
}
|
2022-01-02 19:18:04 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_raw_text() {
|
|
|
|
let b = Paragraph::new()
|
|
|
|
.add_run(Run::new().add_text("Hello"))
|
|
|
|
.add_insert(Insert::new(Run::new().add_text("World")))
|
|
|
|
.add_delete(Delete::new().add_run(Run::new().add_delete_text("!!!!!")))
|
|
|
|
.raw_text();
|
|
|
|
assert_eq!(b, "HelloWorld".to_owned());
|
|
|
|
}}
|
|
|
|
|