2023-04-16 21:46:10 -05:00
|
|
|
use rust_embed::RustEmbed;
|
2023-04-13 12:51:25 -05:00
|
|
|
use markdown::Options;
|
2023-04-16 21:46:10 -05:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
|
|
|
#[derive(RustEmbed)]
|
|
|
|
#[folder = "data/other"]
|
|
|
|
struct OtherData;
|
|
|
|
|
|
|
|
#[derive(RustEmbed)]
|
|
|
|
#[folder = "data/poems"]
|
|
|
|
struct Poems;
|
|
|
|
|
|
|
|
pub fn get_homepage_paragraph() -> String {
|
|
|
|
let homepage_paragraph_content = OtherData::get("homepage.md").expect("Found homepage paragraph.");
|
|
|
|
let homepage_paragraph_to_string = std::str::from_utf8(homepage_paragraph_content.data.as_ref()).expect("Homepage file is valid UTF-8");
|
|
|
|
let test = markdown::to_html_with_options(homepage_paragraph_to_string, &Options::gfm()).unwrap();
|
|
|
|
test
|
|
|
|
}
|
2023-04-13 12:51:25 -05:00
|
|
|
|
|
|
|
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();
|
2023-04-16 21:46:10 -05:00
|
|
|
(
|
|
|
|
poem_title_to_html_string,
|
|
|
|
poem_content_to_html_string,
|
|
|
|
creation_date,
|
|
|
|
)
|
2023-04-13 12:51:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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}"),
|
|
|
|
}
|
2023-04-16 21:46:10 -05:00
|
|
|
}
|