Resolve nameref to heading titles
Section numbering now also records the heading title for each label: a
number_sections_with_titles variant fills a titles table (the existing
number_sections wrapper keeps its signature for tests), and
resolve_section_refs takes that table. \nameref{label} becomes the heading
title, falling back to the number when no title was recorded, while \autoref
keeps producing "Section <number>"; unresolved forms become "??".
A preview test labels a heading inside its braces and renders \ref, \autoref
and \nameref together as "1 and Section 1 and Introduction" with no warnings.
This commit is contained in:
1 parent
c399481c6a
commit
0728d78b29
4 files changed
+64
-10
No files matched your search
+58
-4
@@ -11683,6 +11683,17 @@ fn number_sections(
|
||||
secnumdepth: u32,
|
||||
section_start: u32,
|
||||
chaptered: bool,
|
||||
) -> Vec<(String, String)> {
|
||||
let mut titles = Vec::new();
|
||||
number_sections_with_titles(blocks, secnumdepth, section_start, chaptered, &mut titles)
|
||||
}
|
||||
|
||||
fn number_sections_with_titles(
|
||||
blocks: &mut Vec<SourceParagraph>,
|
||||
secnumdepth: u32,
|
||||
section_start: u32,
|
||||
chaptered: bool,
|
||||
titles: &mut Vec<(String, String)>,
|
||||
) -> Vec<(String, String)> {
|
||||
let mut counters = [section_start, 0u32, 0, 0, 0];
|
||||
let mut chapter = 0u32;
|
||||
@@ -11702,6 +11713,11 @@ fn number_sections(
|
||||
}
|
||||
let level = blocks[index].heading_level;
|
||||
let block_labels = take_labels(&mut blocks[index].text);
|
||||
let block_title = if level.is_some() {
|
||||
blocks[index].text.trim().to_string()
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
if level.is_none() {
|
||||
if !block_labels.is_empty() && blocks[index].text.trim().is_empty() {
|
||||
let value = last_number.clone().unwrap_or_else(|| "??".to_string());
|
||||
@@ -11722,6 +11738,7 @@ fn number_sections(
|
||||
if blocks[index].heading_starred {
|
||||
let value = last_number.clone().unwrap_or_else(|| "??".to_string());
|
||||
for label in block_labels {
|
||||
titles.push((label.clone(), block_title.clone()));
|
||||
labels.push((label, value.clone()));
|
||||
}
|
||||
index += 1;
|
||||
@@ -11739,6 +11756,7 @@ fn number_sections(
|
||||
blocks[index].heading_number = Some(number.clone());
|
||||
last_number = Some(number.clone());
|
||||
for label in block_labels {
|
||||
titles.push((label.clone(), block_title.clone()));
|
||||
labels.push((label, number.clone()));
|
||||
}
|
||||
let title = blocks[index].text.trim().to_string();
|
||||
@@ -11776,6 +11794,7 @@ fn number_sections(
|
||||
if level as u32 > secnumdepth {
|
||||
let value = last_number.clone().unwrap_or_else(|| "??".to_string());
|
||||
for label in block_labels {
|
||||
titles.push((label.clone(), block_title.clone()));
|
||||
labels.push((label, value.clone()));
|
||||
}
|
||||
index += 1;
|
||||
@@ -11784,6 +11803,7 @@ fn number_sections(
|
||||
blocks[index].heading_number = Some(number.clone());
|
||||
last_number = Some(number.clone());
|
||||
for label in block_labels {
|
||||
titles.push((label.clone(), block_title.clone()));
|
||||
labels.push((label, number.clone()));
|
||||
}
|
||||
let title = blocks[index].text.trim().to_string();
|
||||
@@ -11803,7 +11823,11 @@ fn appendix_letter(value: u32) -> String {
|
||||
}
|
||||
|
||||
/// Replace section references and turn unresolved `\ref`/`\eqref` into `??`.
|
||||
fn resolve_section_refs(blocks: &mut [SourceParagraph], labels: &[(String, String)]) {
|
||||
fn resolve_section_refs(
|
||||
blocks: &mut [SourceParagraph],
|
||||
labels: &[(String, String)],
|
||||
titles: &[(String, String)],
|
||||
) {
|
||||
for block in blocks {
|
||||
for (label, value) in labels {
|
||||
block.text = block.text.replace(&format!("\\ref{{{label}}}"), value);
|
||||
@@ -11812,8 +11836,14 @@ fn resolve_section_refs(blocks: &mut [SourceParagraph], labels: &[(String, Strin
|
||||
&format!("\\autoref{{{label}}}"),
|
||||
&format!("Section {value}"),
|
||||
);
|
||||
let name = titles
|
||||
.iter()
|
||||
.find(|(known, _)| known == label)
|
||||
.map(|(_, title)| title.as_str())
|
||||
.unwrap_or(value);
|
||||
block.text = block.text.replace(&format!("\\nameref{{{label}}}"), name);
|
||||
}
|
||||
for command in ["\\ref", "\\eqref", "\\autoref"] {
|
||||
for command in ["\\ref", "\\eqref", "\\autoref", "\\nameref"] {
|
||||
loop {
|
||||
let prefix = format!("{command}{{");
|
||||
let Some(start) = block.text.find(&prefix) else {
|
||||
@@ -12304,11 +12334,13 @@ fn build_document_with_labels(
|
||||
.document_class
|
||||
.as_deref()
|
||||
.is_some_and(|name| matches!(name, "report" | "book"));
|
||||
let section_labels = number_sections(
|
||||
let mut section_titles = Vec::new();
|
||||
let section_labels = number_sections_with_titles(
|
||||
&mut paragraphs,
|
||||
geometry.secnumdepth.unwrap_or(3),
|
||||
geometry_counter(geometry, "section"),
|
||||
chaptered,
|
||||
&mut section_titles,
|
||||
);
|
||||
let label_numbers = section_labels.clone();
|
||||
expand_toc(
|
||||
@@ -12341,7 +12373,7 @@ fn build_document_with_labels(
|
||||
.as_deref()
|
||||
.unwrap_or("List of Tables"),
|
||||
);
|
||||
resolve_section_refs(&mut paragraphs, &references);
|
||||
resolve_section_refs(&mut paragraphs, &references, §ion_titles);
|
||||
number_footnotes(&mut paragraphs);
|
||||
resolve_citations(&mut paragraphs, &geometry.bibliography);
|
||||
append_bibliography(
|
||||
@@ -16544,6 +16576,28 @@ A definition.
|
||||
assert!(!svg.contains("numberwithin"), "{svg}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nameref_resolves_heading_titles() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\section{Introduction\\label{sec:intro}}\n\nSee \\ref{sec:intro} and \\autoref{sec:intro} and \\nameref{sec:intro}.\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 svg = value["pages"][0]["svg"].as_str().expect("svg");
|
||||
let text: String = svg
|
||||
.split('>')
|
||||
.filter_map(|piece| piece.split('<').next())
|
||||
.collect::<String>()
|
||||
.split_whitespace()
|
||||
.collect();
|
||||
assert!(text.contains("1andSection1andIntroduction"), "{text}");
|
||||
assert!(!text.contains("nameref"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn autoref_resolves_section_labels() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\section{Introduction\\label{sec:intro}}\n\nSee \\ref{sec:intro} and \\autoref{sec:intro}.\n\n\\end{document}\n";
|
||||
|
||||
Reference in new issue
Block a user