better handling of empty datas
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-04-19 14:28:53 +02:00
parent f4696fbb1a
commit 3da1ce6aa9
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 49 additions and 30 deletions

View file

@ -26,22 +26,32 @@ pub fn get_page(config: Config) -> String {
let ext = ".md";
// Get bots apps
let mut bots_apps = Vec::new();
for entry in glob(&format!("{projects_dir}/bots/*{ext}")).unwrap() {
bots_apps.push(read_md_file(&entry.unwrap().to_string_lossy()));
}
let bots_apps_loc = format!("{projects_dir}/bots");
let bots_apps = glob(&format!("{bots_apps_loc}/*{ext}"))
.unwrap()
.map(|e| read_md_file(&e.unwrap().to_string_lossy()))
.collect::<Vec<File>>();
// Get perso apps
let mut perso_apps = Vec::new();
for entry in glob(&format!("{projects_dir}/perso/*{ext}")).unwrap() {
perso_apps.push(read_md_file(&entry.unwrap().to_string_lossy()));
}
let perso_apps_loc = format!("{projects_dir}/perso");
let perso_apps = glob(&format!("{perso_apps_loc}/*{ext}"))
.unwrap()
.map(|e| read_md_file(&e.unwrap().to_string_lossy()))
.collect::<Vec<File>>();
config.tmpl.render(
"portfolio.html",
PortfolioTemplate {
bots_app: bots_apps,
persos_app: perso_apps,
bots_app: if bots_apps.is_empty() {
vec![read_md_file(&bots_apps_loc)]
} else {
bots_apps
},
persos_app: if perso_apps.is_empty() {
vec![read_md_file(&perso_apps_loc)]
} else {
perso_apps
},
univ_content: read_md_file(&format!("{projects_dir}/univ{ext}")).content,
},
Infos {

View file

@ -73,7 +73,7 @@ pub struct File {
pub fn read_md_file(filename: &str) -> File {
// Read markdown file
let text = fs::read_to_string(filename).unwrap();
let text = fs::read_to_string(filename).unwrap_or(format!("{filename} is empty"));
read_md(&text)
}
@ -97,12 +97,34 @@ pub fn read_md(raw_text: &str) -> File {
let md_tree = markdown::to_mdast(&text, &parse_option).unwrap();
let md_nodes = md_tree.children().unwrap();
let metadata = match &md_nodes[0] {
markdown::mdast::Node::Yaml(v) => FrontMatter::Yaml(&v.value).parse(),
markdown::mdast::Node::Toml(v) => FrontMatter::Toml(&v.value).parse(),
markdown::mdast::Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(),
_ => FileMetadata::default(),
};
let metadata;
let presence_mermaid;
let presence_code;
if md_nodes.is_empty() {
metadata = FileMetadata::default();
presence_mermaid = false;
presence_code = false;
} else {
metadata = match &md_nodes[0] {
markdown::mdast::Node::Yaml(v) => FrontMatter::Yaml(&v.value).parse(),
markdown::mdast::Node::Toml(v) => FrontMatter::Toml(&v.value).parse(),
markdown::mdast::Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(),
_ => FileMetadata::default(),
};
// Find if document contains mermaid diagram
let mermaid = Some(String::from("mermaid"));
presence_mermaid = md_nodes.iter().any(|x| match x {
markdown::mdast::Node::Code(code) => code.lang == mermaid,
_ => false,
});
// Find if document contains code to highlight
presence_code = md_nodes.iter().any(|x| match x {
markdown::mdast::Node::Code(code) => code.lang != mermaid,
_ => false,
});
}
// Convert to HTML
let html = markdown::to_html_with_options(
@ -114,19 +136,6 @@ pub fn read_md(raw_text: &str) -> File {
)
.unwrap();
// Find if document contains mermaid diagram
let mermaid = Some(String::from("mermaid"));
let presence_mermaid = md_nodes.iter().any(|x| match x {
markdown::mdast::Node::Code(code) => code.lang == mermaid,
_ => false,
});
// Find if document contains code to highlight
let presence_code = md_nodes.iter().any(|x| match x {
markdown::mdast::Node::Code(code) => code.lang != mermaid,
_ => false,
});
File {
metadata: Metadata {
info: metadata,