From: Ruben Beltran del Rio Date: Sat, 15 Apr 2023 14:21:44 +0000 (+0200) Subject: Read the files X-Git-Tag: 1.0.0~12 X-Git-Url: https://git.r.bdr.sh/rbdr/page/commitdiff_plain/1e2d00b62ecce95f71d4bfd60a043c8e86631eee?ds=sidebyside Read the files --- diff --git a/README.gmi b/README.gmi new file mode 100644 index 0000000..62390d4 --- /dev/null +++ b/README.gmi @@ -0,0 +1,58 @@ +# page + +A static website generator for exactly 1 use case. + +"I have a bunch of gemini files that I want to serve as-is, but I also +want to generate some HTML" + +## How to use + +1. Stand on the directory you want to turn into a page +2. run page +3. your output is in ../_html + +So for example: + +``` +$ pwd +/home/rbdr/web/website +$ page +$ ls .. +website/ +website_html/ +``` + +## Front Matter + +You can add some optional front matter. We'll look at the two first lines that +start with `---` + +The format is: + +``` +--- title: the title of the page +--- description: a description +``` + +This only works if they are the first lines of the page. + +## Local Path Translation + +Links that end with `.gmi` will be replaced with `.html` unless they specifically start with `gemini:` + +## Layouts + +page expects a file called _layout.html in the root. It expects three placeholders: + +* {{ content }} the generated HTML from parsing the gemini text files. +* {{ title }} the frontmatter title or an empty string. +* {{ description }} the frontmatter description or an empty string. + + +## Hidden folders + +Hidden folders get ignored, except for the .well-known folder. + +## What happens to files that aren't gemini? + +They're copied as-is. diff --git a/src/file_finder.rs b/src/file_finder.rs new file mode 100644 index 0000000..fc32fa9 --- /dev/null +++ b/src/file_finder.rs @@ -0,0 +1,25 @@ +use crate::file_handler::{File, FileHandler}; +use std::fs::read_dir; +use std::path::PathBuf; + +pub fn find_files(directory_path: PathBuf) -> Vec { + let mut result: Vec = vec![]; + let file_handler = FileHandler::default(); + let entries = read_dir(directory_path).unwrap(); + for entry in entries { + let path = entry.unwrap().path(); + if path.starts_with(".") && !path.starts_with(".well-known") { + continue; + } + if path.is_dir() { + result.append(&mut find_files(path)) + } else { + let file_type = file_handler.identify(&path); + result.push(File { + path: path, + file_type: file_type, + }); + } + } + return result; +} diff --git a/src/file_handler/file_strategies/file.rs b/src/file_handler/file_strategies/file.rs new file mode 100644 index 0000000..530bbd6 --- /dev/null +++ b/src/file_handler/file_strategies/file.rs @@ -0,0 +1,23 @@ +pub struct Strategy {} + +use std::path::PathBuf; + +use crate::file_handler::{FileType, FileHandlerStrategy}; + +impl FileHandlerStrategy for Strategy { + fn is(&self, path: &PathBuf) -> bool { + !path.is_dir() + } + + fn identify(&self) -> FileType { + FileType::File + } + + fn can_handle(&self, path: &PathBuf) -> bool { + !path.is_dir() + } + + fn handle(&self, path: &PathBuf) { + println!("Should copy {}", path.display()) + } +} diff --git a/src/file_handler/file_strategies/gemini.rs b/src/file_handler/file_strategies/gemini.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/file_handler/file_strategies/layout.rs b/src/file_handler/file_strategies/layout.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/file_handler/file_strategies/mod.rs b/src/file_handler/file_strategies/mod.rs new file mode 100644 index 0000000..2e172cd --- /dev/null +++ b/src/file_handler/file_strategies/mod.rs @@ -0,0 +1 @@ +pub mod file; diff --git a/src/file_handler/mod.rs b/src/file_handler/mod.rs new file mode 100644 index 0000000..8038b3c --- /dev/null +++ b/src/file_handler/mod.rs @@ -0,0 +1,54 @@ +mod file_strategies; + +use file_strategies::file::Strategy as FileStrategy; +use std::path::PathBuf; + +pub struct FileHandler { + pub strategies: Vec> +} + +impl Default for FileHandler { + fn default() -> FileHandler { + FileHandler { + strategies: vec![Box::new(FileStrategy{})] + } + } +} + +impl FileHandler { + pub fn identify(&self, path: &PathBuf) -> FileType { + for strategy in self.strategies.iter() { + if strategy.is(&path) { + return strategy.identify(); + } + } + FileType::Unknown + } + + pub fn handle(&self, path: &PathBuf) { + for strategy in self.strategies.iter() { + if strategy.can_handle(path) { + return strategy.handle(path); + } + } + } +} + +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); +} + +pub enum FileType { + Gemini, + File, + Layout, + Unknown, +} + +pub struct File { + pub path: PathBuf, + pub file_type: FileType, +} diff --git a/src/main.rs b/src/main.rs index aa3fb93..4b8a79f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,17 @@ mod gemini_parser; +mod file_finder; +mod file_handler; + +use std::env::current_dir; use crate::gemini_parser::parse; +use crate::file_finder::find_files; fn main() { 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) + println!("{}", html); + + let files = find_files(current_dir().unwrap()); + println!("Found {} files", files.len()); }