Generate table of contents and figure/table lists
This commit is contained in:
1 parent
477e2124c8
commit
4aa1714923
11 files changed
+353
-29
No files matched your search
@@ -273,6 +273,9 @@ pub struct ListItem {
|
||||
pub enum UnresolvedReference {
|
||||
Label(String),
|
||||
Citation(Vec<String>),
|
||||
/// A table-of-contents/list page number for the block with this index.
|
||||
/// The target is resolved to a page number during page layout.
|
||||
TocPage(usize),
|
||||
}
|
||||
|
||||
/// Horizontal alignment of a fixed-width inline box.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
|
||||
use crate::{
|
||||
TeXGlue,
|
||||
TeXGlue, TeXGlueOrder,
|
||||
document::{
|
||||
Block, ColumnAlignment, DocumentModel, EquationNumber, InlineBox, InlineBoxAlign,
|
||||
ListBlock, ListItem, PageMargins, PageSize, PageStyleOverride, ParsedDocument, RuleBox,
|
||||
@@ -382,6 +382,23 @@ struct MinipageBuilder {
|
||||
width: Option<common::Scaled>,
|
||||
}
|
||||
|
||||
/// Marker characters used by placeholders for generated contents lists.
|
||||
const CONTENTS_TOC_MARKER: char = '\u{F6200}';
|
||||
const CONTENTS_FIGURES_MARKER: char = '\u{F6201}';
|
||||
const CONTENTS_TABLES_MARKER: char = '\u{F6202}';
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum ContentsKind {
|
||||
TableOfContents,
|
||||
ListOfFigures,
|
||||
ListOfTables,
|
||||
}
|
||||
|
||||
struct ContentsEntry {
|
||||
runs: Vec<Run>,
|
||||
target: usize,
|
||||
}
|
||||
|
||||
struct InlineBoxSpec {
|
||||
runs: Vec<Run>,
|
||||
width: Option<common::Scaled>,
|
||||
@@ -390,6 +407,41 @@ struct InlineBoxSpec {
|
||||
frame: bool,
|
||||
}
|
||||
|
||||
fn contents_kind(block: &Block) -> Option<ContentsKind> {
|
||||
let Block::Paragraph(runs) = block else {
|
||||
return None;
|
||||
};
|
||||
if runs.len() != 1 {
|
||||
return None;
|
||||
}
|
||||
match runs[0].text.chars().next() {
|
||||
Some(character) if character == CONTENTS_TOC_MARKER => Some(ContentsKind::TableOfContents),
|
||||
Some(character) if character == CONTENTS_FIGURES_MARKER => {
|
||||
Some(ContentsKind::ListOfFigures)
|
||||
}
|
||||
Some(character) if character == CONTENTS_TABLES_MARKER => Some(ContentsKind::ListOfTables),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn contents_line_runs(title: &[Run], target: usize) -> Vec<Run> {
|
||||
let mut runs = title.to_vec();
|
||||
trim_run_end(&mut runs);
|
||||
let mut filler = Run::plain(String::new());
|
||||
filler.glue = Some(TeXGlue {
|
||||
width: common::Scaled::ZERO,
|
||||
stretch: common::Scaled(65536),
|
||||
stretch_order: TeXGlueOrder::Fil,
|
||||
shrink: common::Scaled::ZERO,
|
||||
shrink_order: TeXGlueOrder::Normal,
|
||||
});
|
||||
runs.push(filler);
|
||||
let mut page = Run::plain("00".to_string());
|
||||
page.unresolved_reference = Some(UnresolvedReference::TocPage(target));
|
||||
runs.push(page);
|
||||
runs
|
||||
}
|
||||
|
||||
struct Parser {
|
||||
document: DocumentModel,
|
||||
paragraph: Vec<Run>,
|
||||
@@ -626,6 +678,7 @@ impl Parser {
|
||||
}
|
||||
self.finish_table();
|
||||
self.finish_bibliography();
|
||||
self.finalize_contents();
|
||||
ParsedDocument(self.document)
|
||||
}
|
||||
|
||||
@@ -939,6 +992,15 @@ impl Parser {
|
||||
run.small_caps = self.small_caps;
|
||||
self.target_runs().push(run);
|
||||
}
|
||||
expanded::FrontendEventKind::TableOfContents => {
|
||||
self.push_contents_placeholder(CONTENTS_TOC_MARKER);
|
||||
}
|
||||
expanded::FrontendEventKind::ListOfFigures => {
|
||||
self.push_contents_placeholder(CONTENTS_FIGURES_MARKER);
|
||||
}
|
||||
expanded::FrontendEventKind::ListOfTables => {
|
||||
self.push_contents_placeholder(CONTENTS_TABLES_MARKER);
|
||||
}
|
||||
expanded::FrontendEventKind::GroupStart => self.group_depth += 1,
|
||||
expanded::FrontendEventKind::GroupEnd => {
|
||||
let depth = self.group_depth;
|
||||
@@ -1594,6 +1656,97 @@ impl Parser {
|
||||
}
|
||||
}
|
||||
|
||||
fn push_contents_placeholder(&mut self, marker: char) {
|
||||
self.flush_paragraph();
|
||||
self.push_block(Block::Paragraph(vec![Run::plain(marker.to_string())]));
|
||||
}
|
||||
|
||||
fn finalize_contents(&mut self) {
|
||||
let mut toc_entries = Vec::new();
|
||||
let mut figure_entries = Vec::new();
|
||||
let mut table_entries = Vec::new();
|
||||
for (index, block) in self.document.blocks.iter().enumerate() {
|
||||
match block {
|
||||
Block::Heading { level, runs } if *level <= 3 => {
|
||||
let numbered = runs.first().is_some_and(|run| {
|
||||
run.text
|
||||
.chars()
|
||||
.next()
|
||||
.is_some_and(|character| character.is_ascii_digit())
|
||||
});
|
||||
if numbered {
|
||||
toc_entries.push(ContentsEntry {
|
||||
runs: runs.clone(),
|
||||
target: index,
|
||||
});
|
||||
}
|
||||
}
|
||||
Block::Image {
|
||||
caption: Some(caption),
|
||||
..
|
||||
} => figure_entries.push(ContentsEntry {
|
||||
runs: caption.clone(),
|
||||
target: index,
|
||||
}),
|
||||
Block::Table {
|
||||
caption: Some(caption),
|
||||
..
|
||||
} => table_entries.push(ContentsEntry {
|
||||
runs: caption.clone(),
|
||||
target: index,
|
||||
}),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let has_placeholders = self
|
||||
.document
|
||||
.blocks
|
||||
.iter()
|
||||
.any(|block| contents_kind(block).is_some());
|
||||
if !has_placeholders {
|
||||
return;
|
||||
}
|
||||
let blocks = std::mem::take(&mut self.document.blocks);
|
||||
let sources = std::mem::take(&mut self.document.block_sources);
|
||||
let mut output_index = Vec::with_capacity(blocks.len());
|
||||
let mut next = 0;
|
||||
for block in &blocks {
|
||||
output_index.push(next);
|
||||
next += match contents_kind(block) {
|
||||
Some(ContentsKind::TableOfContents) => 1 + toc_entries.len(),
|
||||
Some(ContentsKind::ListOfFigures) => 1 + figure_entries.len(),
|
||||
Some(ContentsKind::ListOfTables) => 1 + table_entries.len(),
|
||||
None => 1,
|
||||
};
|
||||
}
|
||||
let mut expanded_blocks = Vec::with_capacity(next);
|
||||
let mut expanded_sources = Vec::with_capacity(next);
|
||||
for (index, block) in blocks.into_iter().enumerate() {
|
||||
if let Some(kind) = contents_kind(&block) {
|
||||
let source = sources.get(index).cloned().flatten();
|
||||
let (title, entries) = match kind {
|
||||
ContentsKind::TableOfContents => ("Contents", &toc_entries),
|
||||
ContentsKind::ListOfFigures => ("List of Figures", &figure_entries),
|
||||
ContentsKind::ListOfTables => ("List of Tables", &table_entries),
|
||||
};
|
||||
let mut title_runs = vec![Run::plain(title.to_string())];
|
||||
title_runs[0].bold = true;
|
||||
expanded_blocks.push(Block::Paragraph(title_runs));
|
||||
expanded_sources.push(source.clone());
|
||||
for entry in entries {
|
||||
let target = output_index[entry.target];
|
||||
expanded_blocks.push(Block::Paragraph(contents_line_runs(&entry.runs, target)));
|
||||
expanded_sources.push(source.clone());
|
||||
}
|
||||
} else {
|
||||
expanded_blocks.push(block);
|
||||
expanded_sources.push(sources.get(index).cloned().flatten());
|
||||
}
|
||||
}
|
||||
self.document.blocks = expanded_blocks;
|
||||
self.document.block_sources = expanded_sources;
|
||||
}
|
||||
|
||||
fn flush_paragraph(&mut self) {
|
||||
trim_run_end(&mut self.paragraph);
|
||||
let indent = self.paragraph_indent.take().unwrap_or(common::Scaled::ZERO);
|
||||
|
||||
@@ -245,6 +245,9 @@ pub enum FrontendEventKind {
|
||||
header: bool,
|
||||
},
|
||||
PageNumber,
|
||||
TableOfContents,
|
||||
ListOfFigures,
|
||||
ListOfTables,
|
||||
GroupStart,
|
||||
GroupEnd,
|
||||
}
|
||||
|
||||
@@ -14,7 +14,11 @@ pub fn resolve(parsed: ParsedDocument) -> DocumentModel {
|
||||
let Some(reference) = run.unresolved_reference.take() else {
|
||||
continue;
|
||||
};
|
||||
run.text = match reference {
|
||||
let text = match reference {
|
||||
UnresolvedReference::TocPage(index) => {
|
||||
run.unresolved_reference = Some(UnresolvedReference::TocPage(index));
|
||||
continue;
|
||||
}
|
||||
UnresolvedReference::Label(key) => labels
|
||||
.get(&key)
|
||||
.cloned()
|
||||
@@ -33,6 +37,7 @@ pub fn resolve(parsed: ParsedDocument) -> DocumentModel {
|
||||
format!("[{formatted}]")
|
||||
}
|
||||
};
|
||||
run.text = text;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user