Skip to content

Commit

Permalink
chore: tables
Browse files Browse the repository at this point in the history
  • Loading branch information
kettei-sproutty committed Jan 19, 2024
1 parent e232f25 commit 4e3875e
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ console_error_panic_hook = { version = "0.1.7", optional = true }
docx-rs = { git = "https://github.com/kettei-sproutty/docx-rs", branch = "main" }
serde = { version = "1.0.195", features = ["derive"] }
base64 = "0.21.7"
once_cell = "1.19.0"

[dev-dependencies]
wasm-bindgen-test = "0.3.34"
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn convert(file: &[u8]) -> String {
IMAGES.clear()
}

let document = read_docx(file).unwrap();
let mut document = read_docx(file).unwrap();

let images = &document.images;
images.iter().for_each(|img| {
Expand All @@ -40,6 +40,12 @@ pub fn convert(file: &[u8]) -> String {
unsafe { IMAGES.push(image) }
});

document.styles.styles.iter_mut().for_each(|style| {
let style = style.clone();
let id = style.style_id.clone();
unsafe { state::STYLE_MAP.insert(id, style) };
});

document
.document
.children
Expand Down
6 changes: 4 additions & 2 deletions src/parser/hyperlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use crate::element::{Element, ElementTag};
use super::run::analyze_run;

pub fn analyze_hyperlink(hyperlink: &Hyperlink) -> Element {
let mut element = Element::default();
element.tag = ElementTag::A;
let mut element = Element {
tag: ElementTag::A,
..Element::default()
};

hyperlink.children.iter().for_each(|child| match child {
ParagraphChild::Run(run) => {
Expand Down
3 changes: 2 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod hyperlink;
mod paragraph;
mod run;
mod style;
mod table;

use docx_rs::DocumentChild;
Expand All @@ -19,5 +20,5 @@ pub fn parse_child(child: &DocumentChild) {
}
let children = children.unwrap();

unsafe { CONTAINER.children.append(&mut vec![children]) }
unsafe { CONTAINER.children.push(children) }
}
1 change: 1 addition & 0 deletions src/parser/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
use super::{
hyperlink::analyze_hyperlink,
run::{analyze_run, analyze_run_properties},
style::analyze_style,

Check failure on line 11 in src/parser/paragraph.rs

View workflow job for this annotation

GitHub Actions / build-and-deploy

unused import: `style::analyze_style`
};

pub fn get_paragraph_properties(properties: &ParagraphProperty) -> Vec<String> {
Expand Down
6 changes: 5 additions & 1 deletion src/parser/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl RunElement {

pub fn analyze_run_properties(run_properties: &RunProperty) -> RunElement {
let mut element = RunElement {
tags: vec![ElementTag::Span],
tags: vec![],
text: String::new(),
style: vec![],
};
Expand Down Expand Up @@ -82,6 +82,10 @@ pub fn analyze_run_properties(run_properties: &RunProperty) -> RunElement {
}
}

if element.style.len() > 0 && element.tags.len() == 0 {
element.tags.push(ElementTag::Span);
}

if let Some(vert_align) = &run_properties.vert_align {
match vert_align.val {
VertAlignType::SuperScript => element.tags.push(ElementTag::Sup),
Expand Down
42 changes: 42 additions & 0 deletions src/parser/style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use docx_rs::{RunProperty, Style};

fn analyze_run_properties(run_properties: &RunProperty) -> Vec<String> {
let mut accumulator: Vec<String> = vec![];

if run_properties.bold.is_some() {
accumulator.push("font-weight: bold".to_owned());
};

if run_properties.italic.is_some() {
accumulator.push("font-style: italic".to_owned());
};

if run_properties.underline.is_some() {
accumulator.push("text-decoration: underline".to_owned());
};

if run_properties.sz.is_some() {
accumulator.push(format!(
"font-size: {}px",
run_properties.sz.as_ref().unwrap().val
));
};

if run_properties.strike.is_some() {
accumulator.push("text-decoration: line-through".to_owned());
};

if run_properties.vanish.is_some() {
accumulator.push("visibility: hidden".to_owned());
};

accumulator
}

pub fn analyze_style(style: &Style) -> Vec<String> {
let mut accumulator: Vec<String> = vec![];

accumulator.append(&mut analyze_run_properties(&style.run_property));

return accumulator;
}
30 changes: 28 additions & 2 deletions src/parser/table.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use docx_rs::{Table, TableCellContent, TableChild, TableRow, TableRowChild};

use crate::element::{Element, ElementChildren, ElementTag};
use crate::{
element::{Element, ElementChildren, ElementTag},
state::STYLE_MAP,
};

use super::paragraph::analyze_paragraph;
use super::{paragraph::analyze_paragraph, style::analyze_style};

fn analyze_row(row: &TableRow) -> Vec<ElementChildren> {
let mut children = vec![];
Expand All @@ -11,6 +14,9 @@ fn analyze_row(row: &TableRow) -> Vec<ElementChildren> {
let TableRowChild::TableCell(cell) = child;
let mut element = Element {
tag: ElementTag::Td,
styles: vec![
"border-collapse: collapse; border-spacing: 0; border: 1px solid black;".to_owned(),
],
..Element::default()
};

Expand All @@ -35,13 +41,33 @@ fn analyze_row(row: &TableRow) -> Vec<ElementChildren> {
pub fn analyze_table(table: &Table) -> ElementChildren {
let mut element = Element {
tag: ElementTag::Table,
styles: vec![
"border-collapse: collapse; border-spacing: 0; border: 1px solid black;".to_owned(),
],
..Element::default()
};

if let Some(style) = table.property.get_style() {
unsafe {
if let Some(style) = STYLE_MAP.get(&style.val) {
if let Some(based_on) = style.based_on.as_ref() {
if let Some(based_on) = STYLE_MAP.get(&based_on.val) {
element.styles.append(&mut analyze_style(based_on));
}
}

element.styles.append(&mut analyze_style(style));
}
}
};

table.rows.iter().for_each(|child| {
let TableChild::TableRow(row) = child;
let mut row_element = Element {
tag: ElementTag::Tr,
styles: vec![
"border-collapse: collapse; border-spacing: 0; border: 1px solid black;".to_owned(),
],
..Element::default()
};

Expand Down
11 changes: 11 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::Mutex;

Check failure on line 3 in src/state.rs

View workflow job for this annotation

GitHub Actions / build-and-deploy

unused import: `std::sync::Mutex`

use docx_rs::Style;

use crate::{
element::{Element, ElementTag},
image::HtmlImage,
Expand All @@ -16,3 +22,8 @@ pub static mut CONTAINER: Element = Element {
pub static mut OPTIONS: Options = Options { style_map: vec![] };

pub static mut IMAGES: Vec<HtmlImage> = Vec::new();

pub static mut STYLE_MAP: Lazy<HashMap<String, Style>> = Lazy::new(|| {
let map: HashMap<String, Style> = HashMap::new();
return map;
});

0 comments on commit 4e3875e

Please sign in to comment.