3 use std::fs::{create_dir_all, read_to_string, File as IOFile};
7 use crate::file_handler::{File, FileHandlerStrategy, FileType};
8 use crate::gemini_parser::parse;
9 use crate::html_renderer::render_html;
12 fn is_title(&self, line: &str) -> bool {
13 line.starts_with("--- title:")
16 fn is_description(&self, line: &str) -> bool {
17 line.starts_with("--- description:")
20 fn get_title<'a>(&self, line: &'a str) -> &'a str {
21 line.split_once("--- title:").unwrap().1
24 fn get_description<'a>(&self, line: &'a str) -> &'a str {
25 line.split_once("--- description:").unwrap().1
29 impl FileHandlerStrategy for Strategy {
30 fn is(&self, path: &Path) -> bool {
31 if let Some(extension) = path.extension() {
32 return !path.is_dir() && extension == "gmi";
37 fn identify(&self) -> FileType {
41 fn can_handle(&self, file_type: &FileType) -> bool {
42 matches!(file_type, FileType::Gemini)
45 fn handle_html(&self, source: &Path, destination: &Path, file: &File, layout: &str) {
46 let gemini_contents = read_to_string(&file.path).unwrap();
48 // Front matter extraction
49 let lines: Vec<&str> = gemini_contents.split("\n").collect();
50 let mut lines_found = 0;
52 let mut description = "";
53 if let Some(slice) = lines.get(..2) {
54 for line in slice.iter() {
55 if self.is_title(line) {
56 title = self.get_title(line).trim();
60 if self.is_description(line) {
61 description = self.get_description(line).trim();
68 let gemini_source = lines[lines_found..].join("\n");
69 let content_html = render_html(parse(&gemini_source[..]));
71 let generated_html = layout
72 .replace("{{ title }}", title)
73 .replace("{{ description }}", description)
74 .replace("{{ content }}", &content_html[..]);
76 let relative_path = file.path.strip_prefix(source).unwrap();
77 let mut complete_destination = destination.join(relative_path);
78 complete_destination.set_extension("html");
79 let destination_parent = complete_destination.parent().unwrap();
80 create_dir_all(destination_parent).unwrap();
82 let mut destination_file = IOFile::create(&complete_destination).unwrap();
84 .write_all(generated_html.as_bytes())
88 fn handle_gemini(&self, source: &Path, destination: &Path, file: &File) {
89 let gemini_contents = read_to_string(&file.path).unwrap();
91 // Front matter extraction
92 let lines: Vec<&str> = gemini_contents.split("\n").collect();
93 let mut lines_found = 0;
94 if let Some(slice) = lines.get(..2) {
95 for line in slice.iter() {
96 if self.is_title(line) {
100 if self.is_description(line) {
107 let gemini_source = lines[lines_found..].join("\n");
109 let relative_path = file.path.strip_prefix(source).unwrap();
110 let complete_destination = destination.join(relative_path);
111 let destination_parent = complete_destination.parent().unwrap();
112 create_dir_all(destination_parent).unwrap();
114 let mut destination_file = IOFile::create(&complete_destination).unwrap();
116 .write_all(gemini_source.as_bytes())
123 use std::fs::create_dir_all;
127 use test_utilities::*;
131 let strategy = Strategy {};
132 assert!(strategy.is_title("--- title: Hello!"));
136 fn does_not_detect_other_keys_as_title() {
137 let strategy = Strategy {};
138 assert!(!strategy.is_title("--- description: Hello!"));
142 fn detects_description() {
143 let strategy = Strategy {};
144 assert!(strategy.is_description("--- description: What is this?"));
148 fn does_not_detect_other_keys_as_description() {
149 let strategy = Strategy {};
150 assert!(!strategy.is_description("--- title: What is this?"));
154 fn extracts_title() {
155 let strategy = Strategy {};
156 assert_eq!(strategy.get_title("--- title: Hello!").trim(), "Hello!");
160 fn extracts_description() {
161 let strategy = Strategy {};
164 .get_description("--- description: What is this?")
171 fn identifies_gemini_file() {
172 let test_dir = setup_test_dir();
173 create_test_file(&test_dir.join("test.gmi"), "");
174 let strategy = Strategy {};
175 assert!(strategy.is(&test_dir.join("test.gmi")));
179 fn rejects_non_gemini_file() {
180 let test_dir = setup_test_dir();
181 create_test_file(&test_dir.join("_layout.html"), "");
182 create_test_file(&test_dir.join("image.png"), "");
183 let strategy = Strategy {};
184 assert!(!strategy.is(&test_dir.join("_layout.html")));
185 assert!(!strategy.is(&test_dir.join("image.png")));
186 assert!(!strategy.is(&test_dir));
190 fn identifies_gemini_type() {
191 let strategy = Strategy {};
192 assert!(matches!(strategy.identify(), FileType::Gemini));
196 fn handles_gemini_type() {
197 let strategy = Strategy {};
198 assert!(strategy.can_handle(&FileType::Gemini));
202 fn rejects_non_gemini_types() {
203 let strategy = Strategy {};
204 assert!(!strategy.can_handle(&FileType::Layout));
205 assert!(!strategy.can_handle(&FileType::File));
206 assert!(!strategy.can_handle(&FileType::Unknown));
210 fn handles_html_generation() {
211 let test_dir = setup_test_dir();
212 let source_dir = test_dir.join("source");
213 let output_dir = test_dir.join("output");
214 create_dir_all(&source_dir).expect("Could not create source test directory");
215 create_dir_all(&output_dir).expect("Could not create output test directory");
219 <title>{{ title }}</title>
220 <meta name=\"description\" content=\"{{ description }}\">
222 <body>{{ content }}</body>
226 &source_dir.join("test.gmi"),
228 --- title: Page Is Cool!
229 --- description: My Description
235 let strategy = Strategy {};
237 path: source_dir.join("test.gmi"),
238 file_type: FileType::Gemini,
241 strategy.handle_html(&source_dir, &output_dir, &file, &layout);
243 let html_output = output_dir.join("test.html");
244 assert!(html_output.exists());
245 assert_file_contents(
250 <title>Page Is Cool!</title>
251 <meta name=\"description\" content=\"My Description\">
253 <body><section class=\"h1\">
264 fn handles_gemini_generation() {
265 let test_dir = setup_test_dir();
266 let source_dir = test_dir.join("source");
267 let output_dir = test_dir.join("output");
268 create_dir_all(&source_dir).expect("Could not create source test directory");
269 create_dir_all(&output_dir).expect("Could not create output test directory");
271 &source_dir.join("test.gmi"),
273 --- title: Page Is Cool!
274 --- description: My Description
280 let strategy = Strategy {};
282 path: source_dir.join("test.gmi"),
283 file_type: FileType::Gemini,
286 strategy.handle_gemini(&source_dir, &output_dir, &file);
288 let gemini_output = output_dir.join("test.gmi");
289 assert!(gemini_output.exists());
290 assert_file_contents(
300 fn handles_nested_structure() {
301 let test_dir = setup_test_dir();
302 let source_dir = test_dir.join("source");
303 let output_dir = test_dir.join("output");
304 create_dir_all(&source_dir.join("nested")).expect("Could not create source test directory");
305 create_dir_all(&output_dir).expect("Could not create output test directory");
309 <title>{{ title }}</title>
310 <meta name=\"description\" content=\"{{ description }}\">
312 <body>{{ content }}</body>
316 &source_dir.join("nested/test.gmi"),
318 --- title: Page Is Cool!
319 --- description: My Description
325 let strategy = Strategy {};
327 path: source_dir.join("nested/test.gmi"),
328 file_type: FileType::Gemini,
331 strategy.handle_html(&source_dir, &output_dir, &file, &layout);
333 let html_output = output_dir.join("nested/test.html");
334 assert!(html_output.exists());
335 assert_file_contents(
340 <title>Page Is Cool!</title>
341 <meta name=\"description\" content=\"My Description\">
343 <body><section class=\"h1\">