X-Git-Url: https://git.r.bdr.sh/rbdr/page/blobdiff_plain/5732d284ebc2cc2cbde0f050443b8f137dbf585b..4d946aebe3f70ad18e235d68474b6d489757c927:/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 e987bab..4596378 100644 --- a/src/file_handler/file_strategies/gemini.rs +++ b/src/file_handler/file_strategies/gemini.rs @@ -4,24 +4,23 @@ use std::fs::{create_dir_all, read_to_string, File as IOFile}; use std::io::Write; use std::path::Path; -use crate::file_handler::{File, FileHandlerStrategy, FileType}; -use crate::gemini_parser::parse; -use crate::html_renderer::render_html; +use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy}; +use gema_texto::{gemini_parser::parse, html_renderer::render_html}; impl Strategy { - fn is_title(&self, line: &str) -> bool { + fn is_title(line: &str) -> bool { line.starts_with("--- title:") } - fn is_description(&self, line: &str) -> bool { + fn is_description(line: &str) -> bool { line.starts_with("--- description:") } - fn get_title<'a>(&self, line: &'a str) -> &'a str { + fn get_title(line: &str) -> &str { line.split_once("--- title:").unwrap().1 } - fn get_description<'a>(&self, line: &'a str) -> &'a str { + fn get_description(line: &str) -> &str { line.split_once("--- description:").unwrap().1 } } @@ -46,19 +45,19 @@ impl FileHandlerStrategy for Strategy { let gemini_contents = read_to_string(&file.path).unwrap(); // Front matter extraction - let lines: Vec<&str> = gemini_contents.split("\n").collect(); + 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(); + for line in slice { + if Strategy::is_title(line) { + title = Strategy::get_title(line).trim(); lines_found += 1; continue; } - if self.is_description(line) { - description = self.get_description(line).trim(); + if Strategy::is_description(line) { + description = Strategy::get_description(line).trim(); lines_found += 1; continue; } @@ -66,7 +65,7 @@ impl FileHandlerStrategy for Strategy { } let gemini_source = lines[lines_found..].join("\n"); - let content_html = render_html(parse(&gemini_source[..])); + let content_html = render_html(&parse(&gemini_source[..])); let generated_html = layout .replace("{{ title }}", title) @@ -89,15 +88,15 @@ impl FileHandlerStrategy for Strategy { let gemini_contents = read_to_string(&file.path).unwrap(); // Front matter extraction - let lines: Vec<&str> = gemini_contents.split("\n").collect(); + 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) { + for line in slice { + if Strategy::is_title(line) { lines_found += 1; continue; } - if self.is_description(line) { + if Strategy::is_description(line) { lines_found += 1; continue; } @@ -128,41 +127,33 @@ mod tests { #[test] fn detects_title() { - let strategy = Strategy {}; - assert!(strategy.is_title("--- title: Hello!")); + assert!(Strategy::is_title("--- title: Hello!")); } #[test] fn does_not_detect_other_keys_as_title() { - let strategy = Strategy {}; - assert!(!strategy.is_title("--- description: Hello!")); + assert!(!Strategy::is_title("--- description: Hello!")); } #[test] fn detects_description() { - let strategy = Strategy {}; - assert!(strategy.is_description("--- description: What is this?")); + assert!(Strategy::is_description("--- description: What is this?")); } #[test] fn does_not_detect_other_keys_as_description() { - let strategy = Strategy {}; - assert!(!strategy.is_description("--- title: What is this?")); + assert!(!Strategy::is_description("--- title: What is this?")); } #[test] fn extracts_title() { - let strategy = Strategy {}; - assert_eq!(strategy.get_title("--- title: Hello!").trim(), "Hello!"); + assert_eq!(Strategy::get_title("--- title: Hello!").trim(), "Hello!"); } #[test] fn extracts_description() { - let strategy = Strategy {}; assert_eq!( - strategy - .get_description("--- description: What is this?") - .trim(), + Strategy::get_description("--- description: What is this?").trim(), "What is this?" ); } @@ -238,7 +229,7 @@ Hello world file_type: FileType::Gemini, }; - strategy.handle_html(&source_dir, &output_dir, &file, &layout); + strategy.handle_html(&source_dir, &output_dir, &file, layout); let html_output = output_dir.join("test.html"); assert!(html_output.exists()); @@ -301,7 +292,7 @@ Hello world let test_dir = setup_test_dir(); let source_dir = test_dir.join("source"); let output_dir = test_dir.join("output"); - create_dir_all(&source_dir.join("nested")).expect("Could not create source test directory"); + create_dir_all(source_dir.join("nested")).expect("Could not create source test directory"); create_dir_all(&output_dir).expect("Could not create output test directory"); let layout = "\ @@ -328,7 +319,7 @@ Hello world file_type: FileType::Gemini, }; - strategy.handle_html(&source_dir, &output_dir, &file, &layout); + strategy.handle_html(&source_dir, &output_dir, &file, layout); let html_output = output_dir.join("nested/test.html"); assert!(html_output.exists());