summaryrefslogtreecommitdiffstats
path: root/libimagentrymarkdown/src
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2016-05-24 16:30:06 +0200
committerMatthias Beyer <mail@beyermatthias.de>2016-06-09 16:52:34 +0200
commit70f12dbef7c00c6a711afc9568542d8c9cb3d0d6 (patch)
tree5f850c71ecf86363d80cda0885c8a6167fc8f0e4 /libimagentrymarkdown/src
parentfb2934a96dade2a7a98a9c9e11d57ba08f8c567d (diff)
Implement link extractor
Diffstat (limited to 'libimagentrymarkdown/src')
-rw-r--r--libimagentrymarkdown/src/link.rs78
1 files changed, 75 insertions, 3 deletions
diff --git a/libimagentrymarkdown/src/link.rs b/libimagentrymarkdown/src/link.rs
index 02526486..4699e69a 100644
--- a/libimagentrymarkdown/src/link.rs
+++ b/libimagentrymarkdown/src/link.rs
@@ -1,8 +1,80 @@
use result::Result;
-pub type Link = String;
+use hoedown::renderer::Render;
+use hoedown::Buffer;
+use hoedown::Markdown;
+
+pub struct Link {
+ pub title: String,
+ pub link: String,
+}
+
+struct LinkExtractor {
+ links: Vec<Link>,
+}
+
+impl LinkExtractor {
+
+ pub fn new() -> LinkExtractor {
+ LinkExtractor { links: vec![] }
+ }
+
+ pub fn links(self) -> Vec<Link> {
+ self.links
+ }
+
+}
+
+impl Render for LinkExtractor {
+
+ fn link(&mut self,
+ _: &mut Buffer,
+ content: Option<&Buffer>,
+ link: Option<&Buffer>,
+ _: Option<&Buffer>)
+ -> bool
+ {
+ let link = link.and_then(|l| l.to_str().ok()).map(String::from);
+ let content = content.and_then(|l| l.to_str().ok()).map(String::from);
+
+ match (link, content) {
+ (Some(link), Some(content)) => {
+ self.links.push(Link { link: link, title: content });
+ false
+ },
+
+ (_, _) => {
+ false
+ },
+ }
+
+ }
+
+}
+
+pub fn extract_links(buf: &str) -> Vec<Link> {
+ let mut le = LinkExtractor::new();
+ le.render(&Markdown::new(buf));
+ le.links()
+}
+
+#[cfg(test)]
+mod test {
+ use super::{Link, extract_links};
+
+ #[test]
+ fn test_one_link() {
+ let testtext = "Some [example text](http://example.com).";
+
+ let exp = Link {
+ title: String::from("example text"),
+ link: String::from("http://example.com"),
+ };
+
+ let mut links = extract_links(testtext);
+ assert_eq!(1, links.len());
+ assert_eq!(exp, links.pop().unwrap())
+ }
-pub fn extract_links(buf: &str) -> Result<Vec<Link>> {
- unimplemented!()
}