88 řádky
2.2 KiB
Rust
88 řádky
2.2 KiB
Rust
use chrono::{DateTime, Locale, TimeZone, Utc};
|
|
use chrono_tz::Europe::Prague;
|
|
use lazy_static::lazy_static;
|
|
use regex::{Captures, Regex};
|
|
use std::{collections::HashSet, fmt::Display};
|
|
|
|
use crate::markup::SPOILER_REGEX;
|
|
|
|
lazy_static! {
|
|
static ref MARKUP_QUOTE_REGEX: Regex =
|
|
Regex::new(r#"<a class="quote" href=".+">>>(\d+)<\/a>"#).unwrap();
|
|
}
|
|
|
|
pub fn czech_datetime(utc: &DateTime<Utc>) -> askama::Result<String> {
|
|
let time = Prague.from_utc_datetime(&utc.naive_utc());
|
|
|
|
let time = time
|
|
.format_localized("%d.%m.%Y (%a) %H:%M:%S", Locale::cs_CZ)
|
|
.to_string();
|
|
|
|
Ok(time)
|
|
}
|
|
|
|
pub fn czech_plural(plurals: &str, count: impl Display) -> askama::Result<String> {
|
|
let plurals = plurals.split('|').collect::<Vec<_>>();
|
|
let count = count.to_string().parse::<i64>().unwrap();
|
|
|
|
let one = plurals[0];
|
|
let few = plurals[1];
|
|
let other = plurals[2];
|
|
|
|
if count == 1 {
|
|
Ok(one.into())
|
|
} else if count < 5 && count != 0 {
|
|
Ok(few.into())
|
|
} else {
|
|
Ok(other.into())
|
|
}
|
|
}
|
|
|
|
pub fn inline_post(input: impl Display) -> askama::Result<String> {
|
|
let input = input.to_string();
|
|
|
|
if input.is_empty() {
|
|
return Ok("(bez obsahu)".into());
|
|
}
|
|
|
|
let collapsed = input.split_whitespace().collect::<Vec<_>>().join(" ");
|
|
let spoilered = SPOILER_REGEX
|
|
.replace_all(&collapsed, "(spoiler)")
|
|
.to_string();
|
|
|
|
let truncated = askama::filters::truncate(spoilered, 64)?;
|
|
|
|
Ok(truncated)
|
|
}
|
|
|
|
pub fn get_page(input: &usize, page_size: &i64) -> askama::Result<i64> {
|
|
let page = crate::paginate(*page_size, *input as i64);
|
|
|
|
Ok(page)
|
|
}
|
|
|
|
pub fn add_yous(
|
|
input: impl Display,
|
|
board: &String,
|
|
yous: &HashSet<String>,
|
|
) -> askama::Result<String> {
|
|
let input = input.to_string();
|
|
|
|
let output = MARKUP_QUOTE_REGEX.replace_all(&input, |captures: &Captures| {
|
|
let quote = &captures[0];
|
|
let id = &captures[1];
|
|
|
|
format!(
|
|
"{}{}",
|
|
quote,
|
|
if yous.contains(&format!("{board}/{id}")) {
|
|
" <span class=\"small\">(Ty)</span>"
|
|
} else {
|
|
""
|
|
}
|
|
)
|
|
});
|
|
|
|
Ok(output.to_string())
|
|
}
|