From f5d6c9c05bde96b2dc45d86f9a483106853502e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Mon, 4 Oct 2021 21:23:04 +0200 Subject: [PATCH] rewrite to actually use pulldown idiomatically --- src/main.rs | 80 +++++++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 49 deletions(-) diff --git a/src/main.rs b/src/main.rs index c17acb8..ff3e6a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -407,7 +407,7 @@ fn update_garden>( let mut file_string = String::new(); file.read_to_string(&mut file_string)?; let markdown_source = preprocess_markdown(file_string); - let result = GardenParser::parse(&markdown_source)?; + let result = parse_garden(&markdown_source)?; pages.insert( String::from(path.to_str().unwrap()), @@ -435,66 +435,48 @@ fn update_garden>( Ok(result) } -struct GardenParser<'a> { - parser: Parser<'a>, - last_nontext_event: Option>, - current_top_heading: u32, - top_heading_text: &'a mut Option, - links: &'a mut Vec, -} - struct ParseResult { html: String, title: Option, links: Vec, } -impl<'a> GardenParser<'a> { - fn parse>(text: &'a S) -> anyhow::Result { - let mut title: Option = None; - let mut links: Vec = vec![]; +fn parse_garden>(text: S) -> anyhow::Result { + let mut current_top_heading = 999; + let mut top_heading_text: Option = None; - let parser = GardenParser { - parser: Parser::new_ext(text.as_ref(), Options::all()), - last_nontext_event: None, - current_top_heading: 999, - top_heading_text: &mut title, - links: &mut links, - }; + let mut last_nontext_event: Option = None; - let mut html = String::new(); - html::push_html(&mut html, parser); - html = postprocess_html(html)?; + let mut links: Vec = vec![]; - Ok(ParseResult { html, title, links }) - } -} - -impl<'a> Iterator for GardenParser<'a> { - type Item = Event<'a>; - - fn next(&mut self) -> Option { - let event = self.parser.next(); - - if let Some(event) = &event { - if let Event::Start(Tag::Link(_, str, _)) = &event { - self.links.push(str.to_string()); - } - - if let Some(Event::Start(Tag::Heading(hl))) = self.last_nontext_event { - if hl < self.current_top_heading { - self.current_top_heading = hl; - if let Event::Text(str) = &event { - *self.top_heading_text = Some(str.clone().into_string()); - } - } - } - - self.last_nontext_event = Some(event.clone()); + let parser = Parser::new_ext(text.as_ref(), Options::all()).map(|event| { + if let Event::Start(Tag::Link(_, str, _)) = &event { + links.push(str.to_string()); } + if let Some(Event::Start(Tag::Heading(hl))) = last_nontext_event { + if hl < current_top_heading { + current_top_heading = hl; + if let Event::Text(str) = &event { + top_heading_text = Some(str.clone().into_string()); + } + } + } + + last_nontext_event = Some(event.clone()); + event - } + }); + + let mut html = String::new(); + html::push_html(&mut html, parser); + html = postprocess_html(html)?; + + Ok(ParseResult { + html, + title: top_heading_text, + links, + }) } fn preprocess_markdown(string: String) -> String {