From: Ruben Beltran del Rio Date: Sun, 16 Apr 2023 11:11:39 +0000 (+0200) Subject: Add logic to read and assign files / layout X-Git-Tag: 1.0.0~9 X-Git-Url: https://git.r.bdr.sh/rbdr/page/commitdiff_plain/4fd89b808cabc8afb0d75b9700be1da96989c4b7?ds=inline Add logic to read and assign files / layout --- diff --git a/README.gmi b/README.gmi index 62390d4..22d159f 100644 --- a/README.gmi +++ b/README.gmi @@ -51,7 +51,9 @@ page expects a file called _layout.html in the root. It expects three placeholde ## Hidden folders -Hidden folders get ignored, except for the .well-known folder. +Hidden folders are copied as well, we only make an exception for `.git`, which +is explicitly ignored. This is handy for folders like .well-known, but could +cause unwanted behavior if there's other hidden files in the directory. ## What happens to files that aren't gemini? diff --git a/src/file_finder.rs b/src/file_finder.rs index 598f868..105a999 100644 --- a/src/file_finder.rs +++ b/src/file_finder.rs @@ -2,8 +2,8 @@ use crate::file_handler::{File, FileHandler}; use std::fs::read_dir; use std::path::PathBuf; -pub fn find_files(directory_path: PathBuf) -> Vec { - return find_files_recursively(&directory_path, &directory_path); +pub fn find_files(directory_path: &PathBuf) -> Vec { + return find_files_recursively(directory_path, directory_path); } fn find_files_recursively(root_path: &PathBuf, directory_path: &PathBuf) -> Vec { @@ -13,7 +13,7 @@ fn find_files_recursively(root_path: &PathBuf, directory_path: &PathBuf) -> Vec< for entry in entries { let path = entry.unwrap().path(); let relative_path = path.strip_prefix(&root_path).unwrap(); - if relative_path.starts_with(".") && !relative_path.starts_with(".well-known") { + if relative_path.starts_with(".git") { continue; } if path.is_dir() { diff --git a/src/file_handler/file_strategies/file.rs b/src/file_handler/file_strategies/file.rs index c9e8c96..b1de596 100644 --- a/src/file_handler/file_strategies/file.rs +++ b/src/file_handler/file_strategies/file.rs @@ -2,7 +2,7 @@ pub struct Strategy {} use std::path::PathBuf; -use crate::file_handler::{FileType, FileHandlerStrategy}; +use crate::file_handler::{File, FileType, FileHandlerStrategy}; impl FileHandlerStrategy for Strategy { fn is(&self, path: &PathBuf) -> bool { @@ -13,11 +13,14 @@ impl FileHandlerStrategy for Strategy { FileType::File } - fn can_handle(&self, path: &PathBuf) -> bool { - self.is(path) + fn can_handle(&self, file_type: &FileType) -> bool { + match file_type { + FileType::File => true, + _ => false, + } } - fn handle(&self, path: &PathBuf) { - println!("Should copy {}", path.display()) + fn handle(&self, file: &File) { + println!("Should copy {}", file.path.display()) } } diff --git a/src/file_handler/file_strategies/gemini.rs b/src/file_handler/file_strategies/gemini.rs index ffa2a9f..905e1a9 100644 --- a/src/file_handler/file_strategies/gemini.rs +++ b/src/file_handler/file_strategies/gemini.rs @@ -2,7 +2,7 @@ pub struct Strategy {} use std::path::PathBuf; -use crate::file_handler::{FileType, FileHandlerStrategy}; +use crate::file_handler::{File, FileType, FileHandlerStrategy}; impl FileHandlerStrategy for Strategy { fn is(&self, path: &PathBuf) -> bool { @@ -16,11 +16,14 @@ impl FileHandlerStrategy for Strategy { FileType::Gemini } - fn can_handle(&self, path: &PathBuf) -> bool { - self.is(path) + fn can_handle(&self, file_type: &FileType) -> bool { + match file_type { + FileType::Gemini => true, + _ => false, + } } - fn handle(&self, path: &PathBuf) { - println!("Should convert {}", path.display()) + fn handle(&self, file: &File) { + println!("Should parse and copy {}", file.path.display()) } } diff --git a/src/file_handler/file_strategies/layout.rs b/src/file_handler/file_strategies/layout.rs index cf1bb9f..bac1059 100644 --- a/src/file_handler/file_strategies/layout.rs +++ b/src/file_handler/file_strategies/layout.rs @@ -2,7 +2,7 @@ pub struct Strategy {} use std::path::PathBuf; -use crate::file_handler::{FileType, FileHandlerStrategy}; +use crate::file_handler::{File, FileType, FileHandlerStrategy}; impl FileHandlerStrategy for Strategy { fn is(&self, path: &PathBuf) -> bool { @@ -13,12 +13,12 @@ impl FileHandlerStrategy for Strategy { FileType::Layout } - fn can_handle(&self, path: &PathBuf) -> bool { - self.is(path) + fn can_handle(&self, file_type: &FileType) -> bool { + match file_type { + FileType::Layout => true, + _ => false, + } } - fn handle(&self, path: &PathBuf) { - println!("Should convert {}", path.display()) - } + fn handle(&self, _file: &File) {} } - diff --git a/src/file_handler/mod.rs b/src/file_handler/mod.rs index 44fec6d..53aaba9 100644 --- a/src/file_handler/mod.rs +++ b/src/file_handler/mod.rs @@ -5,9 +5,11 @@ use file_strategies::gemini::Strategy as GeminiStrategy; use file_strategies::layout::Strategy as LayoutStrategy; use std::path::PathBuf; +use std::fs::read_to_string; pub struct FileHandler { - pub strategies: Vec> + pub strategies: Vec>, + pub layout: Option } impl Default for FileHandler { @@ -17,7 +19,8 @@ impl Default for FileHandler { Box::new(GeminiStrategy{}), Box::new(LayoutStrategy{}), Box::new(FileStrategy{}), - ] + ], + layout: None } } } @@ -32,10 +35,30 @@ impl FileHandler { FileType::Unknown } - pub fn handle(&self, path: &PathBuf) { + pub fn get_layout_or_panic(&mut self, files: &Vec) { + for file in files { + match file.file_type { + FileType::Layout => { + let layout_text = read_to_string(&file.path).unwrap(); + self.layout = Some(layout_text); + return; + }, + _ => {} + } + } + panic!("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) { + for file in files { + self.handle(source, destination, file); + } + } + + pub fn handle(&self, source: &PathBuf, destination: &PathBuf, file: &File) { for strategy in self.strategies.iter() { - if strategy.can_handle(path) { - return strategy.handle(path); + if strategy.can_handle(&file.file_type) { + return strategy.handle(file); } } } @@ -44,8 +67,8 @@ impl FileHandler { pub trait FileHandlerStrategy { fn is(&self, path: &PathBuf) -> bool; fn identify(&self) -> FileType; - fn can_handle(&self, path: &PathBuf) -> bool; - fn handle(&self, path: &PathBuf); + fn can_handle(&self, file_type: &FileType) -> bool; + fn handle(&self, file: &File); } pub enum FileType { diff --git a/src/main.rs b/src/main.rs index 4b8a79f..e04da8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,16 +2,38 @@ mod gemini_parser; mod file_finder; mod file_handler; +use std::io::Result; use std::env::current_dir; +use std::fs::create_dir_all; use crate::gemini_parser::parse; use crate::file_finder::find_files; +use crate::file_handler::FileHandler; -fn main() { +fn main() -> Result<()> { let gemini_source = "# Test\n## 2nd H\na line\n another line\n```\npreformat\n=> preformat\n```\n=> http://lol.com\n=> http://lol.com lol\n* lol\nbla\n* lol\n* lmao\n> blabla\n> blabla"; let html = parse(gemini_source); println!("{}", html); - let files = find_files(current_dir().unwrap()); + 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); + + // Step 1. Identify the files + let files = find_files(&source); + + // Step 2. Prepare the target priority + create_dir_all(&destination)?; + println!("Found {} files", files.len()); + + // Step 3. Load the layout + let mut file_handler = FileHandler::default(); + file_handler.get_layout_or_panic(&files); + + // Step 4. Process all files + file_handler.handle_all(&source, &destination, &files); + Ok(()) }