Expand tableofcontents into numbered heading lines
This commit is contained in:
1 parent
f48b10340a
commit
b2f5b67b07
5 files changed
+125
-4
No files matched your search
@@ -858,6 +858,10 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if trimmed == "\\tableofcontents" {
|
||||
body.push_str("\\texrratoc\n\n");
|
||||
continue;
|
||||
}
|
||||
if trimmed.starts_with("\\newtheorem") {
|
||||
if let Some(definition) = newtheorem_definition(trimmed) {
|
||||
geometry.theorem_definitions.push(definition);
|
||||
@@ -3761,9 +3765,57 @@ fn append_bibliography(paragraphs: &mut Vec<SourceParagraph>, bibliography: &[(S
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_toc(paragraphs: &mut Vec<SourceParagraph>) {
|
||||
let entries: Vec<(u8, String)> = paragraphs
|
||||
.iter()
|
||||
.filter(|paragraph| {
|
||||
paragraph.heading_level.is_some()
|
||||
&& !paragraph.heading_starred
|
||||
&& paragraph.heading_number.is_some()
|
||||
})
|
||||
.map(|paragraph| (paragraph.heading_level.unwrap_or(1), paragraph.text.clone()))
|
||||
.collect();
|
||||
let Some(position) = paragraphs
|
||||
.iter()
|
||||
.position(|paragraph| paragraph.text.trim() == "\\texrratoc")
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let space_before = paragraphs[position].space_before;
|
||||
let mut toc = Vec::with_capacity(entries.len());
|
||||
for (index, (level, text)) in entries.iter().enumerate() {
|
||||
toc.push(SourceParagraph {
|
||||
text: text.clone(),
|
||||
page_break: false,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
math: false,
|
||||
left_skip: 12 * (level.saturating_sub(1) as i64),
|
||||
space_before: if index == 0 { space_before } else { 2 * SCALE },
|
||||
space_stretch: 0,
|
||||
space_shrink: 0,
|
||||
equation_number: None,
|
||||
equation_tag: None,
|
||||
equation_rows: Vec::new(),
|
||||
align_centered: false,
|
||||
heading_level: None,
|
||||
heading_starred: false,
|
||||
heading_number: None,
|
||||
font_size: None,
|
||||
footnotes: Vec::new(),
|
||||
float: None,
|
||||
theorem: None,
|
||||
});
|
||||
}
|
||||
paragraphs.splice(position..=position, toc);
|
||||
}
|
||||
|
||||
fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Document {
|
||||
let mut paragraphs = expand_maketitle(paragraphs, geometry);
|
||||
let section_labels = number_sections(&mut paragraphs);
|
||||
expand_toc(&mut paragraphs);
|
||||
let mut references = section_labels;
|
||||
references.extend(number_theorems(
|
||||
&mut paragraphs,
|
||||
@@ -4986,6 +5038,52 @@ A definition.
|
||||
plain_y
|
||||
);
|
||||
}
|
||||
#[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.";
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
let paragraphs =
|
||||
paragraphs_with_definitions(&body, &geometry.theorem_definitions).expect("blocks");
|
||||
let document = build_document(¶graphs, &geometry);
|
||||
let sources: Vec<&str> = document
|
||||
.units
|
||||
.iter()
|
||||
.map(|unit| unit.source.as_str())
|
||||
.collect();
|
||||
assert_eq!(
|
||||
sources
|
||||
.iter()
|
||||
.filter(|source| **source == "1 Intro")
|
||||
.count(),
|
||||
2,
|
||||
"{sources:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
sources
|
||||
.iter()
|
||||
.filter(|source| **source == "1.1 Sub")
|
||||
.count(),
|
||||
2,
|
||||
"{sources:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
sources.iter().filter(|source| **source == "Notes").count(),
|
||||
1,
|
||||
"{sources:?}"
|
||||
);
|
||||
assert!(!sources.iter().any(|source| source.contains("texrratoc")));
|
||||
assert_eq!(document.units[0].source, "1 Intro");
|
||||
assert_eq!(document.units[1].source, "1.1 Sub");
|
||||
assert_eq!(
|
||||
document.units[1]
|
||||
.paragraph_style
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.left_skip,
|
||||
12 * 65_536
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn input_directives_expand_from_the_reader() {
|
||||
let source = "Before \\input{section} after \\input{nested}";
|
||||
|
||||
Reference in new issue
Block a user