cache files

This commit is contained in:
Mylloon 2024-11-09 18:25:13 +01:00
parent 182b17c47f
commit eb55d13c01
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
8 changed files with 66 additions and 43 deletions

View file

@ -31,8 +31,7 @@ fn build_index(config: Config) -> String {
let mut posts = get_posts(&format!("{blog_dir}/{POST_DIR}"));
// Get about
let about: Option<File> =
read_file(&format!("{blog_dir}/about.md"), &TypeFileMetadata::Generic);
let about: Option<File> = read_file(format!("{blog_dir}/about.md"), TypeFileMetadata::Generic);
// Sort from newest to oldest
posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day));

View file

@ -85,26 +85,44 @@ fn build_page(config: Config) -> String {
// Get about
let about = read_file(
&format!("{contacts_dir}/about.md"),
&TypeFileMetadata::Generic,
format!("{contacts_dir}/about.md"),
TypeFileMetadata::Generic,
);
let socials_dir = "socials";
let mut socials = glob(&format!("{contacts_dir}/{socials_dir}/*{ext}"))
.unwrap()
.map(|e| read_file(&e.unwrap().to_string_lossy(), &TypeFileMetadata::Contact).unwrap())
.map(|e| {
read_file(
e.unwrap().to_string_lossy().to_string(),
TypeFileMetadata::Contact,
)
.unwrap()
})
.collect::<Vec<File>>();
let forges_dir = "forges";
let mut forges = glob(&format!("{contacts_dir}/{forges_dir}/*{ext}"))
.unwrap()
.map(|e| read_file(&e.unwrap().to_string_lossy(), &TypeFileMetadata::Contact).unwrap())
.map(|e| {
read_file(
e.unwrap().to_string_lossy().to_string(),
TypeFileMetadata::Contact,
)
.unwrap()
})
.collect::<Vec<File>>();
let others_dir = "others";
let mut others = glob(&format!("{contacts_dir}/{others_dir}/*{ext}"))
.unwrap()
.map(|e| read_file(&e.unwrap().to_string_lossy(), &TypeFileMetadata::Contact).unwrap())
.map(|e| {
read_file(
e.unwrap().to_string_lossy().to_string(),
TypeFileMetadata::Contact,
)
.unwrap()
})
.collect::<Vec<File>>();
// Remove paragraphs in custom statements

View file

@ -53,10 +53,7 @@ fn get_content(
return None;
}
read_file(
&format!("{cours_dir}/{filename}"),
&TypeFileMetadata::Generic,
)
read_file(format!("{cours_dir}/{filename}"), TypeFileMetadata::Generic)
}
fn build_page(info: &web::Query<PathRequest>, config: Config) -> String {

View file

@ -36,8 +36,8 @@ struct StyleAvatar {
#[once(time = 60)]
fn build_page(config: Config) -> String {
let mut file = read_file(
&format!("{}/index.md", config.locations.data_dir),
&TypeFileMetadata::Index,
format!("{}/index.md", config.locations.data_dir),
TypeFileMetadata::Index,
);
// Default values
@ -67,7 +67,7 @@ fn build_page(config: Config) -> String {
}
}
} else {
file = read_file("README.md", &TypeFileMetadata::Generic);
file = read_file("README.md".to_string(), TypeFileMetadata::Generic);
}
config.tmpl.render(

View file

@ -5,11 +5,11 @@ use ramhorns::Content;
use crate::{
config::Config,
template::{InfosPage, NavBar},
utils::{
markdown::{File, TypeFileMetadata},
misc::{make_kw, read_file, Html},
},
template::{InfosPage, NavBar},
};
#[get("/portfolio")]
@ -36,14 +36,20 @@ fn build_page(config: Config) -> String {
// Get about
let about = read_file(
&format!("{projects_dir}/about.md"),
&TypeFileMetadata::Generic,
format!("{projects_dir}/about.md"),
TypeFileMetadata::Generic,
);
// Get apps
let apps = glob(&format!("{apps_dir}/*{ext}"))
.unwrap()
.map(|e| read_file(&e.unwrap().to_string_lossy(), &TypeFileMetadata::Portfolio).unwrap())
.map(|e| {
read_file(
e.unwrap().to_string_lossy().to_string(),
TypeFileMetadata::Portfolio,
)
.unwrap()
})
.collect::<Vec<File>>();
let appdata = if apps.is_empty() {
@ -55,7 +61,13 @@ fn build_page(config: Config) -> String {
// Get archived apps
let archived_apps = glob(&format!("{apps_dir}/archive/*{ext}"))
.unwrap()
.map(|e| read_file(&e.unwrap().to_string_lossy(), &TypeFileMetadata::Portfolio).unwrap())
.map(|e| {
read_file(
e.unwrap().to_string_lossy().to_string(),
TypeFileMetadata::Portfolio,
)
.unwrap()
})
.collect::<Vec<File>>();
let archived_appdata = if archived_apps.is_empty() {

View file

@ -45,7 +45,7 @@ impl<'a> Deserialize<'a> for Tag {
}
/// Metadata for contact entry
#[derive(Content, Debug, Default, Deserialize)]
#[derive(Content, Debug, Default, Deserialize, Clone)]
pub struct FileMetadataContact {
pub title: String,
pub custom: Option<bool>,
@ -56,7 +56,7 @@ pub struct FileMetadataContact {
}
/// Metadata for index page
#[derive(Content, Debug, Default, Deserialize)]
#[derive(Content, Debug, Default, Deserialize, Clone)]
pub struct FileMetadataIndex {
pub name: Option<String>,
pub pronouns: Option<String>,
@ -66,7 +66,7 @@ pub struct FileMetadataIndex {
}
/// Metadata for portfolio cards
#[derive(Content, Debug, Default, Deserialize)]
#[derive(Content, Debug, Default, Deserialize, Clone)]
pub struct FileMetadataPortfolio {
pub title: Option<String>,
pub link: Option<String>,
@ -75,6 +75,7 @@ pub struct FileMetadataPortfolio {
}
/// List of available metadata types
#[derive(Hash, PartialEq, Eq, Clone, Copy)]
pub enum TypeFileMetadata {
Blog,
Contact,
@ -85,7 +86,7 @@ pub enum TypeFileMetadata {
/// Structure who holds all the metadata the file have
/// Usually all fields are None except one
#[derive(Content, Debug, Default, Deserialize)]
#[derive(Content, Debug, Default, Deserialize, Clone)]
pub struct FileMetadata {
pub hardbreaks: bool,
pub blog: Option<FileMetadataBlog>,
@ -96,7 +97,7 @@ pub struct FileMetadata {
#[allow(clippy::struct_excessive_bools)]
/// Global metadata
#[derive(Content, Debug)]
#[derive(Content, Debug, Clone)]
pub struct Metadata {
pub info: FileMetadata,
pub math: bool,
@ -115,7 +116,7 @@ impl Metadata {
}
/// File description
#[derive(Content, Debug)]
#[derive(Content, Debug, Clone)]
pub struct File {
pub metadata: Metadata,
pub content: String,
@ -247,12 +248,8 @@ fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) {
if mime == "text/markdown" {
let mut options = get_options();
options.extension.footnotes = false;
let data = read_md(
&img_path,
&file,
&TypeFileMetadata::Generic,
Some(options),
);
let data =
read_md(&img_path, &file, TypeFileMetadata::Generic, Some(options));
el.replace(&data.content, ContentType::Html);
// Store the metadata for later merging
@ -283,7 +280,7 @@ fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) {
pub fn read_md(
path: &str,
raw_text: &str,
metadata_type: &TypeFileMetadata,
metadata_type: TypeFileMetadata,
options: Option<Options>,
) -> File {
let arena = Arena::new();
@ -333,7 +330,7 @@ fn deserialize_metadata<T: Default + serde::de::DeserializeOwned>(text: &str) ->
}
/// Fetch metadata from AST
pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: &TypeFileMetadata) -> FileMetadata {
pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: TypeFileMetadata) -> FileMetadata {
root.children()
.map(|node| {
let generic = FileMetadata {

View file

@ -53,13 +53,14 @@ impl Responder for Html {
}
/// Read a file
pub fn read_file(filename: &str, expected_file: &TypeFileMetadata) -> Option<File> {
Path::new(filename)
#[cached]
pub fn read_file(filename: String, expected_file: TypeFileMetadata) -> Option<File> {
Path::new(&filename.clone())
.extension()
.and_then(|ext| match ext.to_str().unwrap() {
"pdf" => fs::read(filename).map_or(None, |bytes| Some(read_pdf(bytes))),
_ => fs::read_to_string(filename).map_or(None, |text| {
Some(read_md(filename, &text, expected_file, None))
_ => fs::read_to_string(&filename).map_or(None, |text| {
Some(read_md(&filename, &text, expected_file, None))
}),
})
}

View file

@ -44,8 +44,8 @@ impl Post {
let ext = ".md";
if let Some(file) = read_file(
&format!("{blog_dir}/{}{ext}", self.url),
&TypeFileMetadata::Blog,
format!("{blog_dir}/{}{ext}", self.url),
TypeFileMetadata::Blog,
) {
self.content = Some(file.content);
}
@ -88,8 +88,7 @@ pub fn get_posts(location: &str) -> Vec<Post> {
let options = get_options();
let root = parse_document(&arena, &text, &options);
let mut metadata =
get_metadata(root, &TypeFileMetadata::Blog).blog.unwrap();
let mut metadata = get_metadata(root, TypeFileMetadata::Blog).blog.unwrap();
// Always have a title
metadata.title = metadata
@ -143,8 +142,8 @@ pub fn get_post(
let ext = ".md";
*post = read_file(
&format!("{blog_dir}/{filename}{ext}"),
&TypeFileMetadata::Blog,
format!("{blog_dir}/{filename}{ext}"),
TypeFileMetadata::Blog,
);
let default = (