Add natbib a/b year suffixes for duplicate author-year entries

This commit is contained in:
gloamxun committed 2026-10-19 18:04:19 +08:00
1 parent 87b6b6e44e
commit 2fca019237
6 files changed
+76 -5

No files matched your search

+4
View File
@@ -99,6 +99,9 @@ pub struct DocumentModel {
pub labels: HashMap<String, String>,
pub citations: HashMap<String, usize>,
pub citation_metadata: HashMap<String, CitationMetadata>,
/// Bibliography keys in the order their metadata was declared. Needed to
/// assign natbib-style year suffixes (2014a, 2014b, ...) deterministically.
pub bibliography_order: Vec<String>,
pub author_year_citations: bool,
pub footnotes: Vec<Footnote>,
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(),
+56 -1
View File
@@ -508,6 +508,56 @@ struct Parser {
in_abstract: bool,
}
fn apply_bibliography_year_suffixes(document: &DocumentModel, items: &mut [(String, Vec<Run>)]) {
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<String>> = 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));
}
}