Render itemize lists in the route-D preview
This commit is contained in:
1 parent
63593e7ff3
commit
cea9ccddef
4 files changed
+50
-8
No files matched your search
+30
-5
@@ -81,6 +81,7 @@ struct SourceParagraph {
|
||||
font: ParagraphFont,
|
||||
line_height: i64,
|
||||
math: bool,
|
||||
left_skip: i64,
|
||||
}
|
||||
|
||||
fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
|
||||
@@ -92,10 +93,7 @@ fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
|
||||
if paragraphs.is_empty() {
|
||||
return Err("route-D preview needs at least one paragraph".to_string());
|
||||
}
|
||||
Ok(paragraphs
|
||||
.into_iter()
|
||||
.map(|paragraph| parse_paragraph(paragraph))
|
||||
.collect())
|
||||
Ok(paragraphs.into_iter().flat_map(parse_blocks).collect())
|
||||
}
|
||||
|
||||
fn command_body<'a>(text: &'a str, command: &str) -> Option<&'a str> {
|
||||
@@ -104,6 +102,30 @@ fn command_body<'a>(text: &'a str, command: &str) -> Option<&'a str> {
|
||||
Some(rest.trim())
|
||||
}
|
||||
|
||||
fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
|
||||
if let Some(body) = raw
|
||||
.strip_prefix("\\begin{itemize}")
|
||||
.and_then(|text| text.strip_suffix("\\end{itemize}"))
|
||||
{
|
||||
let items: Vec<SourceParagraph> = body
|
||||
.split("\\item")
|
||||
.map(str::trim)
|
||||
.filter(|item| !item.is_empty())
|
||||
.map(|item| SourceParagraph {
|
||||
text: format!("• {item}"),
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
math: false,
|
||||
left_skip: 12,
|
||||
})
|
||||
.collect();
|
||||
if !items.is_empty() {
|
||||
return items;
|
||||
}
|
||||
}
|
||||
vec![parse_paragraph(raw)]
|
||||
}
|
||||
|
||||
/// Map a small, general LaTeX subset onto route-D paragraph faces. This is not
|
||||
/// a full LaTeX reader; it exists so the live endpoint can show headings and
|
||||
/// emphasis while the semantic layer is built out.
|
||||
@@ -121,6 +143,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
math: true,
|
||||
left_skip: 0,
|
||||
};
|
||||
}
|
||||
let candidates = [
|
||||
@@ -135,6 +158,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
font,
|
||||
line_height,
|
||||
math: false,
|
||||
left_skip: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -143,6 +167,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
font: ParagraphFont::Roman,
|
||||
line_height: 12,
|
||||
math: false,
|
||||
left_skip: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,7 +185,7 @@ fn build_document(paragraphs: &[SourceParagraph]) -> Document {
|
||||
}
|
||||
let style = ParagraphStyle {
|
||||
line_width: 480 * SCALE,
|
||||
left_skip: 0,
|
||||
left_skip: paragraph.left_skip * SCALE,
|
||||
right_skip: 0,
|
||||
par_indent: 0,
|
||||
line_height: paragraph.line_height * SCALE,
|
||||
|
||||
Reference in new issue
Block a user