use std::collections::HashMap; use crate::document::{ Block, CitationMetadata, DocumentModel, ListBlock, ParsedDocument, Run, UnresolvedReference, }; /// Resolves document references after parsing has collected all labels and citations. pub fn resolve(parsed: ParsedDocument) -> DocumentModel { let mut document = parsed.0; fn citation_label( key: &str, citations: &HashMap, metadata: &HashMap, author_year: bool, ) -> String { if author_year && let Some(entry) = metadata.get(key) { match (entry.author.is_empty(), entry.year.is_empty()) { (false, false) => return format!("{}, {}", entry.author, entry.year), (false, true) => return entry.author.clone(), (true, false) => return entry.year.clone(), (true, true) => {} } } citations .get(key) .map(usize::to_string) .unwrap_or_else(|| key.to_string()) } fn resolve_runs( runs: &mut [Run], labels: &HashMap, citations: &HashMap, metadata: &HashMap, author_year: bool, ) { for run in runs { let Some(reference) = run.unresolved_reference.take() else { continue; }; let text = match reference { UnresolvedReference::TocPage(index) => { run.unresolved_reference = Some(UnresolvedReference::TocPage(index)); continue; } UnresolvedReference::Label(key) => labels .get(&key) .cloned() .unwrap_or_else(|| "??".to_string()), UnresolvedReference::Citation(keys) => { if author_year { let formatted = keys .iter() .map(|key| citation_label(key, citations, metadata, true)) .collect::>() .join("; "); format!("({formatted})") } else { let formatted = keys .iter() .map(|key| citation_label(key, citations, metadata, false)) .collect::>() .join(", "); format!("[{formatted}]") } } UnresolvedReference::TextualCitation(keys) => { if author_year { keys.iter() .map(|key| { let label = citation_label(key, citations, metadata, true); match label.rsplit_once(", ") { Some((author, year)) => format!("{author} ({year})"), None => label, } }) .collect::>() .join("; ") } else { let formatted = keys .iter() .map(|key| citation_label(key, citations, metadata, false)) .collect::>() .join(", "); format!("[{formatted}]") } } }; run.text = text; } } fn resolve_list( list: &mut ListBlock, labels: &HashMap, citations: &HashMap, metadata: &HashMap, author_year: bool, ) { for item in &mut list.items { resolve_runs(&mut item.runs, labels, citations, metadata, author_year); for nested in &mut item.nested { resolve_list(nested, labels, citations, metadata, author_year); } } } let labels = &document.labels; let citations = &document.citations; let metadata = &document.citation_metadata; let author_year = document.author_year_citations; for block in &mut document.blocks { match block { Block::Paragraph(runs) | Block::Heading { runs, .. } => { resolve_runs(runs, labels, citations, metadata, author_year) } Block::List(list) => resolve_list(list, labels, citations, metadata, author_year), Block::Table { rows, caption, .. } => { for cell in rows.iter_mut().flatten() { resolve_runs(cell, labels, citations, metadata, author_year); } if let Some(caption) = caption { resolve_runs(caption, labels, citations, metadata, author_year); } } Block::Image { caption, .. } => { if let Some(caption) = caption { resolve_runs(caption, labels, citations, metadata, author_year); } } Block::FramedBox { runs, .. } | Block::Centered { runs } => { resolve_runs(runs, labels, citations, metadata, author_year) } Block::Bibliography(items) => { for (_, runs) in items { resolve_runs(runs, labels, citations, metadata, author_year); } } Block::HorizontalRule { .. } | Block::Math { .. } | Block::VerticalKern(_) | Block::VerticalGlue(_) | Block::Penalty(_) | Block::PageBreak => {} } } document }