]> git.r.bdr.sh - rbdr/page/blobdiff - src/file_handler/file_strategies/gemini.rs
Update file strategy tests
[rbdr/page] / src / file_handler / file_strategies / gemini.rs
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..d8bde1434c19be4fec7af4b95dbe0310cc41778b 100644 (file)
@@ -0,0 +1,294 @@
+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 {
+        if let Some(extension) = path.extension() {
+            return !path.is_dir() && extension == "gmi"
+        }
+        false
+    }
+
+    fn identify(&self) -> FileType {
+        FileType::Gemini
+    }
+
+    fn can_handle(&self, file_type: &FileType) -> bool {
+        match file_type {
+            FileType::Gemini => true,
+            _ => false,
+        }
+    }
+
+    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("<h1>"));
+
+            // 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"));
+        }
+    }
+}