Place standalone rule paragraphs as rule boxes

This commit is contained in:
gloamxun committed 2026-10-25 01:48:17 +08:00
1 parent 4d71094fba
commit d2497dd079
4 files changed
+62 -4

No files matched your search

+58
View File
@@ -3372,6 +3372,42 @@ fn verse_block(raw: &str) -> Option<Vec<SourceParagraph>> {
}
}
fn rule_block(raw: &str) -> Option<SourceParagraph> {
let rest = raw.trim().strip_prefix("\\rule")?;
let mut rest = rest.trim_start();
if rest.starts_with('[') {
let close = rest.find(']')?;
rest = rest[close + 1..].trim_start();
}
let (width, consumed) = braced_group(rest)?;
let width = parse_dimension(width.trim())?;
rest = rest[consumed..].trim_start();
let (height, consumed) = braced_group(rest)?;
let height = parse_dimension(height.trim())?;
if !rest[consumed..].trim().is_empty() {
return None;
}
let mut block = parse_paragraph("");
block.float = Some(FloatSpec {
kind: FloatKind::Figure,
placement: FloatPlacement::Here,
width: Some(ImageLength::Absolute(width)),
height: Some(ImageLength::Absolute(height)),
scale: None,
keep_aspectratio: false,
caption: None,
caption_short: None,
list_caption: None,
caption_starred: false,
image: None,
table: None,
});
block.space_before = 6 * SCALE;
block.space_stretch = 0;
block.space_shrink = 0;
Some(block)
}
fn parse_blocks_raw(
raw: &str,
definitions: &[TheoremDefinition],
@@ -3383,6 +3419,9 @@ fn parse_blocks_raw(
if let Some(block) = standalone_graphic(raw) {
return vec![block];
}
if let Some(block) = rule_block(raw) {
return vec![block];
}
if let Some(blocks) = theorem_block(raw, definitions) {
return blocks;
}
@@ -6887,6 +6926,25 @@ See \ref{fig:a} and \ref{tab:b}.";
assert_eq!(blocks[1].space_before, 5, "12pt itemsep");
}
#[test]
fn rule_paragraphs_become_rule_boxes() {
let blocks = paragraphs("\\rule{20pt}{5pt}").expect("rule");
assert_eq!(blocks.len(), 1);
let spec = blocks[0].float.as_ref().expect("rule float");
assert_eq!(spec.width, Some(ImageLength::Absolute(20 * SCALE)));
assert_eq!(spec.height, Some(ImageLength::Absolute(5 * SCALE)));
assert!(spec.caption.is_none());
let document = build_document(&blocks, &PageGeometry::default());
let Some(VerticalItem::Float(float)) = document.units[0].items.first() else {
panic!("expected float rule unit");
};
assert!(matches!(
float.content.first(),
Some(VerticalItem::Rule { width, height }) if *width == 20 * SCALE && *height == 5 * SCALE
));
}
#[test]
fn verse_keeps_its_line_breaks() {
let blocks =