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

View file

@ -9,7 +9,7 @@ use crate::{
template::{InfosPage, NavBar},
utils::{
markdown::File,
metadata::TypeFileMetadata,
metadata::MType,
misc::{make_kw, read_file, Html},
routes::cours::{excluded, get_filetree},
},
@ -54,7 +54,7 @@ fn get_content(
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 {

View file

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

View file

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

View file

@ -9,9 +9,9 @@ use std::fmt::Debug;
use std::fs;
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
#[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
fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) {
let mut metadata = Metadata {
info: FileMetadata::default(),
info: MFile::default(),
math: false,
mermaid: false,
syntax_highlight: false,
@ -146,8 +146,7 @@ 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, MType::Generic, Some(options));
el.replace(&data.content, ContentType::Html);
// 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
pub fn read_md(
path: &str,
raw_text: &str,
metadata_type: TypeFileMetadata,
options: Option<Options>,
) -> File {
pub fn read_md(path: &str, raw_text: &str, metadata_type: MType, options: Option<Options>) -> File {
let arena = Arena::new();
let mut opt = options.map_or_else(get_options, |specific_opt| specific_opt);
let root = parse_document(&arena, raw_text, &opt);
// Find metadata
let metadata = get_metadata(root, metadata_type);
let metadata = get(root, metadata_type);
// Update comrak render properties
opt.render.hardbreaks = metadata.hardbreaks;

View file

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

View file

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

View file

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