From 3d458f062b370225165d3f1011459c8a25ea852d Mon Sep 17 00:00:00 2001 From: Daniel Moreto Date: Tue, 22 Oct 2024 23:46:55 -0300 Subject: [PATCH 1/8] refact: change markdown.rs to add a new parameter for using file name as slug --- src/markdown.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/markdown.rs b/src/markdown.rs index e5a5d17..b0b635f 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -5,7 +5,7 @@ use frontmatter_gen::{extract, Frontmatter}; use std::fs; use std::path::Path; -pub fn process_file(path: &Path, site_data: &mut Data) -> Result<(), String> { +pub fn process_file(path: &Path, site_data: &mut Data, use_filename_as_slug: bool) -> Result<(), String> { let file_content = fs::read_to_string(path).map_err(|e| e.to_string())?; let (frontmatter, markdown) = parse_front_matter(&file_content)?; let mut options = ComrakOptions::default(); @@ -39,7 +39,13 @@ pub fn process_file(path: &Path, site_data: &mut Data) -> Result<(), String> { let title = get_title(&frontmatter, markdown); let tags = get_tags(&frontmatter); - let slug = get_slug(&frontmatter, path); + let slug = { + if use_filename_as_slug { + path.file_name().expect("Error on getting file name").to_str().expect("Error on converting file name to string").to_string().replace(".md", "") + } else { + get_slug(&frontmatter, path) + } + }; let date = get_date(&frontmatter, path); let extra = frontmatter.get("extra").map(std::borrow::ToOwned::to_owned); From 1de1cc7cb156d59a486bcf4bbc50845a990919dd Mon Sep 17 00:00:00 2001 From: Daniel Moreto Date: Tue, 22 Oct 2024 23:49:33 -0300 Subject: [PATCH 2/8] feat: add checking and generation for pages that needs to have an slug based on file name --- src/site.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/site.rs b/src/site.rs index 69bb226..abdb974 100644 --- a/src/site.rs +++ b/src/site.rs @@ -11,6 +11,8 @@ use std::{fs, process, sync::Arc}; use tera::{Context, Tera}; use walkdir::WalkDir; +const NAME_BASED_SLUG_FILES: [&str; 1] = ["404.md"]; + #[derive(Serialize)] pub struct Data<'a> { pub site: Marmite<'a>, @@ -97,6 +99,29 @@ fn render_templates(site_data: &Data, tera: &Tera, output_dir: &Path) -> Result< )?; } + // Check and guarantees that page 404 was generated even if 404.md is removed + let file_404_path = output_dir.join("404.html"); + if !file_404_path.exists() { + let mut content_context = global_context.clone(); + let page_404_content = Content { + html: String::from("Page not found :/"), + title: String::from("Page not found"), + date: None, + slug: String::from(""), + extra: None, + tags: vec![] + }; + content_context.insert("title", &page_404_content.title); + content_context.insert("content", &page_404_content); + render_html( + "content.html", + "404.html", + tera, + &content_context, + output_dir, + )?; + } + // Render tagged_contents let mut unique_tags: Vec<(String, usize)> = Vec::new(); let tags_dir = output_dir.join("tag"); @@ -347,11 +372,24 @@ fn collect_content(content_dir: &std::path::PathBuf, site_data: &mut Data) { .into_iter() .filter_map(Result::ok) .filter(|e| { - e.path().is_file() && e.path().extension().and_then(|ext| ext.to_str()) == Some("md") + let file_name = e.path().file_name().and_then(|ext| ext.to_str()).expect("Could not get file name"); + let file_extension = e.path().extension().and_then(|ext| ext.to_str()); + e.path().is_file() && !NAME_BASED_SLUG_FILES.contains(&file_name) && file_extension == Some("md") }) .for_each(|entry| { - if let Err(e) = process_file(entry.path(), site_data) { + if let Err(e) = process_file(entry.path(), site_data, false) { error!("Failed to process file {}: {}", entry.path().display(), e); } }); + + NAME_BASED_SLUG_FILES + .into_iter() + .for_each(|slugged_file| { + let slugged_path = content_dir.join(slugged_file); + if slugged_path.exists() { + if let Err(e) = process_file(slugged_path.as_path(), site_data, true) { + error!("Failed to process file {}: {}", slugged_path.as_path().display(), e); + } + } + }) } From aaa05976bcc1f2c76f7f0896ef2455f09bb08683 Mon Sep 17 00:00:00 2001 From: Daniel Moreto Date: Tue, 22 Oct 2024 23:52:24 -0300 Subject: [PATCH 3/8] feat: update base template It was updated to check if current_page variable is fulfilled before checking it --- example/templates/base.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/templates/base.html b/example/templates/base.html index 92078f1..ca2bd8d 100644 --- a/example/templates/base.html +++ b/example/templates/base.html @@ -37,7 +37,7 @@

{{ site.name }}

    {% for item in menu %}
  • - {% if current_page == item.1 %} + {% if current_page and current_page == item.1 %} {% else %} {{ item.0 | safe }} From a7580e9593c239a6aafe6f4b2ccdf5ef740ce167 Mon Sep 17 00:00:00 2001 From: Daniel Moreto Date: Tue, 22 Oct 2024 23:52:53 -0300 Subject: [PATCH 4/8] feat: add a default 404.md file on example content folder --- example/content/404.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 example/content/404.md diff --git a/example/content/404.md b/example/content/404.md new file mode 100644 index 0000000..58bfaab --- /dev/null +++ b/example/content/404.md @@ -0,0 +1,5 @@ +--- +title: Page not found! +--- + +Page not found From d8fd325a89dca6df523822ffc75a0dd4bb65e54d Mon Sep 17 00:00:00 2001 From: Daniel Moreto Date: Tue, 22 Oct 2024 23:54:13 -0300 Subject: [PATCH 5/8] feat: update server.rs It was update to render 404.html page and if it's not found, render a pure 404 text --- src/server.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/server.rs b/src/server.rs index 42cf820..24bd58e 100644 --- a/src/server.rs +++ b/src/server.rs @@ -39,6 +39,7 @@ fn handle_request( }; let file_path = output_folder.join(request_path); + let error_path = output_folder.join("404.html"); if file_path.is_file() { match File::open(&file_path) { @@ -70,6 +71,21 @@ fn handle_request( request_path, request.http_version() ); - Ok(Response::from_string("404 Not Found").with_status_code(404)) + render_not_found(&error_path) + } +} + +fn render_not_found(error_path: &PathBuf) -> Result>>, String> { + match File::open(&error_path) { + Ok(mut file) => { + let mut buffer = Vec::new(); + std::io::copy(&mut file, &mut buffer).map_err(|e| e.to_string())?; + let resp = Response::from_data(buffer); + Ok(resp) + } + Err(err) => { + error!("Error on generating page 404 - {}", err); + Ok(Response::from_string("404 Not Found").with_status_code(404)) + } } } From f075207cd1468f7f669769176c73e0a59a9d2c39 Mon Sep 17 00:00:00 2001 From: Daniel Moreto Date: Wed, 23 Oct 2024 07:15:25 -0300 Subject: [PATCH 6/8] fix: fix format issues --- src/markdown.rs | 13 +++++++++++-- src/site.rs | 32 ++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/markdown.rs b/src/markdown.rs index b0b635f..bd1c49d 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -5,7 +5,11 @@ use frontmatter_gen::{extract, Frontmatter}; use std::fs; use std::path::Path; -pub fn process_file(path: &Path, site_data: &mut Data, use_filename_as_slug: bool) -> Result<(), String> { +pub fn process_file( + path: &Path, + site_data: &mut Data, + use_filename_as_slug: bool, +) -> Result<(), String> { let file_content = fs::read_to_string(path).map_err(|e| e.to_string())?; let (frontmatter, markdown) = parse_front_matter(&file_content)?; let mut options = ComrakOptions::default(); @@ -41,7 +45,12 @@ pub fn process_file(path: &Path, site_data: &mut Data, use_filename_as_slug: boo let tags = get_tags(&frontmatter); let slug = { if use_filename_as_slug { - path.file_name().expect("Error on getting file name").to_str().expect("Error on converting file name to string").to_string().replace(".md", "") + path.file_name() + .expect("Error on getting file name") + .to_str() + .expect("Error on converting file name to string") + .to_string() + .replace(".md", "") } else { get_slug(&frontmatter, path) } diff --git a/src/site.rs b/src/site.rs index abdb974..c2063e0 100644 --- a/src/site.rs +++ b/src/site.rs @@ -109,7 +109,7 @@ fn render_templates(site_data: &Data, tera: &Tera, output_dir: &Path) -> Result< date: None, slug: String::from(""), extra: None, - tags: vec![] + tags: vec![], }; content_context.insert("title", &page_404_content.title); content_context.insert("content", &page_404_content); @@ -372,9 +372,15 @@ fn collect_content(content_dir: &std::path::PathBuf, site_data: &mut Data) { .into_iter() .filter_map(Result::ok) .filter(|e| { - let file_name = e.path().file_name().and_then(|ext| ext.to_str()).expect("Could not get file name"); + let file_name = e + .path() + .file_name() + .and_then(|ext| ext.to_str()) + .expect("Could not get file name"); let file_extension = e.path().extension().and_then(|ext| ext.to_str()); - e.path().is_file() && !NAME_BASED_SLUG_FILES.contains(&file_name) && file_extension == Some("md") + e.path().is_file() + && !NAME_BASED_SLUG_FILES.contains(&file_name) + && file_extension == Some("md") }) .for_each(|entry| { if let Err(e) = process_file(entry.path(), site_data, false) { @@ -382,14 +388,16 @@ fn collect_content(content_dir: &std::path::PathBuf, site_data: &mut Data) { } }); - NAME_BASED_SLUG_FILES - .into_iter() - .for_each(|slugged_file| { - let slugged_path = content_dir.join(slugged_file); - if slugged_path.exists() { - if let Err(e) = process_file(slugged_path.as_path(), site_data, true) { - error!("Failed to process file {}: {}", slugged_path.as_path().display(), e); - } + NAME_BASED_SLUG_FILES.into_iter().for_each(|slugged_file| { + let slugged_path = content_dir.join(slugged_file); + if slugged_path.exists() { + if let Err(e) = process_file(slugged_path.as_path(), site_data, true) { + error!( + "Failed to process file {}: {}", + slugged_path.as_path().display(), + e + ); } - }) + } + }) } From 2dc010d33391f80f324b37668df27d0333453b3b Mon Sep 17 00:00:00 2001 From: Daniel Moreto Date: Wed, 23 Oct 2024 07:28:48 -0300 Subject: [PATCH 7/8] fix: change message when it is not possible to find page 404.html --- src/server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.rs b/src/server.rs index 24bd58e..184b549 100644 --- a/src/server.rs +++ b/src/server.rs @@ -84,7 +84,7 @@ fn render_not_found(error_path: &PathBuf) -> Result>>, S Ok(resp) } Err(err) => { - error!("Error on generating page 404 - {}", err); + error!("Error on rendering page 404 - {}", err); Ok(Response::from_string("404 Not Found").with_status_code(404)) } } From 5ae8c65393b7c5d53c4d70d5ba53d2cbd31ca9a1 Mon Sep 17 00:00:00 2001 From: Daniel Moreto Date: Wed, 23 Oct 2024 07:29:28 -0300 Subject: [PATCH 8/8] refact: set the message of page 404 to be the same as when it is auto-generated --- example/content/404.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/content/404.md b/example/content/404.md index 58bfaab..a738c00 100644 --- a/example/content/404.md +++ b/example/content/404.md @@ -1,5 +1,5 @@ --- -title: Page not found! +title: Page not found --- -Page not found +Page not found :/