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"; let ext = ".md";
// Get bots apps // Get bots apps
let mut bots_apps = Vec::new(); let bots_apps_loc = format!("{projects_dir}/bots");
for entry in glob(&format!("{projects_dir}/bots/*{ext}")).unwrap() { let bots_apps = glob(&format!("{bots_apps_loc}/*{ext}"))
bots_apps.push(read_md_file(&entry.unwrap().to_string_lossy())); .unwrap()
} .map(|e| read_md_file(&e.unwrap().to_string_lossy()))
.collect::<Vec<File>>();
// Get perso apps // Get perso apps
let mut perso_apps = Vec::new(); let perso_apps_loc = format!("{projects_dir}/perso");
for entry in glob(&format!("{projects_dir}/perso/*{ext}")).unwrap() { let perso_apps = glob(&format!("{perso_apps_loc}/*{ext}"))
perso_apps.push(read_md_file(&entry.unwrap().to_string_lossy())); .unwrap()
} .map(|e| read_md_file(&e.unwrap().to_string_lossy()))
.collect::<Vec<File>>();
config.tmpl.render( config.tmpl.render(
"portfolio.html", "portfolio.html",
PortfolioTemplate { PortfolioTemplate {
bots_app: bots_apps, bots_app: if bots_apps.is_empty() {
persos_app: perso_apps, 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, univ_content: read_md_file(&format!("{projects_dir}/univ{ext}")).content,
}, },
Infos { Infos {

View file

@ -73,7 +73,7 @@ pub struct File {
pub fn read_md_file(filename: &str) -> File { pub fn read_md_file(filename: &str) -> File {
// Read markdown 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) read_md(&text)
} }
@ -97,13 +97,35 @@ pub fn read_md(raw_text: &str) -> File {
let md_tree = markdown::to_mdast(&text, &parse_option).unwrap(); let md_tree = markdown::to_mdast(&text, &parse_option).unwrap();
let md_nodes = md_tree.children().unwrap(); let md_nodes = md_tree.children().unwrap();
let metadata = match &md_nodes[0] { 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::Yaml(v) => FrontMatter::Yaml(&v.value).parse(),
markdown::mdast::Node::Toml(v) => FrontMatter::Toml(&v.value).parse(), markdown::mdast::Node::Toml(v) => FrontMatter::Toml(&v.value).parse(),
markdown::mdast::Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(), markdown::mdast::Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(),
_ => FileMetadata::default(), _ => 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 // Convert to HTML
let html = markdown::to_html_with_options( let html = markdown::to_html_with_options(
&text, &text,
@ -114,19 +136,6 @@ pub fn read_md(raw_text: &str) -> File {
) )
.unwrap(); .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 { File {
metadata: Metadata { metadata: Metadata {
info: metadata, info: metadata,