Show page numbers in the table of contents
This commit is contained in:
1 parent
aaa1ed8213
commit
60281dc9ca
5 files changed
+151
-9
No files matched your search
+126
-5
@@ -4842,7 +4842,7 @@ fn append_bibliography(
|
||||
}
|
||||
|
||||
fn expand_toc(paragraphs: &mut Vec<SourceParagraph>, tocdepth: u32, contents_name: &str) {
|
||||
let entries: Vec<(u8, String)> = paragraphs
|
||||
let entries: Vec<(u8, String, String)> = paragraphs
|
||||
.iter()
|
||||
.filter(|paragraph| {
|
||||
paragraph
|
||||
@@ -4852,11 +4852,12 @@ fn expand_toc(paragraphs: &mut Vec<SourceParagraph>, tocdepth: u32, contents_nam
|
||||
&& paragraph.heading_number.is_some()
|
||||
})
|
||||
.map(|paragraph| {
|
||||
let number = paragraph.heading_number.clone().unwrap_or_default();
|
||||
let text = match (¶graph.heading_short, ¶graph.heading_number) {
|
||||
(Some(short), Some(number)) => format!("{number} {short}"),
|
||||
_ => paragraph.text.clone(),
|
||||
};
|
||||
(paragraph.heading_level.unwrap_or(1), text)
|
||||
(paragraph.heading_level.unwrap_or(1), number, text)
|
||||
})
|
||||
.collect();
|
||||
let Some(position) = paragraphs
|
||||
@@ -4874,9 +4875,9 @@ fn expand_toc(paragraphs: &mut Vec<SourceParagraph>, tocdepth: u32, contents_nam
|
||||
let mut heading = bibliography_heading(contents_name);
|
||||
heading.space_before = space_before;
|
||||
toc.push(heading);
|
||||
for (level, text) in entries.iter() {
|
||||
for (level, number, text) in entries.iter() {
|
||||
toc.push(SourceParagraph {
|
||||
text: text.clone(),
|
||||
text: format!("\\texrratocpage{{{number}}} {text}"),
|
||||
page_break: false,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
@@ -5024,6 +5025,18 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
&geometry.bibliography,
|
||||
geometry.ref_name.as_deref().unwrap_or("References"),
|
||||
);
|
||||
// Table-of-contents entries carry an invisible marker naming the heading
|
||||
// whose page number they show; resolve it once the units exist.
|
||||
let mut toc_targets: Vec<(usize, String)> = Vec::new();
|
||||
for (index, paragraph) in paragraphs.iter_mut().enumerate() {
|
||||
if let Some(rest) = paragraph.text.trim_start().strip_prefix("\\texrratocpage{") {
|
||||
if let Some((number, tail)) = rest.split_once('}') {
|
||||
let number = number.trim().to_string();
|
||||
paragraph.text = tail.trim_start().to_string();
|
||||
toc_targets.push((index, number));
|
||||
}
|
||||
}
|
||||
}
|
||||
let line_width = geometry.line_width.unwrap_or(480 * SCALE);
|
||||
let base_units: Vec<Unit> = paragraphs
|
||||
.iter()
|
||||
@@ -5321,6 +5334,20 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
units.push(Unit::insertion(id, 0, height, footnote.items));
|
||||
}
|
||||
}
|
||||
for (index, number) in toc_targets {
|
||||
let Some(target) = paragraphs
|
||||
.iter()
|
||||
.position(|paragraph| paragraph.heading_number.as_deref() == Some(number.as_str()))
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
if let (Some(entry), Some(target)) = (
|
||||
units.get_mut(unit_for_paragraph[index]),
|
||||
unit_for_paragraph.get(target).copied(),
|
||||
) {
|
||||
entry.toc_target = Some(target as u64);
|
||||
}
|
||||
}
|
||||
let mut document = Document::new(700 * SCALE, units);
|
||||
if footnote_count > 0 {
|
||||
document.insertion_classes.push(InsertionClass {
|
||||
@@ -5424,12 +5451,75 @@ fn stamp_page_numbers(
|
||||
}
|
||||
}
|
||||
|
||||
/// Fill in table-of-contents page numbers from a laid-out engine. Returns
|
||||
/// true when at least one entry changed and the document needs re-layout.
|
||||
fn stamp_toc_pages(document: &mut Document, engine: &Engine, geometry: &PageGeometry) -> bool {
|
||||
let mut page_of = std::collections::HashMap::new();
|
||||
for (index, page) in engine.pages().iter().enumerate() {
|
||||
for (unit, _) in &page.sources {
|
||||
page_of.entry(*unit).or_insert(index);
|
||||
}
|
||||
}
|
||||
let numbering = geometry.page_numbering.as_deref().unwrap_or("arabic");
|
||||
let updates: Vec<(u64, String)> = document
|
||||
.units
|
||||
.iter()
|
||||
.filter(|unit| unit.toc_target.is_some() && !unit.source.contains("\\hfill"))
|
||||
.filter_map(|unit| {
|
||||
let target = unit.toc_target?;
|
||||
let page = page_of.get(&target)?;
|
||||
Some((
|
||||
unit.id,
|
||||
format!(
|
||||
"{} \\hfill {}",
|
||||
unit.source.trim_end(),
|
||||
page_number_text(*page, numbering)
|
||||
),
|
||||
))
|
||||
})
|
||||
.collect();
|
||||
let mut changed = false;
|
||||
for (id, source) in updates {
|
||||
let Some(index) = document.units.iter().position(|unit| unit.id == id) else {
|
||||
continue;
|
||||
};
|
||||
let Some(style) = document.units[index].paragraph_style.clone() else {
|
||||
continue;
|
||||
};
|
||||
let font = document.units[index]
|
||||
.items
|
||||
.iter()
|
||||
.find_map(|item| match item {
|
||||
VerticalItem::Paragraph(paragraph) => Some(paragraph.font),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or(ParagraphFont::Roman);
|
||||
let previous = &document.units[index];
|
||||
let mut replacement = Unit::paragraph_with_style_and_face(id, source, style, font);
|
||||
replacement.space_before = previous.space_before;
|
||||
replacement.space_stretch = previous.space_stretch;
|
||||
replacement.space_shrink = previous.space_shrink;
|
||||
replacement.toc_target = previous.toc_target;
|
||||
document.units[index] = replacement;
|
||||
changed = true;
|
||||
}
|
||||
changed
|
||||
}
|
||||
|
||||
fn render_json(
|
||||
document: &Document,
|
||||
engine: &Engine,
|
||||
edit: Option<(&[usize], usize)>,
|
||||
geometry: &PageGeometry,
|
||||
) -> String {
|
||||
let mut document = document.clone();
|
||||
let relaid = if stamp_toc_pages(&mut document, engine, geometry) {
|
||||
Some(layout_document(&document))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let engine = relaid.as_ref().unwrap_or(engine);
|
||||
let document = &document;
|
||||
let mut json = String::new();
|
||||
let page_width_mm = document.paper_width as f64 / SCALE as f64 * 25.4 / 72.0;
|
||||
let page_height_mm = document.paper_height as f64 / SCALE as f64 * 25.4 / 72.0;
|
||||
@@ -7185,7 +7275,10 @@ A definition.
|
||||
let mut blocks = paragraphs_with_definitions(&body, &[]).expect("paragraphs");
|
||||
number_sections(&mut blocks, 3, 0);
|
||||
expand_toc(&mut blocks, 1, "Contents");
|
||||
let section = blocks.iter().filter(|block| block.text == "1 A").count();
|
||||
let section = blocks
|
||||
.iter()
|
||||
.filter(|block| block.text == "1 A" || block.text.ends_with(" 1 A"))
|
||||
.count();
|
||||
let subsection = blocks.iter().filter(|block| block.text == "1.1 B").count();
|
||||
assert_eq!(section, 2, "heading plus one TOC line");
|
||||
assert_eq!(subsection, 1, "heading only; no TOC line");
|
||||
@@ -7227,6 +7320,34 @@ A definition.
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_of_contents_entries_gain_page_numbers() {
|
||||
let source = "\\tableofcontents\n\n\\section{One}\n\nFirst body paragraph.\n\n\\newpage\n\n\\section{Two}\n\nSecond body paragraph.";
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
let paragraphs = paragraphs(&body).expect("paragraphs");
|
||||
let mut document = build_document(¶graphs, &geometry);
|
||||
apply_geometry(&mut document, &geometry);
|
||||
let engine = layout_document(&document);
|
||||
assert!(stamp_toc_pages(&mut document, &engine, &geometry));
|
||||
let entries: Vec<&Unit> = document
|
||||
.units
|
||||
.iter()
|
||||
.filter(|unit| unit.toc_target.is_some())
|
||||
.collect();
|
||||
assert_eq!(entries.len(), 2, "{entries:?}");
|
||||
assert!(
|
||||
entries[0].source.ends_with("\\hfill 1"),
|
||||
"{}",
|
||||
entries[0].source
|
||||
);
|
||||
assert!(
|
||||
entries[1].source.ends_with("\\hfill 2"),
|
||||
"{}",
|
||||
entries[1].source
|
||||
);
|
||||
assert!(!stamp_toc_pages(&mut document, &engine, &geometry));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_of_contents_lists_numbered_headings() {
|
||||
let source = "\\tableofcontents\n\n\\section{Intro}\n\nText.\n\n\\subsection{Sub}\n\nMore.\n\n\\section*{Notes}\n\nEnd.";
|
||||
|
||||
Reference in new issue
Block a user