2018-06-17 18:06:47 +02:00
|
|
|
use gettextrs::gettext;
|
2018-04-23 12:54:37 +02:00
|
|
|
use heck::CamelCase;
|
2018-06-20 16:29:19 +02:00
|
|
|
use pulldown_cmark::{Event, Parser, Options, Tag, html};
|
2018-06-16 19:39:22 +02:00
|
|
|
use rocket::{
|
|
|
|
http::uri::Uri,
|
|
|
|
response::{Redirect, Flash}
|
|
|
|
};
|
2018-04-23 12:54:37 +02:00
|
|
|
|
2018-04-23 13:10:15 +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 {
|
2018-04-23 13:10:15 +02:00
|
|
|
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
|
|
|
|
2018-06-19 23:20:27 +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 16:29:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
pub fn md_to_html(md: &str) -> String {
|
|
|
|
let parser = Parser::new_ext(md, Options::all());
|
|
|
|
let parser = parser.flat_map(|evt| match evt {
|
|
|
|
Event::Text(txt) => txt.chars().fold((vec![], false, String::new(), 0), |(mut events, in_mention, text_acc, n), c| {
|
|
|
|
if in_mention {
|
|
|
|
if c.is_alphanumeric() || c == '@' || c == '.' || c == '-' || c == '_' {
|
|
|
|
(events, in_mention, text_acc + c.to_string().as_ref(), n + 1)
|
|
|
|
} else {
|
|
|
|
let short_mention = text_acc.clone();
|
|
|
|
let short_mention = short_mention.splitn(1, '@').nth(0).unwrap_or("");
|
|
|
|
let link = Tag::Link(format!("/@/{}/", text_acc).into(), short_mention.to_string().into());
|
|
|
|
|
|
|
|
events.push(Event::Start(link.clone()));
|
|
|
|
events.push(Event::Text(format!("@{}", short_mention).into()));
|
|
|
|
events.push(Event::End(link));
|
|
|
|
|
|
|
|
(events, false, c.to_string(), n + 1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if c == '@' {
|
|
|
|
events.push(Event::Text(text_acc.into()));
|
|
|
|
(events, true, String::new(), n + 1)
|
|
|
|
} else {
|
|
|
|
if n >= (txt.len() - 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).0,
|
|
|
|
_ => vec![evt]
|
|
|
|
});
|
|
|
|
let mut buf = String::new();
|
|
|
|
html::push_html(&mut buf, parser);
|
|
|
|
buf
|
|
|
|
|
|
|
|
// let root = parse_document(&arena, md, &ComrakOptions{
|
|
|
|
// smart: true,
|
|
|
|
// safe: true,
|
|
|
|
// ext_strikethrough: true,
|
|
|
|
// ext_tagfilter: true,
|
|
|
|
// ext_table: true,
|
|
|
|
// // ext_autolink: true,
|
|
|
|
// ext_tasklist: true,
|
|
|
|
// ext_superscript: true,
|
|
|
|
// ext_header_ids: Some("title".to_string()),
|
|
|
|
// ext_footnotes: true,
|
|
|
|
// ..ComrakOptions::default()
|
|
|
|
// });
|
|
|
|
}
|