use std::collections::{HashMap, HashSet, VecDeque};
use roxmltree::{Document, Node as XmlNode};
use crate::backend::images::ImagePolicy;
use crate::backend::markdown::escape_text;
use crate::backend::ooxml::Package;
use crate::backend::DeclarativeBackend;
use crate::error::ConversionError;
use crate::source::SourceDocument;
use docling_core::{
inline_paragraph_node, DoclingDocument, InlineRun, ListItemDclx, Node, PictureDescription,
PictureImage, Script, Table, TableStructure, TrackChanges,
};
pub struct OdfBackend;
#[derive(Default, Clone, Copy, PartialEq)]
pub(super) struct Fmt {
pub(super) bold: bool,
pub(super) italic: bool,
pub(super) strike: bool,
pub(super) underline: bool,
pub(super) script: u8, }
impl Fmt {
fn to_inline_run(self, text: &str) -> InlineRun {
InlineRun {
text: text.to_string(),
bold: self.bold,
italic: self.italic,
underline: self.underline,
strike: self.strike,
script: match self.script {
1 => Script::Sub,
2 => Script::Super,
_ => Script::Baseline,
},
code: false,
formula: false,
}
}
}
#[derive(Default, Clone)]
struct StyleInfo {
parent: Option<String>,
display_name: Option<String>,
bold: Option<bool>,
italic: Option<bool>,
strike: Option<bool>,
underline: Option<bool>,
script: Option<u8>,
list_style: Option<String>,
list_level: Option<i64>,
}
#[derive(Default, Clone)]
pub(super) struct OdfLevel {
pub(super) numbered: bool,
pub(super) start: i64,
pub(super) prefix: String,
pub(super) suffix: String,
pub(super) format: String,
}
pub(super) fn odf_marker(counter: i64, level: Option<&OdfLevel>) -> String {
let (prefix, suffix, format) = level
.map(|l| (l.prefix.as_str(), l.suffix.as_str(), l.format.as_str()))
.unwrap_or_default();
let num_fmt = match format {
"a" => Some("lowerLetter"),
"A" => Some("upperLetter"),
"i" => Some("lowerRoman"),
"I" => Some("upperRoman"),
_ => None,
};
let n = super::docx::format_enum_counter(counter, num_fmt);
if prefix.is_empty() && suffix.is_empty() {
format!("{n}.")
} else {
format!("{prefix}{n}{suffix}")
}
}
type ListStyles = HashMap<String, HashMap<i64, OdfLevel>>;
#[derive(Clone)]
pub(super) struct ChartInfo {
pub(super) kind: String,
pub(super) table: Table,
}
pub(super) struct Styles {
map: HashMap<String, StyleInfo>,
lists: ListStyles,
pub(super) charts: HashMap<String, ChartInfo>,
pub(super) images: HashMap<String, PictureImage>,
parts: Option<HashSet<String>>,
policy: ImagePolicy,
hidden_pages: HashSet<String>,
comment_slots: HashMap<roxmltree::NodeId, usize>,
}
impl DeclarativeBackend for OdfBackend {
fn convert(&self, source: &SourceDocument) -> Result<DoclingDocument, ConversionError> {
convert_odf(source, &ImagePolicy::default(), None)
}
}
fn manifest_encrypts_content(manifest: &str) -> bool {
let Ok(dom) = Document::parse(manifest) else {
return false;
};
dom.descendants().any(|e| {
e.tag_name().name() == "file-entry"
&& e.attributes()
.any(|a| a.name() == "full-path" && a.value() == "content.xml")
&& e.children()
.any(|c| c.tag_name().name() == "encryption-data")
})
}
pub(crate) fn convert_odf(
source: &SourceDocument,
policy: &ImagePolicy,
track_changes: Option<TrackChanges>,
) -> Result<DoclingDocument, ConversionError> {
if let Some(mode @ (TrackChanges::Reject | TrackChanges::All)) = track_changes {
return Err(ConversionError::UnsupportedOption(format!(
"odf: track_changes={mode} is not implemented by the ODF reader (its text is the \
accepted view); accept is"
)));
}
{
let mut pkg = Package::open(&source.bytes);
if let Some(manifest) = pkg.as_mut().and_then(|p| p.read("META-INF/manifest.xml")) {
if manifest_encrypts_content(&manifest) {
return Err(crate::backend::offcrypto::unsupported(
source.format.as_str(),
"ODF package encryption",
));
}
}
let (content, styles_xml) = match pkg.as_mut() {
Some(pkg) => (
pkg.read("content.xml")
.ok_or_else(|| ConversionError::Parse("odf: no content.xml".into()))?,
pkg.read("styles.xml").unwrap_or_default(),
),
None => (source.text()?.to_string(), String::new()),
};
let content_dom =
Document::parse(&content).map_err(|e| ConversionError::with_source("odf", e))?;
if pkg.is_none() {
let root = content_dom.root_element().tag_name().name();
if root != "document" && root != "document-content" {
return Err(ConversionError::Parse(
"odf: not a zip package or flat office XML".into(),
));
}
}
let styles_dom = Document::parse(&styles_xml).ok();
let mut styles = parse_styles(&content_dom, styles_dom.as_ref());
styles.policy = policy.clone();
if let Some(pkg) = pkg.as_mut() {
styles.parts = Some(pkg.names().map(str::to_string).collect());
styles.charts = load_charts(pkg, &content_dom, &styles);
let names: Vec<String> = pkg
.names()
.filter(|n| n.starts_with("Pictures/"))
.map(str::to_string)
.collect();
for name in names {
if let Some(img) = pkg
.read_bytes(&name)
.and_then(|b| crate::backend::ooxml::picture_image(&name, b))
{
styles.images.insert(name, img);
}
}
}
let mut doc = DoclingDocument::new(&source.name);
let Some(body) = content_dom
.root_element()
.children()
.find(|n| n.has_tag_name("body"))
else {
return Ok(doc);
};
let mut built: Option<super::odf_tree::Built> = None;
let mut wrapped = false;
for office in body.children().filter(XmlNode::is_element) {
match office.tag_name().name() {
"text" => {
let comments = annotations(office);
styles.comment_slots = comment_slots(&comments);
let furniture = master_page_parts(&content_dom, styles_dom.as_ref());
walk_text(office, &styles, &mut doc);
push_master_page_furniture(&furniture, &styles, &mut doc);
push_note_furniture(office, &mut doc);
push_comment_sections(&comments, &mut doc);
built = Some(super::odf_tree::build_text(
office, &styles, &comments, &furniture,
));
}
"spreadsheet" => {
walk_spreadsheet(office, &styles, &mut doc);
built = Some(super::odf_tree::build_spreadsheet(office, &styles));
}
"presentation" => {
walk_presentation(office, &styles, &mut doc);
built = Some(super::odf_tree::build_presentation(office, &styles));
}
_ => continue,
}
wrapped = true;
}
if !wrapped {
let class = attr(content_dom.root_element(), "class");
if class == Some("presentation") || body.children().any(|c| c.has_tag_name("page")) {
walk_presentation(body, &styles, &mut doc);
built = Some(super::odf_tree::build_presentation(body, &styles));
} else if class == Some("spreadsheet") {
walk_spreadsheet(body, &styles, &mut doc);
built = Some(super::odf_tree::build_spreadsheet(body, &styles));
} else {
let comments = annotations(body);
styles.comment_slots = comment_slots(&comments);
let furniture = master_page_parts(&content_dom, styles_dom.as_ref());
walk_text(body, &styles, &mut doc);
push_master_page_furniture(&furniture, &styles, &mut doc);
push_note_furniture(body, &mut doc);
push_comment_sections(&comments, &mut doc);
built = Some(super::odf_tree::build_text(
body, &styles, &comments, &furniture,
));
}
}
if let Some(built) = built {
for (page_no, width, height) in built.pages {
doc.push(Node::PageInfo {
page_no,
width: width as f32,
height: height as f32,
});
}
doc.tree = Some(built.tree);
}
Ok(doc)
}
}
pub(super) fn is_list_tag(name: &str) -> bool {
matches!(name, "list" | "ordered-list" | "unordered-list")
}
pub(super) fn is_transparent_container(name: &str) -> bool {
matches!(
name,
"section"
| "table-of-content"
| "alphabetical-index"
| "illustration-index"
| "table-index"
| "object-index"
| "user-index"
| "bibliography"
| "index-body"
| "index-title"
)
}
fn parse_styles(content: &Document, styles: Option<&Document>) -> Styles {
let mut map = HashMap::new();
let mut lists = HashMap::new();
let mut hidden_pages = HashSet::new();
for dom in [Some(content), styles].into_iter().flatten() {
for s in dom.descendants() {
match s.tag_name().name() {
"style" => {
if let Some(name) = attr(s, "name") {
map.insert(name.to_string(), style_info(s));
let hides_page = attr(s, "family") == Some("drawing-page")
&& s.children().any(|c| {
c.has_tag_name("drawing-page-properties")
&& attr(c, "visibility") == Some("hidden")
});
if hides_page {
hidden_pages.insert(name.to_string());
}
}
}
"list-style" => {
if let Some(name) = attr(s, "name") {
let mut levels = HashMap::new();
for lv in s.children().filter(XmlNode::is_element) {
let level: i64 =
attr(lv, "level").and_then(|v| v.parse().ok()).unwrap_or(1);
let numbered = lv.tag_name().name() == "list-level-style-number";
let start = attr(lv, "start-value")
.and_then(|v| v.parse().ok())
.map(|n: i64| n.max(1))
.unwrap_or(1);
let prefix = attr(lv, "num-prefix").unwrap_or("").to_string();
let suffix = attr(lv, "num-suffix").unwrap_or("").to_string();
let format = attr(lv, "num-format").unwrap_or("").to_string();
levels.insert(
level,
OdfLevel {
numbered,
start,
prefix,
suffix,
format,
},
);
}
lists.insert(name.to_string(), levels);
}
}
_ => {}
}
}
}
Styles {
map,
lists,
charts: HashMap::new(),
images: HashMap::new(),
parts: None,
policy: ImagePolicy::default(),
hidden_pages,
comment_slots: HashMap::new(),
}
}
pub(super) fn slide_is_hidden(page: XmlNode, styles: &Styles) -> bool {
attr(page, "style-name").is_some_and(|name| styles.hidden_pages.contains(name))
}
pub(super) fn section_is_hidden(section: XmlNode) -> bool {
attr(section, "display") == Some("none")
}
fn load_charts(
pkg: &mut Package,
content: &Document,
styles: &Styles,
) -> HashMap<String, ChartInfo> {
let mut charts = HashMap::new();
for obj in content.descendants().filter(|n| n.has_tag_name("object")) {
let Some(href) = attr(obj, "href") else {
continue;
};
let name = href.trim_start_matches("./");
if charts.contains_key(name) {
continue;
}
let Some(xml) = pkg.read(&format!("{name}/content.xml")) else {
continue;
};
let Ok(dom) = Document::parse(&xml) else {
continue;
};
if !dom.descendants().any(|n| n.has_tag_name("chart")) {
continue;
}
let kind = dom
.descendants()
.filter(|n| n.has_tag_name("chart"))
.find_map(|n| attr(n, "class").and_then(chart_kind))
.or_else(|| {
dom.descendants()
.filter(|n| n.has_tag_name("series"))
.find_map(|n| attr(n, "class").and_then(chart_kind))
})
.unwrap_or("other_chart")
.to_string();
let Some(table_node) = dom.descendants().find(|n| n.has_tag_name("table")) else {
continue;
};
if let Some(table) = parse_table(table_node, styles) {
charts.insert(name.to_string(), ChartInfo { kind, table });
}
}
charts
}
pub(super) fn chart_kind(class: &str) -> Option<&'static str> {
match class {
"chart:bar" => Some("bar_chart"),
"chart:line" => Some("line_chart"),
"chart:circle" | "chart:pie" => Some("pie_chart"),
"chart:scatter" => Some("scatter_plot"),
_ => None,
}
}
fn style_info(s: XmlNode) -> StyleInfo {
let mut info = StyleInfo {
parent: attr(s, "parent-style-name").map(str::to_string),
display_name: attr(s, "display-name").map(str::to_string),
list_style: attr(s, "list-style-name").map(str::to_string),
list_level: attr(s, "list-level").and_then(|v| v.parse().ok()),
..Default::default()
};
if let Some(tp) = s
.children()
.find(|c| c.has_tag_name("text-properties") || c.has_tag_name("properties"))
{
info.bold = attr(tp, "font-weight").map(is_bold);
info.italic = attr(tp, "font-style").map(|v| v == "italic" || v == "oblique");
info.strike = attr(tp, "text-line-through-style")
.or_else(|| attr(tp, "text-crossing-out"))
.map(|v| v != "none");
info.underline = attr(tp, "text-underline-style")
.or_else(|| attr(tp, "text-underline"))
.map(|v| v != "none");
info.script = attr(tp, "text-position").map(|v| {
if v.starts_with("super") {
2
} else if v.starts_with("sub") {
1
} else {
0
}
});
}
info
}
fn is_bold(v: &str) -> bool {
v == "bold" || v.parse::<i32>().map(|n| n >= 600).unwrap_or(false)
}
pub(super) fn resolve_fmt(styles: &Styles, name: Option<&str>, base: Fmt) -> Fmt {
let mut fmt = base;
let mut chain = Vec::new();
let mut cur = name.map(str::to_string);
let mut seen = std::collections::HashSet::new();
while let Some(n) = cur {
if !seen.insert(n.clone()) {
break;
}
if let Some(info) = styles.map.get(&n) {
chain.push(info.clone());
cur = info.parent.clone();
} else {
break;
}
}
for info in chain.into_iter().rev() {
if let Some(b) = info.bold {
fmt.bold = b;
}
if let Some(i) = info.italic {
fmt.italic = i;
}
if let Some(s) = info.strike {
fmt.strike = s;
}
if let Some(u) = info.underline {
fmt.underline = u;
}
if let Some(sc) = info.script {
fmt.script = sc;
}
}
fmt
}
pub(super) fn paragraph_list_style(styles: &Styles, name: Option<&str>) -> Option<(String, i64)> {
let mut cur = name.map(str::to_string);
let mut seen = HashSet::new();
while let Some(n) = cur {
if !seen.insert(n.clone()) {
break;
}
let info = styles.map.get(&n)?;
if let Some(list) = &info.list_style {
if list.is_empty() {
return None;
}
return Some((list.clone(), info.list_level.unwrap_or(1).max(1)));
}
cur = info.parent.clone();
}
None
}
pub(super) fn paragraph_style_names(styles: &Styles, name: Option<&str>) -> Vec<String> {
let mut out = Vec::new();
if let Some(n) = name {
out.push(n.to_string());
if let Some(info) = styles.map.get(n) {
if let Some(p) = &info.parent {
out.push(p.clone());
}
if let Some(d) = &info.display_name {
out.push(d.clone());
}
}
}
out
}
#[derive(Clone)]
pub(super) struct Run {
pub(super) text: String,
pub(super) fmt: Fmt,
pub(super) href: Option<String>,
}
pub(super) fn hyperlink_from_href(href: Option<&str>) -> Option<String> {
let href = href?.trim();
if href.is_empty() {
return None;
}
let scheme_len = href.find(':').filter(|&k| {
let (first, rest) = href[..k].split_at(1);
k > 0
&& first.chars().all(|c| c.is_ascii_alphabetic())
&& rest
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '.' | '-'))
});
let Some(k) = scheme_len else {
return Some(href.to_string()); };
let scheme = href[..k].to_ascii_lowercase();
let rest = &href[k + 1..];
if rest.is_empty() {
return None; }
let Some(authority) = rest.strip_prefix("//") else {
return Some(format!("{scheme}:{rest}")); };
let host_end = authority.find(['/', '?', '#']).unwrap_or(authority.len());
let (host, tail) = authority.split_at(host_end);
if host.is_empty() || (host.starts_with('[') && !host.contains(']')) {
return None;
}
let slash = if tail.starts_with('/') { "" } else { "/" };
Some(format!(
"{scheme}://{}{slash}{tail}",
host.to_ascii_lowercase()
))
}
pub(super) fn collect_runs(el: XmlNode, styles: &Styles, base: Fmt, out: &mut Vec<Run>) {
collect_runs_linked(el, styles, base, None, out)
}
fn collect_runs_linked(
el: XmlNode,
styles: &Styles,
base: Fmt,
href: Option<&str>,
out: &mut Vec<Run>,
) {
let run = |text: String, fmt: Fmt| Run {
text,
fmt,
href: href.map(str::to_string),
};
for child in el.children() {
if child.is_text() {
if let Some(t) = child.text() {
out.push(run(t.to_string(), base));
}
} else if child.is_element() {
match child.tag_name().name() {
"span" => {
let fmt = resolve_fmt(styles, attr(child, "style-name"), base);
collect_runs_linked(child, styles, fmt, href, out);
}
"a" => {
let fmt = resolve_fmt(styles, attr(child, "style-name"), base);
let link = hyperlink_from_href(attr(child, "href"));
collect_runs_linked(child, styles, fmt, link.as_deref().or(href), out);
}
"line-break" => out.push(run("\n".into(), base)),
"tab" | "tab-stop" => out.push(run("\t".into(), base)),
"s" => {
let n: usize = attr(child, "c").and_then(|v| v.parse().ok()).unwrap_or(1);
out.push(run(" ".repeat(n), base));
}
"object" | "binary-data" => {}
"note" => {}
"annotation" | "annotation-end" => {}
"text-box" => {}
_ => collect_runs_linked(child, styles, base, href, out),
}
}
}
}
pub(super) fn note_texts(body: XmlNode) -> Vec<String> {
note_bodies(body)
.into_iter()
.map(|(_, text)| text)
.collect()
}
pub(super) fn note_bodies(body: XmlNode) -> Vec<(roxmltree::NodeId, String)> {
body.descendants()
.filter(|n| n.has_tag_name("note"))
.filter_map(|note| {
let note_body = note.children().find(|c| c.has_tag_name("note-body"))?;
let text: String = note_body
.descendants()
.filter(|n| n.is_text())
.filter_map(|n| n.text())
.collect::<String>()
.trim()
.to_string();
(!text.is_empty()).then_some((note.id(), text))
})
.collect()
}
pub(super) fn note_calls(el: XmlNode) -> (String, Vec<(usize, roxmltree::NodeId)>) {
fn walk(el: XmlNode, text: &mut String, out: &mut Vec<(usize, roxmltree::NodeId)>) {
for child in el.children() {
if child.is_text() {
text.push_str(child.text().unwrap_or_default());
continue;
}
if !child.is_element() {
continue;
}
match child.tag_name().name() {
"line-break" => text.push('\n'),
"tab" | "tab-stop" => text.push('\t'),
"s" => {
let n: usize = attr(child, "c").and_then(|v| v.parse().ok()).unwrap_or(1);
text.push_str(&" ".repeat(n));
}
"object" | "binary-data" => {}
"note" => out.push((text.chars().count(), child.id())),
"annotation" | "annotation-end" | "text-box" => {}
_ => walk(child, text, out),
}
}
}
let mut text = String::new();
let mut out = Vec::new();
if el.has_tag_name("list-item") || el.has_tag_name("list-header") {
for c in el.children().filter(|c| c.has_tag_name("p")) {
walk(c, &mut text, &mut out);
}
} else {
walk(el, &mut text, &mut out);
}
(text, out)
}
pub(super) struct MasterPart<'a, 'i> {
pub(super) footer: bool,
pub(super) el: XmlNode<'a, 'i>,
}
pub(super) fn master_page_parts<'a, 'i>(
content: &'a Document<'i>,
styles: Option<&'a Document<'i>>,
) -> Vec<MasterPart<'a, 'i>> {
let dom = styles.unwrap_or(content);
let Some(master) = dom
.root_element()
.children()
.find(|c| c.has_tag_name("master-styles"))
else {
return Vec::new();
};
let mut out = Vec::new();
for page in master.children().filter(|c| c.has_tag_name("master-page")) {
for (footer, tags) in [
(false, ["header", "header-left", "header-first"]),
(true, ["footer", "footer-left", "footer-first"]),
] {
for el in page
.children()
.filter(|c| tags.contains(&c.tag_name().name()))
{
let shown = attr(el, "display") != Some("false");
let has_content = el.descendants().any(|n| {
n.has_tag_name("image")
|| n.has_tag_name("object")
|| n.is_text() && !n.text().unwrap_or("").trim().is_empty()
});
if shown && has_content {
out.push(MasterPart { footer, el });
}
}
}
}
out
}
fn push_master_page_furniture(parts: &[MasterPart], styles: &Styles, doc: &mut DoclingDocument) {
for part in parts {
let mut sub = DoclingDocument::new("");
walk_blocks(
part.el.children().filter(XmlNode::is_element),
styles,
&mut sub,
);
for n in sub.nodes {
doc.nodes.push(Node::Furniture {
layer: docling_core::ContentLayer::Furniture,
inner: Box::new(n),
});
}
}
}
fn push_note_furniture(body: XmlNode, doc: &mut DoclingDocument) {
for text in note_texts(body) {
doc.nodes.push(Node::Furniture {
layer: docling_core::ContentLayer::Furniture,
inner: Box::new(Node::Paragraph {
text: super::markdown::escape_html(&super::markdown::escape_underscores(&text)),
}),
});
}
}
#[derive(Clone, Debug)]
pub(super) struct Annotation {
id: roxmltree::NodeId,
pub(super) anchor: Option<roxmltree::NodeId>,
name: Option<String>,
parent: Option<String>,
pub(super) text: String,
}
pub(super) fn annotations(body: XmlNode) -> Vec<Annotation> {
body.descendants()
.filter(|n| n.has_tag_name("annotation"))
.filter(|a| !a.ancestors().any(|x| x.has_tag_name("tracked-changes")))
.filter_map(|a| {
let child_text = |tag: &str| {
a.children()
.find(|c| c.has_tag_name(tag))
.map(|c| {
c.descendants()
.filter(|n| n.is_text())
.filter_map(|n| n.text())
.collect::<String>()
.trim()
.to_string()
})
.filter(|t| !t.is_empty())
};
let mut head: Vec<String> = Vec::new();
if let Some(creator) = child_text("creator") {
match child_text("creator-initials").or_else(|| child_text("sender-initials")) {
Some(initials) => head.push(format!("author: {creator} ({initials})")),
None => head.push(format!("author: {creator}")),
}
}
if let Some(date) = child_text("date") {
head.push(format!("time: {date}"));
}
let body: String = a
.children()
.filter(|c| c.has_tag_name("p"))
.map(|p| para_plain_text(p).trim().to_string())
.filter(|t| !t.is_empty())
.collect::<Vec<_>>()
.join("\n");
let text = match (head.is_empty(), body.is_empty()) {
(true, true) => return None,
(false, true) => format!("[{}]", head.join(", ")),
(true, false) => body,
(false, false) => format!("[{}]: {body}", head.join(", ")),
};
Some(Annotation {
id: a.id(),
anchor: a
.ancestors()
.skip(1)
.find(|x| x.has_tag_name("p") || x.has_tag_name("h"))
.map(|x| x.id()),
name: attr(a, "name").map(str::to_string),
parent: attr(a, "parent-name").map(str::to_string),
text,
})
})
.collect()
}
pub(super) fn comment_threads(annotations: &[Annotation]) -> Vec<Vec<usize>> {
let by_name: HashMap<&str, usize> = annotations
.iter()
.enumerate()
.filter_map(|(i, a)| a.name.as_deref().map(|n| (n, i)))
.collect();
let mut replies: Vec<Vec<usize>> = vec![Vec::new(); annotations.len()];
let mut is_reply = vec![false; annotations.len()];
for (i, a) in annotations.iter().enumerate() {
if let Some(parent) = a.parent.as_deref().and_then(|p| by_name.get(p)) {
if *parent != i {
replies[*parent].push(i);
is_reply[i] = true;
}
}
}
let mut seen = vec![false; annotations.len()];
let mut threads = Vec::new();
for start in (0..annotations.len())
.filter(|&i| !is_reply[i])
.chain(0..annotations.len())
{
if seen[start] {
continue;
}
let mut thread = Vec::new();
let mut stack = vec![start];
while let Some(i) = stack.pop() {
if std::mem::replace(&mut seen[i], true) {
continue;
}
thread.push(i);
stack.extend(replies[i].iter().rev());
}
threads.push(thread);
}
threads
}
fn comment_slots(annotations: &[Annotation]) -> HashMap<roxmltree::NodeId, usize> {
comment_threads(annotations)
.into_iter()
.flatten()
.enumerate()
.map(|(slot, i)| (annotations[i].id, slot))
.collect()
}
fn push_comment_sections(annotations: &[Annotation], doc: &mut DoclingDocument) {
for (t, thread) in comment_threads(annotations).iter().enumerate() {
for &i in thread {
doc.nodes.push(Node::CommentSection {
name: format!("comment-{}", t + 1),
text: annotations[i].text.clone(),
refs_note_text: false,
grouped: true,
});
}
}
}
fn comment_slots_in<'a, 'i: 'a>(
els: impl Iterator<Item = XmlNode<'a, 'i>>,
styles: &Styles,
) -> Vec<usize> {
let mut slots = Vec::new();
for el in els {
for a in el
.descendants()
.filter(|n| n.has_tag_name("annotation"))
.filter(|a| {
a.ancestors()
.skip(1)
.find(|x| x.has_tag_name("p") || x.has_tag_name("h"))
.is_some_and(|x| x.id() == el.id())
})
{
if let Some(&slot) = styles.comment_slots.get(&a.id()) {
slots.push(slot);
}
}
}
slots
}
fn wrap_last_commented(doc: &mut DoclingDocument, slots: Vec<usize>) {
if slots.is_empty() {
return;
}
if let Some(last) = doc.nodes.last_mut() {
let inner = std::mem::replace(last, Node::PageBreak);
*last = Node::Commented {
comments: slots,
inner: Box::new(inner),
};
}
}
fn runs_to_text(mut runs: Vec<Run>) -> String {
let mut merged: Vec<Run> = Vec::new();
for mut r in runs.drain(..) {
if let Some(last) = merged.last_mut() {
if last.fmt == r.fmt && last.href == r.href {
last.text.push_str(&r.text);
continue;
}
if last.href != r.href {
last.text = last.text.trim_end().to_string();
r.text = r.text.trim_start().to_string();
}
}
merged.push(r);
}
merged
.iter()
.map(|r| serialize_run(&r.text, r.fmt, r.href.as_deref()))
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
.join(" ")
.trim()
.to_string()
}
fn runs_to_inline(runs: Vec<Run>) -> Vec<InlineRun> {
let mut merged: Vec<Run> = Vec::new();
for r in runs {
if let Some(last) = merged.last_mut() {
if last.fmt == r.fmt && last.href == r.href {
last.text.push_str(&r.text);
continue;
}
}
merged.push(r);
}
if let Some(first) = merged.first_mut() {
first.text = first.text.trim_start().to_string();
}
if let Some(last) = merged.last_mut() {
last.text = last.text.trim_end().to_string();
}
merged
.into_iter()
.filter(|r| !r.text.is_empty())
.map(|r| r.fmt.to_inline_run(&r.text))
.collect()
}
fn serialize_run(text: &str, fmt: Fmt, href: Option<&str>) -> String {
if text.is_empty() {
return String::new();
}
let mut s = escape_text(text);
if fmt.bold {
s = format!("**{s}**");
}
if fmt.italic {
s = format!("*{s}*");
}
if fmt.strike {
s = format!("~~{s}~~");
}
if let Some(href) = href {
s = format!("[{s}]({href})");
}
s
}
pub(super) fn is_svg(el: XmlNode) -> bool {
el.tag_name()
.namespace()
.is_some_and(|ns| ns.contains("svg"))
}
pub(super) fn frame_alt(img: XmlNode) -> Option<String> {
let frame = img.parent().filter(|p| p.has_tag_name("frame"))?;
["title", "desc"].into_iter().find_map(|tag| {
frame
.children()
.find(|c| c.has_tag_name(tag) && is_svg(*c))
.map(|c| {
c.descendants()
.filter(|n| n.is_text())
.filter_map(|n| n.text())
.collect::<String>()
.trim()
.to_string()
})
.filter(|t| !t.is_empty())
})
}
pub(super) fn frame_display(img: XmlNode) -> Option<[f32; 2]> {
let frame = img.parent().filter(|p| p.has_tag_name("frame"))?;
let w = odf_length_in(attr(frame, "width")?)?;
let h = odf_length_in(attr(frame, "height")?)?;
(w > 0.0 && h > 0.0).then_some([w, h])
}
pub(super) fn odf_length_in(s: &str) -> Option<f32> {
let s = s.trim();
let split = s
.find(|c: char| c.is_ascii_alphabetic() || c == '%')
.unwrap_or(s.len());
let value: f32 = s[..split].trim().parse().ok()?;
let per_inch = match s[split..].trim() {
"in" | "" => 1.0,
"cm" => 2.54,
"mm" => 25.4,
"pt" => 72.0,
"pc" => 6.0,
"px" => 96.0,
_ => return None,
};
Some(value / per_inch)
}
pub(super) fn with_frame_display(
image: Option<PictureImage>,
img: XmlNode,
) -> Option<PictureImage> {
image.map(|mut i| {
i.display_size = frame_display(img);
i
})
}
fn odf_picture(styles: &Styles, img: XmlNode) -> Option<Node> {
let href = attr(img, "href").unwrap_or("");
if !image_can_be_bitmap(img, href) {
return None;
}
let picture = |image: Option<PictureImage>| {
Some(Node::Picture {
caption: None,
caption_href: None,
image: with_frame_display(image, img),
classification: None,
description: frame_alt(img).map(PictureDescription::alt_text),
caption_parent: Default::default(),
caption_location: None,
})
};
if href.is_empty() {
return picture(None); }
let name = href.trim_start_matches("./").trim_start_matches('#');
match &styles.parts {
Some(parts) if parts.contains(name) => return picture(styles.images.get(name).cloned()),
_ => {}
}
if href.starts_with("http://") || href.starts_with("https://") {
if !styles.policy.sources.remote() {
return None;
}
#[cfg(feature = "fetch-images")]
{
return crate::backend::images::fetch_remote_with(href, &styles.policy)
.and_then(|img| picture(Some(img)));
}
#[cfg(not(feature = "fetch-images"))]
{
return None;
}
}
None
}
fn walk_text(text: XmlNode, styles: &Styles, doc: &mut DoclingDocument) {
walk_blocks(text.children().filter(XmlNode::is_element), styles, doc);
}
fn walk_blocks<'a, 'i: 'a>(
els: impl Iterator<Item = XmlNode<'a, 'i>>,
styles: &Styles,
doc: &mut DoclingDocument,
) {
let mut prev_state: Option<ListCont> = None;
let mut style_run: Option<StyleListRun> = None;
for el in els {
if is_list_tag(el.tag_name().name()) {
style_run = None;
prev_state = add_odf_list(el, styles, doc, 0, 1, false, prev_state.take());
} else if let Some((style, level)) = el
.has_tag_name("p")
.then(|| paragraph_list_style(styles, attr(el, "style-name")))
.flatten()
{
prev_state = None;
add_style_list_paragraph(el, style, level, styles, doc, &mut style_run);
} else {
prev_state = None;
style_run = None;
handle_block(el, styles, doc, 0, &mut Vec::new());
}
}
}
struct StyleListRun {
style: String,
enumerated: bool,
counter: i64,
prefix: String,
suffix: String,
}
fn add_style_list_paragraph(
el: XmlNode,
style: String,
level: i64,
styles: &Styles,
doc: &mut DoclingDocument,
run: &mut Option<StyleListRun>,
) {
emit_paragraph_images(el, styles, doc);
let mut runs = Vec::new();
collect_runs(el, styles, Fmt::default(), &mut runs);
let text = clean_lines(&runs_to_text(runs));
if text.is_empty() {
return;
}
let fresh = run.as_ref().is_none_or(|r| r.style != style);
if fresh {
let lv = list_level(styles, Some(&style), level);
*run = Some(StyleListRun {
style,
enumerated: lv.is_some_and(|l| l.numbered),
counter: lv.map_or(0, |l| l.start - 1),
prefix: lv.map(|l| l.prefix.clone()).unwrap_or_default(),
suffix: lv.map(|l| l.suffix.clone()).unwrap_or_default(),
});
}
let Some(r) = run.as_mut() else {
return;
};
r.counter += 1;
let depth = (level - 1).clamp(0, u8::MAX as i64) as u8;
push_odf_list_item(
doc,
text,
depth,
r.enumerated,
r.counter,
&r.prefix,
&r.suffix,
fresh,
);
wrap_last_commented(doc, comment_slots_in(std::iter::once(el), styles));
walk_text_boxes(el, styles, doc);
}
fn handle_block(
el: XmlNode,
styles: &Styles,
doc: &mut DoclingDocument,
list_level: u8,
counters: &mut Vec<u64>,
) {
let _ = counters;
match el.tag_name().name() {
"h" => {
emit_paragraph_images(el, styles, doc);
let level = attr(el, "outline-level")
.or_else(|| attr(el, "level"))
.and_then(|v| v.parse::<i64>().ok())
.unwrap_or(1)
.max(1);
let mut runs = Vec::new();
collect_runs(el, styles, Fmt::default(), &mut runs);
let text = runs_to_text(runs);
if !text.is_empty() {
doc.push(Node::Heading {
level: (level + 1) as u8,
text,
});
wrap_last_commented(doc, comment_slots_in(std::iter::once(el), styles));
}
walk_text_boxes(el, styles, doc);
}
"p" => {
emit_paragraph_images(el, styles, doc);
let names = paragraph_style_names(styles, attr(el, "style-name"));
let mut runs = Vec::new();
collect_runs(el, styles, Fmt::default(), &mut runs);
let text = runs_to_text(runs.clone());
if text.is_empty() {
walk_text_boxes(el, styles, doc);
return;
}
if names.iter().any(|n| n == "Title") {
doc.push(Node::Heading { level: 1, text });
} else if names.iter().any(|n| n == "Subtitle") {
doc.push(Node::Heading { level: 2, text });
} else {
doc.push(inline_paragraph_node(text, runs_to_inline(runs), false));
}
wrap_last_commented(doc, comment_slots_in(std::iter::once(el), styles));
walk_text_boxes(el, styles, doc);
}
n if is_list_tag(n) => {
add_odf_list(el, styles, doc, list_level, 1, false, None);
}
"table" => {
if let Some(table) = parse_table(el, styles) {
doc.push(Node::Table(table));
}
}
n if is_transparent_container(n) => {
let start = doc.nodes.len();
walk_blocks(el.children().filter(XmlNode::is_element), styles, doc);
if section_is_hidden(el) {
hide_nodes_from(doc, start);
}
}
"frame" => {
emit_frame_graphic(el, styles, doc);
walk_text_boxes(el, styles, doc);
}
_ => {}
}
}
pub(super) fn in_text_box(n: XmlNode, root: XmlNode) -> bool {
n.ancestors()
.skip(1)
.take_while(|a| *a != root)
.any(|a| a.has_tag_name("text-box"))
}
pub(super) fn text_boxes<'a, 'i>(el: XmlNode<'a, 'i>) -> Vec<XmlNode<'a, 'i>> {
el.descendants()
.filter(|n| n.has_tag_name("text-box") && !in_text_box(*n, el))
.collect()
}
fn walk_text_boxes(el: XmlNode, styles: &Styles, doc: &mut DoclingDocument) {
for tb in text_boxes(el) {
walk_blocks(tb.children().filter(XmlNode::is_element), styles, doc);
}
}
fn emit_paragraph_images(el: XmlNode, styles: &Styles, doc: &mut DoclingDocument) {
for frame in el
.descendants()
.filter(|n| n.has_tag_name("frame") && !in_text_box(*n, el))
{
emit_frame_graphic(frame, styles, doc);
}
for img in el
.descendants()
.filter(|n| n.has_tag_name("image") && !n.ancestors().any(|a| a.has_tag_name("frame")))
{
if let Some(node) = odf_picture(styles, img) {
doc.push(node);
}
}
}
fn emit_frame_graphic(frame: XmlNode, styles: &Styles, doc: &mut DoclingDocument) -> bool {
if let Some(obj) = frame.children().find(|c| c.has_tag_name("object")) {
let name = attr(obj, "href").unwrap_or("").trim_start_matches("./");
let info = styles
.charts
.get(name)
.cloned()
.or_else(|| inline_chart(obj, styles));
if let Some(info) = info {
doc.push(Node::Chart {
kind: info.kind,
table: info.table,
caption: None,
location: None,
});
return true;
}
}
let picture = frame
.children()
.filter(|c| {
c.has_tag_name("image")
&& !attr(*c, "href")
.unwrap_or("")
.trim_start_matches("./")
.starts_with("ObjectReplacements/")
})
.find_map(|img| odf_picture(styles, img));
if let Some(node) = picture {
doc.push(node);
return true;
}
false
}
pub(super) fn inline_chart(obj: XmlNode, styles: &Styles) -> Option<ChartInfo> {
let chart = obj.descendants().find(|n| n.has_tag_name("chart"))?;
let kind = attr(chart, "class")
.and_then(chart_kind)
.or_else(|| {
obj.descendants()
.filter(|n| n.has_tag_name("series"))
.find_map(|n| attr(n, "class").and_then(chart_kind))
})
.unwrap_or("other_chart")
.to_string();
let table_node = obj.descendants().find(|n| n.has_tag_name("table"))?;
parse_table(table_node, styles).map(|table| ChartInfo { kind, table })
}
#[derive(Clone)]
struct ListCont {
enumerated: bool,
counter: i64,
has_last: bool,
prefix: String,
suffix: String,
}
pub(super) fn list_items<'a, 'i>(list: XmlNode<'a, 'i>) -> impl Iterator<Item = XmlNode<'a, 'i>> {
list.children()
.filter(|c| c.has_tag_name("list-item") || c.has_tag_name("list-header"))
}
pub(super) fn odf_item_content<'a, 'i>(
item: XmlNode<'a, 'i>,
styles: &Styles,
) -> (String, Vec<XmlNode<'a, 'i>>) {
let mut parts: Vec<String> = Vec::new();
let mut nested: Vec<XmlNode> = Vec::new();
for child in item.children().filter(XmlNode::is_element) {
match child.tag_name().name() {
n if is_list_tag(n) => nested.push(child),
"p" => {
let mut runs = Vec::new();
collect_runs(child, styles, Fmt::default(), &mut runs);
let text = clean_lines(&runs_to_text(runs));
if !text.is_empty() {
parts.push(text);
}
}
_ => {}
}
}
(parts.join(" "), nested)
}
pub(super) fn item_has_content(item: XmlNode, styles: &Styles) -> bool {
!odf_item_content(item, styles).0.is_empty()
|| item
.children()
.filter(|c| c.has_tag_name("p"))
.flat_map(|p| p.descendants())
.any(|n| n.has_tag_name("image") || n.has_tag_name("object") || n.has_tag_name("note"))
}
pub(super) fn item_headings<'a, 'i>(
item: XmlNode<'a, 'i>,
) -> impl Iterator<Item = XmlNode<'a, 'i>> {
item.children().filter(|c| c.has_tag_name("h"))
}
pub(super) fn clean_lines(text: &str) -> String {
text.lines()
.map(str::trim)
.filter(|l| !l.is_empty())
.collect::<Vec<_>>()
.join(" ")
}
pub(super) fn list_has_renderable(list: XmlNode, styles: &Styles) -> bool {
list_items(list).any(|item| {
let (_, nested) = odf_item_content(item, styles);
item_has_content(item, styles)
|| item_headings(item).next().is_some()
|| nested.iter().any(|n| list_has_renderable(*n, styles))
})
}
pub(super) fn list_has_direct_text(list: XmlNode, styles: &Styles) -> bool {
list_items(list).any(|item| item_has_content(item, styles))
}
pub(super) fn list_starts_with_empty_nested(list: XmlNode, styles: &Styles) -> bool {
if let Some(item) = list_items(list).next() {
let (_, nested) = odf_item_content(item, styles);
return !item_has_content(item, styles)
&& nested.iter().any(|n| list_has_renderable(*n, styles));
}
false
}
pub(super) fn level_is_enumerated(
styles: &Styles,
list: XmlNode,
level: i64,
fallback: bool,
) -> bool {
list_level(styles, attr(list, "style-name"), level)
.map(|lv| lv.numbered)
.unwrap_or_else(|| match list.tag_name().name() {
"ordered-list" => true,
"unordered-list" => false,
_ => fallback,
})
}
pub(super) fn level_start(styles: &Styles, list: XmlNode, level: i64) -> i64 {
list_level(styles, attr(list, "style-name"), level)
.map(|lv| lv.start)
.unwrap_or(1)
}
pub(super) fn list_level<'a>(
styles: &'a Styles,
name: Option<&str>,
level: i64,
) -> Option<&'a OdfLevel> {
styles.lists.get(name?)?.get(&level)
}
pub(super) fn level_affixes(styles: &Styles, list: XmlNode, level: i64) -> (String, String) {
list_level(styles, attr(list, "style-name"), level)
.map(|lv| (lv.prefix.clone(), lv.suffix.clone()))
.unwrap_or_default()
}
fn add_odf_list(
list: XmlNode,
styles: &Styles,
doc: &mut DoclingDocument,
depth: u8,
style_level: i64,
enumerated_fallback: bool,
continued: Option<ListCont>,
) -> Option<ListCont> {
if !list_has_renderable(list, styles) {
return None;
}
let style_enum = level_is_enumerated(styles, list, style_level, enumerated_fallback);
let should_continue = continued.as_ref().map(|c| c.has_last).unwrap_or(false)
&& list_starts_with_empty_nested(list, styles);
if !should_continue && !list_has_direct_text(list, styles) {
for item in list_items(list) {
let (_text, nested) = odf_item_content(item, styles);
emit_item_headings(item, styles, doc);
for n in nested {
add_odf_list(n, styles, doc, depth, style_level + 1, style_enum, None);
}
}
return None;
}
let (mut counter, current_enum) = match (should_continue, &continued) {
(true, Some(c)) => (c.counter, c.enumerated),
_ => (level_start(styles, list, style_level) - 1, style_enum),
};
let (prefix, suffix) = match (should_continue, &continued) {
(true, Some(c)) => (c.prefix.clone(), c.suffix.clone()),
_ => level_affixes(styles, list, style_level),
};
let mut has_last = should_continue;
let mut first = !should_continue;
for item in list_items(list) {
let (text, nested) = odf_item_content(item, styles);
let has_content = item_has_content(item, styles);
let nested: Vec<XmlNode> = nested
.into_iter()
.filter(|n| list_has_renderable(*n, styles))
.collect();
emit_item_headings(item, styles, doc);
if !has_content && nested.is_empty() {
continue;
}
if !has_content {
for n in &nested {
add_odf_list(
*n,
styles,
doc,
depth + 1,
style_level + 1,
style_enum,
None,
);
}
continue;
}
counter += 1;
push_odf_list_item(
doc,
text,
depth,
current_enum,
counter,
&prefix,
&suffix,
std::mem::take(&mut first),
);
wrap_last_commented(
doc,
comment_slots_in(item.children().filter(|c| c.has_tag_name("p")), styles),
);
for p in item.children().filter(|c| c.has_tag_name("p")) {
walk_text_boxes(p, styles, doc);
}
has_last = true;
for n in &nested {
add_odf_list(
*n,
styles,
doc,
depth + 1,
style_level + 1,
style_enum,
None,
);
}
}
Some(ListCont {
enumerated: current_enum,
counter,
has_last,
prefix,
suffix,
})
}
#[allow(clippy::too_many_arguments)]
fn push_odf_list_item(
doc: &mut DoclingDocument,
text: String,
depth: u8,
enumerated: bool,
counter: i64,
prefix: &str,
suffix: &str,
first: bool,
) {
let (ordered, number, marker) = if enumerated {
let n = counter.max(0) as u64;
(true, n, Some(format!("{prefix}{n}{suffix}")))
} else {
(false, 0, None)
};
let item_marker = format!(
"{n}{}",
if suffix.is_empty() { "." } else { suffix },
n = counter.max(0)
);
let (ordered, marker, text, dclx) = match marker {
Some(m) if !is_plain_number_marker(&item_marker) => {
let dclx = Some(ListItemDclx {
ordered: true,
marker: Some(m),
text: text.clone(),
runs: Vec::new(),
});
(false, None, format!("{item_marker} {text}"), dclx)
}
marker => (ordered, marker, text, None),
};
doc.push(Node::ListItem {
ordered,
number,
first_in_list: first,
text,
level: depth,
marker,
location: None,
dclx,
href: None,
layer: None,
});
}
fn emit_item_headings(item: XmlNode, styles: &Styles, doc: &mut DoclingDocument) {
for h in item_headings(item) {
handle_block(h, styles, doc, 0, &mut Vec::new());
}
}
fn is_plain_number_marker(marker: &str) -> bool {
marker
.strip_suffix('.')
.is_some_and(|d| !d.is_empty() && d.chars().all(|c| c.is_ascii_digit()))
}
fn parse_table(table: XmlNode, styles: &Styles) -> Option<Table> {
let tr_rows: Vec<XmlNode> = table
.descendants()
.filter(|n| {
n.has_tag_name("table-row")
&& n.ancestors().find(|a| a.has_tag_name("table")) == Some(table)
})
.collect();
if tr_rows.is_empty() {
return None;
}
let cells_of = |tr: &XmlNode| -> Vec<(bool, usize)> {
tr.children()
.filter(|c| c.has_tag_name("table-cell") || c.has_tag_name("covered-table-cell"))
.map(|c| {
(
c.has_tag_name("covered-table-cell"),
repeat(c, "number-columns-repeated"),
)
})
.collect()
};
let num_cols = tr_rows
.iter()
.map(|tr| cells_of(tr).iter().map(|(_, r)| r).sum::<usize>())
.max()
.unwrap_or(0);
if num_cols == 0 {
return None;
}
let nrows = tr_rows.len();
let mut grid = vec![vec![String::new(); num_cols]; nrows];
let mut col_cont = vec![vec![false; num_cols]; nrows];
let mut row_cont = vec![vec![false; num_cols]; nrows];
let mut blocks: Vec<Vec<Vec<Node>>> = vec![vec![Vec::new(); num_cols]; nrows];
let mut any_rich = false;
for (ri, tr) in tr_rows.iter().enumerate() {
let mut ci = 0usize;
for tc in tr
.children()
.filter(|c| c.has_tag_name("table-cell") || c.has_tag_name("covered-table-cell"))
{
let reps = repeat(tc, "number-columns-repeated");
if tc.has_tag_name("covered-table-cell") {
ci = (ci + reps).min(num_cols);
continue;
}
let text = cell_text(tc, styles);
let cell_nodes = cell_blocks_of(tc, styles);
any_rich |= !cell_nodes.is_empty();
let cspan = repeat(tc, "number-columns-spanned");
let rspan = repeat(tc, "number-rows-spanned");
for _ in 0..reps {
if ci >= num_cols {
break;
}
grid[ri][ci] = text.clone();
blocks[ri][ci] = cell_nodes.clone();
let r_end = (ri + rspan).min(nrows);
let c_end = (ci + cspan).min(num_cols);
for rr in ri..r_end {
for cc in ci..c_end {
if rr == ri && cc == ci {
continue;
}
grid[rr][cc] = text.clone();
col_cont[rr][cc] |= cc > ci;
row_cont[rr][cc] |= rr > ri;
}
}
ci += 1;
}
}
}
let (min_r, max_r, min_c, max_c) = data_bounds(&grid).unwrap_or((0, 0, 0, 0));
let slice = |g: Vec<Vec<bool>>| -> Vec<Vec<bool>> {
g[min_r..=max_r]
.iter()
.map(|row| row[min_c..=max_c].to_vec())
.collect()
};
let rows: Vec<Vec<String>> = grid[min_r..=max_r]
.iter()
.map(|row| row[min_c..=max_c].to_vec())
.collect();
let header_row = (0..rows.len()).map(|r| r == 0).collect();
let cell_blocks = any_rich.then(|| {
blocks[min_r..=max_r]
.iter()
.map(|row| row[min_c..=max_c].to_vec())
.collect()
});
Some(Table {
rows,
location: None,
structure: Some(TableStructure {
header_row,
col_continuation: slice(col_cont),
row_continuation: slice(row_cont),
row_header: Vec::new(),
col_header: Vec::new(),
}),
cell_blocks,
caption: None,
cells: None,
caption_parent: Default::default(),
caption_location: None,
})
}
fn data_bounds(grid: &[Vec<String>]) -> Option<(usize, usize, usize, usize)> {
let non_empty = |r: &Vec<String>| r.iter().any(|c| !c.trim().is_empty());
let (min_r, max_r) = (
grid.iter().position(non_empty)?,
grid.iter().rposition(non_empty)?,
);
let ncols = grid[0].len();
let col_used = |c: usize| (min_r..=max_r).any(|r| !grid[r][c].trim().is_empty());
let min_c = (0..ncols).find(|&c| col_used(c))?;
let max_c = (0..ncols).rposition(col_used)?;
Some((min_r, max_r, min_c, max_c))
}
fn cell_text(tc: XmlNode, styles: &Styles) -> String {
if is_rich_cell(tc, styles) {
rich_cell_markdown(tc, styles)
} else {
plain_cell_text(tc)
}
}
fn cell_blocks_of(tc: XmlNode, styles: &Styles) -> Vec<Node> {
if !is_rich_cell(tc, styles) {
return Vec::new();
}
let mut sub = DoclingDocument::new("");
walk_blocks(tc.children().filter(XmlNode::is_element), styles, &mut sub);
sub.nodes
}
pub(super) fn is_rich_cell(tc: XmlNode, styles: &Styles) -> bool {
if cell_has_image(tc) {
return true;
}
let mut non_empty_paragraphs = 0;
for child in tc.children().filter(XmlNode::is_element) {
match child.tag_name().name() {
n if is_list_tag(n) && list_has_renderable(child, styles) => return true,
"h" if !clean_lines(¶_plain_text(child)).is_empty() => return true,
"table" if table_has_content(child) => return true,
"p" => {
if !clean_lines(¶_plain_text(child)).is_empty() {
non_empty_paragraphs += 1;
}
if cell_has_image(child) {
return true;
}
}
_ => {}
}
}
non_empty_paragraphs > 1 || (attr(tc, "value-type").is_none() && non_empty_paragraphs > 0)
}
pub(super) fn cell_has_image(n: XmlNode) -> bool {
n.descendants().any(|d| d.has_tag_name("image"))
}
pub(super) fn table_has_content(table: XmlNode) -> bool {
table
.descendants()
.filter(|n| n.has_tag_name("table-cell"))
.any(|c| !plain_cell_text(c).trim().is_empty() || cell_has_image(c))
}
pub(super) fn plain_cell_text(tc: XmlNode) -> String {
tc.children()
.filter(|c| c.has_tag_name("p") || c.has_tag_name("h"))
.map(para_plain_text)
.filter(|t| !t.is_empty())
.collect::<Vec<_>>()
.join("\n\n")
}
pub(super) fn para_plain_text(el: XmlNode) -> String {
let mut out = String::new();
para_plain_into(el, &mut out);
out
}
fn para_plain_into(el: XmlNode, out: &mut String) {
for child in el.children() {
if child.is_text() {
out.push_str(child.text().unwrap_or(""));
} else if child.is_element() {
match child.tag_name().name() {
"line-break" => out.push('\n'),
"tab" | "tab-stop" => out.push('\t'),
"s" => {
let n: usize = attr(child, "c").and_then(|v| v.parse().ok()).unwrap_or(1);
out.push_str(&" ".repeat(n));
}
"object" | "binary-data" => {}
"note" => {}
"annotation" | "annotation-end" => {}
"text-box" => {}
_ => para_plain_into(child, out),
}
}
}
}
fn rich_cell_markdown(tc: XmlNode, styles: &Styles) -> String {
let mut sub = DoclingDocument::new("");
let mut prev_state: Option<ListCont> = None;
for child in tc.children().filter(XmlNode::is_element) {
match child.tag_name().name() {
n if is_list_tag(n) => {
prev_state = add_odf_list(child, styles, &mut sub, 0, 1, false, prev_state.take());
}
"table" => {
prev_state = None;
if let Some(table) = parse_table(child, styles) {
let text = table
.rows
.iter()
.flatten()
.filter(|c| !c.is_empty())
.cloned()
.collect::<Vec<_>>()
.join(" ");
if !text.is_empty() {
sub.push(Node::TextDump(text));
}
}
}
"p" | "h" | "frame" => {
prev_state = None;
handle_block(child, styles, &mut sub, 0, &mut Vec::new());
}
_ => {}
}
}
sub.export_to_table_cell_markdown().trim().to_string()
}
fn walk_spreadsheet(sheet: XmlNode, styles: &Styles, doc: &mut DoclingDocument) {
for table in sheet.children().filter(|c| c.has_tag_name("table")) {
let start = doc.nodes.len();
add_ods_sheet(table, styles, doc);
if attr(table, "display") == Some("false") {
hide_nodes_from(doc, start);
}
}
}
fn hide_nodes_from(doc: &mut DoclingDocument, start: usize) {
for node in &mut doc.nodes[start..] {
match node {
Node::Furniture { .. } => {}
Node::ListItem { layer, .. } => {
if layer.is_none() {
*layer = Some(docling_core::ContentLayer::Invisible);
}
}
_ => {
let inner = std::mem::replace(node, Node::PageBreak);
*node = Node::Furniture {
layer: docling_core::ContentLayer::Invisible,
inner: Box::new(inner),
};
}
}
}
}
fn add_ods_sheet(table: XmlNode, styles: &Styles, doc: &mut DoclingDocument) {
let mut cells: HashMap<(usize, usize), String> = HashMap::new();
let mut row_idx = 0usize;
for row in table.children().filter(|c| c.has_tag_name("table-row")) {
let rrep = repeat(row, "number-rows-repeated");
let mut row_cells: Vec<(usize, String)> = Vec::new();
let mut col_idx = 0usize;
let mut row_has_content = false;
for cell in row
.children()
.filter(|c| c.has_tag_name("table-cell") || c.has_tag_name("covered-table-cell"))
{
let crep = repeat(cell, "number-columns-repeated");
let covered = cell.has_tag_name("covered-table-cell");
let text = if cell_has_image(cell) {
rich_cell_markdown(cell, styles)
} else {
ods_cell_text(cell)
};
if !text.is_empty() || covered {
row_has_content = true;
for c in 0..crep.min(1024) {
row_cells.push((col_idx + c, text.clone()));
}
}
col_idx += crep;
}
if row_has_content {
for r in 0..rrep.min(1024) {
for (c, text) in &row_cells {
cells.insert((row_idx + r, *c), text.clone());
}
}
}
row_idx += rrep;
}
emit_sheet_regions(&cells, doc);
for img in table
.children()
.filter(|n| n.has_tag_name("shapes"))
.flat_map(|shapes| shapes.descendants())
.filter(|n| n.has_tag_name("image"))
{
if let Some(node) = odf_picture(styles, img) {
doc.push(node);
}
}
}
pub(crate) fn emit_sheet_regions(
cells: &HashMap<(usize, usize), String>,
doc: &mut DoclingDocument,
) {
if cells.is_empty() {
return;
}
let mut order: Vec<(usize, usize)> = cells.keys().copied().collect();
order.sort_unstable();
let mut visited: HashSet<(usize, usize)> = HashSet::new();
for (ri, ci) in order {
{
if visited.contains(&(ri, ci)) {
continue;
}
let mut region: HashSet<(usize, usize)> = HashSet::new();
let mut queue: VecDeque<(usize, usize)> = VecDeque::new();
queue.push_back((ri, ci));
region.insert((ri, ci));
let (mut rmin, mut rmax, mut cmin, mut cmax) = (ri, ri, ci, ci);
while let Some((r, c)) = queue.pop_front() {
rmin = rmin.min(r);
rmax = rmax.max(r);
cmin = cmin.min(c);
cmax = cmax.max(c);
for (dr, dc) in [(0i64, 1i64), (0, -1), (1, 0), (-1, 0)] {
let nr = r as i64 + dr;
let nc = c as i64 + dc;
if nr < 0 || nc < 0 {
continue;
}
let key = (nr as usize, nc as usize);
if !region.contains(&key) && cells.contains_key(&key) {
region.insert(key);
queue.push_back(key);
}
}
}
visited.extend(region.iter().copied());
let rows: Vec<Vec<String>> = (rmin..=rmax)
.map(|r| {
(cmin..=cmax)
.map(|c| cells.get(&(r, c)).cloned().unwrap_or_default())
.collect()
})
.collect();
doc.push(Node::Table(Table {
rows,
location: None,
structure: None,
cell_blocks: None,
cells: None,
caption: None,
caption_parent: Default::default(),
caption_location: None,
}));
}
}
}
pub(super) fn repeat(node: XmlNode, name: &str) -> usize {
attr(node, name)
.and_then(|v| v.parse().ok())
.filter(|&n: &usize| n >= 1)
.unwrap_or(1)
}
pub(super) fn ods_cell_text(cell: XmlNode) -> String {
cell.children()
.filter(|c| c.has_tag_name("p") || c.has_tag_name("h"))
.map(|p| {
p.descendants()
.filter(|n| n.is_text())
.filter_map(|n| n.text())
.collect::<String>()
})
.filter(|s| !s.trim().is_empty())
.collect::<Vec<_>>()
.join("\n")
}
fn walk_presentation(pres: XmlNode, styles: &Styles, doc: &mut DoclingDocument) {
for (idx, page) in pres
.children()
.filter(|c| c.has_tag_name("page"))
.enumerate()
{
let name = attr(page, "name")
.filter(|n| !n.is_empty())
.map(str::to_string)
.unwrap_or_else(|| format!("slide-{}", idx + 1));
let start = doc.nodes.len();
if !slide_has_visible_title(page) {
doc.push(Node::Heading {
level: 1,
text: name,
});
}
walk_slide(page, styles, doc);
if slide_is_hidden(page, styles) {
hide_nodes_from(doc, start);
}
}
}
pub(super) fn image_can_be_bitmap(img: XmlNode, href: &str) -> bool {
if let Some(mime) = attr(img, "mime-type") {
return mime.starts_with("image/") && mime != "image/svg+xml";
}
if href.is_empty() {
if let Some(data) = img
.children()
.find(|c| c.has_tag_name("binary-data"))
.and_then(|d| d.text())
{
let head: String = data
.chars()
.filter(|c| !c.is_whitespace())
.take(16)
.collect();
return docling_core::base64::decode(&head)
.map(|bytes| is_raster_magic(&bytes))
.unwrap_or(false);
}
return false;
}
let name = href.rsplit('/').next().unwrap_or(href);
let suffix = match name.rsplit_once('.') {
Some((_, ext)) => ext.to_ascii_lowercase(),
None => String::new(),
};
matches!(
suffix.as_str(),
"bmp" | "gif" | "jpeg" | "jpg" | "png" | "tif" | "tiff" | "webp"
)
}
fn is_raster_magic(b: &[u8]) -> bool {
b.starts_with(&[0x89, b'P', b'N', b'G'])
|| b.starts_with(&[0xFF, 0xD8, 0xFF])
|| b.starts_with(b"GIF8")
|| b.starts_with(b"BM")
|| b.starts_with(&[b'I', b'I', 0x2A, 0x00])
|| b.starts_with(&[b'M', b'M', 0x00, 0x2A])
|| (b.starts_with(b"RIFF") && b.get(8..12) == Some(b"WEBP"))
}
pub(super) fn element_has_text(el: XmlNode) -> bool {
el.descendants()
.filter(|n| n.is_text())
.any(|n| !n.text().unwrap_or("").trim().is_empty())
}
pub(super) fn is_slide_title_element(el: XmlNode, is_first_text_content: bool) -> bool {
if attr(el, "class") == Some("title") {
return true;
}
is_first_text_content && el.tag_name().name() == "custom-shape"
}
pub(super) fn slide_has_visible_title(page: XmlNode) -> bool {
let mut seen_text = false;
for el in page.children().filter(XmlNode::is_element) {
let tag = el.tag_name().name();
if tag == "notes" || tag == "par" {
continue;
}
if is_slide_title_element(el, !seen_text) {
return true;
}
if element_has_text(el) {
seen_text = true;
}
}
false
}
fn walk_slide(page: XmlNode, styles: &Styles, doc: &mut DoclingDocument) {
let mut seen_text = false;
for el in page.children().filter(XmlNode::is_element) {
let tag = el.tag_name().name();
if tag == "notes" || tag == "par" {
continue;
}
let has_text = element_has_text(el);
let is_title = is_slide_title_element(el, !seen_text);
if has_text {
seen_text = true;
}
if tag == "frame" {
walk_slide_frame(el, styles, doc, is_title);
} else {
walk_textbox_children(
el.children().filter(XmlNode::is_element),
styles,
doc,
is_title,
);
}
}
}
fn walk_slide_frame(frame: XmlNode, styles: &Styles, doc: &mut DoclingDocument, is_title: bool) {
let mut chart_count = 0usize;
if let Some(obj) = frame.children().find(|c| c.has_tag_name("object")) {
let name = attr(obj, "href").unwrap_or("").trim_start_matches("./");
let info = styles
.charts
.get(name)
.cloned()
.or_else(|| inline_chart(obj, styles));
if let Some(info) = info {
doc.push(Node::Chart {
kind: info.kind,
table: info.table,
caption: None,
location: None,
});
chart_count += 1;
}
}
for tbl in frame
.descendants()
.filter(|n| n.has_tag_name("table") && !n.ancestors().any(|a| a.has_tag_name("object")))
{
if let Some(t) = parse_table(tbl, styles) {
doc.push(Node::Table(t));
}
}
for img in frame.descendants().filter(|n| n.has_tag_name("image")) {
let href = attr(img, "href").unwrap_or("");
if chart_count > 0
&& (href.is_empty()
|| href
.trim_start_matches("./")
.starts_with("ObjectReplacements/"))
{
continue;
}
if let Some(node) = odf_picture(styles, img) {
doc.push(node);
}
}
for tb in frame.descendants().filter(|n| n.has_tag_name("text-box")) {
walk_textbox_children(
tb.children().filter(XmlNode::is_element),
styles,
doc,
is_title,
);
}
}
fn walk_textbox_children<'a, 'i: 'a>(
els: impl Iterator<Item = XmlNode<'a, 'i>>,
styles: &Styles,
doc: &mut DoclingDocument,
is_title: bool,
) {
let mut prev_state: Option<ListCont> = None;
for el in els {
match el.tag_name().name() {
"h" => {
prev_state = None;
handle_block(el, styles, doc, 0, &mut Vec::new());
}
"p" => {
prev_state = None;
let mut runs = Vec::new();
collect_runs(el, styles, Fmt::default(), &mut runs);
let text = runs_to_text(runs.clone());
if text.is_empty() {
continue;
}
if is_title {
doc.push(Node::Heading { level: 1, text });
} else {
doc.push(inline_paragraph_node(text, runs_to_inline(runs), false));
}
}
n if is_list_tag(n) => {
prev_state = add_odf_list(el, styles, doc, 0, 1, false, prev_state.take());
}
_ => {}
}
}
}
pub(super) fn attr<'a>(node: XmlNode<'a, '_>, name: &str) -> Option<&'a str> {
node.attributes()
.find(|a| a.name() == name)
.map(|a| a.value())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sheet_regions_scan_cells_not_the_bounding_box() {
let mut cells: HashMap<(usize, usize), String> = HashMap::new();
cells.insert((0, 0), "a".into());
cells.insert((1_000_000, 16_000), "b".into());
let mut doc = DoclingDocument::new("s");
let t = std::time::Instant::now();
emit_sheet_regions(&cells, &mut doc);
assert!(t.elapsed().as_secs() < 5);
let tables = doc
.nodes
.iter()
.filter(|n| matches!(n, Node::Table(_)))
.count();
assert_eq!(tables, 2);
}
#[test]
fn formatting_resolves_through_parent_chain() {
let content = r#"<root xmlns:style="s" xmlns:fo="f">
<style:style style:name="Strong" style:family="text">
<style:text-properties fo:font-weight="bold"/></style:style>
<style:style style:name="P1" style:family="text" style:parent-style-name="Strong"/>
<style:style style:name="P2" style:family="text" style:parent-style-name="P1"/>
<style:style style:name="T1" style:family="text">
<style:text-properties fo:font-style="italic"/></style:style>
</root>"#;
let dom = Document::parse(content).unwrap();
let styles = parse_styles(&dom, None);
let f = resolve_fmt(&styles, Some("P2"), Fmt::default());
assert!(f.bold && !f.italic, "bold inherited through P2→P1→Strong");
let t = resolve_fmt(&styles, Some("T1"), Fmt::default());
assert!(t.italic && !t.bold);
}
#[test]
fn text_after_inline_elements_survives() {
let xml = r#"<root xmlns:text="x">
<text:p>with <text:span>bold</text:span>, and <text:span>italic</text:span> formatting</text:p>
<text:p>Fassung:<text:tab/>zweite Durchsicht</text:p></root>"#;
let dom = Document::parse(xml).unwrap();
let styles = parse_styles(&dom, None);
let mut doc = DoclingDocument::new("t");
walk_text(dom.root_element(), &styles, &mut doc);
let md = doc.export_to_markdown();
assert!(
md.contains("with bold, and italic formatting"),
"tails around spans survive:\n{md}"
);
assert!(
md.contains("zweite Durchsicht"),
"tail after a tab survives:\n{md}"
);
}
fn odt_bytes(body: &str, parts: &[(&str, &[u8])]) -> Vec<u8> {
let content = format!(
r#"<office:document-content xmlns:office="urn:o" xmlns:text="x"
xmlns:draw="d" xmlns:xlink="l">
<office:body><office:text>{body}</office:text></office:body></office:document-content>"#
);
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
let opts = zip::write::SimpleFileOptions::default();
use std::io::Write;
zip.start_file("mimetype", opts).unwrap();
zip.write_all(b"application/vnd.oasis.opendocument.text")
.unwrap();
zip.start_file("content.xml", opts).unwrap();
zip.write_all(content.as_bytes()).unwrap();
for (name, data) in parts {
zip.start_file(*name, opts).unwrap();
zip.write_all(data).unwrap();
}
zip.finish().unwrap().into_inner()
}
fn odt_markdown(body: &str) -> String {
let source = crate::SourceDocument::from_bytes(
"t.odt",
crate::InputFormat::Odt,
odt_bytes(body, &[]),
);
OdfBackend
.convert(&source)
.expect("converts")
.export_to_markdown()
.trim()
.to_string()
}
#[test]
fn odt_hyperlinks_render_as_markdown_links() {
assert_eq!(
odt_markdown(
r#"<text:p>Watch <text:a xlink:href="https://example.com/talk">the example talk</text:a><text:span> for context.</text:span></text:p>"#
),
"Watch [the example talk](https://example.com/talk) for context."
);
assert_eq!(
odt_markdown(
r##"<text:h text:outline-level="2">Read <text:a xlink:href="#details">details</text:a> now</text:h>"##
),
"### Read [details](#details) now"
);
assert_eq!(
odt_markdown(
r#"<text:list><text:list-item><text:p><text:a xlink:href="list-target.odt#part">linked list item</text:a></text:p></text:list-item></text:list>"#
),
"- [linked list item](list-target.odt#part)"
);
}
#[test]
fn odt_hyperlink_targets_are_classified_like_docling() {
for (href, expected) in [
(" ", "linked text"),
("http://[::1", "linked text"),
("http://", "linked text"),
("http:", "linked text"),
("guide.odt#part", "[linked text](guide.odt#part)"),
("#part", "[linked text](#part)"),
("https://example.com", "[linked text](https://example.com/)"),
] {
assert_eq!(
odt_markdown(&format!(
r#"<text:p><text:a xlink:href="{href}">linked text</text:a></text:p>"#
)),
expected,
"href {href:?}"
);
}
assert_eq!(
odt_markdown(r#"<text:p>Before <text:a>malformed link</text:a> after</text:p>"#),
"Before malformed link after"
);
}
#[test]
fn odt_external_image_references_yield_no_picture() {
let frame = |href: &str| {
format!(
r#"<text:p><draw:frame><draw:image xlink:href="{href}"/></draw:frame></text:p>"#
)
};
assert_eq!(odt_markdown(&frame("/tmp/canary.png")), "");
assert_eq!(odt_markdown(&frame("/etc/passwd")), "");
assert_eq!(odt_markdown(&frame("../outside.png")), "");
assert_eq!(odt_markdown(&frame("https://example.com/x.png")), "");
let source = crate::SourceDocument::from_bytes(
"t.odt",
crate::InputFormat::Odt,
odt_bytes(&frame("Pictures/1.png"), &[("Pictures/1.png", b"\x89PNG")]),
);
let md = OdfBackend.convert(&source).unwrap().export_to_markdown();
assert_eq!(md.trim(), "<!-- image -->");
}
#[test]
fn dangling_embedded_object_is_skipped() {
let content = r#"<office:document-content xmlns:office="urn:o" xmlns:text="x"
xmlns:draw="d" xmlns:xlink="l">
<office:body><office:text>
<text:p>Before the dangling object.</text:p>
<text:p><draw:frame draw:name="dangling">
<draw:object xlink:href="./Object 1"/></draw:frame></text:p>
<text:p>After the dangling object.</text:p>
</office:text></office:body></office:document-content>"#;
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
let opts = zip::write::SimpleFileOptions::default();
use std::io::Write;
zip.start_file("mimetype", opts).unwrap();
zip.write_all(b"application/vnd.oasis.opendocument.text")
.unwrap();
zip.start_file("content.xml", opts).unwrap();
zip.write_all(content.as_bytes()).unwrap();
let bytes = zip.finish().unwrap().into_inner();
let source =
crate::SourceDocument::from_bytes("dangling.odt", crate::InputFormat::Odt, bytes);
let doc = OdfBackend
.convert(&source)
.expect("conversion must not abort");
let md = doc.export_to_markdown();
assert!(md.contains("Before the dangling object."), "{md}");
assert!(md.contains("After the dangling object."), "{md}");
assert!(!md.contains("<!-- image -->"), "no phantom picture:\n{md}");
}
#[test]
fn section_content_is_walked() {
let xml = r#"<root xmlns:text="x" xmlns:table="t">
<text:p>Outside.</text:p>
<text:section text:name="S1">
<text:h text:outline-level="1">Inside heading</text:h>
<text:p>Inside paragraph.</text:p>
<text:section text:name="Nested">
<text:p>Nested paragraph.</text:p>
</text:section>
</text:section></root>"#;
let dom = Document::parse(xml).unwrap();
let styles = parse_styles(&dom, None);
let mut doc = DoclingDocument::new("s");
walk_text(dom.root_element(), &styles, &mut doc);
let md = doc.export_to_markdown();
assert!(md.contains("Outside."), "pre-section text:\n{md}");
assert!(md.contains("## Inside heading"), "section heading:\n{md}");
assert!(md.contains("Inside paragraph."), "section paragraph:\n{md}");
assert!(
md.contains("Nested paragraph."),
"nested section content:\n{md}"
);
}
#[test]
fn index_body_content_is_walked() {
let xml = r#"<root xmlns:text="x" xmlns:table="t">
<text:p>Intro.</text:p>
<text:table-of-content text:name="TOC">
<text:table-of-content-source text:outline-level="10">
<text:index-title-template>Template title</text:index-title-template>
</text:table-of-content-source>
<text:index-body>
<text:index-title text:name="TOC_Head"><text:p>Contents</text:p></text:index-title>
<text:p>Chapter One<text:tab/>1</text:p>
<text:h text:outline-level="1">Chapter One</text:h>
<table:table><table:table-row>
<table:table-cell><text:p>Cell</text:p></table:table-cell>
</table:table-row></table:table>
</text:index-body>
</text:table-of-content>
<text:alphabetical-index text:name="Idx" text:display="none">
<text:index-body><text:p>Hidden entry</text:p></text:index-body>
</text:alphabetical-index>
<text:p>Outro.</text:p></root>"#;
let dom = Document::parse(xml).unwrap();
let styles = parse_styles(&dom, None);
let mut doc = DoclingDocument::new("s");
walk_text(dom.root_element(), &styles, &mut doc);
assert_eq!(
doc.export_to_markdown(),
"Intro.\n\nContents\n\nChapter One\t1\n\n## Chapter One\n\n| Cell |\n|--------|\n\nOutro.\n"
);
assert!(doc.nodes.iter().any(|n| matches!(
n,
Node::Furniture {
layer: docling_core::ContentLayer::Invisible,
..
}
)));
for container in [
"table-of-content",
"user-index",
"bibliography",
"index-body",
] {
assert!(is_transparent_container(container));
}
assert!(!is_transparent_container("table-of-content-source"));
}
#[test]
fn style_list_name_paragraphs_are_list_items() {
let xml = r#"<root xmlns:text="x" xmlns:style="s">
<office:styles xmlns:office="o">
<text:list-style style:name="LB"><text:list-level-style-bullet text:level="1" text:bullet-char="•"/></text:list-style>
<text:list-style style:name="LN"><text:list-level-style-number text:level="1" style:num-format="1" style:num-suffix=")" text:start-value="3"/></text:list-style>
<style:style style:name="Bul" style:family="paragraph" style:list-style-name="LB"/>
<style:style style:name="Inh" style:family="paragraph" style:parent-style-name="Bul"/>
<style:style style:name="P1" style:family="paragraph" style:parent-style-name="Inh"/>
<style:style style:name="Num" style:family="paragraph" style:list-style-name="LN"/>
<style:style style:name="Off" style:family="paragraph" style:parent-style-name="Num" style:list-style-name=""/>
</office:styles>
<text:p>Intro:</text:p>
<text:p text:style-name="Bul">one</text:p>
<text:p text:style-name="Inh">two</text:p>
<text:p text:style-name="P1">three</text:p>
<text:p text:style-name="Num">four</text:p>
<text:p text:style-name="Num">five</text:p>
<text:p text:style-name="Off">plain again</text:p></root>"#;
let dom = Document::parse(xml).unwrap();
let styles = parse_styles(&dom, None);
assert_eq!(
paragraph_list_style(&styles, Some("P1")),
Some(("LB".to_string(), 1))
);
assert_eq!(paragraph_list_style(&styles, Some("Off")), None);
assert_eq!(paragraph_list_style(&styles, Some("nope")), None);
let mut doc = DoclingDocument::new("s");
walk_text(dom.root_element(), &styles, &mut doc);
assert_eq!(
doc.export_to_markdown(),
"Intro:\n\n- one\n- two\n- three\n\n- 3) four\n- 4) five\n\nplain again\n"
);
}
#[test]
fn headings_inside_list_items_are_headings() {
let xml = r#"<root xmlns:text="x">
<text:list text:style-name="Outline">
<text:list-item>
<text:h text:outline-level="1">Context</text:h>
<text:list><text:list-item><text:h text:outline-level="2">Background</text:h></text:list-item></text:list>
</text:list-item>
</text:list>
<text:p>Body one.</text:p>
<text:list text:style-name="Outline" text:continue-numbering="true">
<text:list-item><text:list><text:list-item><text:h text:outline-level="2">Target</text:h></text:list-item></text:list></text:list-item>
</text:list>
<text:list>
<text:list-item><text:h text:outline-level="3">Inside bullets</text:h></text:list-item>
<text:list-item><text:p>bullet</text:p></text:list-item>
</text:list></root>"#;
let dom = Document::parse(xml).unwrap();
let styles = parse_styles(&dom, None);
let mut doc = DoclingDocument::new("s");
walk_text(dom.root_element(), &styles, &mut doc);
assert_eq!(
doc.export_to_markdown(),
"## Context\n\n### Background\n\nBody one.\n\n### Target\n\n#### Inside bullets\n\n- bullet\n"
);
}
#[test]
fn annotations_are_notes_not_body_text() {
let xml = r#"<root xmlns:text="x" xmlns:office="o" xmlns:dc="d" xmlns:loext="l">
<text:h text:outline-level="1">Budget<office:annotation office:name="c1"><dc:creator>Alpha</dc:creator><dc:date>2026-01-15T10:00:00</dc:date><loext:sender-initials>RA</loext:sender-initials><text:p>Rename</text:p></office:annotation></text:h>
<text:p>Total is <office:annotation office:name="c2"><dc:creator>Alpha</dc:creator><text:p>Verify</text:p><text:p>twice</text:p></office:annotation>500 EUR.<office:annotation-end office:name="c2"/></text:p>
<text:p>Reply first: <office:annotation loext:parent-name="c1"><dc:creator>Bravo</dc:creator><dc:date>2026-01-16T09:00:00</dc:date><text:p>Agreed</text:p></office:annotation>text.</text:p>
<text:list><text:list-item><text:p>Item<office:annotation><text:p>Bare</text:p></office:annotation></text:p></text:list-item></text:list></root>"#;
let dom = Document::parse(xml).unwrap();
let comments = annotations(dom.root_element());
assert_eq!(
comments.iter().map(|c| c.text.as_str()).collect::<Vec<_>>(),
[
"[author: Alpha (RA), time: 2026-01-15T10:00:00]: Rename",
"[author: Alpha]: Verify\ntwice",
"[author: Bravo, time: 2026-01-16T09:00:00]: Agreed",
"Bare",
]
);
assert_eq!(comment_threads(&comments), [vec![0, 2], vec![1], vec![3]]);
let mut styles = parse_styles(&dom, None);
styles.comment_slots = comment_slots(&comments);
let mut doc = DoclingDocument::new("s");
walk_text(dom.root_element(), &styles, &mut doc);
push_comment_sections(&comments, &mut doc);
assert_eq!(
doc.export_to_markdown(),
"## Budget\n\nTotal is 500 EUR.\n\nReply first: text.\n\n- Item\n"
);
let wrapped: Vec<Vec<usize>> = doc
.nodes
.iter()
.filter_map(|n| match n {
Node::Commented { comments, .. } => Some(comments.clone()),
_ => None,
})
.collect();
assert_eq!(wrapped, [vec![0], vec![2], vec![1], vec![3]]);
let sections: Vec<(String, String)> = doc
.nodes
.iter()
.filter_map(|n| match n {
Node::CommentSection { name, text, .. } => Some((name.clone(), text.clone())),
_ => None,
})
.collect();
assert_eq!(sections.len(), 4);
assert_eq!(sections[0].0, "comment-1");
assert_eq!(sections[1].0, "comment-1");
assert_eq!(sections[2].0, "comment-2");
assert_eq!(sections[3].0, "comment-3");
assert!(sections[1].1.ends_with("Agreed"));
}
#[test]
fn master_page_headers_and_footers_are_furniture() {
let xml = r#"<office:document xmlns:office="o" xmlns:text="x" xmlns:style="s" xmlns:table="t">
<office:master-styles>
<style:master-page style:name="Standard">
<style:header><text:p>Head <text:page-number>1</text:page-number></text:p>
<table:table><table:table-row><table:table-cell><text:p>Version</text:p></table:table-cell></table:table-row></table:table>
</style:header>
<style:header-left style:display="false"><text:p>Unused</text:p></style:header-left>
<style:footer><text:p>Foot</text:p></style:footer>
</style:master-page>
<style:master-page style:name="Endnote"><style:header/><style:footer><text:p> </text:p></style:footer></style:master-page>
</office:master-styles>
<office:body><office:text><text:p>Body.</text:p></office:text></office:body></office:document>"#;
let dom = Document::parse(xml).unwrap();
let parts = master_page_parts(&dom, None);
assert_eq!(
parts.iter().map(|p| p.footer).collect::<Vec<_>>(),
[false, true]
);
let styles = parse_styles(&dom, None);
let mut doc = DoclingDocument::new("s");
push_master_page_furniture(&parts, &styles, &mut doc);
let furniture: Vec<String> = doc
.nodes
.iter()
.map(|n| match n {
Node::Furniture { inner, .. } => match &**inner {
Node::Paragraph { text } => text.clone(),
Node::Table(t) => format!("table {}", t.rows[0].join("|")),
other => format!("{other:?}"),
},
other => format!("body {other:?}"),
})
.collect();
assert_eq!(furniture, ["Head 1", "table Version", "Foot"]);
assert_eq!(doc.export_to_markdown(), "");
}
#[test]
fn text_box_blocks_are_separate_items() {
let xml = r#"<root xmlns:text="x" xmlns:draw="d" xmlns:table="t">
<text:p>Anchor.<draw:frame><draw:text-box><text:p>First.</text:p><text:list><text:list-item><text:p>item</text:p></text:list-item></text:list><table:table><table:table-row><table:table-cell><text:p>Cell</text:p></table:table-cell></table:table-row></table:table></draw:text-box></draw:frame> tail.</text:p>
<text:p><draw:frame><draw:text-box><text:p>Only a box.<draw:frame><draw:text-box><text:p>Inner.</text:p></draw:text-box></draw:frame></text:p></draw:text-box></draw:frame></text:p>
<draw:frame><draw:text-box><text:h text:outline-level="1">Boxed heading</text:h></draw:text-box></draw:frame>
<text:p>After.</text:p></root>"#;
let dom = Document::parse(xml).unwrap();
let styles = parse_styles(&dom, None);
let mut doc = DoclingDocument::new("s");
walk_text(dom.root_element(), &styles, &mut doc);
assert_eq!(
doc.export_to_markdown(),
"Anchor. tail.\n\nFirst.\n\n- item\n\n| Cell |\n|--------|\n\nOnly a box.\n\nInner.\n\n## Boxed heading\n\nAfter.\n"
);
let p = dom.root_element().first_element_child().unwrap();
assert_eq!(text_boxes(p).len(), 1);
assert_eq!(para_plain_text(p), "Anchor. tail.");
}
#[test]
fn frame_title_is_the_pictures_alt_text() {
let xml = r#"<root xmlns:text="x" xmlns:draw="d" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:xlink="l">
<text:p>Before <draw:frame draw:name="I" svg:width="2.5in" svg:height="3.175cm"><draw:image xlink:href="Pictures/x.png"/><svg:title>A gateway</svg:title><svg:desc>Longer</svg:desc></draw:frame> after.<text:title>Doc title</text:title></text:p></root>"#;
let dom = Document::parse(xml).unwrap();
let p = dom.root_element().first_element_child().unwrap();
assert_eq!(para_plain_text(p), "Before A gatewayLonger after.Doc title");
assert!(is_svg(
p.descendants()
.find(|n| n.has_tag_name("title") && n.tag_name().namespace().is_some())
.unwrap()
));
let img = p.descendants().find(|n| n.has_tag_name("image")).unwrap();
assert_eq!(frame_alt(img).as_deref(), Some("A gateway"));
let [w, h] = frame_display(img).unwrap();
assert!((w - 2.5).abs() < 1e-5 && (h - 1.25).abs() < 1e-5, "{w} {h}");
assert_eq!(odf_length_in("72pt"), Some(1.0));
assert_eq!(odf_length_in("96px"), Some(1.0));
assert_eq!(odf_length_in("10%"), None);
}
#[test]
fn odf_markers_follow_num_format_and_affixes() {
let level = |format: &str, prefix: &str, suffix: &str| OdfLevel {
numbered: true,
start: 1,
prefix: prefix.into(),
suffix: suffix.into(),
format: format.into(),
};
assert_eq!(odf_marker(1, Some(&level("a", "", ")"))), "a)");
assert_eq!(odf_marker(3, Some(&level("i", "", "."))), "iii.");
assert_eq!(odf_marker(4, Some(&level("I", "", ""))), "IV.");
assert_eq!(odf_marker(3, Some(&level("1", "(", ")"))), "(3)");
assert_eq!(odf_marker(27, Some(&level("A", "", "."))), "AA.");
assert_eq!(odf_marker(2, Some(&level("", "", ""))), "2.");
assert_eq!(odf_marker(2, None), "2.");
}
#[test]
fn ods_sheet_splits_into_regions() {
let xml = r#"<root xmlns:table="t" xmlns:text="x">
<table:table>
<table:table-row><table:table-cell/>
<table:table-cell><text:p>Title</text:p></table:table-cell></table:table-row>
<table:table-row><table:table-cell/></table:table-row>
<table:table-row><table:table-cell/>
<table:table-cell><text:p>H1</text:p></table:table-cell>
<table:table-cell><text:p>H2</text:p></table:table-cell></table:table-row>
<table:table-row><table:table-cell/>
<table:table-cell><text:p>1</text:p></table:table-cell>
<table:table-cell><text:p>2</text:p></table:table-cell></table:table-row>
</table:table></root>"#;
let dom = Document::parse(xml).unwrap();
let table = dom.descendants().find(|n| n.has_tag_name("table")).unwrap();
let mut doc = DoclingDocument::new("t");
add_ods_sheet(table, &parse_styles(&dom, None), &mut doc);
let tables: Vec<&Table> = doc
.nodes
.iter()
.filter_map(|n| match n {
Node::Table(t) => Some(t),
_ => None,
})
.collect();
assert_eq!(tables.len(), 2, "title singleton + data region");
assert_eq!(tables[0].rows, vec![vec!["Title".to_string()]]);
assert_eq!(tables[1].rows.len(), 2, "header + one data row");
assert_eq!(tables[1].rows[0], vec!["H1".to_string(), "H2".to_string()]);
}
#[test]
fn list_continues_across_empty_nested_item() {
let xml = r#"<root xmlns:text="x" xmlns:style="s">
<style:list-style style:name="L1">
<text:list-level-style-number text:level="1"/></style:list-style>
<style:list-style style:name="L2">
<text:list-level-style-bullet text:level="1"/>
<text:list-level-style-bullet text:level="2"/></style:list-style>
<office:body xmlns:office="o"><office:text>
<text:list text:style-name="L1">
<text:list-item><text:p>one</text:p></text:list-item>
<text:list-item><text:p>two</text:p></text:list-item>
</text:list>
<text:list text:style-name="L2">
<text:list-item><text:list>
<text:list-item><text:p>bullet</text:p></text:list-item>
</text:list></text:list-item>
<text:list-item><text:p>three</text:p></text:list-item>
</text:list>
</office:text></office:body></root>"#;
let dom = Document::parse(xml).unwrap();
let styles = parse_styles(&dom, None);
let body = dom.descendants().find(|n| n.has_tag_name("text")).unwrap();
let mut doc = DoclingDocument::new("t");
walk_text(body, &styles, &mut doc);
let items: Vec<(u64, u8, &str)> = doc
.nodes
.iter()
.filter_map(|n| match n {
Node::ListItem {
number,
level,
text,
..
} => Some((*number, *level, text.as_str())),
_ => None,
})
.collect();
assert_eq!(
items,
vec![
(1, 0, "one"),
(2, 0, "two"),
(0, 1, "bullet"),
(3, 0, "three"),
]
);
}
fn table_of(xml: &str) -> Table {
let dom = Document::parse(xml).unwrap();
let styles = parse_styles(&dom, None);
let table = dom.descendants().find(|n| n.has_tag_name("table")).unwrap();
parse_table(table, &styles).unwrap()
}
#[test]
fn rich_cell_renders_list_plain_cell_drops_formatting() {
let xml = r#"<root xmlns:table="t" xmlns:text="x">
<table:table><table:table-row>
<table:table-cell><text:p>List:</text:p><text:list>
<text:list-item><text:p>a</text:p></text:list-item>
<text:list-item><text:p>b</text:p></text:list-item></text:list></table:table-cell>
<table:table-cell><text:p>plain <text:span>bold</text:span></text:p></table:table-cell>
</table:table-row></table:table></root>"#;
let t = table_of(xml);
assert_eq!(t.rows[0][0], "List:\n\n- a\n- b");
assert_eq!(t.rows[0][1], "plain bold");
}
#[test]
fn oo1x_lists_tabs_and_heading_levels_map() {
let xml = r#"<root xmlns:text="x">
<text:h text:level="2">Section</text:h>
<text:p>a<text:tab-stop/><text:span>b</text:span></text:p>
<text:ordered-list>
<text:list-item><text:p>one</text:p>
<text:unordered-list>
<text:list-item><text:p>sub</text:p></text:list-item>
</text:unordered-list></text:list-item>
<text:list-item><text:p>two</text:p></text:list-item>
</text:ordered-list></root>"#;
let dom = Document::parse(xml).unwrap();
let styles = parse_styles(&dom, None);
let mut doc = DoclingDocument::new("s");
walk_text(dom.root_element(), &styles, &mut doc);
let md = doc.export_to_markdown();
assert!(md.contains("### Section"), "text:level heading:\n{md}");
assert!(md.contains("a\tb"), "tab-stop expands:\n{md}");
assert!(md.contains("1. one"), "ordered-list numbers:\n{md}");
assert!(md.contains("- sub"), "nested unordered-list bullets:\n{md}");
assert!(md.contains("2. two"), "numbering continues:\n{md}");
}
#[test]
fn oo1x_style_properties_element_and_attribute_names() {
let xml = r#"<root xmlns:style="s" xmlns:fo="f">
<style:style style:name="T1" style:family="text">
<style:properties fo:font-weight="bold"
style:text-crossing-out="single-line"
style:text-underline="single"/></style:style></root>"#;
let dom = Document::parse(xml).unwrap();
let styles = parse_styles(&dom, None);
let f = resolve_fmt(&styles, Some("T1"), Fmt::default());
assert!(f.bold && f.strike && f.underline);
}
#[test]
fn oo1x_body_without_wrapper_converts() {
let xml = r#"<office:document-content xmlns:office="o" xmlns:text="x"
office:class="text" office:version="1.0">
<office:body><text:p>Aus der Registratur.</text:p></office:body>
</office:document-content>"#;
let source =
SourceDocument::from_bytes("reg.sxw", crate::InputFormat::Odt, xml.as_bytes().to_vec());
let doc = OdfBackend.convert(&source).unwrap();
assert!(doc.export_to_markdown().contains("Aus der Registratur."));
}
#[test]
fn flat_odf_single_file_converts() {
let xml = r#"<office:document xmlns:office="o" xmlns:text="x" xmlns:table="t">
<office:automatic-styles><table:body/></office:automatic-styles>
<office:body><office:text>
<text:h text:outline-level="1">Flat</text:h>
<text:p>No zip here.</text:p>
</office:text></office:body></office:document>"#;
let source = SourceDocument::from_bytes(
"flat.fodt",
crate::InputFormat::Odt,
xml.as_bytes().to_vec(),
);
let doc = OdfBackend.convert(&source).unwrap();
let md = doc.export_to_markdown();
assert!(md.contains("## Flat"), "{md}");
assert!(md.contains("No zip here."), "{md}");
}
#[test]
fn flat_inline_chart_object_renders_as_chart() {
let xml = r#"<root xmlns:draw="d" xmlns:chart="c" xmlns:table="t" xmlns:text="x" xmlns:office="o">
<draw:frame><draw:object>
<office:document>
<chart:chart chart:class="chart:bar">
<table:table>
<table:table-row>
<table:table-cell office:value-type="string"><text:p>Row 1</text:p></table:table-cell>
<table:table-cell office:value-type="float"><text:p>9.1</text:p></table:table-cell>
</table:table-row>
</table:table>
</chart:chart>
</office:document>
</draw:object>
<draw:image><office:binary-data>VkNMTVRGAQAx</office:binary-data></draw:image>
</draw:frame></root>"#;
let dom = Document::parse(xml).unwrap();
let styles = parse_styles(&dom, None);
let mut doc = DoclingDocument::new("c");
let frame = dom.descendants().find(|n| n.has_tag_name("frame")).unwrap();
walk_slide_frame(frame, &styles, &mut doc, false);
let charts: Vec<_> = doc
.nodes
.iter()
.filter(|n| matches!(n, Node::Chart { .. }))
.collect();
assert_eq!(charts.len(), 1, "inline object renders as one chart");
assert!(
matches!(charts[0], Node::Chart { kind, .. } if kind == "bar_chart"),
"chart:class maps"
);
assert!(
!doc.nodes.iter().any(|n| matches!(n, Node::Picture { .. })),
"SVM preview is not a picture"
);
assert!(
!doc.nodes.iter().any(|n| matches!(n, Node::Table(_))),
"the chart's data table is not doubled as a standalone table"
);
}
#[test]
fn inline_binary_image_magic_gates_pictures() {
let xml = r#"<root xmlns:draw="d" xmlns:office="o">
<draw:image draw:name="png"><office:binary-data>iVBORw0KGgoA</office:binary-data></draw:image>
<draw:image draw:name="svm"><office:binary-data>VkNMTVRGAQAx</office:binary-data></draw:image>
</root>"#;
let dom = Document::parse(xml).unwrap();
let imgs: Vec<XmlNode> = dom
.descendants()
.filter(|n| n.has_tag_name("image"))
.collect();
assert!(image_can_be_bitmap(imgs[0], ""), "PNG magic");
assert!(!image_can_be_bitmap(imgs[1], ""), "SVM magic");
}
#[test]
fn merged_cells_repeat_into_covered_columns() {
let xml = r#"<root xmlns:table="t" xmlns:text="x">
<table:table>
<table:table-row>
<table:table-cell table:number-columns-spanned="2"><text:p>Wide</text:p></table:table-cell>
<table:covered-table-cell/>
<table:table-cell><text:p>C</text:p></table:table-cell>
</table:table-row>
<table:table-row>
<table:table-cell><text:p>x</text:p></table:table-cell>
<table:table-cell><text:p>y</text:p></table:table-cell>
<table:table-cell><text:p>z</text:p></table:table-cell>
</table:table-row>
</table:table></root>"#;
let t = table_of(xml);
assert_eq!(t.rows[0], vec!["Wide", "Wide", "C"]);
assert_eq!(t.rows[1], vec!["x", "y", "z"]);
}
}