Handle citation notes and bibliography commands

This commit is contained in:
gloamxun committed 2026-10-25 01:11:31 +08:00
1 parent 22c81cbb88
commit 8bf56bac09
4 files changed
+75 -18

No files matched your search

+71 -14
View File
@@ -987,19 +987,6 @@ fn expand_conditionals_at(
output
}
fn counter_assignment(line: &str, name: &str) -> Option<u32> {
let rest = line.strip_prefix("\\setcounter")?.trim_start();
let rest = rest.strip_prefix('{')?;
let close = rest.find('}')?;
if rest[..close].trim() != name {
return None;
}
let rest = rest[close + 1..].trim_start();
let rest = rest.strip_prefix('{')?;
let close = rest.find('}')?;
rest[..close].trim().parse().ok()
}
fn any_counter_assignment(line: &str) -> Option<(String, u32)> {
let rest = line.strip_prefix("\\setcounter")?.trim_start();
let rest = rest.strip_prefix('{')?;
@@ -1126,6 +1113,16 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
}
continue;
}
if trimmed.starts_with(r"\bibliographystyle") || trimmed.starts_with(r"\bibliography{") {
let rest = trimmed
.strip_prefix(r"\bibliographystyle")
.or_else(|| trimmed.strip_prefix(r"\bibliography"));
if let Some(rest) = rest
&& braced_group(rest.trim_start()).is_some()
{
continue;
}
}
if let Some((name, target)) = macro_alias(trimmed) {
let definition = geometry
.macros
@@ -1512,14 +1509,46 @@ fn extract_bibliography(body: &mut String) -> Vec<(String, String)> {
}
/// Replace `\cite{...}` with `[n]`/`[n, m]` from the bibliography order.
/// Remove `\nocite{...}` commands; citations are resolved from the written
/// `thebibliography` block and there is no BibTeX pass to extend it.
fn strip_nocite(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut rest = text;
while let Some(index) = rest.find(r"\nocite") {
out.push_str(&rest[..index]);
let after = rest[index + r"\nocite".len()..].trim_start();
let Some((_, consumed)) = braced_group(after) else {
out.push_str(&rest[index..]);
return out;
};
rest = &after[consumed..];
}
out.push_str(rest);
out
}
fn resolve_citations(paragraphs: &mut [SourceParagraph], bibliography: &[(String, String)]) {
let keys: Vec<&str> = bibliography.iter().map(|(key, _)| key.as_str()).collect();
for paragraph in paragraphs {
paragraph.text = strip_nocite(&paragraph.text);
let mut text = String::with_capacity(paragraph.text.len());
let mut rest = paragraph.text.as_str();
while let Some(index) = rest.find(r"\cite") {
text.push_str(&rest[..index]);
let after = rest[index + r"\cite".len()..].trim_start();
let command =
if rest[index..].starts_with(r"\citep") || rest[index..].starts_with(r"\citet") {
&rest[index..index + 6]
} else {
&rest[index..index + 5]
};
let mut after = rest[index + command.len()..].trim_start();
// `\cite[p.~3]{key}` drops the optional note but keeps the number.
while let Some(note) = after.strip_prefix('[') {
let Some(end) = note.find(']') else {
break;
};
after = note[end + 1..].trim_start();
}
let Some((body, consumed)) = braced_group(after) else {
text.push_str(&rest[index..]);
rest = "";
@@ -6785,6 +6814,34 @@ A definition.
assert_eq!(document.units[0].source, "See [?].");
}
#[test]
fn citation_notes_and_nocite_are_dropped() {
let source = "See \\citep[p.~3]{alpha} and \\citet{alpha}.\\nocite{beta}\n\n\\begin{thebibliography}{9}\n\\bibitem{alpha} Alpha entry.\n\\bibitem{beta} Beta entry.\n\\end{thebibliography}";
let (body, geometry) = geometry_and_body(source);
let blocks = paragraphs(&body).unwrap();
let document = build_document(&blocks, &geometry);
let sources: Vec<&str> = document
.units
.iter()
.map(|unit| unit.source.as_str())
.collect();
assert_eq!(sources[0], "See [1] and [1].");
assert!(sources.contains(&"[1] Alpha entry."), "{sources:?}");
assert!(sources.contains(&"[2] Beta entry."), "{sources:?}");
}
#[test]
fn bibliography_commands_stay_out_of_the_body() {
let source = "\\bibliographystyle{plain}\n\\bibliography{refs}\n\nBody.";
let (body, geometry) = geometry_and_body(source);
assert!(!body.contains("bibliograph"), "{body:?}");
let blocks = paragraphs(&body).unwrap();
assert_eq!(blocks.len(), 1);
assert_eq!(blocks[0].text, "Body.");
let document = build_document(&blocks, &geometry);
assert_eq!(document.units.len(), 1);
}
#[test]
fn footnotes_number_across_paragraphs() {
let blocks = paragraphs("One\\footnote{A}\n\nTwo\\footnote{B}").unwrap();