diff --git a/README.md b/README.md index a30966a..4d5e5bc 100644 --- a/README.md +++ b/README.md @@ -385,7 +385,9 @@ BibTeX `@string` macros and single-level `crossref` inheritance are supported. Manual `thebibliography` entries also work. Documents loading `natbib` or `arxiv.sty` render `\citet`/`\citep` as author-year citations, format `.bib` entries in plainnat order (`First Last`, `In ...`, `pages`, publisher/year), -and omit numeric bibliography labels; other documents keep numeric citations. Custom `.bst` +add deterministic year suffixes (`2014a`, `2014b`) when several entries share +an author and year, and omit numeric bibliography labels; other documents keep +numeric citations. Custom `.bst` sorting/formatting and page-number references (`\pageref`) are not implemented. A paragraph starts with the current `\parindent` (17pt by default); the diff --git a/crates/texrra-core/src/document.rs b/crates/texrra-core/src/document.rs index 73dd95a..38eb55e 100644 --- a/crates/texrra-core/src/document.rs +++ b/crates/texrra-core/src/document.rs @@ -99,6 +99,9 @@ pub struct DocumentModel { pub labels: HashMap, pub citations: HashMap, pub citation_metadata: HashMap, + /// Bibliography keys in the order their metadata was declared. Needed to + /// assign natbib-style year suffixes (2014a, 2014b, ...) deterministically. + pub bibliography_order: Vec, pub author_year_citations: bool, pub footnotes: Vec, pub page_style: PageStyle, @@ -125,6 +128,7 @@ impl Default for DocumentModel { labels: HashMap::new(), citations: HashMap::new(), citation_metadata: HashMap::new(), + bibliography_order: Vec::new(), author_year_citations: false, footnotes: Vec::new(), page_style: PageStyle::default(), diff --git a/crates/texrra-core/src/document_parser.rs b/crates/texrra-core/src/document_parser.rs index c7dae71..34fc32b 100644 --- a/crates/texrra-core/src/document_parser.rs +++ b/crates/texrra-core/src/document_parser.rs @@ -508,6 +508,56 @@ struct Parser { in_abstract: bool, } +fn apply_bibliography_year_suffixes(document: &DocumentModel, items: &mut [(String, Vec)]) { + for (key, runs) in items { + let Some(metadata) = document.citation_metadata.get(key) else { + continue; + }; + if metadata.year.len() != 5 { + continue; + } + let base = &metadata.year[..4]; + let suffix = &metadata.year[4..]; + if let Some(run) = runs + .iter_mut() + .rev() + .find(|run| run.text.contains(base) && !run.text.contains(&metadata.year)) + { + run.text = run.text.replacen(base, &format!("{base}{suffix}"), 1); + } + } +} + +fn assign_citation_year_suffixes(document: &mut DocumentModel) { + let mut groups: HashMap<(String, String), Vec> = HashMap::new(); + for key in &document.bibliography_order { + let Some(metadata) = document.citation_metadata.get(key) else { + continue; + }; + if metadata.author.is_empty() || metadata.year.is_empty() { + continue; + } + groups + .entry((metadata.author.clone(), metadata.year.clone())) + .or_default() + .push(key.clone()); + } + for keys in groups.into_values() { + if keys.len() < 2 { + continue; + } + for (index, key) in keys.into_iter().enumerate() { + let Some(metadata) = document.citation_metadata.get_mut(&key) else { + continue; + }; + if metadata.year.len() == 4 { + let letter = char::from(b'a' + (index as u8).min(25)); + metadata.year.push(letter); + } + } + } +} + impl Parser { fn new() -> Self { Self { @@ -1312,6 +1362,9 @@ impl Parser { self.start_bibliography_item(key.clone()) } expanded::FrontendEventKind::BibliographyMetadata { key, author, year } => { + if !self.document.bibliography_order.contains(key) { + self.document.bibliography_order.push(key.clone()); + } self.document.citation_metadata.insert( key.clone(), crate::document::CitationMetadata { @@ -2239,9 +2292,11 @@ impl Parser { fn finish_bibliography(&mut self) { self.finish_bibliography_item(); - if let Some(bibliography) = self.bibliography.take() + assign_citation_year_suffixes(&mut self.document); + if let Some(mut bibliography) = self.bibliography.take() && !bibliography.items.is_empty() { + apply_bibliography_year_suffixes(&self.document, &mut bibliography.items); self.push_block(Block::Bibliography(bibliography.items)); } } diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index af077a3..cf61b23 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -14,7 +14,8 @@ contents and figure/table lists with dotted leaders; native PDF rectangles; unif chapter headings with chapter-prefixed section numbering and page breaks; deferred top-float queue for figures; document-level custom `\maketitle`; abstract quotation formatting, unnumbered `\paragraph` headings, natbib-style -author-year citations and plainnat bibliography formatting; +author-year citations and plainnat bibliography formatting with duplicate-year +`2014a`/`2014b` suffixes; `\vbox`/`\vtop` vertical-material capture with inline splicing (`build_positioned_pages` is the only page builder and no separate break planner remains); conditional Base-14 font embedding and natural-width centered diff --git a/docs/STATUS.md b/docs/STATUS.md index 4f176c9..d53afc5 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -69,8 +69,9 @@ the source changes; the SVG preview is refreshed per page. per-span alignment, and partial `\cmidrule`/`\cline` rules over a column range. - Citations: numeric citations by default; `natbib`/`arxiv.sty` selects author-year `\citet`/`\citep` from `.bib` metadata, formats bibliography entries in - plainnat order (`First Last`, `In ...`, `pages`, publisher/year), and drops - numeric bibliography labels. + plainnat order (`First Last`, `In ...`, `pages`, publisher/year), adds + deterministic `2014a`/`2014b` suffixes for duplicate author-year entries, and + drops numeric bibliography labels. - Live preview: paragraph cache, page-hash diffing, warm rebuilds in the low milliseconds for small edits. diff --git a/tests/typeset.rs b/tests/typeset.rs index 765a0cc..b12b2c8 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -1355,6 +1355,10 @@ fn natbib_citations_use_author_year() { !second.contains(">[1, 3]"), "numeric citation was not replaced: {second}" ); + assert!( + second.contains("2014a") && second.contains("2014b"), + "duplicate author-year citations should get a/b suffixes: {second}" + ); let third = page_svg(&value, 2); let george = third.find("Geor").expect("bibliography given name missing"); let kour = third @@ -1368,4 +1372,8 @@ fn natbib_citations_use_author_year() { !third.contains(">[1]"), "author-year bibliography should omit numeric labels: {third}" ); + assert!( + third.contains("2014a") && third.contains("2014b"), + "bibliography years should carry a/b suffixes: {third}" + ); }