Add abstract quotations, unnumbered paragraphs, and author-year citations

This commit is contained in:
gloamxun committed 2026-10-19 15:39:02 +08:00
1 parent 3406f84e86
commit 7f1f202de1
11 files changed
+366 -60

No files matched your search

+74 -21
View File
@@ -1,14 +1,38 @@
use std::collections::HashMap;
use crate::document::{Block, DocumentModel, ListBlock, ParsedDocument, Run, UnresolvedReference};
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<String, usize>,
metadata: &HashMap<String, CitationMetadata>,
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<String, String>,
citations: &HashMap<String, usize>,
metadata: &HashMap<String, CitationMetadata>,
author_year: bool,
) {
for run in runs {
let Some(reference) = run.unresolved_reference.take() else {
@@ -24,17 +48,42 @@ pub fn resolve(parsed: ParsedDocument) -> DocumentModel {
.cloned()
.unwrap_or_else(|| "??".to_string()),
UnresolvedReference::Citation(keys) => {
let formatted = keys
.iter()
.map(|key| {
citations
.get(key)
.map(usize::to_string)
.unwrap_or_else(|| key.clone())
})
.collect::<Vec<_>>()
.join(", ");
format!("[{formatted}]")
if author_year {
let formatted = keys
.iter()
.map(|key| citation_label(key, citations, metadata, true))
.collect::<Vec<_>>()
.join("; ");
format!("({formatted})")
} else {
let formatted = keys
.iter()
.map(|key| citation_label(key, citations, metadata, false))
.collect::<Vec<_>>()
.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::<Vec<_>>()
.join("; ")
} else {
let formatted = keys
.iter()
.map(|key| citation_label(key, citations, metadata, false))
.collect::<Vec<_>>()
.join(", ");
format!("[{formatted}]")
}
}
};
run.text = text;
@@ -45,42 +94,46 @@ pub fn resolve(parsed: ParsedDocument) -> DocumentModel {
list: &mut ListBlock,
labels: &HashMap<String, String>,
citations: &HashMap<String, usize>,
metadata: &HashMap<String, CitationMetadata>,
author_year: bool,
) {
for item in &mut list.items {
resolve_runs(&mut item.runs, labels, citations);
resolve_runs(&mut item.runs, labels, citations, metadata, author_year);
for nested in &mut item.nested {
resolve_list(nested, labels, citations);
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)
resolve_runs(runs, labels, citations, metadata, author_year)
}
Block::List(list) => resolve_list(list, labels, citations),
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);
resolve_runs(cell, labels, citations, metadata, author_year);
}
if let Some(caption) = caption {
resolve_runs(caption, labels, citations);
resolve_runs(caption, labels, citations, metadata, author_year);
}
}
Block::Image { caption, .. } => {
if let Some(caption) = caption {
resolve_runs(caption, labels, citations);
resolve_runs(caption, labels, citations, metadata, author_year);
}
}
Block::FramedBox { runs, .. } | Block::Centered { runs } => {
resolve_runs(runs, labels, citations)
resolve_runs(runs, labels, citations, metadata, author_year)
}
Block::Bibliography(items) => {
for (_, runs) in items {
resolve_runs(runs, labels, citations);
resolve_runs(runs, labels, citations, metadata, author_year);
}
}
Block::HorizontalRule { .. }