From 7b99aee552d95f0df4d4550ff2dec873a963e620 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 6 Dec 2015 11:01:45 +0100 Subject: [PATCH] Rewrite markdown parsing, so we can extract links and quotes (and maybe more) --- src/module/helpers/content.rs | 75 +++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 20 deletions(-) diff --git a/src/module/helpers/content.rs b/src/module/helpers/content.rs index 64054e37..a082763b 100644 --- a/src/module/helpers/content.rs +++ b/src/module/helpers/content.rs @@ -1,25 +1,60 @@ - -pub fn extract_links(s: &String) -> Vec { +mod markdown { use pulldown_cmark::Parser; use pulldown_cmark::Event; use pulldown_cmark::Tag; - Parser::new(&s[..]) - .filter_map(|e| { - match e { - Event::Start(t) => Some(t), - Event::End(t) => Some(t), - _ => None - } - }) - .filter_map(|tag| { - match tag { - Tag::Link(url, text) => Some((url, text)), - _ => None - } - }) - .map(|(url, text)| { - text.into_owned() - }).collect::>() -} + pub struct MarkdownParser<'a> { + text: &'a String + } + impl<'a> MarkdownParser<'a> { + + pub fn new(s: &'a String) -> MarkdownParser { + MarkdownParser { + text: s + } + } + + pub fn links(&self) -> Vec { + Parser::new(&self.text[..]) + .filter_map(|e| { + match e { + Event::Start(t) => Some(t), + Event::End(t) => Some(t), + _ => None + } + }) + .filter_map(|tag| { + match tag { + Tag::Link(url, text) => Some((url, text)), + _ => None + } + }) + .map(|(url, text)| { + text.into_owned() + }).collect::>() + } + + pub fn codeblocks(&self) -> Vec { + Parser::new(&self.text[..]) + .filter_map(|e| { + match e { + Event::Start(t) => Some(t), + Event::End(t) => Some(t), + _ => None + } + }) + .filter_map(|tag| { + match tag { + Tag::CodeBlock(text) => Some(text), + _ => None + } + }) + .map(|text| { + text.into_owned() + }).collect::>() + } + + } + +}