From a3dd207e3b1ebcbcb6cec0f703a695e51ae4ece0 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 24 Jun 2022 17:57:10 +0200 Subject: Add link, images (resource) This is still some messy code that needs cleaning up, but it adds support for links and images, of the resource kind (`[a](b)`). References (`[a][b]`) are parsed and will soon be supported, but need matching. * Fix bug to pad percent-encoded bytes when normalizing urls * Fix bug with escapes counting as balancing in destination * Add `space_or_tab_one_line_ending`, to parse whitespace including up to one line ending (but not a blank line) * Add `ParserState` to share codes, definitions, etc --- src/construct/partial_space_or_tab.rs | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/construct/partial_space_or_tab.rs') diff --git a/src/construct/partial_space_or_tab.rs b/src/construct/partial_space_or_tab.rs index 024a4b2..43bdc53 100644 --- a/src/construct/partial_space_or_tab.rs +++ b/src/construct/partial_space_or_tab.rs @@ -35,6 +35,45 @@ pub fn space_or_tab() -> Box { space_or_tab_min_max(1, usize::MAX) } +pub fn space_or_tab_one_line_ending() -> Box { + Box::new(|tokenizer, code| { + tokenizer.attempt(space_or_tab(), move |ok| { + Box::new(move |tokenizer, code| match code { + Code::CarriageReturnLineFeed | Code::Char('\r' | '\n') => { + tokenizer.enter(TokenType::LineEnding); + tokenizer.consume(code); + tokenizer.exit(TokenType::LineEnding); + ( + State::Fn(Box::new(tokenizer.attempt_opt( + space_or_tab(), + move |_t, code| { + if !matches!( + code, + Code::None + | Code::CarriageReturnLineFeed + | Code::Char('\r' | '\n') + ) { + (State::Ok, Some(vec![code])) + } else { + (State::Nok, None) + } + }, + ))), + None, + ) + } + _ => { + if ok { + (State::Ok, Some(vec![code])) + } else { + (State::Nok, None) + } + } + }) + })(tokenizer, code) + }) +} + /// Between `x` and `y` `space_or_tab` /// /// ```bnf -- cgit