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
use super::{ImageExtractor, NumberingResolver, StyleResolver};
use crate::core::ast::ReferenceDefinitions;
use crate::{ConvertOptions, Result};
use std::collections::{HashMap, HashSet};
/// Shared mutable state threaded through the conversion pipeline.
///
/// Carries references to the document's relationships, numbering definitions,
/// images, styles, and accumulates footnote / endnote / comment definitions
/// encountered during extraction. Custom [`AstExtractor`](crate::adapters::docx::AstExtractor)
/// implementations receive this context to register references and resolve
/// document resources.
pub struct ConversionContext<'a> {
rels: &'a HashMap<String, String>,
numbering: &'a mut NumberingResolver,
image_extractor: &'a mut ImageExtractor,
options: &'a ConvertOptions,
style_resolver: &'a StyleResolver<'a>,
footnotes: Vec<String>,
footnote_index_by_id: HashMap<isize, usize>,
footnote_text_by_id: HashMap<isize, String>,
endnotes: Vec<String>,
endnote_index_by_id: HashMap<isize, usize>,
endnote_text_by_id: HashMap<isize, String>,
comments: Vec<(String, String)>,
seen_comment_ids: HashSet<String>,
comment_text_by_id: HashMap<String, String>,
missing_references: Vec<String>,
in_table_cell: bool,
}
impl<'a> ConversionContext<'a> {
/// Creates a new context from the parsed DOCX components.
#[allow(clippy::too_many_arguments)]
pub fn new(
rels: &'a HashMap<String, String>,
numbering: &'a mut NumberingResolver,
image_extractor: &'a mut ImageExtractor,
options: &'a ConvertOptions,
docx_comments: Option<&'a rs_docx::document::Comments<'a>>,
docx_footnotes: Option<&'a rs_docx::document::FootNotes<'a>>,
docx_endnotes: Option<&'a rs_docx::document::EndNotes<'a>>,
style_resolver: &'a StyleResolver<'a>,
) -> Self {
let comment_text_by_id = docx_comments
.map(|comments| {
comments
.comments
.iter()
.filter_map(|comment| {
comment
.id
.map(|id| (id.to_string(), comment.content.text().to_string()))
})
.collect::<HashMap<_, _>>()
})
.unwrap_or_default();
let footnote_text_by_id = docx_footnotes
.map(|footnotes| {
footnotes
.content
.iter()
.filter_map(|footnote| {
footnote.id.map(|id| {
let text = footnote
.content
.iter()
.filter_map(|bc| match bc {
rs_docx::document::BodyContent::Paragraph(p) => {
Some(p.text().to_string())
}
_ => None,
})
.collect::<Vec<_>>()
.join(" ");
(id, text)
})
})
.collect::<HashMap<_, _>>()
})
.unwrap_or_default();
let endnote_text_by_id = docx_endnotes
.map(|endnotes| {
endnotes
.content
.iter()
.filter_map(|endnote| {
endnote.id.map(|id| {
let text = endnote
.content
.iter()
.filter_map(|bc| match bc {
rs_docx::document::BodyContent::Paragraph(p) => {
Some(p.text().to_string())
}
_ => None,
})
.collect::<Vec<_>>()
.join(" ");
(id, text)
})
})
.collect::<HashMap<_, _>>()
})
.unwrap_or_default();
Self {
rels,
numbering,
image_extractor,
options,
style_resolver,
footnotes: Vec::new(),
footnote_index_by_id: HashMap::new(),
footnote_text_by_id,
endnotes: Vec::new(),
endnote_index_by_id: HashMap::new(),
endnote_text_by_id,
comments: Vec::new(),
seen_comment_ids: HashSet::new(),
comment_text_by_id,
missing_references: Vec::new(),
in_table_cell: false,
}
}
// ── Table cell state ────────────────────────────────────────────
/// Marks whether conversion is currently inside a table cell.
pub fn set_in_table_cell(&mut self, value: bool) {
self.in_table_cell = value;
}
/// Returns `true` if conversion is currently inside a table cell.
pub fn is_in_table_cell(&self) -> bool {
self.in_table_cell
}
// ── Reference registration ──────────────────────────────────────
/// Registers a comment reference and returns the Markdown footnote marker
/// (e.g., `[^c3]`). Duplicate calls with the same `id` return the same marker.
pub fn register_comment_reference(&mut self, id: &str) -> String {
if !self.seen_comment_ids.contains(id) {
let comment_text = self.comment_text_by_id.get(id).cloned().unwrap_or_else(|| {
self.missing_references.push(format!("comment:{id}"));
String::new()
});
self.comments.push((id.to_string(), comment_text));
self.seen_comment_ids.insert(id.to_string());
}
format!("[^c{}]", id)
}
/// Registers a footnote reference and returns the Markdown footnote marker
/// (e.g., `[^1]`). Duplicate calls with the same `id` return the same marker.
pub fn register_footnote_reference(&mut self, id: isize) -> String {
if let Some(idx) = self.footnote_index_by_id.get(&id).copied() {
return format!("[^{}]", idx);
}
let footnote_text = self
.footnote_text_by_id
.get(&id)
.cloned()
.unwrap_or_else(|| {
self.missing_references.push(format!("footnote:{id}"));
String::new()
});
self.footnotes.push(footnote_text);
let idx = self.footnotes.len();
self.footnote_index_by_id.insert(id, idx);
format!("[^{}]", idx)
}
/// Registers an endnote reference and returns the Markdown footnote marker
/// (e.g., `[^en1]`). Duplicate calls with the same `id` return the same marker.
pub fn register_endnote_reference(&mut self, id: isize) -> String {
if let Some(idx) = self.endnote_index_by_id.get(&id).copied() {
return format!("[^en{}]", idx);
}
let endnote_text = self
.endnote_text_by_id
.get(&id)
.cloned()
.unwrap_or_else(|| {
self.missing_references.push(format!("endnote:{id}"));
String::new()
});
self.endnotes.push(endnote_text);
let idx = self.endnotes.len();
self.endnote_index_by_id.insert(id, idx);
format!("[^en{}]", idx)
}
// ── Reference output ────────────────────────────────────────────
/// Builds the collected [`ReferenceDefinitions`] for the final document.
pub fn reference_definitions(&self) -> ReferenceDefinitions {
ReferenceDefinitions {
footnotes: self.footnotes.clone(),
endnotes: self.endnotes.clone(),
comments: self.comments.clone(),
}
}
/// Drains and returns any reference IDs that could not be resolved.
///
/// Used by [`DocxToMarkdown`](crate::DocxToMarkdown) when strict reference
/// validation is enabled.
pub fn take_missing_references(&mut self) -> Vec<String> {
std::mem::take(&mut self.missing_references)
}
// ── Document resource access ────────────────────────────────────
/// Looks up a relationship target (e.g., hyperlink URL) by its ID.
pub fn relationship_target(&self, id: &str) -> Option<&str> {
self.rels.get(id).map(String::as_str)
}
/// Extracts an image from a `<w:drawing>` element using the configured
/// [`ImageHandling`](crate::ImageHandling) strategy.
pub fn extract_image_from_drawing(
&mut self,
drawing: &rs_docx::document::Drawing,
) -> Result<Option<String>> {
self.image_extractor
.extract_from_drawing(drawing, self.rels)
}
/// Extracts an image from a `<w:pict>` (VML) element using the configured
/// [`ImageHandling`](crate::ImageHandling) strategy.
pub fn extract_image_from_pict(
&mut self,
pict: &rs_docx::document::Pict,
) -> Result<Option<String>> {
self.image_extractor.extract_from_pict(pict, self.rels)
}
// ── Style resolution ────────────────────────────────────────────
/// Resolves the effective character properties for a run, merging direct
/// properties with inherited style chain values.
pub fn resolve_run_property(
&self,
direct_props: Option<&rs_docx::formatting::CharacterProperty<'a>>,
run_style_id: Option<&str>,
para_style_id: Option<&str>,
) -> rs_docx::formatting::CharacterProperty<'a> {
self.style_resolver
.resolve_run_property(direct_props, run_style_id, para_style_id)
}
/// Resolves the effective paragraph properties, merging direct properties
/// with inherited style chain values.
pub fn resolve_paragraph_property(
&self,
direct_props: Option<&rs_docx::formatting::ParagraphProperty<'a>>,
para_style_id: Option<&str>,
) -> rs_docx::formatting::ParagraphProperty<'a> {
self.style_resolver
.resolve_paragraph_property(direct_props, para_style_id)
}
// ── List numbering ──────────────────────────────────────────────
/// Returns the next list marker for the given numbering definition and level
/// (e.g., `"1."`, `"-"`, `"가."`, `"(a)"`).
pub fn next_list_marker(&mut self, num_id: i32, ilvl: i32) -> String {
self.numbering.next_marker(num_id, ilvl)
}
/// Returns the indentation level for the given numbering definition and level.
pub fn list_indent_level(&self, num_id: i32, ilvl: i32) -> usize {
self.numbering.get_indent(num_id, ilvl)
}
// ── Option accessors ────────────────────────────────────────────
/// Whether the [`preserve_whitespace`](crate::ConvertOptions::preserve_whitespace)
/// option is enabled.
pub fn preserve_whitespace(&self) -> bool {
self.options.preserve_whitespace
}
/// Whether HTML `<u>` tags should be emitted for underlined text.
pub fn html_underline_enabled(&self) -> bool {
self.options.html_underline
}
/// Whether HTML `<s>` tags should be emitted for strikethrough text.
pub fn html_strikethrough_enabled(&self) -> bool {
self.options.html_strikethrough
}
// ── Counters ────────────────────────────────────────────────────
/// Number of footnotes registered so far.
pub fn footnote_count(&self) -> usize {
self.footnotes.len()
}
/// Number of endnotes registered so far.
pub fn endnote_count(&self) -> usize {
self.endnotes.len()
}
/// Number of comments registered so far.
pub fn comment_count(&self) -> usize {
self.comments.len()
}
/// Returns the comment `(id, text)` at the given index, if it exists.
pub fn comment_at(&self, index: usize) -> Option<(&str, &str)> {
self.comments
.get(index)
.map(|(id, text)| (id.as_str(), text.as_str()))
}
}