block: minimize work when determining block
This commit is contained in:
parent
e55e7a129f
commit
3e4bb127c7
1 changed files with 35 additions and 26 deletions
61
src/block.rs
61
src/block.rs
|
@ -1079,21 +1079,24 @@ impl<'s> IdentifiedBlock<'s> {
|
||||||
impl<'s> Kind<'s> {
|
impl<'s> Kind<'s> {
|
||||||
/// Determine if a line continues the block.
|
/// Determine if a line continues the block.
|
||||||
fn continues(&mut self, line: &'s str) -> bool {
|
fn continues(&mut self, line: &'s str) -> bool {
|
||||||
let IdentifiedBlock { kind: next, .. } = IdentifiedBlock::new(line);
|
|
||||||
match self {
|
match self {
|
||||||
Self::Atom(..)
|
Self::Atom(..)
|
||||||
| Self::Fenced {
|
| Self::Fenced {
|
||||||
has_closing_fence: true,
|
has_closing_fence: true,
|
||||||
..
|
..
|
||||||
} => false,
|
} => false,
|
||||||
Self::Blockquote => matches!(next, Self::Blockquote | Self::Paragraph),
|
Self::Blockquote => matches!(
|
||||||
|
IdentifiedBlock::new(line).kind,
|
||||||
|
Self::Blockquote | Self::Paragraph
|
||||||
|
),
|
||||||
Self::Heading { level } => {
|
Self::Heading { level } => {
|
||||||
|
let next = IdentifiedBlock::new(line).kind;
|
||||||
matches!(next, Self::Paragraph)
|
matches!(next, Self::Paragraph)
|
||||||
|| matches!(next, Self::Heading { level: l } if l == *level )
|
|| matches!(next, Self::Heading { level: l } if l == *level )
|
||||||
}
|
}
|
||||||
Self::Paragraph | Self::Table { caption: true } => {
|
Self::Paragraph | Self::Table { caption: true } => !line
|
||||||
!matches!(next, Self::Atom(Blankline))
|
.trim_matches(|c: char| c.is_ascii_whitespace())
|
||||||
}
|
.is_empty(),
|
||||||
Self::ListItem {
|
Self::ListItem {
|
||||||
indent,
|
indent,
|
||||||
last_blankline,
|
last_blankline,
|
||||||
|
@ -1101,25 +1104,29 @@ impl<'s> Kind<'s> {
|
||||||
} => {
|
} => {
|
||||||
let line_t = line.trim_start_matches(|c: char| c.is_ascii_whitespace());
|
let line_t = line.trim_start_matches(|c: char| c.is_ascii_whitespace());
|
||||||
let whitespace = line.len() - line_t.len();
|
let whitespace = line.len() - line_t.len();
|
||||||
|
let next = IdentifiedBlock::new(line).kind;
|
||||||
let para = !*last_blankline && matches!(next, Self::Paragraph);
|
let para = !*last_blankline && matches!(next, Self::Paragraph);
|
||||||
*last_blankline = matches!(next, Self::Atom(Blankline));
|
*last_blankline = matches!(next, Self::Atom(Blankline));
|
||||||
*last_blankline || whitespace > *indent || para
|
*last_blankline || whitespace > *indent || para
|
||||||
}
|
}
|
||||||
Self::Definition {
|
Self::Definition {
|
||||||
indent,
|
indent,
|
||||||
footnote,
|
footnote: true,
|
||||||
last_blankline,
|
last_blankline,
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
if *footnote {
|
let next = IdentifiedBlock::new(line).kind;
|
||||||
let line_t = line.trim_start_matches(|c: char| c.is_ascii_whitespace());
|
let line_t = line.trim_start_matches(|c: char| c.is_ascii_whitespace());
|
||||||
let whitespace = line.len() - line_t.len();
|
let whitespace = line.len() - line_t.len();
|
||||||
let cont_para = !*last_blankline && matches!(next, Self::Paragraph);
|
let cont_para = !*last_blankline && matches!(next, Self::Paragraph);
|
||||||
*last_blankline = matches!(next, Self::Atom(Blankline));
|
*last_blankline = matches!(next, Self::Atom(Blankline));
|
||||||
whitespace > *indent || *last_blankline || cont_para
|
whitespace > *indent || *last_blankline || cont_para
|
||||||
} else {
|
}
|
||||||
line.starts_with(' ') && !matches!(next, Self::Atom(Blankline))
|
Self::Definition { .. } => {
|
||||||
}
|
let blankline = line
|
||||||
|
.trim_matches(|c: char| c.is_ascii_whitespace())
|
||||||
|
.is_empty();
|
||||||
|
line.starts_with(' ') && !blankline
|
||||||
}
|
}
|
||||||
Self::Fenced {
|
Self::Fenced {
|
||||||
fence_length,
|
fence_length,
|
||||||
|
@ -1132,7 +1139,7 @@ impl<'s> Kind<'s> {
|
||||||
fence_length: l,
|
fence_length: l,
|
||||||
spec,
|
spec,
|
||||||
..
|
..
|
||||||
} = next
|
} = IdentifiedBlock::new(line).kind
|
||||||
{
|
{
|
||||||
if spec.is_empty() {
|
if spec.is_empty() {
|
||||||
*has_closing_fence = k == *kind
|
*has_closing_fence = k == *kind
|
||||||
|
@ -1143,17 +1150,19 @@ impl<'s> Kind<'s> {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
Self::Table { caption } => {
|
Self::Table { caption } => {
|
||||||
matches!(next, Self::Table { .. } | Self::Atom(Blankline)) || {
|
let line_t = line.trim_matches(|c: char| c.is_ascii_whitespace());
|
||||||
if line
|
line_t.is_empty()
|
||||||
.trim_matches(|c: char| c.is_ascii_whitespace())
|
|| (line_t.starts_with('|')
|
||||||
.starts_with("^ ")
|
&& line_t.ends_with('|')
|
||||||
{
|
&& !line_t.ends_with("\\|"))
|
||||||
*caption = true;
|
|| {
|
||||||
true
|
if line_t.starts_with("^ ") {
|
||||||
} else {
|
*caption = true;
|
||||||
false
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue