use f_office_xml::{Document, NodeId};
use crate::body::field_code;
use crate::body::{FIELD_CHAR, FieldChar, ParagraphIds};
use crate::props::{RunProps, UNDERLINE, Underline};
use crate::{ns::W, reader::PartReader, styles::Styles};
#[derive(Default)]
struct Field {
code: String,
result: bool,
toc: bool,
}
#[derive(Default)]
struct Fields {
open: Vec<Field>,
toc_results: usize,
}
impl Fields {
fn begin(&mut self) {
self.open.push(Field::default());
}
fn simple(&mut self, code: &str) {
self.open.push(Field {
code: code.to_owned(),
..Field::default()
});
self.separate();
}
fn code(&mut self, text: &str) {
if let Some(field) = self.open.last_mut().filter(|f| !f.result) {
field.code.push_str(text);
}
}
fn separate(&mut self) {
if let Some(field) = self.open.last_mut().filter(|f| !f.result) {
field.result = true;
field.toc =
field_code::kind(&field.code).is_some_and(|kind| kind.eq_ignore_ascii_case("TOC"));
self.toc_results += usize::from(field.toc);
}
}
fn end(&mut self) {
if let Some(field) = self.open.pop() {
self.toc_results -= usize::from(field.toc);
}
}
const fn in_toc_result(&self) -> bool {
self.toc_results > 0
}
}
pub(crate) fn prepare(doc: &mut Document, (part, ids): (&str, &ParagraphIds), styles: &Styles) {
if let Ok(root) = doc.root() {
walk(
doc,
(part, ids),
root,
styles,
false,
&mut Fields::default(),
);
}
}
fn walk(
doc: &mut Document,
(part, ids): (&str, &ParagraphIds),
node: NodeId,
styles: &Styles,
linked: bool,
fields: &mut Fields,
) -> (bool, bool) {
if crate::ns::is_text_box_content(doc, node)
|| ["footnote", "endnote"]
.iter()
.any(|name| doc.is(node, W, name))
{
let mut own = Fields::default();
let children = doc.child_elements(node).collect::<Vec<_>>();
for child in children {
walk(doc, (part, ids), child, styles, false, &mut own);
}
return (false, false);
}
let simple = doc.is(node, W, "fldSimple");
if simple {
fields.simple(doc.attribute(node, Some(W), "instr").unwrap_or_default());
}
read_field(doc, (part, ids), node, fields);
let linked = linked || doc.is(node, W, "hyperlink");
let mut seen = (false, false);
if doc.is(node, W, "t") || doc.is(node, W, "delText") {
let inside = linked && fields.in_toc_result();
seen = (inside, !inside);
}
let children = doc.child_elements(node).collect::<Vec<_>>();
for child in children {
let child = walk(doc, (part, ids), child, styles, linked, fields);
seen.0 |= child.0;
seen.1 |= child.1;
}
if doc.is(node, W, "r") && seen.0 && !seen.1 {
omit_style(doc, (part, ids), node, styles);
}
if simple {
fields.end();
}
seen
}
fn read_field(
doc: &Document,
(part, ids): (&str, &ParagraphIds),
node: NodeId,
fields: &mut Fields,
) {
if doc.is(node, W, "fldChar") {
match PartReader::of_part(doc, part, ids).enumeration(node, "fldCharType", FIELD_CHAR) {
Some(FieldChar::Begin) => fields.begin(),
Some(FieldChar::Separate) => fields.separate(),
Some(FieldChar::End) => fields.end(),
None => {}
}
} else if doc.is(node, W, "instrText") {
fields.code(&doc.text_content(node));
}
}
fn omit_style(
doc: &mut Document,
(part, ids): (&str, &ParagraphIds),
run: NodeId,
styles: &Styles,
) {
let Some(rpr) = doc.child(run, W, "rPr") else {
return;
};
let Some(style_node) = doc.child(rpr, W, "rStyle") else {
return;
};
let Some(id) = doc
.attribute(style_node, Some(W), "val")
.filter(|id| styles.is_link_style(id))
else {
return;
};
let style = styles.character(id);
let direct = RunProps::read(&mut PartReader::of_part(doc, part, ids), rpr);
let underline = styles.character_underline(id);
let children = doc
.children(rpr)
.iter()
.copied()
.filter(|child| {
if *child == style_node {
return false;
}
if doc.is(*child, W, "color") && direct.color.is_some() && direct.color == style.color {
return false;
}
if doc.is(*child, W, "b") && direct.bold.is_some() && direct.bold == style.bold {
return false;
}
let kind = || {
PartReader::of_part(doc, part, ids)
.enumeration(*child, "val", UNDERLINE)
.unwrap_or(Underline::Single)
};
!(doc.is(*child, W, "u") && underline == Some(kind()))
})
.collect();
doc.set_children(rpr, children);
}
#[cfg(test)]
mod tests;