Add paragraph indentation and hfill

This commit is contained in:
gloamxun committed 2026-10-19 12:43:30 +08:00
1 parent bad477fbee
commit e44d4c8669
11 files changed
+362 -42

No files matched your search

+112 -5
View File
@@ -407,6 +407,8 @@ struct Parser {
centered: Option<Vec<Run>>,
current_source: Option<SourceLocation>,
paragraph_source: Option<SourceLocation>,
paragraph_indent: Option<common::Scaled>,
suppress_indent: bool,
}
impl Parser {
@@ -444,6 +446,8 @@ impl Parser {
centered: None,
current_source: None,
paragraph_source: None,
paragraph_indent: None,
suppress_indent: false,
}
}
@@ -467,9 +471,12 @@ impl Parser {
if let Some(candidate) =
rich_paragraph_candidate(expanded, item_index, self.document.blocks.len())
{
if let Some(runs) =
if let Some(mut runs) =
paragraph_cache.get_rich(&candidate.key, &candidate.math_expressions)
{
self.begin_paragraph_if_needed();
let indent = self.paragraph_indent.take().unwrap_or(common::Scaled::ZERO);
prepend_paragraph_indent(&mut runs, indent);
self.push_block_at(Block::Paragraph(runs), Some(candidate.source));
self.current_source = Some(candidate.end_source);
item_index = candidate.end_index;
@@ -494,8 +501,12 @@ impl Parser {
if !pending_text.is_empty() {
self.current_source = pending_source.take();
if paragraph_break && self.can_reuse_plain_paragraph() {
let runs = paragraph_cache.parse(&pending_text);
let mut runs = paragraph_cache.parse(&pending_text);
if !runs.is_empty() {
self.begin_paragraph_if_needed();
let indent =
self.paragraph_indent.take().unwrap_or(common::Scaled::ZERO);
prepend_paragraph_indent(&mut runs, indent);
self.push_block_at(
Block::Paragraph(runs),
self.current_source.clone(),
@@ -535,7 +546,9 @@ impl Parser {
if let Some(Block::Paragraph(runs)) =
self.document.blocks.get(candidate.block_index)
{
paragraph_cache.insert_rich(candidate.key, runs.clone());
let mut cached = runs.clone();
strip_paragraph_indent(&mut cached);
paragraph_cache.insert_rich(candidate.key, cached);
}
}
cache_scan_pending = true;
@@ -555,7 +568,9 @@ impl Parser {
&& candidate.end_index == expanded.items.len()
&& let Some(Block::Paragraph(runs)) = self.document.blocks.get(candidate.block_index)
{
paragraph_cache.insert_rich(candidate.key, runs.clone());
let mut cached = runs.clone();
strip_paragraph_indent(&mut cached);
paragraph_cache.insert_rich(candidate.key, cached);
}
while !self.lists.is_empty() {
self.finish_list();
@@ -656,6 +671,7 @@ impl Parser {
expanded::FrontendEventKind::MakeTitle => {
self.flush_paragraph();
self.document.show_title = true;
self.suppress_indent = true;
}
expanded::FrontendEventKind::InlineBoxStart => {
self.inline_boxes.push(Vec::new());
@@ -675,6 +691,8 @@ impl Parser {
bold: false,
italic: false,
monospace: false,
paragraph_indent: false,
};
self.target_runs().push(run);
}
@@ -872,6 +890,14 @@ impl Parser {
self.start_heading(*level, *numbered)
}
expanded::FrontendEventKind::HeadingEnd => self.finish_heading(),
expanded::FrontendEventKind::Parindent(width) => {
self.document.parindent = *width;
}
expanded::FrontendEventKind::NoIndent => {
if self.paragraph.is_empty() {
self.suppress_indent = true;
}
}
expanded::FrontendEventKind::CaptionStart => {
self.flush_paragraph();
self.caption_capture = Some(Vec::new());
@@ -931,6 +957,8 @@ impl Parser {
bold,
italic: true,
monospace: false,
paragraph_indent: false,
});
}
@@ -963,10 +991,14 @@ impl Parser {
},
heading.source,
);
self.suppress_indent = true;
}
}
fn append_text(&mut self, text: &str) {
if normalize_whitespace(text).is_empty() {
return;
}
if !text.is_empty()
&& self.paragraph.is_empty()
&& self.heading.is_none()
@@ -1043,6 +1075,8 @@ impl Parser {
bold,
italic,
monospace,
paragraph_indent: false,
});
}
@@ -1071,6 +1105,8 @@ impl Parser {
bold,
italic,
monospace,
paragraph_indent: false,
});
}
@@ -1104,6 +1140,8 @@ impl Parser {
bold,
italic,
monospace,
paragraph_indent: false,
});
}
@@ -1147,10 +1185,27 @@ impl Parser {
bold,
italic,
monospace,
paragraph_indent: false,
});
}
fn target_runs(&mut self) -> &mut Vec<Run> {
let paragraph_target = self.inline_boxes.is_empty()
&& self.centered.is_none()
&& self.framed_box.is_none()
&& self.metadata_capture.is_none()
&& self.caption_capture.is_none()
&& self.heading.is_none()
&& self.table.is_none()
&& self.lists.is_empty()
&& !self
.bibliography
.as_ref()
.is_some_and(|bibliography| bibliography.current.is_some());
if paragraph_target {
self.begin_paragraph_if_needed();
}
if let Some(runs) = self.inline_boxes.last_mut() {
runs
} else if let Some(runs) = &mut self.centered {
@@ -1178,6 +1233,23 @@ impl Parser {
}
}
fn begin_paragraph_if_needed(&mut self) {
if !self.paragraph.is_empty() {
return;
}
// Empty appends (for example whitespace-only tokens or macro-definition
// text in the prelude) must not freeze the indentation of the paragraph
// that eventually receives the first real run. Re-evaluate until a run
// actually lands in the paragraph.
self.paragraph_indent = Some(if self.suppress_indent {
common::Scaled::ZERO
} else {
self.document.parindent
});
self.suppress_indent = false;
self.paragraph_source = self.current_source.clone();
}
fn append_literal_ampersand(&mut self) {
let bold = self.bold;
let italic = self.italic;
@@ -1214,6 +1286,8 @@ impl Parser {
bold,
italic,
monospace,
paragraph_indent: false,
});
}
@@ -1227,8 +1301,10 @@ impl Parser {
fn flush_paragraph(&mut self) {
trim_run_end(&mut self.paragraph);
let indent = self.paragraph_indent.take().unwrap_or(common::Scaled::ZERO);
if !self.paragraph.is_empty() {
let runs = std::mem::take(&mut self.paragraph);
let mut runs = std::mem::take(&mut self.paragraph);
prepend_paragraph_indent(&mut runs, indent);
let source = self.paragraph_source.take();
self.push_block_at(Block::Paragraph(runs), source);
} else {
@@ -1523,6 +1599,35 @@ pub fn parse_document(
Parser::new().parse(source, paragraph_cache)
}
fn indent_run(indent: common::Scaled) -> Run {
Run {
text: String::new(),
math: None,
kern: Some(indent),
glue: None,
penalty: None,
rule: None,
box_run: None,
unresolved_reference: None,
bold: false,
italic: false,
monospace: false,
paragraph_indent: true,
}
}
fn prepend_paragraph_indent(runs: &mut Vec<Run>, indent: common::Scaled) {
if indent.0 > 0 {
runs.insert(0, indent_run(indent));
}
}
fn strip_paragraph_indent(runs: &mut Vec<Run>) {
if runs.first().is_some_and(|run| run.paragraph_indent) {
runs.remove(0);
}
}
fn append_run(runs: &mut Vec<Run>, mut text: String, bold: bool, italic: bool, monospace: bool) {
if text.is_empty() {
return;
@@ -1560,6 +1665,8 @@ fn append_run(runs: &mut Vec<Run>, mut text: String, bold: bool, italic: bool, m
bold,
italic,
monospace,
paragraph_indent: false,
});
}