]> git.r.bdr.sh - rbdr/page/blobdiff - src/file_handler/file_strategies/gemini.rs
Update layout tests
[rbdr/page] / src / file_handler / file_strategies / gemini.rs
index 04e2b6aae554e9cf2094bb1cbb1b6407096e2def..d8bde1434c19be4fec7af4b95dbe0310cc41778b 100644 (file)
@@ -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::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 {
 
 impl Strategy {
     fn is_title(&self, line: &str) -> bool {
@@ -44,7 +45,7 @@ impl FileHandlerStrategy for Strategy {
         }
     }
 
         }
     }
 
-    fn handle(&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
         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 = "";
         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 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)
 
         let generated_html = layout
             .replace("{{ title }}", title)
@@ -75,11 +78,217 @@ impl FileHandlerStrategy for Strategy {
 
 
         let relative_path = file.path.strip_prefix(&source).unwrap();
 
 
         let relative_path = file.path.strip_prefix(&source).unwrap();
-        let complete_destination = destination.join(relative_path);
+        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();
     }
         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"));
+        }
+    }
 }
 }