nekrochan/src/filters.rs

88 řádky
2.2 KiB
Rust
Surový Normální zobrazení Historie

2024-01-15 15:06:25 +00:00
use chrono::{DateTime, Locale, TimeZone, Utc};
use chrono_tz::Europe::Prague;
2023-12-11 15:18:43 +00:00
use lazy_static::lazy_static;
2023-12-25 13:53:44 +00:00
use regex::{Captures, Regex};
2023-12-11 15:18:43 +00:00
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=".+">&gt;&gt;(\d+)<\/a>"#).unwrap();
}
2024-01-15 15:06:25 +00:00
pub fn czech_datetime(utc: &DateTime<Utc>) -> askama::Result<String> {
let time = Prague.from_utc_datetime(&utc.naive_utc());
2023-12-11 15:18:43 +00:00
let time = time
2024-01-02 11:58:47 +00:00
.format_localized("%d.%m.%Y (%a) %H:%M:%S", Locale::cs_CZ)
2023-12-11 15:18:43 +00:00
.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())
2024-01-08 17:45:11 +00:00
} else if count < 5 && count != 0 {
2023-12-11 15:18:43 +00:00
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,
2023-12-11 15:59:32 +00:00
if yous.contains(&format!("{board}/{id}")) {
2023-12-11 15:18:43 +00:00
" <span class=\"small\">(Ty)</span>"
} else {
""
}
)
});
Ok(output.to_string())
}