X-Git-Url: https://git.r.bdr.sh/rbdr/page/blobdiff_plain/ea5297364f8a1b2c4e684140024b60a83b087b50..fb0da1eec8c545c3010bc3e838a402ce6ebd31d5:/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 2427422..977464c 100644 --- a/src/file_handler/file_strategies/gemini.rs +++ b/src/file_handler/file_strategies/gemini.rs @@ -1,8 +1,29 @@ 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; + +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 { @@ -23,7 +44,71 @@ impl FileHandlerStrategy for Strategy { } } - fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File) { - println!("Should parse and copy {}", file.path.display()) + fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &String) { + 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 = ""; + 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; + } + } + + let gemini_source = lines[lines_found..].join("\n"); + let content_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; + 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(); } }