Skip to content

Commit 4111931

Browse files
feat: parse styles
1 parent 685360f commit 4111931

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/parser/paragraph.rs

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
use super::{
99
hyperlink::analyze_hyperlink,
1010
run::{analyze_run, analyze_run_properties},
11+
style::analyze_style,
1112
};
1213

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

21+
if let Some(style) = properties.style.as_ref() {
22+
unsafe {
23+
if let Some(style) = crate::state::STYLE_MAP.get(&style.val) {
24+
if let Some(based_on) = style.based_on.as_ref() {
25+
if let Some(based_on) = crate::state::STYLE_MAP.get(&based_on.val) {
26+
props.append(&mut analyze_style(&based_on));
27+
}
28+
}
29+
30+
props.append(&mut analyze_style(&style));
31+
}
32+
}
33+
}
34+
2035
props
2136
}
2237

src/parser/style.rs

+31-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use docx_rs::{RunProperty, Style};
1+
use docx_rs::{ParagraphProperty, RunProperty, Style};
22

33
fn analyze_run_properties(run_properties: &RunProperty) -> Vec<String> {
44
let mut accumulator: Vec<String> = vec![];
@@ -15,12 +15,12 @@ fn analyze_run_properties(run_properties: &RunProperty) -> Vec<String> {
1515
accumulator.push("text-decoration: underline".to_owned());
1616
};
1717

18-
if run_properties.sz.is_some() {
19-
accumulator.push(format!(
20-
"font-size: {}px",
21-
run_properties.sz.as_ref().unwrap().val
22-
));
23-
};
18+
// if run_properties.sz.is_some() {
19+
// accumulator.push(format!(
20+
// "font-size: {}px",
21+
// run_properties.sz.as_ref().unwrap().val
22+
// ));
23+
// };
2424

2525
if run_properties.strike.is_some() {
2626
accumulator.push("text-decoration: line-through".to_owned());
@@ -30,13 +30,37 @@ fn analyze_run_properties(run_properties: &RunProperty) -> Vec<String> {
3030
accumulator.push("visibility: hidden".to_owned());
3131
};
3232

33+
if let Some(color) = &run_properties.color.as_ref() {
34+
let value = &color.val;
35+
if value.len().eq(&6) || value.len().eq(&8) {
36+
if let Ok(_) = u32::from_str_radix(&value, 16) {
37+
accumulator.push(format!("color: #{}", value));
38+
} else {
39+
accumulator.push(format!("color: {}", value));
40+
}
41+
} else {
42+
accumulator.push(format!("color: {}", value));
43+
}
44+
}
45+
46+
accumulator
47+
}
48+
49+
pub fn analyze_paragraph_properties(properties: &ParagraphProperty) -> Vec<String> {
50+
let mut accumulator: Vec<String> = vec![];
51+
52+
if let Some(alignment) = &properties.alignment.as_ref() {
53+
accumulator.push(format!("text-align: {}", alignment.val));
54+
};
55+
3356
accumulator
3457
}
3558

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

3962
accumulator.append(&mut analyze_run_properties(&style.run_property));
63+
accumulator.append(&mut analyze_paragraph_properties(&style.paragraph_property));
4064

4165
return accumulator;
4266
}

0 commit comments

Comments
 (0)