Apply cargo fmt
This commit is contained in:
+64
-20
@@ -44,12 +44,25 @@ pub type Cell = Vec<Span>;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Block {
|
||||
Heading { level: u8, spans: Vec<Span> },
|
||||
Heading {
|
||||
level: u8,
|
||||
spans: Vec<Span>,
|
||||
},
|
||||
Paragraph(Vec<Span>),
|
||||
List { indent: usize, marker: Marker, spans: Vec<Span> },
|
||||
List {
|
||||
indent: usize,
|
||||
marker: Marker,
|
||||
spans: Vec<Span>,
|
||||
},
|
||||
Quote(Vec<Span>),
|
||||
Code { language: String, text: String },
|
||||
Table { header: Vec<Cell>, rows: Vec<Vec<Cell>> },
|
||||
Code {
|
||||
language: String,
|
||||
text: String,
|
||||
},
|
||||
Table {
|
||||
header: Vec<Cell>,
|
||||
rows: Vec<Vec<Cell>>,
|
||||
},
|
||||
Rule,
|
||||
}
|
||||
|
||||
@@ -129,9 +142,7 @@ pub fn parse(source: &str) -> Vec<Block> {
|
||||
}
|
||||
|
||||
// A pipe table: a header row, a `| --- |` separator, then the body.
|
||||
if trimmed.contains('|')
|
||||
&& lines.peek().is_some_and(|next| is_separator_row(next))
|
||||
{
|
||||
if trimmed.contains('|') && lines.peek().is_some_and(|next| is_separator_row(next)) {
|
||||
flush(&mut paragraph, &mut blocks, false);
|
||||
flush(&mut quote, &mut blocks, true);
|
||||
let header = split_row(trimmed);
|
||||
@@ -218,7 +229,8 @@ fn heading_of(line: &str) -> Option<(u8, &str)> {
|
||||
if (1..=6).contains(&hashes) {
|
||||
let rest = &line[hashes..];
|
||||
// ATX headings need a space after the hashes.
|
||||
rest.strip_prefix(' ').map(|rest| (hashes as u8, rest.trim()))
|
||||
rest.strip_prefix(' ')
|
||||
.map(|rest| (hashes as u8, rest.trim()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -237,12 +249,18 @@ fn list_item_of(line: &str) -> Option<(usize, Marker, &str)> {
|
||||
let rest_trimmed = rest.trim_start();
|
||||
let lowered = rest_trimmed.to_ascii_lowercase();
|
||||
if let Some(task) = lowered.strip_prefix("[ ] ") {
|
||||
(Marker::Task(false), &rest_trimmed[rest_trimmed.len() - task.len()..])
|
||||
(
|
||||
Marker::Task(false),
|
||||
&rest_trimmed[rest_trimmed.len() - task.len()..],
|
||||
)
|
||||
} else if let Some(task) = lowered
|
||||
.strip_prefix("[x] ")
|
||||
.or_else(|| lowered.strip_prefix("[/] "))
|
||||
{
|
||||
(Marker::Task(true), &rest_trimmed[rest_trimmed.len() - task.len()..])
|
||||
(
|
||||
Marker::Task(true),
|
||||
&rest_trimmed[rest_trimmed.len() - task.len()..],
|
||||
)
|
||||
} else {
|
||||
(Marker::Bullet, rest)
|
||||
}
|
||||
@@ -252,7 +270,9 @@ fn list_item_of(line: &str) -> Option<(usize, Marker, &str)> {
|
||||
return None;
|
||||
}
|
||||
let rest = &trimmed[digits.len()..];
|
||||
let rest = rest.strip_prefix(". ").or_else(|| rest.strip_prefix(") "))?;
|
||||
let rest = rest
|
||||
.strip_prefix(". ")
|
||||
.or_else(|| rest.strip_prefix(") "))?;
|
||||
(Marker::Number(digits.parse().ok()?), rest)
|
||||
};
|
||||
|
||||
@@ -304,7 +324,10 @@ fn parse_inline_into(text: &str, style: Style, out: &mut Vec<Span>) {
|
||||
flush!();
|
||||
out.push(Span {
|
||||
text: inner.to_owned(),
|
||||
style: Style { code: true, ..style },
|
||||
style: Style {
|
||||
code: true,
|
||||
..style
|
||||
},
|
||||
link: None,
|
||||
image: false,
|
||||
});
|
||||
@@ -314,21 +337,42 @@ fn parse_inline_into(text: &str, style: Style, out: &mut Vec<Span>) {
|
||||
|
||||
if let Some((inner, tail)) = delimited(rest, "**").or_else(|| delimited(rest, "__")) {
|
||||
flush!();
|
||||
parse_inline_into(inner, Style { bold: true, ..style }, out);
|
||||
parse_inline_into(
|
||||
inner,
|
||||
Style {
|
||||
bold: true,
|
||||
..style
|
||||
},
|
||||
out,
|
||||
);
|
||||
rest = tail;
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some((inner, tail)) = delimited(rest, "~~") {
|
||||
flush!();
|
||||
parse_inline_into(inner, Style { strike: true, ..style }, out);
|
||||
parse_inline_into(
|
||||
inner,
|
||||
Style {
|
||||
strike: true,
|
||||
..style
|
||||
},
|
||||
out,
|
||||
);
|
||||
rest = tail;
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some((inner, tail)) = delimited(rest, "*").or_else(|| delimited(rest, "_")) {
|
||||
flush!();
|
||||
parse_inline_into(inner, Style { italic: true, ..style }, out);
|
||||
parse_inline_into(
|
||||
inner,
|
||||
Style {
|
||||
italic: true,
|
||||
..style
|
||||
},
|
||||
out,
|
||||
);
|
||||
rest = tail;
|
||||
continue;
|
||||
}
|
||||
@@ -540,10 +584,8 @@ pub fn render(ui: &mut Ui, blocks: &[Block], palette: &Palette, base_size: f32)
|
||||
Block::Rule => {
|
||||
let stroke = egui::Stroke::new(1.0, palette.border);
|
||||
let width = ui.available_width();
|
||||
let (rect, _) = ui.allocate_exact_size(
|
||||
egui::vec2(width, base_size * 0.6),
|
||||
egui::Sense::hover(),
|
||||
);
|
||||
let (rect, _) = ui
|
||||
.allocate_exact_size(egui::vec2(width, base_size * 0.6), egui::Sense::hover());
|
||||
ui.painter().line_segment(
|
||||
[
|
||||
egui::pos2(rect.left(), rect.center().y),
|
||||
@@ -712,7 +754,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn parses_pipe_tables() {
|
||||
let blocks = parse("| Key | What it does |\n| --- | :---: |\n| `a` | first |\n| b | second |\n\nafter");
|
||||
let blocks = parse(
|
||||
"| Key | What it does |\n| --- | :---: |\n| `a` | first |\n| b | second |\n\nafter",
|
||||
);
|
||||
let Block::Table { header, rows } = &blocks[0] else {
|
||||
panic!("expected a table, got {blocks:?}");
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user