2023-12-11 15:18:43 +00:00
|
|
|
use chrono::{DateTime, Locale, Utc};
|
|
|
|
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=".+">>>(\d+)<\/a>"#).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn czech_humantime(time: &DateTime<Utc>) -> askama::Result<String> {
|
|
|
|
let duration = (Utc::now() - *time).abs();
|
|
|
|
|
2023-12-12 17:54:48 +00:00
|
|
|
let seconds = duration.num_seconds();
|
2023-12-11 15:18:43 +00:00
|
|
|
let minutes = duration.num_minutes();
|
|
|
|
let hours = duration.num_hours();
|
|
|
|
let days = duration.num_days();
|
|
|
|
let weeks = duration.num_weeks();
|
|
|
|
let months = duration.num_days() / 30;
|
|
|
|
let years = duration.num_days() / 365;
|
|
|
|
|
|
|
|
let mut time = "Teď".into();
|
|
|
|
|
2023-12-12 17:54:48 +00:00
|
|
|
if seconds > 0 {
|
|
|
|
time = format!(
|
|
|
|
"{} {}",
|
|
|
|
seconds,
|
|
|
|
czech_plural("sekunda|sekundy|sekund", seconds)?
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-11 15:18:43 +00:00
|
|
|
if minutes > 0 {
|
|
|
|
time = format!(
|
|
|
|
"{} {}",
|
|
|
|
minutes,
|
|
|
|
czech_plural("minuta|minuty|minut", minutes)?
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if hours > 0 {
|
|
|
|
time = format!("{} {}", hours, czech_plural("hodina|hodiny|hodin", hours)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
if days > 0 {
|
|
|
|
time = format!("{} {}", days, czech_plural("den|dny|dnů", days)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
if weeks > 0 {
|
|
|
|
time = format!("{} {}", weeks, czech_plural("týden|týdny|týdnů", weeks)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
if months > 0 {
|
|
|
|
time = format!(
|
|
|
|
"{} {}",
|
|
|
|
months,
|
|
|
|
czech_plural("měsíc|měsíce|měsíců", months)?
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if years > 0 {
|
|
|
|
time = format!("{} {}", years, czech_plural("rok|roky|let", years)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(time)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn czech_datetime(time: &DateTime<Utc>) -> askama::Result<String> {
|
|
|
|
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())
|
|
|
|
} else if count < 5 {
|
|
|
|
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())
|
|
|
|
}
|