diff options
| author | Jokler <jokler.contact@gmail.com> | 2018-03-10 01:30:13 +0100 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2018-03-10 01:30:13 +0100 |
| commit | 9be7f31ee2d37800c7d23a9fff7d7ab8a2e076ed (patch) | |
| tree | 9c89710c7669cf3b3fbed35a2292f8c142bb35bf /src | |
| parent | e5e7a8d49729601b62e81d28e547d3828e839b28 (diff) | |
| download | frippy-9be7f31ee2d37800c7d23a9fff7d7ab8a2e076ed.tar.gz frippy-9be7f31ee2d37800c7d23a9fff7d7ab8a2e076ed.zip | |
Decode html encoded characters in titles
Diffstat (limited to 'src')
| -rw-r--r-- | src/plugins/url.rs | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/plugins/url.rs b/src/plugins/url.rs index 4c33cba..e75d893 100644 --- a/src/plugins/url.rs +++ b/src/plugins/url.rs @@ -1,3 +1,4 @@ +extern crate htmlescape; extern crate regex; use irc::client::prelude::*; @@ -35,23 +36,24 @@ impl Url { Some(captures.get(2)?.as_str().to_owned()) } - fn get_title<'a>(&self, body: &'a str) -> Option<&'a str> { + fn get_title<'a>(&self, body: &str) -> Result<String, UrlError> { let title = body.find("<title>") .map(|start| body.find("</title>").map(|end| &body[start + 7..end])) - .and_then(|s| s); + .and_then(|s| s).ok_or(ErrorKind::MissingTitle)?; + debug!("Title: {:?}", title); - title + htmlescape::decode_html(title).map_err(|_| ErrorKind::HtmlDecoding.into()) } fn url(&self, text: &str) -> Result<String, UrlError> { let url = self.grep_url(text).ok_or(ErrorKind::MissingUrl)?; let body = utils::download(&url, Some(self.max_kib)).context(ErrorKind::Download)?; - let title = self.get_title(&body).ok_or(ErrorKind::MissingTitle)?; + let title = self.get_title(&body)?; - Ok(title.to_owned()) + Ok(title.replace('\n', "|").replace('\r', "|")) } } @@ -110,5 +112,9 @@ pub mod error { /// Missing title error #[fail(display = "No title was found")] MissingTitle, + + /// Html decoding error + #[fail(display = "Failed to decode Html characters")] + HtmlDecoding, } } |
