Refactor custom functionality in helper function

This commit is contained in:
Matthias Beyer 2015-12-06 11:25:13 +01:00
parent 7b99aee552
commit 4d3479291d

View file

@ -16,43 +16,35 @@ mod markdown {
}
pub fn links(&self) -> Vec<String> {
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::<Vec<String>>()
self.extract_tag(|tag| {
match tag {
Tag::Link(url, _) => Some(url.into_owned()),
_ => None
}
})
}
pub fn codeblocks(&self) -> Vec<String> {
self.extract_tag(|tag| {
match tag {
Tag::CodeBlock(text) => Some(text.into_owned()),
_ => None
}
})
}
fn extract_tag<F>(&self, f: F) -> Vec<String>
where F: FnMut(Tag) -> Option<String>
{
Parser::new(&self.text[..])
.filter_map(|e| {
match e {
Event::Start(t) => Some(t),
Event::End(t) => Some(t),
_ => None
Event::Start(t) | Event::End(t) => Some(t),
_ => None
}
})
.filter_map(|tag| {
match tag {
Tag::CodeBlock(text) => Some(text),
_ => None
}
})
.map(|text| {
text.into_owned()
}).collect::<Vec<String>>()
.filter_map(f)
.collect::<Vec<String>>()
}
}