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
//! Run element converter - handles text runs with formatting.
use super::ConversionContext;
use crate::Result;
use rs_docx::document::{BreakType, Run, RunContent};
/// Converter for Run elements.
pub struct RunConverter;
impl RunConverter {
/// Converts a Run to Markdown text with formatting.
pub fn convert<'a>(
run: &Run<'a>,
context: &mut ConversionContext<'a>,
para_style_id: Option<&str>,
) -> Result<String> {
let mut text = String::new();
// Extract text from run content
for content in &run.content {
match content {
RunContent::Text(t) => {
text.push_str(&t.text);
}
RunContent::Break(br) => match br.ty {
Some(BreakType::Page) => text.push_str("\n\n---\n\n"),
Some(BreakType::Column) => text.push_str("\n\n"),
_ => text.push('\n'),
},
RunContent::Tab(_) => {
text.push('\t');
}
RunContent::CarriageReturn(_) => {
text.push('\n');
}
RunContent::NoBreakHyphen(_) => {
text.push('\u{2011}');
}
RunContent::SoftHyphen(_) => {
text.push('\u{00AD}');
}
RunContent::Drawing(drawing) => {
// Handle inline images (DrawingML)
if let Some(img_md) = context.extract_image_from_drawing(drawing)? {
text.push_str(&img_md);
}
}
RunContent::Pict(pict) => {
// Handle legacy images (VML)
if let Some(img_md) = context.extract_image_from_pict(pict)? {
text.push_str(&img_md);
}
}
RunContent::Sym(sym) => {
// Symbol character - use Unicode if possible
if let Some(char_code) = &sym.char {
// Try to decode hex char code
if let Ok(code) = u32::from_str_radix(char_code, 16) {
if let Some(c) = char::from_u32(code) {
text.push(c);
}
}
}
}
RunContent::FootnoteReference(fnref) => {
if let Some(id_str) = &fnref.id {
if let Ok(id_num) = id_str.parse::<isize>() {
let marker = context.register_footnote_reference(id_num);
text.push_str(&marker);
}
}
}
RunContent::EndnoteReference(enref) => {
if let Some(id_str) = &enref.id {
if let Ok(id_num) = id_str.parse::<isize>() {
let marker = context.register_endnote_reference(id_num);
text.push_str(&marker);
}
}
}
RunContent::CommentReference(cref) => {
// Extract comment ID and look up comment text
if let Some(id) = &cref.id {
let marker = context.register_comment_reference(id.as_ref());
text.push_str(&marker);
}
}
RunContent::PTab(_) => {
text.push('\t');
}
RunContent::LastRenderedPageBreak(_) => {
text.push_str("\n\n---\n\n");
}
RunContent::PgNum(_) => {
text.push_str("{PAGE}");
}
RunContent::AnnotationRef(_)
| RunContent::FootnoteRef(_)
| RunContent::EndnoteRef(_)
| RunContent::Separator(_)
| RunContent::ContinuationSeparator(_) => {}
_ => {}
}
}
// Apply formatting if text is not empty
if text.is_empty() {
return Ok(text);
}
// Run Style ID
let mut run_style_id = None;
if let Some(props) = &run.property {
if let Some(style) = &props.style_id {
run_style_id = Some(style.value.as_ref());
}
}
// Check formatting via resolver
let effective_props =
context.resolve_run_property(run.property.as_ref(), run_style_id, para_style_id);
text = Self::apply_formatting(&text, &effective_props, context);
Ok(text)
}
/// Applies text formatting based on run properties.
fn apply_formatting(
text: &str,
props: &rs_docx::formatting::CharacterProperty<'_>,
context: &ConversionContext<'_>,
) -> String {
let mut result = text.to_string();
// Check for bold
let is_bold = props
.bold
.as_ref()
.map(|b| b.value.unwrap_or(true))
.unwrap_or(false);
// Check for italic
let is_italic = props
.italics
.as_ref()
.map(|i| i.value.unwrap_or(true))
.unwrap_or(false);
// Check for underline
let has_underline = props.underline.is_some();
// Check for strikethrough
let has_strike = props
.strike
.as_ref()
.map(|s| s.value.unwrap_or(true))
.unwrap_or(false);
// Apply formatting in order: underline (HTML), strike, bold, italic
if has_underline && context.html_underline_enabled() {
result = format!("<u>{}</u>", result);
}
if has_strike {
if context.html_strikethrough_enabled() {
result = format!("<s>{}</s>", result);
} else {
result = format!("~~{}~~", result);
}
}
if is_bold && is_italic {
result = format!("<strong>*{}*</strong>", result);
} else if is_bold {
result = format!("<strong>{}</strong>", result);
} else if is_italic {
result = format!("*{}*", result);
}
result
}
}