Indent quotation blocks on both margins
This commit is contained in:
1 parent
a9ab265be4
commit
7dd94d7fbe
4 files changed
+139
-8
No files matched your search
+135
-4
@@ -2018,6 +2018,8 @@ struct SourceParagraph {
|
||||
list_depth: Option<u8>,
|
||||
/// Width of the generated list label, in points.
|
||||
list_label: i64,
|
||||
/// Extra right margin in points, used by quotation blocks.
|
||||
right_skip: i64,
|
||||
}
|
||||
|
||||
/// The kind of a LaTeX float environment.
|
||||
@@ -2224,14 +2226,54 @@ fn paragraphs_with_geometry(
|
||||
)
|
||||
}
|
||||
|
||||
/// Blank-line splitter that keeps environments which may contain blank lines
|
||||
/// (quotation, quote, verse, abstract) in one chunk.
|
||||
fn split_display_chunks(source: &str) -> Vec<&str> {
|
||||
const OPENINGS: [&str; 4] = [
|
||||
"\\begin{quotation}",
|
||||
"\\begin{quote}",
|
||||
"\\begin{verse}",
|
||||
"\\begin{abstract}",
|
||||
];
|
||||
fn depth(text: &str) -> i64 {
|
||||
let mut depth = 0;
|
||||
for opening in OPENINGS {
|
||||
let closing = opening.replacen("\\begin", "\\end", 1);
|
||||
depth += text.matches(opening).count() as i64;
|
||||
depth -= text.matches(&closing).count() as i64;
|
||||
}
|
||||
depth
|
||||
}
|
||||
let mut chunks = Vec::new();
|
||||
let mut start = 0;
|
||||
let bytes = source.as_bytes();
|
||||
let mut index = 0;
|
||||
while index + 1 < bytes.len() {
|
||||
if bytes[index] == b'\n' && bytes[index + 1] == b'\n' {
|
||||
let chunk = &source[start..index];
|
||||
if depth(chunk) <= 0 {
|
||||
chunks.push(chunk);
|
||||
start = index + 2;
|
||||
}
|
||||
index += 2;
|
||||
} else {
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
if start < source.len() {
|
||||
chunks.push(&source[start..]);
|
||||
}
|
||||
chunks
|
||||
}
|
||||
|
||||
fn paragraphs_with_spacing(
|
||||
source: &str,
|
||||
definitions: &[TheoremDefinition],
|
||||
equation_start: u32,
|
||||
spacing: ListSpacing,
|
||||
) -> Result<Vec<SourceParagraph>, String> {
|
||||
let paragraphs: Vec<&str> = source
|
||||
.split("\n\n")
|
||||
let paragraphs: Vec<&str> = split_display_chunks(source)
|
||||
.into_iter()
|
||||
.map(str::trim)
|
||||
.filter(|paragraph| !paragraph.is_empty())
|
||||
.collect();
|
||||
@@ -2246,6 +2288,7 @@ fn paragraphs_with_spacing(
|
||||
result.push(SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: format!("\\texrratable{{{index}}}"),
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
@@ -2430,6 +2473,7 @@ fn float_block(raw: &str) -> Option<SourceParagraph> {
|
||||
Some(SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: String::new(),
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
@@ -2745,6 +2789,7 @@ fn theorem_block(raw: &str, definitions: &[TheoremDefinition]) -> Option<Vec<Sou
|
||||
let heading = SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: format!("{display}."),
|
||||
font: ParagraphFont::Bold,
|
||||
line_height: 14,
|
||||
@@ -2783,6 +2828,7 @@ fn theorem_block(raw: &str, definitions: &[TheoremDefinition]) -> Option<Vec<Sou
|
||||
let statement = SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: body,
|
||||
font: if italic {
|
||||
ParagraphFont::Italic
|
||||
@@ -3105,6 +3151,7 @@ fn list_paragraph(
|
||||
font_size: None,
|
||||
list_depth: Some(depth as u8),
|
||||
list_label: label_width,
|
||||
right_skip: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3121,6 +3168,7 @@ fn verbatim_blocks(raw: &str) -> Option<Vec<SourceParagraph>> {
|
||||
blocks.push(SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: text.to_string(),
|
||||
page_break: false,
|
||||
centered: false,
|
||||
@@ -3255,6 +3303,7 @@ fn standalone_graphic(raw: &str) -> Option<SourceParagraph> {
|
||||
Some(SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: String::new(),
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
@@ -3316,6 +3365,7 @@ fn parse_blocks_raw(
|
||||
let heading = SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: "Abstract".to_string(),
|
||||
font: ParagraphFont::Bold,
|
||||
line_height: 12,
|
||||
@@ -3343,6 +3393,7 @@ fn parse_blocks_raw(
|
||||
let body = SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: body.trim().to_string(),
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
@@ -3400,6 +3451,25 @@ fn parse_blocks_raw(
|
||||
if let Some(blocks) = list_blocks(raw, 0, definitions, spacing) {
|
||||
return blocks;
|
||||
}
|
||||
if let Some(body) = raw
|
||||
.strip_prefix("\\begin{quotation}")
|
||||
.and_then(|text| text.strip_suffix("\\end{quotation}"))
|
||||
{
|
||||
let mut blocks = Vec::new();
|
||||
for paragraph in body.trim().split("\n\n") {
|
||||
let text = paragraph.trim();
|
||||
if text.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let mut block = parse_paragraph(text);
|
||||
block.left_skip = 20;
|
||||
block.right_skip = 20;
|
||||
blocks.push(block);
|
||||
}
|
||||
if !blocks.is_empty() {
|
||||
return blocks;
|
||||
}
|
||||
}
|
||||
if let Some(body) = raw
|
||||
.strip_prefix("\\begin{quote}")
|
||||
.and_then(|text| text.strip_suffix("\\end{quote}"))
|
||||
@@ -3413,7 +3483,8 @@ fn parse_blocks_raw(
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
math: false,
|
||||
left_skip: 12,
|
||||
left_skip: 20,
|
||||
right_skip: 20,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
space_before: 0,
|
||||
@@ -4186,6 +4257,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
return SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: inner.trim().to_string(),
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
@@ -4216,6 +4288,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
return SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: body,
|
||||
font,
|
||||
font_size,
|
||||
@@ -4248,6 +4321,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
return SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: inner.trim().to_string(),
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
@@ -4280,6 +4354,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
return SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: inner.trim().to_string(),
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
@@ -4316,6 +4391,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
return SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: math.trim().to_string(),
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
@@ -4345,6 +4421,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
return SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: title,
|
||||
font: ParagraphFont::Bold,
|
||||
line_height: if level == 1 {
|
||||
@@ -4394,6 +4471,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
return SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: text.to_string(),
|
||||
font,
|
||||
line_height,
|
||||
@@ -4423,6 +4501,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: raw.to_string(),
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
@@ -4604,6 +4683,7 @@ fn title_block(text: String, size: f64, space_before: i64) -> SourceParagraph {
|
||||
SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text,
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
@@ -4975,6 +5055,7 @@ fn bibliography_heading(text: &str) -> SourceParagraph {
|
||||
SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: text.to_string(),
|
||||
page_break: false,
|
||||
centered: false,
|
||||
@@ -5014,6 +5095,7 @@ fn append_bibliography(
|
||||
paragraphs.push(SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: format!("[{}] {entry}", index + 1),
|
||||
page_break: false,
|
||||
centered: false,
|
||||
@@ -5079,6 +5161,7 @@ fn expand_toc(paragraphs: &mut Vec<SourceParagraph>, tocdepth: u32, contents_nam
|
||||
toc.push(SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: format!("\\texrratocpage{{{number}}} {text}"),
|
||||
page_break: false,
|
||||
centered: false,
|
||||
@@ -5161,6 +5244,7 @@ fn expand_float_lists(
|
||||
list.push(SourceParagraph {
|
||||
list_depth: None,
|
||||
list_label: 0,
|
||||
right_skip: 0,
|
||||
text: format!("\\texrrafloatpage{{{prefix}}} {text}"),
|
||||
page_break: false,
|
||||
centered: false,
|
||||
@@ -5456,7 +5540,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
right_aligned: paragraph.right_aligned,
|
||||
looseness: geometry.looseness.unwrap_or(0),
|
||||
ragged_right: false,
|
||||
right_skip: geometry.right_skip.unwrap_or(0),
|
||||
right_skip: geometry.right_skip.unwrap_or(0) + paragraph.right_skip * SCALE,
|
||||
hang_indent: geometry.hang_indent.unwrap_or(0),
|
||||
hang_after: geometry.hang_after.unwrap_or(1) as i32,
|
||||
par_shape: geometry.par_shape.clone(),
|
||||
@@ -6720,6 +6804,53 @@ See \ref{fig:a} and \ref{tab:b}.";
|
||||
assert_eq!(blocks[1].space_before, 5, "12pt itemsep");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quotation_indents_both_margins() {
|
||||
let source = "\\begin{quotation}\nOne paragraph.\n\nTwo paragraph.\n\\end{quotation}";
|
||||
let blocks = paragraphs(source).expect("paragraphs");
|
||||
assert_eq!(blocks.len(), 2);
|
||||
assert!(
|
||||
blocks
|
||||
.iter()
|
||||
.all(|block| block.left_skip == 20 && block.right_skip == 20)
|
||||
);
|
||||
assert!(blocks[0].text.starts_with("One"));
|
||||
assert!(blocks[1].text.starts_with("Two"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quotation_narrows_the_measure() {
|
||||
let long = "alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu nu xi omicron pi rho sigma tau upsilon phi chi psi omega";
|
||||
let plain = paragraphs(long).expect("paragraphs");
|
||||
let indented = paragraphs(&format!("\\begin{{quotation}}\n{long}\n\\end{{quotation}}"))
|
||||
.expect("paragraphs");
|
||||
let geometry = PageGeometry {
|
||||
line_width: Some(200 * SCALE),
|
||||
..PageGeometry::default()
|
||||
};
|
||||
let plain_document = build_document(&plain, &geometry);
|
||||
let quote_document = build_document(&indented, &geometry);
|
||||
let lines = |document: &Document| {
|
||||
document.units[0]
|
||||
.items
|
||||
.iter()
|
||||
.find_map(|item| match item {
|
||||
VerticalItem::Paragraph(paragraph) => Some(paragraph.lines.len()),
|
||||
_ => None,
|
||||
})
|
||||
.expect("paragraph")
|
||||
};
|
||||
assert!(lines("e_document) > lines(&plain_document));
|
||||
assert_eq!(
|
||||
quote_document.units[0]
|
||||
.paragraph_style
|
||||
.as_ref()
|
||||
.expect("style")
|
||||
.right_skip,
|
||||
20 * SCALE
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_items_hang_their_continuation_lines() {
|
||||
let source = "\\begin{itemize}\n\\item alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu nu xi omicron\n\\end{itemize}";
|
||||
|
||||
Reference in new issue
Block a user