3 use std::fs::{create_dir_all, read_to_string, File as IOFile};
7 use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy};
8 use crate::gemini_parser::parse;
9 use crate::html_renderer::render_html;
12 fn is_title(line: &str) -> bool {
13 line.starts_with("--- title:")
16 fn is_description(line: &str) -> bool {
17 line.starts_with("--- description:")
20 fn get_title(line: &str) -> &str {
21 line.split_once("--- title:").unwrap().1
24 fn get_description(line: &str) -> &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) {
55 if Strategy::is_title(line) {
56 title = Strategy::get_title(line).trim();
60 if Strategy::is_description(line) {
61 description = Strategy::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) {
96 if Strategy::is_title(line) {
100 if Strategy::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 assert!(Strategy::is_title("--- title: Hello!"));
135 fn does_not_detect_other_keys_as_title() {
136 assert!(!Strategy::is_title("--- description: Hello!"));
140 fn detects_description() {
141 assert!(Strategy::is_description("--- description: What is this?"));
145 fn does_not_detect_other_keys_as_description() {
146 assert!(!Strategy::is_description("--- title: What is this?"));
150 fn extracts_title() {
151 assert_eq!(Strategy::get_title("--- title: Hello!").trim(), "Hello!");
155 fn extracts_description() {
157 Strategy::get_description("--- description: What is this?").trim(),
163 fn identifies_gemini_file() {
164 let test_dir = setup_test_dir();
165 create_test_file(&test_dir.join("test.gmi"), "");
166 let strategy = Strategy {};
167 assert!(strategy.is(&test_dir.join("test.gmi")));
171 fn rejects_non_gemini_file() {
172 let test_dir = setup_test_dir();
173 create_test_file(&test_dir.join("_layout.html"), "");
174 create_test_file(&test_dir.join("image.png"), "");
175 let strategy = Strategy {};
176 assert!(!strategy.is(&test_dir.join("_layout.html")));
177 assert!(!strategy.is(&test_dir.join("image.png")));
178 assert!(!strategy.is(&test_dir));
182 fn identifies_gemini_type() {
183 let strategy = Strategy {};
184 assert!(matches!(strategy.identify(), FileType::Gemini));
188 fn handles_gemini_type() {
189 let strategy = Strategy {};
190 assert!(strategy.can_handle(&FileType::Gemini));
194 fn rejects_non_gemini_types() {
195 let strategy = Strategy {};
196 assert!(!strategy.can_handle(&FileType::Layout));
197 assert!(!strategy.can_handle(&FileType::File));
198 assert!(!strategy.can_handle(&FileType::Unknown));
202 fn handles_html_generation() {
203 let test_dir = setup_test_dir();
204 let source_dir = test_dir.join("source");
205 let output_dir = test_dir.join("output");
206 create_dir_all(&source_dir).expect("Could not create source test directory");
207 create_dir_all(&output_dir).expect("Could not create output test directory");
211 <title>{{ title }}</title>
212 <meta name=\"description\" content=\"{{ description }}\">
214 <body>{{ content }}</body>
218 &source_dir.join("test.gmi"),
220 --- title: Page Is Cool!
221 --- description: My Description
227 let strategy = Strategy {};
229 path: source_dir.join("test.gmi"),
230 file_type: FileType::Gemini,
233 strategy.handle_html(&source_dir, &output_dir, &file, layout);
235 let html_output = output_dir.join("test.html");
236 assert!(html_output.exists());
237 assert_file_contents(
242 <title>Page Is Cool!</title>
243 <meta name=\"description\" content=\"My Description\">
245 <body><section class=\"h1\">
256 fn handles_gemini_generation() {
257 let test_dir = setup_test_dir();
258 let source_dir = test_dir.join("source");
259 let output_dir = test_dir.join("output");
260 create_dir_all(&source_dir).expect("Could not create source test directory");
261 create_dir_all(&output_dir).expect("Could not create output test directory");
263 &source_dir.join("test.gmi"),
265 --- title: Page Is Cool!
266 --- description: My Description
272 let strategy = Strategy {};
274 path: source_dir.join("test.gmi"),
275 file_type: FileType::Gemini,
278 strategy.handle_gemini(&source_dir, &output_dir, &file);
280 let gemini_output = output_dir.join("test.gmi");
281 assert!(gemini_output.exists());
282 assert_file_contents(
292 fn handles_nested_structure() {
293 let test_dir = setup_test_dir();
294 let source_dir = test_dir.join("source");
295 let output_dir = test_dir.join("output");
296 create_dir_all(source_dir.join("nested")).expect("Could not create source test directory");
297 create_dir_all(&output_dir).expect("Could not create output test directory");
301 <title>{{ title }}</title>
302 <meta name=\"description\" content=\"{{ description }}\">
304 <body>{{ content }}</body>
308 &source_dir.join("nested/test.gmi"),
310 --- title: Page Is Cool!
311 --- description: My Description
317 let strategy = Strategy {};
319 path: source_dir.join("nested/test.gmi"),
320 file_type: FileType::Gemini,
323 strategy.handle_html(&source_dir, &output_dir, &file, layout);
325 let html_output = output_dir.join("nested/test.html");
326 assert!(html_output.exists());
327 assert_file_contents(
332 <title>Page Is Cool!</title>
333 <meta name=\"description\" content=\"My Description\">
335 <body><section class=\"h1\">