rename structs

This commit is contained in:
Mylloon 2024-11-10 10:58:08 +01:00
parent b1c4bbdb27
commit fa4d0ba7e8
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
9 changed files with 55 additions and 70 deletions

View file

@ -7,7 +7,7 @@ use crate::{
template::{InfosPage, NavBar}, template::{InfosPage, NavBar},
utils::{ utils::{
markdown::File, markdown::File,
metadata::TypeFileMetadata, metadata::MType,
misc::{make_kw, read_file, Html}, misc::{make_kw, read_file, Html},
routes::blog::{build_rss, get_post, get_posts, Post, BLOG_DIR, MIME_TYPE_RSS, POST_DIR}, routes::blog::{build_rss, get_post, get_posts, Post, BLOG_DIR, MIME_TYPE_RSS, POST_DIR},
}, },
@ -32,7 +32,7 @@ fn build_index(config: Config) -> String {
let mut posts = get_posts(&format!("{blog_dir}/{POST_DIR}")); let mut posts = get_posts(&format!("{blog_dir}/{POST_DIR}"));
// Get about // 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"), MType::Generic);
// Sort from newest to oldest // Sort from newest to oldest
posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day)); posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day));

View file

@ -8,7 +8,7 @@ use crate::{
template::{InfosPage, NavBar}, template::{InfosPage, NavBar},
utils::{ utils::{
markdown::File, markdown::File,
metadata::TypeFileMetadata, metadata::MType,
misc::{make_kw, read_file, Html}, misc::{make_kw, read_file, Html},
routes::contact::{find_links, remove_paragraphs}, routes::contact::{find_links, remove_paragraphs},
}, },
@ -85,10 +85,7 @@ fn build_page(config: Config) -> String {
let ext = ".md"; let ext = ".md";
// Get about // Get about
let about = read_file( let about = read_file(format!("{contacts_dir}/about.md"), MType::Generic);
format!("{contacts_dir}/about.md"),
TypeFileMetadata::Generic,
);
let socials_dir = "socials"; let socials_dir = "socials";
let mut socials = glob(&format!("{contacts_dir}/{socials_dir}/*{ext}")) let mut socials = glob(&format!("{contacts_dir}/{socials_dir}/*{ext}"))
@ -96,7 +93,7 @@ fn build_page(config: Config) -> String {
.map(|e| { .map(|e| {
read_file( read_file(
e.unwrap().to_string_lossy().to_string(), e.unwrap().to_string_lossy().to_string(),
TypeFileMetadata::Contact, MType::Contact,
) )
.unwrap() .unwrap()
}) })
@ -108,7 +105,7 @@ fn build_page(config: Config) -> String {
.map(|e| { .map(|e| {
read_file( read_file(
e.unwrap().to_string_lossy().to_string(), e.unwrap().to_string_lossy().to_string(),
TypeFileMetadata::Contact, MType::Contact,
) )
.unwrap() .unwrap()
}) })
@ -120,7 +117,7 @@ fn build_page(config: Config) -> String {
.map(|e| { .map(|e| {
read_file( read_file(
e.unwrap().to_string_lossy().to_string(), e.unwrap().to_string_lossy().to_string(),
TypeFileMetadata::Contact, MType::Contact,
) )
.unwrap() .unwrap()
}) })

View file

@ -9,7 +9,7 @@ use crate::{
template::{InfosPage, NavBar}, template::{InfosPage, NavBar},
utils::{ utils::{
markdown::File, markdown::File,
metadata::TypeFileMetadata, metadata::MType,
misc::{make_kw, read_file, Html}, misc::{make_kw, read_file, Html},
routes::cours::{excluded, get_filetree}, routes::cours::{excluded, get_filetree},
}, },
@ -54,7 +54,7 @@ fn get_content(
return None; return None;
} }
read_file(format!("{cours_dir}/{filename}"), TypeFileMetadata::Generic) read_file(format!("{cours_dir}/{filename}"), MType::Generic)
} }
fn build_page(info: &web::Query<PathRequest>, config: Config) -> String { fn build_page(info: &web::Query<PathRequest>, config: Config) -> String {

View file

@ -7,7 +7,7 @@ use crate::{
template::{InfosPage, NavBar}, template::{InfosPage, NavBar},
utils::{ utils::{
markdown::File, markdown::File,
metadata::TypeFileMetadata, metadata::MType,
misc::{make_kw, read_file, Html}, misc::{make_kw, read_file, Html},
}, },
}; };
@ -38,7 +38,7 @@ struct StyleAvatar {
fn build_page(config: Config) -> String { fn build_page(config: Config) -> String {
let mut file = read_file( let mut file = read_file(
format!("{}/index.md", config.locations.data_dir), format!("{}/index.md", config.locations.data_dir),
TypeFileMetadata::Index, MType::Index,
); );
// Default values // Default values
@ -68,7 +68,7 @@ fn build_page(config: Config) -> String {
} }
} }
} else { } else {
file = read_file("README.md".to_string(), TypeFileMetadata::Generic); file = read_file("README.md".to_string(), MType::Generic);
} }
config.tmpl.render( config.tmpl.render(

View file

@ -8,7 +8,7 @@ use crate::{
template::{InfosPage, NavBar}, template::{InfosPage, NavBar},
utils::{ utils::{
markdown::File, markdown::File,
metadata::TypeFileMetadata, metadata::MType,
misc::{make_kw, read_file, Html}, misc::{make_kw, read_file, Html},
}, },
}; };
@ -38,7 +38,7 @@ fn build_page(config: Config) -> String {
// Get about // Get about
let about = read_file( let about = read_file(
format!("{projects_dir}/about.md"), format!("{projects_dir}/about.md"),
TypeFileMetadata::Generic, MType::Generic,
); );
// Get apps // Get apps
@ -47,7 +47,7 @@ fn build_page(config: Config) -> String {
.map(|e| { .map(|e| {
read_file( read_file(
e.unwrap().to_string_lossy().to_string(), e.unwrap().to_string_lossy().to_string(),
TypeFileMetadata::Portfolio, MType::Portfolio,
) )
.unwrap() .unwrap()
}) })
@ -65,7 +65,7 @@ fn build_page(config: Config) -> String {
.map(|e| { .map(|e| {
read_file( read_file(
e.unwrap().to_string_lossy().to_string(), e.unwrap().to_string_lossy().to_string(),
TypeFileMetadata::Portfolio, MType::Portfolio,
) )
.unwrap() .unwrap()
}) })

View file

@ -9,9 +9,9 @@ use std::fmt::Debug;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use crate::utils::metadata::TypeFileMetadata; use crate::utils::metadata::MType;
use super::metadata::{get_metadata, FileMetadata, Metadata}; use super::metadata::{get, MFile, Metadata};
/// File description /// File description
#[derive(Content, Debug, Clone)] #[derive(Content, Debug, Clone)]
@ -122,7 +122,7 @@ fn custom_img_size(html: &str) -> String {
/// Fix local images to base64 and integration of markdown files /// Fix local images to base64 and integration of markdown files
fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) { fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) {
let mut metadata = Metadata { let mut metadata = Metadata {
info: FileMetadata::default(), info: MFile::default(),
math: false, math: false,
mermaid: false, mermaid: false,
syntax_highlight: false, syntax_highlight: false,
@ -146,8 +146,7 @@ fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) {
if mime == "text/markdown" { if mime == "text/markdown" {
let mut options = get_options(); let mut options = get_options();
options.extension.footnotes = false; options.extension.footnotes = false;
let data = let data = read_md(&img_path, &file, MType::Generic, Some(options));
read_md(&img_path, &file, TypeFileMetadata::Generic, Some(options));
el.replace(&data.content, ContentType::Html); el.replace(&data.content, ContentType::Html);
// Store the metadata for later merging // Store the metadata for later merging
@ -175,19 +174,14 @@ fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) {
} }
/// Transform markdown string to File structure /// Transform markdown string to File structure
pub fn read_md( pub fn read_md(path: &str, raw_text: &str, metadata_type: MType, options: Option<Options>) -> File {
path: &str,
raw_text: &str,
metadata_type: TypeFileMetadata,
options: Option<Options>,
) -> File {
let arena = Arena::new(); let arena = Arena::new();
let mut opt = options.map_or_else(get_options, |specific_opt| specific_opt); let mut opt = options.map_or_else(get_options, |specific_opt| specific_opt);
let root = parse_document(&arena, raw_text, &opt); let root = parse_document(&arena, raw_text, &opt);
// Find metadata // Find metadata
let metadata = get_metadata(root, metadata_type); let metadata = get(root, metadata_type);
// Update comrak render properties // Update comrak render properties
opt.render.hardbreaks = metadata.hardbreaks; opt.render.hardbreaks = metadata.hardbreaks;

View file

@ -69,7 +69,7 @@ pub struct FileMetadataPortfolio {
/// List of available metadata types /// List of available metadata types
#[derive(Hash, PartialEq, Eq, Clone, Copy)] #[derive(Hash, PartialEq, Eq, Clone, Copy)]
pub enum TypeFileMetadata { pub enum MType {
Blog, Blog,
Contact, Contact,
Generic, Generic,
@ -80,7 +80,7 @@ pub enum TypeFileMetadata {
/// Structure who holds all the metadata the file have /// Structure who holds all the metadata the file have
/// Usually all fields are None except one /// Usually all fields are None except one
#[derive(Content, Debug, Default, Deserialize, Clone)] #[derive(Content, Debug, Default, Deserialize, Clone)]
pub struct FileMetadata { pub struct MFile {
pub hardbreaks: bool, pub hardbreaks: bool,
pub blog: Option<FileMetadataBlog>, pub blog: Option<FileMetadataBlog>,
pub contact: Option<FileMetadataContact>, pub contact: Option<FileMetadataContact>,
@ -92,7 +92,7 @@ pub struct FileMetadata {
/// Global metadata /// Global metadata
#[derive(Content, Debug, Clone)] #[derive(Content, Debug, Clone)]
pub struct Metadata { pub struct Metadata {
pub info: FileMetadata, pub info: MFile,
pub math: bool, pub math: bool,
pub mermaid: bool, pub mermaid: bool,
pub syntax_highlight: bool, pub syntax_highlight: bool,
@ -114,44 +114,44 @@ fn deserialize_metadata<T: Default + serde::de::DeserializeOwned>(text: &str) ->
} }
/// Fetch metadata from AST /// Fetch metadata from AST
pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: TypeFileMetadata) -> FileMetadata { pub fn get<'a>(root: &'a AstNode<'a>, mtype: MType) -> MFile {
root.children() root.children()
.map(|node| { .map(|node| {
let generic = FileMetadata { let generic = MFile {
hardbreaks: true, hardbreaks: true,
..FileMetadata::default() ..MFile::default()
}; };
match &node.data.borrow().value { match &node.data.borrow().value {
// Extract metadata from frontmatter // Extract metadata from frontmatter
NodeValue::FrontMatter(text) => match mtype { NodeValue::FrontMatter(text) => match mtype {
TypeFileMetadata::Blog => { MType::Blog => {
let metadata: FileMetadataBlog = deserialize_metadata(text); let metadata: FileMetadataBlog = deserialize_metadata(text);
FileMetadata { MFile {
blog: Some(metadata.clone()), blog: Some(metadata.clone()),
hardbreaks: metadata.hardbreaks.unwrap_or_default(), hardbreaks: metadata.hardbreaks.unwrap_or_default(),
..FileMetadata::default() ..MFile::default()
} }
} }
TypeFileMetadata::Contact => { MType::Contact => {
let mut metadata: FileMetadataContact = deserialize_metadata(text); let mut metadata: FileMetadataContact = deserialize_metadata(text);
// Trim descriptions // Trim descriptions
if let Some(desc) = &mut metadata.description { if let Some(desc) = &mut metadata.description {
desc.clone_from(&desc.trim().into()); desc.clone_from(&desc.trim().into());
} }
FileMetadata { MFile {
contact: Some(metadata), contact: Some(metadata),
..FileMetadata::default() ..MFile::default()
} }
} }
TypeFileMetadata::Generic => generic, MType::Generic => generic,
TypeFileMetadata::Index => FileMetadata { MType::Index => MFile {
index: Some(deserialize_metadata(text)), index: Some(deserialize_metadata(text)),
..FileMetadata::default() ..MFile::default()
}, },
TypeFileMetadata::Portfolio => FileMetadata { MType::Portfolio => MFile {
portfolio: Some(deserialize_metadata(text)), portfolio: Some(deserialize_metadata(text)),
..FileMetadata::default() ..MFile::default()
}, },
}, },
_ => generic, _ => generic,
@ -160,22 +160,22 @@ pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: TypeFileMetadata) -> FileM
.next() .next()
.map_or_else( .map_or_else(
|| match mtype { || match mtype {
TypeFileMetadata::Blog => FileMetadata { MType::Blog => MFile {
blog: Some(FileMetadataBlog::default()), blog: Some(FileMetadataBlog::default()),
..FileMetadata::default() ..MFile::default()
}, },
TypeFileMetadata::Contact => FileMetadata { MType::Contact => MFile {
contact: Some(FileMetadataContact::default()), contact: Some(FileMetadataContact::default()),
..FileMetadata::default() ..MFile::default()
}, },
TypeFileMetadata::Generic => FileMetadata::default(), MType::Generic => MFile::default(),
TypeFileMetadata::Index => FileMetadata { MType::Index => MFile {
index: Some(FileMetadataIndex::default()), index: Some(FileMetadataIndex::default()),
..FileMetadata::default() ..MFile::default()
}, },
TypeFileMetadata::Portfolio => FileMetadata { MType::Portfolio => MFile {
portfolio: Some(FileMetadataPortfolio::default()), portfolio: Some(FileMetadataPortfolio::default()),
..FileMetadata::default() ..MFile::default()
}, },
}, },
|data| data, |data| data,

View file

@ -13,7 +13,7 @@ use crate::config::FileConfiguration;
use super::{ use super::{
markdown::{read_md, File}, markdown::{read_md, File},
metadata::{FileMetadata, Metadata, TypeFileMetadata}, metadata::{MFile, MType, Metadata},
}; };
#[cached] #[cached]
@ -57,7 +57,7 @@ impl Responder for Html {
/// Read a file /// Read a file
#[cached] #[cached]
pub fn read_file(filename: String, expected_file: TypeFileMetadata) -> Option<File> { pub fn read_file(filename: String, expected_file: MType) -> Option<File> {
Path::new(&filename.clone()) Path::new(&filename.clone())
.extension() .extension()
.and_then(|ext| match ext.to_str().unwrap() { .and_then(|ext| match ext.to_str().unwrap() {
@ -73,7 +73,7 @@ fn read_pdf(data: Vec<u8>) -> File {
File { File {
metadata: Metadata { metadata: Metadata {
info: FileMetadata::default(), info: MFile::default(),
mermaid: false, mermaid: false,
syntax_highlight: false, syntax_highlight: false,
math: false, math: false,

View file

@ -19,7 +19,7 @@ use crate::{
utils::{ utils::{
date::Date, date::Date,
markdown::{get_options, File}, markdown::{get_options, File},
metadata::{get_metadata, FileMetadataBlog, TypeFileMetadata}, metadata::{get, FileMetadataBlog, MType},
misc::{get_url, make_kw, read_file}, misc::{get_url, make_kw, read_file},
}, },
}; };
@ -44,10 +44,7 @@ impl Post {
let blog_dir = format!("{data_dir}/{BLOG_DIR}/{POST_DIR}"); let blog_dir = format!("{data_dir}/{BLOG_DIR}/{POST_DIR}");
let ext = ".md"; let ext = ".md";
if let Some(file) = read_file( if let Some(file) = read_file(format!("{blog_dir}/{}{ext}", self.url), MType::Blog) {
format!("{blog_dir}/{}{ext}", self.url),
TypeFileMetadata::Blog,
) {
self.content = Some(file.content); self.content = Some(file.content);
} }
} }
@ -89,7 +86,7 @@ pub fn get_posts(location: &str) -> Vec<Post> {
let options = get_options(); let options = get_options();
let root = parse_document(&arena, &text, &options); let root = parse_document(&arena, &text, &options);
let mut metadata = get_metadata(root, TypeFileMetadata::Blog).blog.unwrap(); let mut metadata = get(root, MType::Blog).blog.unwrap();
// Always have a title // Always have a title
metadata.title = metadata metadata.title = metadata
@ -142,10 +139,7 @@ pub fn get_post(
let blog_dir = format!("{data_dir}/{BLOG_DIR}/{POST_DIR}"); let blog_dir = format!("{data_dir}/{BLOG_DIR}/{POST_DIR}");
let ext = ".md"; let ext = ".md";
*post = read_file( *post = read_file(format!("{blog_dir}/{filename}{ext}"), MType::Blog);
format!("{blog_dir}/{filename}{ext}"),
TypeFileMetadata::Blog,
);
let default = ( let default = (
filename, filename,