1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! DOCX styles parsing.
use crate::error::{Error, Result};
use crate::model::{HeadingLevel, TextStyle};
use std::collections::HashMap;
/// Style type (paragraph, character, table, etc.)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StyleType {
Paragraph,
Character,
Table,
Numbering,
}
/// A parsed style definition.
#[derive(Debug, Clone, Default)]
pub struct Style {
/// Style ID (e.g., "Heading1")
pub id: String,
/// Style name (e.g., "Heading 1")
pub name: String,
/// Style type
pub style_type: Option<StyleType>,
/// Based on another style
pub based_on: Option<String>,
/// Paragraph properties
pub paragraph_props: ParagraphProps,
/// Run (text) properties
pub run_props: RunProps,
/// Outline level (for headings)
pub outline_level: Option<u8>,
}
/// Paragraph-level properties from a style.
#[derive(Debug, Clone, Default)]
#[allow(dead_code)]
pub struct ParagraphProps {
pub justification: Option<String>,
pub spacing_before: Option<i32>,
pub spacing_after: Option<i32>,
pub line_spacing: Option<i32>,
pub indent_left: Option<i32>,
pub indent_right: Option<i32>,
pub indent_hanging: Option<i32>,
pub indent_first_line: Option<i32>,
}
/// Run-level (character) properties from a style.
#[derive(Debug, Clone, Default)]
pub struct RunProps {
pub bold: Option<bool>,
pub italic: Option<bool>,
pub underline: Option<bool>,
pub strike: Option<bool>,
pub font_name: Option<String>,
pub font_size: Option<u32>,
pub color: Option<String>,
pub highlight: Option<String>,
}
impl RunProps {
/// Convert to TextStyle.
#[allow(dead_code)]
pub fn to_text_style(&self) -> TextStyle {
TextStyle {
bold: self.bold.unwrap_or(false),
italic: self.italic.unwrap_or(false),
underline: self.underline.unwrap_or(false),
strikethrough: self.strike.unwrap_or(false),
font: self.font_name.clone(),
size: self.font_size,
color: self.color.clone(),
highlight: self.highlight.clone(),
..Default::default()
}
}
/// Merge with another RunProps (other takes precedence).
pub fn merge(&mut self, other: &RunProps) {
if other.bold.is_some() {
self.bold = other.bold;
}
if other.italic.is_some() {
self.italic = other.italic;
}
if other.underline.is_some() {
self.underline = other.underline;
}
if other.strike.is_some() {
self.strike = other.strike;
}
if other.font_name.is_some() {
self.font_name = other.font_name.clone();
}
if other.font_size.is_some() {
self.font_size = other.font_size;
}
if other.color.is_some() {
self.color = other.color.clone();
}
if other.highlight.is_some() {
self.highlight = other.highlight.clone();
}
}
}
/// Collection of styles from styles.xml.
#[derive(Debug, Clone, Default)]
pub struct StyleMap {
/// Styles by ID
pub styles: HashMap<String, Style>,
/// Default paragraph style
pub default_paragraph: Option<String>,
/// Default character style
pub default_character: Option<String>,
}
impl StyleMap {
/// Parse styles from XML content.
pub fn parse(xml: &str) -> Result<Self> {
// Handle empty content
if xml.trim().is_empty() {
return Ok(Self::default());
}
let mut map = StyleMap::default();
let mut reader = quick_xml::Reader::from_str(xml);
reader.config_mut().trim_text(true);
let mut buf = Vec::new();
let mut current_style: Option<Style> = None;
let mut in_style = false;
let mut in_ppr = false;
let mut in_rpr = false;
loop {
match reader.read_event_into(&mut buf) {
Ok(quick_xml::events::Event::Start(e)) => {
let name = e.name();
match name.as_ref() {
b"w:style" => {
let mut style = Style::default();
for attr in e.attributes().flatten() {
match attr.key.as_ref() {
b"w:styleId" => {
style.id = String::from_utf8_lossy(&attr.value).to_string();
}
b"w:type" => {
let t = String::from_utf8_lossy(&attr.value);
style.style_type = match t.as_ref() {
"paragraph" => Some(StyleType::Paragraph),
"character" => Some(StyleType::Character),
"table" => Some(StyleType::Table),
"numbering" => Some(StyleType::Numbering),
_ => None,
};
}
b"w:default" => {
let is_default =
String::from_utf8_lossy(&attr.value) == "1";
if is_default {
if let Some(ref style_type) = style.style_type {
match style_type {
StyleType::Paragraph => {
map.default_paragraph =
Some(style.id.clone());
}
StyleType::Character => {
map.default_character =
Some(style.id.clone());
}
_ => {}
}
}
}
}
_ => {}
}
}
current_style = Some(style);
in_style = true;
}
b"w:pPr" if in_style => {
in_ppr = true;
}
b"w:rPr" if in_style => {
in_rpr = true;
}
_ => {}
}
}
Ok(quick_xml::events::Event::Empty(e)) => {
let name = e.name();
if let Some(ref mut style) = current_style {
match name.as_ref() {
b"w:name" => {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == b"w:val" {
style.name =
String::from_utf8_lossy(&attr.value).to_string();
}
}
}
b"w:basedOn" => {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == b"w:val" {
style.based_on =
Some(String::from_utf8_lossy(&attr.value).to_string());
}
}
}
b"w:outlineLvl" if in_ppr => {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == b"w:val" {
let val = String::from_utf8_lossy(&attr.value);
style.outline_level = val.parse().ok();
}
}
}
b"w:jc" if in_ppr => {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == b"w:val" {
style.paragraph_props.justification =
Some(String::from_utf8_lossy(&attr.value).to_string());
}
}
}
b"w:b" if in_rpr => {
let val = get_bool_attr(&e, b"w:val");
style.run_props.bold = Some(val.unwrap_or(true));
}
b"w:i" if in_rpr => {
let val = get_bool_attr(&e, b"w:val");
style.run_props.italic = Some(val.unwrap_or(true));
}
b"w:u" if in_rpr => {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == b"w:val" {
let val = String::from_utf8_lossy(&attr.value);
style.run_props.underline = Some(val != "none");
}
}
}
b"w:strike" if in_rpr => {
let val = get_bool_attr(&e, b"w:val");
style.run_props.strike = Some(val.unwrap_or(true));
}
b"w:sz" if in_rpr => {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == b"w:val" {
let val = String::from_utf8_lossy(&attr.value);
style.run_props.font_size = val.parse().ok();
}
}
}
b"w:color" if in_rpr => {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == b"w:val" {
let val = String::from_utf8_lossy(&attr.value);
if val != "auto" {
style.run_props.color = Some(val.to_string());
}
}
}
}
b"w:rFonts" if in_rpr => {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == b"w:ascii" {
style.run_props.font_name =
Some(String::from_utf8_lossy(&attr.value).to_string());
break;
}
}
}
_ => {}
}
}
}
Ok(quick_xml::events::Event::End(e)) => match e.name().as_ref() {
b"w:style" => {
if let Some(style) = current_style.take() {
map.styles.insert(style.id.clone(), style);
}
in_style = false;
in_ppr = false;
in_rpr = false;
}
b"w:pPr" => {
in_ppr = false;
}
b"w:rPr" => {
in_rpr = false;
}
_ => {}
},
Ok(quick_xml::events::Event::Eof) => break,
Err(e) => return Err(Error::XmlParse(e.to_string())),
_ => {}
}
buf.clear();
}
Ok(map)
}
/// Get a style by ID, resolving inheritance.
pub fn get_resolved(&self, id: &str) -> Option<Style> {
let mut style = self.styles.get(id)?.clone();
// Resolve inheritance chain (max 10 levels to prevent infinite loops)
let mut depth = 0;
let mut current_based_on = style.based_on.clone();
while let Some(ref base_id) = current_based_on {
if depth > 10 {
break;
}
if let Some(base) = self.styles.get(base_id) {
// Merge base properties (base first, then override)
let mut merged_run = base.run_props.clone();
merged_run.merge(&style.run_props);
style.run_props = merged_run;
if style.outline_level.is_none() {
style.outline_level = base.outline_level;
}
current_based_on = base.based_on.clone();
} else {
break;
}
depth += 1;
}
Some(style)
}
/// Get the heading level for a style ID.
pub fn get_heading_level(&self, style_id: &str) -> HeadingLevel {
if let Some(style) = self.get_resolved(style_id) {
if let Some(level) = style.outline_level {
return HeadingLevel::from_number(level + 1);
}
// Also check for Title/Subtitle styles
let name_lower = style.name.to_lowercase();
if name_lower == "title" {
return HeadingLevel::H1;
}
if name_lower == "subtitle" {
return HeadingLevel::H2;
}
}
HeadingLevel::None
}
}
/// Helper to get a boolean attribute value.
fn get_bool_attr(e: &quick_xml::events::BytesStart, key: &[u8]) -> Option<bool> {
for attr in e.attributes().flatten() {
if attr.key.as_ref() == key {
let val = String::from_utf8_lossy(&attr.value);
return Some(val != "0" && val != "false");
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_styles() {
let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:style w:type="paragraph" w:styleId="Heading1">
<w:name w:val="Heading 1"/>
<w:basedOn w:val="Normal"/>
<w:pPr>
<w:outlineLvl w:val="0"/>
</w:pPr>
<w:rPr>
<w:b/>
<w:sz w:val="32"/>
</w:rPr>
</w:style>
</w:styles>"#;
let map = StyleMap::parse(xml).unwrap();
assert!(map.styles.contains_key("Heading1"));
let style = map.styles.get("Heading1").unwrap();
assert_eq!(style.name, "Heading 1");
assert_eq!(style.outline_level, Some(0));
assert_eq!(style.run_props.bold, Some(true));
assert_eq!(style.run_props.font_size, Some(32));
}
#[test]
fn test_heading_level() {
let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:style w:type="paragraph" w:styleId="Title">
<w:name w:val="Title"/>
</w:style>
<w:style w:type="paragraph" w:styleId="Heading2">
<w:name w:val="Heading 2"/>
<w:pPr>
<w:outlineLvl w:val="1"/>
</w:pPr>
</w:style>
</w:styles>"#;
let map = StyleMap::parse(xml).unwrap();
assert_eq!(map.get_heading_level("Title"), HeadingLevel::H1);
assert_eq!(map.get_heading_level("Heading2"), HeadingLevel::H2);
assert_eq!(map.get_heading_level("Unknown"), HeadingLevel::None);
}
}