X-Git-Url: https://git.r.bdr.sh/rbdr/page/blobdiff_plain/ea5297364f8a1b2c4e684140024b60a83b087b50..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 2427422..d8bde14 100644 --- a/src/file_handler/file_strategies/gemini.rs +++ b/src/file_handler/file_strategies/gemini.rs @@ -1,8 +1,30 @@ pub struct Strategy {} use std::path::PathBuf; +use std::io::Write; +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 { + line.starts_with("--- title:") + } + + fn is_description(&self, line: &str) -> bool { + line.starts_with("--- description:") + } + + fn get_title<'a>(&self, line: &'a str) -> &'a str { + line.split_once("--- title:").unwrap().1 + } + + fn get_description<'a>(&self, line: &'a str) -> &'a str { + line.split_once("--- description:").unwrap().1 + } +} impl FileHandlerStrategy for Strategy { fn is(&self, path: &PathBuf) -> bool { @@ -23,7 +45,250 @@ impl FileHandlerStrategy for Strategy { } } - fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File) { - println!("Should parse and copy {}", file.path.display()) + fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &str) { + let gemini_contents = read_to_string(&file.path).unwrap(); + + // Front matter extraction + let lines: Vec<&str> = gemini_contents.split("\n").collect(); + let mut lines_found = 0; + let mut title = ""; + let mut description = ""; + 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 = render_html(parse(&gemini_source[..])); + + let generated_html = layout + .replace("{{ title }}", title) + .replace("{{ description }}", description) + .replace("{{ content }}", &content_html[..]); + + + let relative_path = file.path.strip_prefix(&source).unwrap(); + let mut complete_destination = destination.join(relative_path); + complete_destination.set_extension("html"); + let destination_parent = complete_destination.parent().unwrap(); + create_dir_all(destination_parent).unwrap(); + + let mut destination_file = IOFile::create(&complete_destination).unwrap(); + destination_file.write_all(generated_html.as_bytes()).unwrap(); + } + + fn handle_gemini(&self, source: &PathBuf, destination: &PathBuf, file: &File) { + let gemini_contents = read_to_string(&file.path).unwrap(); + + // Front matter extraction + let lines: Vec<&str> = gemini_contents.split("\n").collect(); + let mut lines_found = 0; + 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; + } + } + } + + let gemini_source = lines[lines_found..].join("\n"); + + let relative_path = file.path.strip_prefix(&source).unwrap(); + let complete_destination = destination.join(relative_path); + let destination_parent = complete_destination.parent().unwrap(); + create_dir_all(destination_parent).unwrap(); + + let mut destination_file = IOFile::create(&complete_destination).unwrap(); + 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("