Plume/plume-common/src/utils.rs

100 lines
3.9 KiB
Rust
Raw Normal View History

2018-06-17 18:06:47 +02:00
use gettextrs::gettext;
2018-04-23 12:54:37 +02:00
use heck::CamelCase;
use pulldown_cmark::{Event, Parser, Options, Tag, html};
use rocket::{
http::uri::Uri,
response::{Redirect, Flash}
};
2018-04-23 12:54:37 +02:00
/// Remove non alphanumeric characters and CamelCase a string
2018-04-23 12:54:37 +02:00
pub fn make_actor_id(name: String) -> String {
name.as_str()
.to_camel_case()
.to_string()
.chars()
.filter(|c| c.is_alphanumeric())
.collect()
2018-04-23 12:54:37 +02:00
}
2018-04-23 16:25:39 +02:00
pub fn requires_login(message: &str, url: Uri) -> Flash<Redirect> {
Flash::new(Redirect::to(Uri::new(format!("/login?m={}", gettext(message.to_string())))), "callback", url.as_str())
2018-04-23 16:25:39 +02:00
}
2018-06-20 22:58:11 +02:00
/// Returns (HTML, mentions)
pub fn md_to_html(md: &str) -> (String, Vec<String>) {
let parser = Parser::new_ext(md, Options::all());
2018-06-20 20:22:34 +02:00
2018-06-20 22:58:11 +02:00
let (parser, mentions): (Vec<Vec<Event>>, Vec<Vec<String>>) = parser.map(|evt| match evt {
Event::Text(txt) => {
let (evts, _, _, _, new_mentions) = txt.chars().fold((vec![], false, String::new(), 0, vec![]), |(mut events, in_mention, text_acc, n, mut mentions), c| {
if in_mention {
2018-07-18 19:00:49 +02:00
let char_matches = c.is_alphanumeric() || c == '@' || c == '.' || c == '-' || c == '_';
if char_matches && (n < (txt.chars().count() - 1)) {
2018-06-20 22:58:11 +02:00
(events, in_mention, text_acc + c.to_string().as_ref(), n + 1, mentions)
} else {
2018-07-18 19:00:49 +02:00
let mention = if char_matches {
text_acc + c.to_string().as_ref()
} else {
text_acc
};
2018-06-20 22:58:11 +02:00
let short_mention = mention.clone();
let short_mention = short_mention.splitn(1, '@').nth(0).unwrap_or("");
let link = Tag::Link(format!("/@/{}/", mention).into(), short_mention.to_string().into());
2018-06-20 22:58:11 +02:00
mentions.push(mention);
events.push(Event::Start(link.clone()));
events.push(Event::Text(format!("@{}", short_mention).into()));
events.push(Event::End(link));
2018-06-20 22:58:11 +02:00
(events, false, c.to_string(), n + 1, mentions)
}
} else {
2018-06-20 22:58:11 +02:00
if c == '@' {
events.push(Event::Text(text_acc.into()));
(events, true, String::new(), n + 1, mentions)
} else {
if n >= (txt.chars().count() - 1) { // Add the text after at the end, even if it is not followed by a mention.
events.push(Event::Text((text_acc.clone() + c.to_string().as_ref()).into()))
}
(events, in_mention, text_acc + c.to_string().as_ref(), n + 1, mentions)
}
}
2018-06-20 22:58:11 +02:00
});
(evts, new_mentions)
},
_ => (vec![evt], vec![])
}).unzip();
let parser = parser.into_iter().flatten();
let mentions = mentions.into_iter().flatten().map(|m| String::from(m.trim()));
2018-06-20 20:22:34 +02:00
// TODO: fetch mentionned profiles in background, if needed
let mut buf = String::new();
html::push_html(&mut buf, parser);
2018-06-20 22:58:11 +02:00
(buf, mentions.collect())
}
2018-07-18 18:35:50 +02:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mentions() {
let tests = vec![
("nothing", vec![]),
("@mention", vec!["mention"]),
("@mention@instance.tld", vec!["mention@instance.tld"]),
("@many @mentions", vec!["many", "mentions"]),
("@start with a mentions", vec!["start"]),
("mention at @end", vec!["end"]),
("between parenthesis (@test)", vec!["test"]),
("with some punctuation @test!", vec!["test"]),
2018-07-18 19:00:49 +02:00
(" @spaces ", vec!["spaces"]),
2018-07-18 18:35:50 +02:00
];
for (md, mentions) in tests {
assert_eq!(md_to_html(md).1, mentions.into_iter().map(|s| s.to_string()).collect::<Vec<String>>());
}
}
}