Format numeric bibliography authors with initials

This commit is contained in:
gloamxun committed 2026-10-20 02:05:40 +08:00
1 parent 261f0ea278
commit b8c2be8837
3 files changed
+43 -1

No files matched your search

+38 -1
View File
@@ -910,6 +910,38 @@ fn source_directory(file_name: &str) -> &Path {
.unwrap_or_else(|| Path::new("."))
}
fn numeric_author_list(author: &str) -> String {
author
.split(" and ")
.map(|name| {
let name = name.trim();
if name.eq_ignore_ascii_case("others") {
return "et al.".to_string();
}
let (last, given) = match name.split_once(',') {
Some((last, given)) => (last.trim(), given.trim()),
None => {
let mut words = name.split_whitespace().collect::<Vec<_>>();
let last = words.pop().unwrap_or("");
(last, "")
}
};
let initials = given
.split_whitespace()
.filter_map(|word| word.chars().find(|ch| ch.is_alphabetic()))
.map(|ch| format!("{ch}."))
.collect::<Vec<_>>()
.join(" ");
if initials.is_empty() {
last.to_string()
} else {
format!("{last}, {initials}")
}
})
.collect::<Vec<_>>()
.join(", ")
}
fn natbib_author_list(author: &str) -> String {
let names = author
.split(" and ")
@@ -944,7 +976,12 @@ impl BibEntry {
let mut parts = Vec::new();
let author = field("author");
if !author.is_empty() {
parts.push(format!("{author}."));
let authors = numeric_author_list(author);
if authors.ends_with('.') {
parts.push(authors);
} else {
parts.push(format!("{authors}."));
}
} else if !field("editor").is_empty() {
parts.push(format!("{} (ed.).", field("editor")));
}