Skip to content

Commit

Permalink
feat(converter): add images
Browse files Browse the repository at this point in the history
  • Loading branch information
kettei-sproutty committed Jan 18, 2024
1 parent 7dbbb82 commit 2229763
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 10 deletions.
10 changes: 9 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ wasm-bindgen = "0.2.84"
# code size when deploying.
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"

[dev-dependencies]
wasm-bindgen-test = "0.3.34"
Expand Down
3 changes: 3 additions & 0 deletions src/element.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(dead_code)]
use std::fmt::{Display, Formatter, Result};

#[derive(PartialEq, Eq)]
pub enum ElementTag {
H1,
H2,
Expand Down Expand Up @@ -111,6 +112,7 @@ pub enum ElementChildren {
}

pub struct Element {
pub id: Option<String>,
pub tag: ElementTag,
pub styles: Vec<String>,
pub classes: Vec<String>,
Expand Down Expand Up @@ -138,6 +140,7 @@ impl Element {
impl Default for Element {
fn default() -> Self {
Element {
id: None,
tag: ElementTag::P,
children: vec![],
styles: vec![],
Expand Down
27 changes: 27 additions & 0 deletions src/image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pub struct HtmlImage {
pub id: String,
pub src: String,
pub size: (u32, u32),
}

impl HtmlImage {
pub fn to_string(&self) -> String {
format!(
"<img id=\"{}\" src=\"{}\" width=\"{}\" height=\"{}\" />",
self.id,
self.src,
format!("{}px", self.size.0 / 10000),
format!("{}px", self.size.1 / 10000),
)
}
}

impl Default for HtmlImage {
fn default() -> Self {
HtmlImage {
id: String::new(),
src: String::new(),
size: (0, 0),
}
}
}
22 changes: 19 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
mod element;
mod image;
mod options;
mod parser;
mod state;
mod utils;

use base64::{engine::general_purpose, Engine as _};
use docx_rs::read_docx;
use state::CONTAINER;
use image::HtmlImage;
use state::{CONTAINER, IMAGES};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
Expand All @@ -18,11 +21,24 @@ pub fn convert(file: &[u8]) -> String {
utils::set_panic_hook();
unsafe {
CONTAINER.children.clear();
IMAGES.clear()
}

let document = read_docx(file).unwrap();
// let images = &document.images;
// alert(format!("images: {:?}", images).as_str());

let images = &document.images;
images.iter().for_each(|img| {
let (id, _file_type, image, _png) = img;

let src = general_purpose::STANDARD.encode(&image.0);
let image = HtmlImage {
id: id.to_string(),
src: format!("data:image/png;base64,{}", src),
..Default::default()
};

unsafe { IMAGES.push(image) }
});

document
.document
Expand Down
29 changes: 25 additions & 4 deletions src/parser/paragraph.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use docx_rs::{Paragraph, ParagraphChild, ParagraphProperty};

use crate::element::{Element, ElementChildren, ElementTag};
use crate::{
alert,
element::{Element, ElementChildren, ElementTag},
};

use super::{
hyperlink::analyze_hyperlink,
Expand All @@ -21,9 +24,19 @@ pub fn analyze_paragraph(paragraph: &Paragraph) -> ElementChildren {
let mut element = Element::default();

let tag = &paragraph.property.style.as_ref();
if tag.is_some() {
let tag = &tag.unwrap().val;
element.tag = ElementTag::from_style(tag)
if let Some(tag) = tag {
if tag.val.eq("ListParagraph") {
let numbering_property = &paragraph.property.numbering_property.as_ref();
if let Some(property) = numbering_property {
let id = &property.id.as_ref().unwrap().id;
match id {
_ => element.tag = ElementTag::Ul,
}
}
} else {
let tag = &tag.val;
element.tag = ElementTag::from_style(tag);
}
}

let mut run_property = analyze_run_properties(&paragraph.property.run_property);
Expand All @@ -34,6 +47,14 @@ pub fn analyze_paragraph(paragraph: &Paragraph) -> ElementChildren {
paragraph.children.iter().for_each(|child| match child {
ParagraphChild::Run(run) => {
let mut children = analyze_run(run);
if element.tag == ElementTag::Ul || element.tag == ElementTag::Ol {
children.iter_mut().for_each(|child| {
if let ElementChildren::Element(child) = child {
alert(&format!("child: {}", child.to_string()));
child.tag = ElementTag::Li;
}
});
}
element.children.append(&mut children);
}
ParagraphChild::Hyperlink(hyperlink) => {
Expand Down
35 changes: 33 additions & 2 deletions src/parser/run.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use docx_rs::{Run, RunChild, RunProperty, Text};
use docx_rs::{Drawing, DrawingData, Run, RunChild, RunProperty, Text};

use crate::element::{ElementChildren, ElementTag};
use crate::{
element::{ElementChildren, ElementTag},
image::HtmlImage,
state::IMAGES,
};

pub struct RunElement {
pub tags: Vec<ElementTag>,
Expand Down Expand Up @@ -100,6 +104,32 @@ fn analyze_run_text(text: &Text) -> Option<ElementChildren> {
Some(ElementChildren::Text(text.text.to_string()))
}

fn analyze_run_image(image: &Drawing) -> Option<ElementChildren> {
match &image.data {
Some(DrawingData::Pic(pic)) => unsafe {
let image = IMAGES.iter().find(|picture| picture.id.eq(&pic.id));
if image.is_none() {
return None;
}

let image = image.unwrap();
if image.src.is_empty() {
return None;
}

let img = HtmlImage {
id: image.id.clone(),
src: image.src.clone(),
size: pic.size,
};

return Some(ElementChildren::Text(img.to_string()));
},
Some(DrawingData::TextBox(_)) => None,
None => None,
}
}

pub fn analyze_run(run: &Run) -> Vec<ElementChildren> {
let mut element = analyze_run_properties(&run.run_property);

Expand All @@ -108,6 +138,7 @@ pub fn analyze_run(run: &Run) -> Vec<ElementChildren> {
.iter()
.filter_map(|child| match child {
RunChild::Text(text) => analyze_run_text(text),
RunChild::Drawing(image) => analyze_run_image(image),
_ => None,
})
.collect();
Expand Down
4 changes: 4 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::{
element::{Element, ElementTag},
image::HtmlImage,
options::Options,
};

pub static mut CONTAINER: Element = Element {
id: None,
tag: ElementTag::Div,
children: vec![],
styles: vec![],
Expand All @@ -12,3 +14,5 @@ pub static mut CONTAINER: Element = Element {

#[allow(dead_code)]
pub static mut OPTIONS: Options = Options { style_map: vec![] };

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

0 comments on commit 2229763

Please sign in to comment.