Polish preprint title details and plainnat bibliography formatting

This commit is contained in:
gloamxun committed 2026-10-19 15:53:47 +08:00
1 parent 7f1f202de1
commit d211889e93
8 files changed
+120 -11

No files matched your search

+87 -1
View File
@@ -889,6 +889,25 @@ fn source_directory(file_name: &str) -> &Path {
.unwrap_or_else(|| Path::new("."))
}
fn natbib_author_list(author: &str) -> String {
let names = author
.split(" and ")
.map(|name| {
let name = name.trim();
match name.split_once(',') {
Some((last, first)) => format!("{} {}", first.trim(), last.trim()),
None => name.to_string(),
}
})
.collect::<Vec<_>>();
match names.as_slice() {
[] => String::new(),
[only] => only.clone(),
[first, second] => format!("{first} and {second}"),
[rest @ .., last] => format!("{}, and {last}", rest.join(", ")),
}
}
struct BibEntry {
kind: String,
key: String,
@@ -896,7 +915,10 @@ struct BibEntry {
}
impl BibEntry {
fn to_latex(&self) -> String {
fn to_latex(&self, author_year: bool) -> String {
if author_year {
return self.natbib_latex();
}
let field = |name: &str| self.fields.get(name).map(String::as_str).unwrap_or("");
let mut parts = Vec::new();
let author = field("author");
@@ -964,6 +986,70 @@ impl BibEntry {
}
escape_bib_text(&parts.join(" "))
}
/// plainnat/author-year layout used by `natbib` and the arXiv class.
fn natbib_latex(&self) -> String {
let field = |name: &str| self.fields.get(name).map(String::as_str).unwrap_or("");
let authors = if !field("author").is_empty() {
natbib_author_list(field("author"))
} else if !field("editor").is_empty() {
format!("{} (ed.)", field("editor"))
} else {
String::new()
};
let mut output = String::new();
if !authors.is_empty() {
output.push_str(&authors);
output.push_str(". ");
}
let title = field("title");
if !title.is_empty() {
output.push_str(title);
output.push_str(". ");
}
let mut tail = Vec::new();
let booktitle = field("booktitle");
if !booktitle.is_empty() {
tail.push(format!("In \\emph{{{booktitle}}}"));
} else if !field("journal").is_empty() {
tail.push(format!("\\emph{{{}}}", field("journal")));
}
if !field("volume").is_empty() {
let mut volume = field("volume").to_string();
if !field("number").is_empty() {
volume.push_str(&format!("({})", field("number")));
}
tail.push(volume);
}
if !field("pages").is_empty() {
tail.push(format!(
"pages {}",
field("pages").replace("--", "\u{2013}")
));
}
if !tail.is_empty() {
output.push_str(&tail.join(", "));
output.push_str(", ");
}
let publisher = if field("publisher").is_empty() {
field("organization")
} else {
field("publisher")
};
let year = field("year");
match (publisher.is_empty(), year.is_empty()) {
(false, false) => output.push_str(&format!("{publisher}, {year}.")),
(false, true) => output.push_str(&format!("{publisher}.")),
(true, false) => output.push_str(&format!("{year}.")),
(true, true) => {
if output.ends_with(", ") {
output.truncate(output.len() - 2);
output.push('.');
}
}
}
escape_bib_text(output.trim())
}
}
fn escape_bib_text(source: &str) -> String {