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#">>(\d+)<\/a>"#).unwrap(); } pub fn czech_datetime(utc: &DateTime) -> askama::Result { 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 { let plurals = plurals.split('|').collect::>(); let count = count.to_string().parse::().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 { let input = input.to_string(); if input.is_empty() { return Ok("(bez obsahu)".into()); } let collapsed = input.split_whitespace().collect::>().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 { let page = crate::paginate(*page_size, *input as i64); Ok(page) } pub fn add_yous( input: impl Display, board: &String, yous: &HashSet, ) -> askama::Result { 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}")) { " (Ty)" } else { "" } ) }); Ok(output.to_string()) }