Add natbib a/b year suffixes for duplicate author-year entries
This commit is contained in:
1 parent
87b6b6e44e
commit
2fca019237
6 files changed
+76
-5
No files matched your search
@@ -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
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -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
|
||||
|
||||
+3
-2
@@ -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.
|
||||
|
||||
|
||||
@@ -1355,6 +1355,10 @@ fn natbib_citations_use_author_year() {
|
||||
!second.contains(">[1, 3]</text>"),
|
||||
"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]</text>"),
|
||||
"author-year bibliography should omit numeric labels: {third}"
|
||||
);
|
||||
assert!(
|
||||
third.contains("2014a") && third.contains("2014b"),
|
||||
"bibliography years should carry a/b suffixes: {third}"
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user