diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-04-18 19:14:40 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-04-18 19:14:40 +0200 |
| commit | dd0a540c2002f479ac56a7e0169e86d0f6f14d85 (patch) | |
| tree | a50d034305cee8a92bf86cfffee859ee4f80349c /src/file_handler/file_strategies | |
| parent | 160a27bd2aa334a881f7c54847ef406925f87633 (diff) | |
Generate gemini and html separately
Diffstat (limited to 'src/file_handler/file_strategies')
| -rw-r--r-- | src/file_handler/file_strategies/file.rs | 22 | ||||
| -rw-r--r-- | src/file_handler/file_strategies/gemini.rs | 30 | ||||
| -rw-r--r-- | src/file_handler/file_strategies/layout.rs | 3 |
3 files changed, 47 insertions, 8 deletions
diff --git a/src/file_handler/file_strategies/file.rs b/src/file_handler/file_strategies/file.rs index 8eafdaa..2346128 100644 --- a/src/file_handler/file_strategies/file.rs +++ b/src/file_handler/file_strategies/file.rs @@ -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); } } diff --git a/src/file_handler/file_strategies/gemini.rs b/src/file_handler/file_strategies/gemini.rs index 1c8a111..977464c 100644 --- a/src/file_handler/file_strategies/gemini.rs +++ b/src/file_handler/file_strategies/gemini.rs @@ -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(); + } } diff --git a/src/file_handler/file_strategies/layout.rs b/src/file_handler/file_strategies/layout.rs index 793b460..f51bf7a 100644 --- a/src/file_handler/file_strategies/layout.rs +++ b/src/file_handler/file_strategies/layout.rs @@ -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) {} } |