diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-08-15 11:05:22 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-08-15 11:05:22 +0200 |
commit | 3a4de4b3a89e7171f8e3e0b6bf02fcd2d5c5f748 (patch) | |
tree | 6aec470579ec3021b158ff7aa1e784ecacedb1ad /src/construct/paragraph.rs | |
parent | 30dccab8678bd134e084afdafbf3e14d047f0965 (diff) | |
download | markdown-rs-3a4de4b3a89e7171f8e3e0b6bf02fcd2d5c5f748.tar.gz markdown-rs-3a4de4b3a89e7171f8e3e0b6bf02fcd2d5c5f748.tar.bz2 markdown-rs-3a4de4b3a89e7171f8e3e0b6bf02fcd2d5c5f748.zip |
Refactor paragraph, data resolvers
Diffstat (limited to '')
-rw-r--r-- | src/construct/paragraph.rs | 65 |
1 files changed, 37 insertions, 28 deletions
diff --git a/src/construct/paragraph.rs b/src/construct/paragraph.rs index e9fd377..b33346a 100644 --- a/src/construct/paragraph.rs +++ b/src/construct/paragraph.rs @@ -36,7 +36,6 @@ use crate::event::{Content, Kind, Link, Name}; use crate::resolve::Name as ResolveName; use crate::state::{Name as StateName, State}; use crate::tokenizer::Tokenizer; -use crate::util::skip::opt as skip_opt; /// Before paragraph. /// @@ -96,47 +95,57 @@ pub fn resolve(tokenizer: &mut Tokenizer) { if event.kind == Kind::Enter && event.name == Name::Paragraph { // Exit:Paragraph let mut exit_index = index + 3; - let mut enter_next_index = - skip_opt(&tokenizer.events, exit_index + 1, &[Name::LineEnding]); - // Enter:Paragraph - enter_next_index = skip_opt( - &tokenizer.events, - enter_next_index, - &[Name::SpaceOrTab, Name::BlockQuotePrefix], - ); - // Find future `Paragraphs`. - while enter_next_index < tokenizer.events.len() - && tokenizer.events[enter_next_index].name == Name::Paragraph - { - // Remove Exit:Paragraph, Enter:LineEnding, Exit:LineEnding, Enter:Paragraph. + loop { + let mut enter_index = exit_index + 1; + + if enter_index == tokenizer.events.len() + || tokenizer.events[enter_index].name != Name::LineEnding + { + break; + } + + enter_index += 2; + + while enter_index < tokenizer.events.len() { + let event = &tokenizer.events[enter_index]; + + if event.name != Name::SpaceOrTab + && event.name != Name::BlockQuotePrefix + && event.name != Name::BlockQuoteMarker + { + break; + } + + enter_index += 1; + } + + if enter_index == tokenizer.events.len() + || tokenizer.events[enter_index].name != Name::Paragraph + { + break; + } + + // Remove Exit:Paragraph, Enter:LineEnding, Exit:LineEnding. tokenizer.map.add(exit_index, 3, vec![]); // Remove Enter:Paragraph. - tokenizer.map.add(enter_next_index, 1, vec![]); + tokenizer.map.add(enter_index, 1, vec![]); // Add Exit:LineEnding position info to Exit:Data. - let line_ending_exit = &tokenizer.events[exit_index + 2]; - let line_ending_point = line_ending_exit.point.clone(); - let data_exit = &mut tokenizer.events[exit_index - 1]; - data_exit.point = line_ending_point; + tokenizer.events[exit_index - 1].point = + tokenizer.events[exit_index + 2].point.clone(); // Link Enter:Data on the previous line to Enter:Data on this line. if let Some(link) = &mut tokenizer.events[exit_index - 2].link { - link.next = Some(enter_next_index + 1); + link.next = Some(enter_index + 1); } - if let Some(link) = &mut tokenizer.events[enter_next_index + 1].link { + if let Some(link) = &mut tokenizer.events[enter_index + 1].link { link.previous = Some(exit_index - 2); } // Potential next start. - exit_index = enter_next_index + 3; - enter_next_index = skip_opt(&tokenizer.events, exit_index + 1, &[Name::LineEnding]); - enter_next_index = skip_opt( - &tokenizer.events, - enter_next_index, - &[Name::SpaceOrTab, Name::BlockQuotePrefix], - ); + exit_index = enter_index + 3; } // Move to `Exit:Paragraph`. |