diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-07-12 17:47:08 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-07-12 17:47:08 +0200 |
commit | 2e3b7abaa9877b658fa4f8f2612acc617dff60bb (patch) | |
tree | e823d041521a4af33a7e552ba58f1d4b63335be3 /src/util/skip.rs | |
parent | 75522b867b15b9a400275cfec9a2ead4ff535473 (diff) | |
download | markdown-rs-2e3b7abaa9877b658fa4f8f2612acc617dff60bb.tar.gz markdown-rs-2e3b7abaa9877b658fa4f8f2612acc617dff60bb.tar.bz2 markdown-rs-2e3b7abaa9877b658fa4f8f2612acc617dff60bb.zip |
Fix a lot of list things
* Add `ListItem`, `ListOrdered`, and `ListUnordered` tokens
* Add support for multiline list items
* Add support for tight lists
* Fix bug where 10 digit long list item values worked
* Fix skip bug when skipping over nested events
Diffstat (limited to 'src/util/skip.rs')
-rw-r--r-- | src/util/skip.rs | 41 |
1 files changed, 34 insertions, 7 deletions
diff --git a/src/util/skip.rs b/src/util/skip.rs index 971beb6..3307734 100644 --- a/src/util/skip.rs +++ b/src/util/skip.rs @@ -1,7 +1,7 @@ //! Utilities to deal with lists of events. use crate::token::Token; -use crate::tokenizer::Event; +use crate::tokenizer::{Event, EventType}; /// Skip from `index`, optionally past `token_types`. pub fn opt(events: &[Event], index: usize, token_types: &[Token]) -> usize { @@ -13,33 +13,60 @@ pub fn opt_back(events: &[Event], index: usize, token_types: &[Token]) -> usize skip_opt_with_direction(events, index, token_types, false) } +pub fn to(events: &[Event], mut index: usize, token_types: &[Token]) -> usize { + while index < events.len() { + let current = &events[index].token_type; + + if token_types.contains(current) { + break; + } + + index += 1; + } + + index +} + /// Skip internals. fn skip_opt_with_direction( events: &[Event], - index: usize, + mut index: usize, token_types: &[Token], forward: bool, ) -> usize { - let mut index = index; + let mut balance = 0; + let open = if forward { + EventType::Enter + } else { + EventType::Exit + }; while index < events.len() { let current = &events[index].token_type; - if !token_types.contains(current) { + if !token_types.contains(current) || events[index].event_type != open { break; } - // assert_eq!(events[index].event_type, EventType::Enter); index = if forward { index + 1 } else { index - 1 }; + balance += 1; loop { - if events[index].token_type == *current { - // assert_eq!(events[index].event_type, EventType::Exit); + balance = if events[index].event_type == open { + balance + 1 + } else { + balance - 1 + }; + + if events[index].token_type == *current && balance == 0 { + println!("close:it! {:?} {:?}", events[index].token_type, balance); index = if forward { index + 1 } else { index - 1 }; + println!("index:break: {:?}", index); break; } index = if forward { index + 1 } else { index - 1 }; + println!("index:loop: {:?}", index); } } |