X-Git-Url: https://git.r.bdr.sh/rbdr/page/blobdiff_plain/45fbf824248215a737d5d0b52920b43b68941ad3..260e8ec69b8e08b9fd105bf688e7a3a9fafecd61:/src/file_handler/file_strategies/gemini.rs diff --git a/src/file_handler/file_strategies/gemini.rs b/src/file_handler/file_strategies/gemini.rs index 977464c..d8bde14 100644 --- a/src/file_handler/file_strategies/gemini.rs +++ b/src/file_handler/file_strategies/gemini.rs @@ -6,6 +6,7 @@ use std::fs::{create_dir_all, read_to_string, File as IOFile}; use crate::file_handler::{File, FileType, FileHandlerStrategy}; use crate::gemini_parser::parse; +use crate::html_renderer::render_html; impl Strategy { fn is_title(&self, line: &str) -> bool { @@ -44,7 +45,7 @@ impl FileHandlerStrategy for Strategy { } } - fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &String) { + fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &str) { let gemini_contents = read_to_string(&file.path).unwrap(); // Front matter extraction @@ -52,21 +53,23 @@ impl FileHandlerStrategy for Strategy { let mut lines_found = 0; let mut title = ""; let mut description = ""; - for line in lines[..2].iter() { - if self.is_title(&line) { - title = self.get_title(&line).trim(); - lines_found = lines_found + 1; - continue; - } - if self.is_description(&line) { - description = self.get_description(&line).trim(); - lines_found = lines_found + 1; - continue; + if let Some(slice) = lines.get(..2) { + for line in slice.iter() { + if self.is_title(&line) { + title = self.get_title(&line).trim(); + lines_found = lines_found + 1; + continue; + } + if self.is_description(&line) { + description = self.get_description(&line).trim(); + lines_found = lines_found + 1; + continue; + } } } let gemini_source = lines[lines_found..].join("\n"); - let content_html = parse(&gemini_source[..]); + let content_html = render_html(parse(&gemini_source[..])); let generated_html = layout .replace("{{ title }}", title) @@ -90,14 +93,16 @@ impl FileHandlerStrategy for Strategy { // Front matter extraction let lines: Vec<&str> = gemini_contents.split("\n").collect(); let mut lines_found = 0; - for line in lines[..2].iter() { - if self.is_title(&line) { - lines_found = lines_found + 1; - continue; - } - if self.is_description(&line) { - lines_found = lines_found + 1; - continue; + if let Some(slice) = lines.get(..2) { + for line in slice.iter() { + if self.is_title(&line) { + lines_found = lines_found + 1; + continue; + } + if self.is_description(&line) { + lines_found = lines_found + 1; + continue; + } } } @@ -112,3 +117,178 @@ impl FileHandlerStrategy for Strategy { destination_file.write_all(gemini_source.as_bytes()).unwrap(); } } + +#[cfg(test)] +mod tests { + use super::*; + use std::fs; + + fn setup() -> Strategy { + Strategy {} + } + + fn fixtures_dir() -> PathBuf { + PathBuf::from("tests/fixtures") + } + + fn fixture_path(filename: &str) -> PathBuf { + fixtures_dir().join(filename) + } + + fn read_fixture(filename: &str) -> String { + fs::read_to_string(fixture_path(filename)) + .unwrap_or_else(|_| panic!("Failed to read fixture file: {}", filename)) + } + + mod front_matter_tests { + use super::*; + + #[test] + fn detects_title() { + let strategy = setup(); + let content = read_fixture("test1.gmi"); + let first_line = content.lines().next().unwrap(); + assert!(strategy.is_title(first_line)); + } + + #[test] + fn detects_description() { + let strategy = setup(); + let content = read_fixture("test1.gmi"); + let second_line = content.lines().nth(1).unwrap(); + assert!(strategy.is_description(second_line)); + } + + #[test] + fn extracts_title() { + let strategy = setup(); + let content = read_fixture("test1.gmi"); + let first_line = content.lines().next().unwrap(); + assert_eq!(strategy.get_title(first_line).trim(), "Test Title"); + } + + #[test] + fn extracts_description() { + let strategy = setup(); + let content = read_fixture("test1.gmi"); + let second_line = content.lines().nth(1).unwrap(); + assert_eq!(strategy.get_description(second_line).trim(), "Test Description"); + } + } + + mod file_type_tests { + use super::*; + + #[test] + fn identifies_gemini_files() { + let strategy = setup(); + assert!(strategy.is(&fixture_path("test1.gmi"))); + assert!(!strategy.is(&fixture_path("_layout.html"))); + assert!(!strategy.is(&fixtures_dir())); + } + + #[test] + fn identifies_file_type() { + let strategy = setup(); + assert!(matches!(strategy.identify(), FileType::Gemini)); + } + + #[test] + fn handles_correct_file_type() { + let strategy = setup(); + assert!(strategy.can_handle(&FileType::Gemini)); + assert!(!strategy.can_handle(&FileType::Layout)); + assert!(!strategy.can_handle(&FileType::File)); + assert!(!strategy.can_handle(&FileType::Unknown)); + } + } + + mod file_handling_tests { + use super::*; + + #[test] + fn handles_html_generation() { + let strategy = setup(); + let source = fixtures_dir(); + let output = fixture_path("output"); + let layout = read_fixture("_layout.html"); + + let file = File { + path: fixture_path("test1.gmi"), + file_type: FileType::Gemini, + }; + + strategy.handle_html( + &source, + &output, + &file, + &layout, + ); + + let generated_path = output.join("test1.html"); + assert!(generated_path.exists()); + + let content = fs::read_to_string(generated_path.clone()).unwrap(); + assert!(content.contains("Test Title")); + assert!(content.contains("

")); + + // Cleanup + let _ = fs::remove_file(generated_path); + } + + #[test] + fn handles_gemini_generation() { + let strategy = setup(); + let source = fixtures_dir(); + let output = fixture_path("output"); + + let file = File { + path: fixture_path("test1.gmi"), + file_type: FileType::Gemini, + }; + + strategy.handle_gemini( + &source, + &output, + &file, + ); + + let generated_path = output.join("test1.gmi"); + assert!(generated_path.exists()); + + let content = fs::read_to_string(&generated_path).unwrap(); + assert!(content.contains("# Heading")); + assert!(!content.contains("Test Title")); // Front matter should be removed + + // Cleanup + let _ = fs::remove_file(generated_path); + } + + #[test] + fn handles_nested_structure() { + let strategy = setup(); + let source = fixtures_dir(); + let output = fixture_path("output"); + let layout = read_fixture("_layout.html"); + + let file = File { + path: fixture_path("nested/nested.gmi"), + file_type: FileType::Gemini, + }; + + strategy.handle_html( + &source, + &output, + &file, + &layout, + ); + + let generated_path = output.join("nested").join("nested.html"); + assert!(generated_path.exists()); + + // Cleanup + let _ = fs::remove_file(generated_path); + let _ = fs::remove_dir(output.join("nested")); + } + } +}