97 lines
3.3 KiB
Rust
97 lines
3.3 KiB
Rust
|
use std::collections::VecDeque;
|
||
|
use crate::void_app::Poems;
|
||
|
use markdown::Options;
|
||
|
|
||
|
pub fn get_poem(slug: String) -> (String, String, String) {
|
||
|
let filename = String::from(String::from(slug.clone()) + ".md");
|
||
|
let creation_date =
|
||
|
String::from(String::from("<br>Written on: ") + filename.split("_").next().unwrap());
|
||
|
let poem_content = Poems::get(&filename).expect("Found poem {filename:?}");
|
||
|
let mut poem_to_str = std::str::from_utf8(poem_content.data.as_ref())
|
||
|
.expect("Title is valid UT8.")
|
||
|
.lines();
|
||
|
let poem_title = poem_to_str.next().unwrap();
|
||
|
let poem_content = poem_to_str.into_iter().collect::<Vec<&str>>().join("\n");
|
||
|
let poem_title_to_html_string =
|
||
|
markdown::to_html_with_options(poem_title, &Options::gfm()).unwrap();
|
||
|
let poem_content_to_html_string =
|
||
|
markdown::to_html_with_options(poem_content.as_str(), &Options::gfm()).unwrap();
|
||
|
(poem_title_to_html_string, poem_content_to_html_string, creation_date)
|
||
|
}
|
||
|
|
||
|
pub fn get_poem_list() -> Vec<(String, String)> {
|
||
|
let mut poem_list = Vec::new();
|
||
|
for p in Poems::iter() {
|
||
|
let filename = p.to_string();
|
||
|
let poem_content = Poems::get(&filename).expect("Found poem {filename:?}");
|
||
|
let mut poem_to_str = std::str::from_utf8(poem_content.data.as_ref())
|
||
|
.expect("Title is valid UT8.")
|
||
|
.lines();
|
||
|
let title_markdown = poem_to_str.next().expect("No title specified.");
|
||
|
let title = markdown::to_html_with_options(title_markdown, &Options::gfm()).unwrap();
|
||
|
let slug = String::from(filename.trim_end_matches(".md"));
|
||
|
poem_list.push((title.clone(), slug.clone()));
|
||
|
}
|
||
|
log::trace!("{poem_list:?}");
|
||
|
poem_list
|
||
|
}
|
||
|
|
||
|
pub fn get_oldest_entry(current: String) -> String {
|
||
|
let mut poem_list = VecDeque::from(get_poem_list());
|
||
|
let oldest = poem_list
|
||
|
.pop_front()
|
||
|
.expect("There is an entry in this list of poems.")
|
||
|
.1;
|
||
|
if current == oldest {
|
||
|
return format!("/poems/{current}#");
|
||
|
}
|
||
|
format!("/poems/{oldest}")
|
||
|
}
|
||
|
|
||
|
pub fn get_latest_entry(current: String) -> String {
|
||
|
let mut poem_list = get_poem_list();
|
||
|
let latest = poem_list
|
||
|
.pop()
|
||
|
.expect("There is an entry in this list of poems.")
|
||
|
.1;
|
||
|
if current == latest {
|
||
|
return format!("/poems/{current}#");
|
||
|
}
|
||
|
format!("/poems/{latest}")
|
||
|
}
|
||
|
|
||
|
pub fn get_previous_entry(current: String) -> String {
|
||
|
let poem_list = get_poem_list();
|
||
|
match poem_list.iter().enumerate().find_map(|(index, p)| {
|
||
|
if p.1 == current {
|
||
|
if index != 0 {
|
||
|
Some(poem_list[index - 1].1.clone())
|
||
|
} else {
|
||
|
None
|
||
|
}
|
||
|
} else {
|
||
|
None
|
||
|
}
|
||
|
}) {
|
||
|
Some(entry) => format!("/poems/{entry}"),
|
||
|
None => format!("/poems/{current}"),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn get_next_entry(current: String) -> String {
|
||
|
let poem_list = get_poem_list();
|
||
|
match poem_list.iter().enumerate().find_map(|(index, p)| {
|
||
|
if p.1 == current {
|
||
|
if index != poem_list.len() - 1 {
|
||
|
Some(poem_list[index + 1].1.clone())
|
||
|
} else {
|
||
|
None
|
||
|
}
|
||
|
} else {
|
||
|
None
|
||
|
}
|
||
|
}) {
|
||
|
Some(entry) => format!("/poems/{entry}"),
|
||
|
None => format!("/poems/{current}"),
|
||
|
}
|
||
|
}
|