rewrite to actually use pulldown idiomatically
This commit is contained in:
parent
bca533c701
commit
f5d6c9c05b
1 changed files with 31 additions and 49 deletions
80
src/main.rs
80
src/main.rs
|
@ -407,7 +407,7 @@ fn update_garden<P: AsRef<Path>>(
|
|||
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<P: AsRef<Path>>(
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
struct GardenParser<'a> {
|
||||
parser: Parser<'a>,
|
||||
last_nontext_event: Option<Event<'a>>,
|
||||
current_top_heading: u32,
|
||||
top_heading_text: &'a mut Option<String>,
|
||||
links: &'a mut Vec<String>,
|
||||
}
|
||||
|
||||
struct ParseResult {
|
||||
html: String,
|
||||
title: Option<String>,
|
||||
links: Vec<String>,
|
||||
}
|
||||
|
||||
impl<'a> GardenParser<'a> {
|
||||
fn parse<S: AsRef<str>>(text: &'a S) -> anyhow::Result<ParseResult> {
|
||||
let mut title: Option<String> = None;
|
||||
let mut links: Vec<String> = vec![];
|
||||
fn parse_garden<S: AsRef<str>>(text: S) -> anyhow::Result<ParseResult> {
|
||||
let mut current_top_heading = 999;
|
||||
let mut top_heading_text: Option<String> = 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<Event> = None;
|
||||
|
||||
let mut html = String::new();
|
||||
html::push_html(&mut html, parser);
|
||||
html = postprocess_html(html)?;
|
||||
let mut links: Vec<String> = vec![];
|
||||
|
||||
Ok(ParseResult { html, title, links })
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for GardenParser<'a> {
|
||||
type Item = Event<'a>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
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 {
|
||||
|
|
Loading…
Reference in a new issue