]> git.r.bdr.sh - rbdr/page/commitdiff
Generate gemini and html separately
authorRuben Beltran del Rio <redacted>
Tue, 18 Apr 2023 17:14:40 +0000 (19:14 +0200)
committerRuben Beltran del Rio <redacted>
Tue, 18 Apr 2023 17:14:40 +0000 (19:14 +0200)
src/file_handler/file_strategies/file.rs
src/file_handler/file_strategies/gemini.rs
src/file_handler/file_strategies/layout.rs
src/file_handler/mod.rs
src/main.rs

index 8eafdaa898a431fc6dfc530ea351f75676010957..2346128494c796e79efe3501a1bda02e3758f832 100644 (file)
@@ -5,6 +5,16 @@ use std::fs::{copy, create_dir_all};
 
 use crate::file_handler::{File, FileType, FileHandlerStrategy};
 
+impl Strategy {
+    fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File) {
+        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();
+        copy(&file.path, &complete_destination).unwrap();
+    }
+}
+
 impl FileHandlerStrategy for Strategy {
     fn is(&self, path: &PathBuf) -> bool {
         !path.is_dir()
@@ -21,11 +31,11 @@ impl FileHandlerStrategy for Strategy {
         }
     }
 
-    fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File, _l: &String) {
-        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();
-        copy(&file.path, &complete_destination).unwrap();
+    fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, _l: &String) {
+        return self.handle(source, destination, file);
+    }
+
+    fn handle_gemini(&self, source: &PathBuf, destination: &PathBuf, file: &File) {
+        return self.handle(source, destination, file);
     }
 }
index 1c8a111f7a7b2f5dc1c83445e9025a19dd4664ca..977464cf8e72490b15ac2c2c11ee68097db340c7 100644 (file)
@@ -44,7 +44,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: &String) {
         let gemini_contents = read_to_string(&file.path).unwrap();
 
         // Front matter extraction
@@ -83,4 +83,32 @@ impl FileHandlerStrategy for Strategy {
         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;
+        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;
+            }
+        }
+
+        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();
+    }
 }
index 793b4609da6726db1ae45513a5d5c2ee285cccf0..f51bf7a91ae6b9158e03a13723849f14c11f05fc 100644 (file)
@@ -22,5 +22,6 @@ impl FileHandlerStrategy for Strategy {
 
     // We don't implement handling for layout, as we assume there's only one
     // and it got handled before.
-    fn handle(&self, _s: &PathBuf, _d: &PathBuf, _f: &File, _l: &String) {}
+    fn handle_html(&self, _s: &PathBuf, _d: &PathBuf,  _f: &File, _l: &String) {}
+    fn handle_gemini(&self, _s: &PathBuf, _d: &PathBuf, _f: &File) {}
 }
index 4932ef17f567f70066e8ce480fd03b6ec1afc81a..9a0133adf999f78172243084449775ad7787768c 100644 (file)
@@ -49,17 +49,19 @@ impl FileHandler {
         Err("No layout found. Please ensure there's a _layout.html file at the root")
     }
 
-    pub fn handle_all(&self, source: &PathBuf, destination: &PathBuf, files: &Vec<File>) {
+    pub fn handle_all(&self, source: &PathBuf, html_destination: &PathBuf, gemini_destination: &PathBuf, files: &Vec<File>) {
         for file in files {
-            self.handle(source, destination, file);
+            self.handle(source, html_destination, gemini_destination, file);
         }
     }
 
-    pub fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File) {
+    pub fn handle(&self, source: &PathBuf, html_destination: &PathBuf, gemini_destination: &PathBuf, file: &File) {
         for strategy in self.strategies.iter() {
             if strategy.can_handle(&file.file_type) {
                 let layout = self.layout.as_ref().unwrap();
-                return strategy.handle(source, destination, file, layout);
+                strategy.handle_html(source, html_destination, file, layout);
+                strategy.handle_gemini(source, gemini_destination, file);
+                return;
             }
         }
     }
@@ -69,7 +71,8 @@ pub trait FileHandlerStrategy {
     fn is(&self, path: &PathBuf) -> bool;
     fn identify(&self) -> FileType;
     fn can_handle(&self, file_type: &FileType) -> bool;
-    fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &String);
+    fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &String);
+    fn handle_gemini(&self, source: &PathBuf, destination: &PathBuf, file: &File);
 }
 
 pub enum FileType {
index c6de435878859cdfebf369748e292e9491c24142..fc085d8a39d478970b6a5e1f09222994f530784b 100644 (file)
@@ -14,17 +14,23 @@ fn main() -> Result<()> {
     let source = current_dir()?;
     let source_name = source.file_name().unwrap().to_string_lossy();
     let parent = source.parent().unwrap();
-    let destination_name = format!("{}_html", source_name);
-    let destination = parent.join(destination_name);
+    let gemini_destination_name = format!("{}_gemini", source_name);
+    let gemini_destination = parent.join(gemini_destination_name);
+    let html_destination_name = format!("{}_html", source_name);
+    let html_destination = parent.join(html_destination_name);
 
     // Step 1. Identify the files
     let files = find_files(&source);
 
     // Step 2. Prepare the target priority
-    match remove_dir_all(&destination) {
+    match remove_dir_all(&html_destination) {
         _ => {}
     };
-    create_dir_all(&destination)?;
+    create_dir_all(&html_destination)?;
+    match remove_dir_all(&gemini_destination) {
+        _ => {}
+    };
+    create_dir_all(&gemini_destination)?;
 
     // Step 3. Load the layout
     let mut file_handler = FileHandler::default();
@@ -37,6 +43,6 @@ fn main() -> Result<()> {
     }
 
     // Step 4. Process all files
-    file_handler.handle_all(&source, &destination, &files);
+    file_handler.handle_all(&source, &html_destination, &gemini_destination, &files);
     Ok(())
 }