Render custom bibitem labels
The optional label of \bibitem[Label]{key} was parsed and thrown away, so the
list and citations always used the running number. The extractor now carries the
label in the entry (behind a private marker) and the bibliography list prints
[Label]; citation resolution uses the same label for that key, while unlabeled
entries keep their positional number.
A preview test gives the first entry a label and leaves the second plain,
then checks the list markers and the two citation markers with no warnings.
This commit is contained in:
1 parent
85d5c87b12
commit
9f642a2bbd
4 files changed
+67
-15
No files matched your search
+61
-9
@@ -6625,19 +6625,24 @@ fn extract_bibliography(body: &mut String) -> Vec<(String, String)> {
|
||||
let mut rest = inner.as_str();
|
||||
while let Some(index) = rest.find(r"\bibitem") {
|
||||
let after = rest[index + r"\bibitem".len()..].trim_start();
|
||||
let after = match after.strip_prefix('[') {
|
||||
let (label, after) = match after.strip_prefix('[') {
|
||||
Some(label) => match label.split_once(']') {
|
||||
Some((_, tail)) => tail.trim_start(),
|
||||
None => after,
|
||||
Some((label, tail)) => (Some(label.trim().to_string()), tail.trim_start()),
|
||||
None => (None, after),
|
||||
},
|
||||
None => after,
|
||||
None => (None, after),
|
||||
};
|
||||
let Some((key, consumed)) = braced_group(after) else {
|
||||
break;
|
||||
};
|
||||
let tail = &after[consumed..];
|
||||
let next = tail.find(r"\bibitem").unwrap_or(tail.len());
|
||||
entries.push((key.trim().to_string(), tail[..next].trim().to_string()));
|
||||
let entry = tail[..next].trim().to_string();
|
||||
let entry = match label {
|
||||
Some(label) => format!("\u{E030}{label}\u{E031}{entry}"),
|
||||
None => entry,
|
||||
};
|
||||
entries.push((key.trim().to_string(), entry));
|
||||
rest = &tail[next..];
|
||||
}
|
||||
entries
|
||||
@@ -6692,9 +6697,17 @@ fn resolve_citations(paragraphs: &mut [SourceParagraph], bibliography: &[(String
|
||||
let numbers: Vec<String> = body
|
||||
.split(',')
|
||||
.map(|key| {
|
||||
keys.iter()
|
||||
.position(|candidate| *candidate == key.trim())
|
||||
.map_or_else(|| "?".to_string(), |position| (position + 1).to_string())
|
||||
let key = key.trim();
|
||||
let Some(position) = keys.iter().position(|candidate| *candidate == key) else {
|
||||
return "?".to_string();
|
||||
};
|
||||
if let Some((_, entry)) = bibliography.get(position)
|
||||
&& let Some(rest) = entry.strip_prefix('\u{E030}')
|
||||
&& let Some((label, _)) = rest.split_once('\u{E031}')
|
||||
{
|
||||
return label.to_string();
|
||||
}
|
||||
(position + 1).to_string()
|
||||
})
|
||||
.collect();
|
||||
let _ = write!(text, "[{}]", numbers.join(", "));
|
||||
@@ -12176,13 +12189,24 @@ fn append_bibliography(
|
||||
}
|
||||
paragraphs.push(bibliography_heading(heading));
|
||||
for (index, (_, entry)) in bibliography.iter().enumerate() {
|
||||
let marker = match entry.strip_prefix('\u{E030}') {
|
||||
Some(rest) => rest
|
||||
.split_once('\u{E031}')
|
||||
.map(|(label, _)| label.to_string())
|
||||
.unwrap_or_else(|| format!("{}", index + 1)),
|
||||
None => format!("{}", index + 1),
|
||||
};
|
||||
let body = entry
|
||||
.split_once('\u{E031}')
|
||||
.map(|(_, body)| body)
|
||||
.unwrap_or(entry);
|
||||
paragraphs.push(SourceParagraph {
|
||||
width: None,
|
||||
multline: false,
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: format!("[{}] {entry}", index + 1),
|
||||
text: format!("[{marker}] {body}"),
|
||||
page_break: false,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
@@ -16669,6 +16693,34 @@ A definition.
|
||||
assert!(!svg.contains("numberwithin"), "{svg}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bibitem_optional_labels_replace_the_number() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\nSee \\cite{a} and \\cite{b}.\n\n\\begin{thebibliography}{9}\n\\bibitem[Author(2020)]{a} Alpha entry.\n\\bibitem{b} Beta entry.\n\\end{thebibliography}\n\n\\end{document}\n";
|
||||
let json = preview_json(source).expect("preview");
|
||||
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
|
||||
assert_eq!(
|
||||
value["warnings"].as_array().map(Vec::len),
|
||||
Some(0),
|
||||
"{}",
|
||||
value["warnings"]
|
||||
);
|
||||
let pages = value["pages"].as_array().expect("pages");
|
||||
let mut text = String::new();
|
||||
for page in pages {
|
||||
if let Some(svg) = page["svg"].as_str() {
|
||||
text.push_str(
|
||||
&svg.split('>')
|
||||
.filter_map(|piece| piece.split('<').next())
|
||||
.collect::<String>(),
|
||||
);
|
||||
}
|
||||
}
|
||||
let text: String = text.split_whitespace().collect();
|
||||
assert!(text.contains("[Author(2020)]Alphaentry."), "{text}");
|
||||
assert!(text.contains("[2]Betaentry."), "{text}");
|
||||
assert!(text.contains("See[Author(2020)]and[2]."), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_footnote_numbers_pair_marks_and_texts() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\nA\\footnotemark[7] B\\footnotetext[7]{Custom note.}\n\n\\end{document}\n";
|
||||
|
||||
Reference in new issue
Block a user