Skip to content

Commit

Permalink
feat: parse styles
Browse files Browse the repository at this point in the history
  • Loading branch information
kettei-sproutty committed Jan 19, 2024
1 parent 685360f commit 4111931
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
15 changes: 15 additions & 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,
};

pub fn get_paragraph_properties(properties: &ParagraphProperty) -> Vec<String> {
Expand All @@ -17,6 +18,20 @@ pub fn get_paragraph_properties(properties: &ParagraphProperty) -> Vec<String> {
props.push(format!("text-align: {}", alignment.val));
};

if let Some(style) = properties.style.as_ref() {
unsafe {
if let Some(style) = crate::state::STYLE_MAP.get(&style.val) {
if let Some(based_on) = style.based_on.as_ref() {
if let Some(based_on) = crate::state::STYLE_MAP.get(&based_on.val) {
props.append(&mut analyze_style(&based_on));
}
}

props.append(&mut analyze_style(&style));
}
}
}

props
}

Expand Down
38 changes: 31 additions & 7 deletions src/parser/style.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use docx_rs::{RunProperty, Style};
use docx_rs::{ParagraphProperty, RunProperty, Style};

fn analyze_run_properties(run_properties: &RunProperty) -> Vec<String> {
let mut accumulator: Vec<String> = vec![];
Expand All @@ -15,12 +15,12 @@ fn analyze_run_properties(run_properties: &RunProperty) -> Vec<String> {
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.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());
Expand All @@ -30,13 +30,37 @@ fn analyze_run_properties(run_properties: &RunProperty) -> Vec<String> {
accumulator.push("visibility: hidden".to_owned());
};

if let Some(color) = &run_properties.color.as_ref() {
let value = &color.val;
if value.len().eq(&6) || value.len().eq(&8) {
if let Ok(_) = u32::from_str_radix(&value, 16) {
accumulator.push(format!("color: #{}", value));
} else {
accumulator.push(format!("color: {}", value));
}
} else {
accumulator.push(format!("color: {}", value));
}
}

accumulator
}

pub fn analyze_paragraph_properties(properties: &ParagraphProperty) -> Vec<String> {
let mut accumulator: Vec<String> = vec![];

if let Some(alignment) = &properties.alignment.as_ref() {
accumulator.push(format!("text-align: {}", alignment.val));
};

accumulator
}

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

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

return accumulator;
}

0 comments on commit 4111931

Please sign in to comment.