feat: Add table

main
bokuweb 2019-11-12 19:21:08 +09:00
parent f4a93bd4dd
commit 7153db3ed8
4 changed files with 77 additions and 12 deletions

View File

@ -1,10 +1,16 @@
use super::Paragraph;
use super::{Paragraph, Table};
use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug)]
pub struct Document {
paragraphs: Vec<Paragraph>,
children: Vec<DocumentContent>,
}
#[derive(Debug, Clone)]
pub enum DocumentContent {
Paragraph(Paragraph),
Table(Table),
}
impl Document {
@ -13,7 +19,12 @@ impl Document {
}
pub fn add_paragraph(mut self, p: Paragraph) -> Self {
self.paragraphs.push(p);
self.children.push(DocumentContent::Paragraph(p));
self
}
pub fn add_table(mut self, t: Table) -> Self {
self.children.push(DocumentContent::Table(t));
self
}
}
@ -21,21 +32,24 @@ impl Document {
impl Default for Document {
fn default() -> Self {
Self {
paragraphs: Vec::new(),
children: Vec::new(),
}
}
}
impl BuildXML for Document {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new();
b.declaration(Some(true))
let mut b = XMLBuilder::new()
.declaration(Some(true))
.open_document()
.open_body()
.add_children(&self.paragraphs)
.close()
.close()
.build()
.open_body();
for c in &self.children {
match c {
DocumentContent::Paragraph(p) => b = b.add_child(p),
DocumentContent::Table(t) => b = b.add_child(t),
}
}
b.close().close().build()
}
}

View File

@ -15,6 +15,7 @@ mod run_property_default;
mod style;
mod sz;
mod sz_cs;
mod table;
mod table_borders;
mod table_cell;
mod table_cell_borders;
@ -46,6 +47,7 @@ pub use run_property_default::*;
pub use style::*;
pub use sz::*;
pub use sz_cs::*;
pub use table::*;
pub use table_borders::*;
pub use table_cell::*;
pub use table_cell_borders::*;

View File

@ -0,0 +1,49 @@
use super::{TableProperty, TableRow};
use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct Table {
property: TableProperty,
rows: Vec<TableRow>,
}
impl Table {
pub fn new(rows: Vec<TableRow>) -> Table {
let property = TableProperty::new();
Self { property, rows }
}
}
impl BuildXML for Table {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new()
.open_table()
.add_child(&self.property)
.add_children(&self.rows);
b.close().build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_row() {
let b = Table::new(vec![TableRow::new(vec![])]).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:tbl><w:tblPr><w:tblW w:w="9638" w:type="dxa" /><w:jc w:val="left" /><w:tblBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:left w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000" /></w:tblBorders><w:tblCellMar>
<w:top w:w="55" w:type="dxa" />
<w:left w:w="54" w:type="dxa" />
<w:bottom w:w="55" w:type="dxa" />
<w:right w:w="55" w:type="dxa" />
</w:tblCellMar></w:tblPr><w:tr><w:trPr /></w:tr></w:tbl>"#
);
}
}

View File

@ -3,7 +3,7 @@ use crate::documents::BuildXML;
use crate::types::*;
use crate::xml_builder::*;
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct TableProperty {
width: TableWidth,
justification: Justification,