]>
Commit | Line | Data |
---|---|---|
102a4884 RBR |
1 | pub struct Strategy {} |
2 | ||
68fa37d6 | 3 | use std::fs::{create_dir_all, read_to_string, File as IOFile}; |
5732d284 RBR |
4 | use std::io::Write; |
5 | use std::path::Path; | |
102a4884 | 6 | |
8766e441 | 7 | use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy}; |
4d946aeb | 8 | use gema_texto::{gemini_parser::parse, html_renderer::render_html}; |
68fa37d6 RBR |
9 | |
10 | impl Strategy { | |
8766e441 | 11 | fn is_title(line: &str) -> bool { |
68fa37d6 RBR |
12 | line.starts_with("--- title:") |
13 | } | |
14 | ||
8766e441 | 15 | fn is_description(line: &str) -> bool { |
68fa37d6 RBR |
16 | line.starts_with("--- description:") |
17 | } | |
18 | ||
8766e441 | 19 | fn get_title(line: &str) -> &str { |
68fa37d6 RBR |
20 | line.split_once("--- title:").unwrap().1 |
21 | } | |
22 | ||
8766e441 | 23 | fn get_description(line: &str) -> &str { |
68fa37d6 RBR |
24 | line.split_once("--- description:").unwrap().1 |
25 | } | |
26 | } | |
102a4884 RBR |
27 | |
28 | impl FileHandlerStrategy for Strategy { | |
5732d284 | 29 | fn is(&self, path: &Path) -> bool { |
102a4884 | 30 | if let Some(extension) = path.extension() { |
5732d284 | 31 | return !path.is_dir() && extension == "gmi"; |
102a4884 RBR |
32 | } |
33 | false | |
34 | } | |
35 | ||
36 | fn identify(&self) -> FileType { | |
37 | FileType::Gemini | |
38 | } | |
39 | ||
4fd89b80 | 40 | fn can_handle(&self, file_type: &FileType) -> bool { |
5732d284 | 41 | matches!(file_type, FileType::Gemini) |
102a4884 RBR |
42 | } |
43 | ||
5732d284 | 44 | fn handle_html(&self, source: &Path, destination: &Path, file: &File, layout: &str) { |
68fa37d6 RBR |
45 | let gemini_contents = read_to_string(&file.path).unwrap(); |
46 | ||
47 | // Front matter extraction | |
8766e441 | 48 | let lines: Vec<&str> = gemini_contents.split('\n').collect(); |
68fa37d6 RBR |
49 | let mut lines_found = 0; |
50 | let mut title = ""; | |
51 | let mut description = ""; | |
260e8ec6 | 52 | if let Some(slice) = lines.get(..2) { |
8766e441 RBR |
53 | for line in slice { |
54 | if Strategy::is_title(line) { | |
55 | title = Strategy::get_title(line).trim(); | |
5732d284 | 56 | lines_found += 1; |
260e8ec6 RBR |
57 | continue; |
58 | } | |
8766e441 RBR |
59 | if Strategy::is_description(line) { |
60 | description = Strategy::get_description(line).trim(); | |
5732d284 | 61 | lines_found += 1; |
260e8ec6 RBR |
62 | continue; |
63 | } | |
68fa37d6 RBR |
64 | } |
65 | } | |
66 | ||
67 | let gemini_source = lines[lines_found..].join("\n"); | |
8766e441 | 68 | let content_html = render_html(&parse(&gemini_source[..])); |
68fa37d6 RBR |
69 | |
70 | let generated_html = layout | |
71 | .replace("{{ title }}", title) | |
72 | .replace("{{ description }}", description) | |
73 | .replace("{{ content }}", &content_html[..]); | |
74 | ||
5732d284 | 75 | let relative_path = file.path.strip_prefix(source).unwrap(); |
0e3bcda2 RBR |
76 | let mut complete_destination = destination.join(relative_path); |
77 | complete_destination.set_extension("html"); | |
68fa37d6 RBR |
78 | let destination_parent = complete_destination.parent().unwrap(); |
79 | create_dir_all(destination_parent).unwrap(); | |
80 | ||
81 | let mut destination_file = IOFile::create(&complete_destination).unwrap(); | |
5732d284 RBR |
82 | destination_file |
83 | .write_all(generated_html.as_bytes()) | |
84 | .unwrap(); | |
102a4884 | 85 | } |
dd0a540c | 86 | |
5732d284 | 87 | fn handle_gemini(&self, source: &Path, destination: &Path, file: &File) { |
dd0a540c RBR |
88 | let gemini_contents = read_to_string(&file.path).unwrap(); |
89 | ||
90 | // Front matter extraction | |
8766e441 | 91 | let lines: Vec<&str> = gemini_contents.split('\n').collect(); |
dd0a540c | 92 | let mut lines_found = 0; |
260e8ec6 | 93 | if let Some(slice) = lines.get(..2) { |
8766e441 RBR |
94 | for line in slice { |
95 | if Strategy::is_title(line) { | |
5732d284 | 96 | lines_found += 1; |
260e8ec6 RBR |
97 | continue; |
98 | } | |
8766e441 | 99 | if Strategy::is_description(line) { |
5732d284 | 100 | lines_found += 1; |
260e8ec6 RBR |
101 | continue; |
102 | } | |
dd0a540c RBR |
103 | } |
104 | } | |
105 | ||
106 | let gemini_source = lines[lines_found..].join("\n"); | |
107 | ||
5732d284 | 108 | let relative_path = file.path.strip_prefix(source).unwrap(); |
dd0a540c RBR |
109 | let complete_destination = destination.join(relative_path); |
110 | let destination_parent = complete_destination.parent().unwrap(); | |
111 | create_dir_all(destination_parent).unwrap(); | |
112 | ||
113 | let mut destination_file = IOFile::create(&complete_destination).unwrap(); | |
5732d284 RBR |
114 | destination_file |
115 | .write_all(gemini_source.as_bytes()) | |
116 | .unwrap(); | |
dd0a540c | 117 | } |
102a4884 | 118 | } |
260e8ec6 RBR |
119 | |
120 | #[cfg(test)] | |
121 | mod tests { | |
5732d284 RBR |
122 | use std::fs::create_dir_all; |
123 | ||
260e8ec6 | 124 | use super::*; |
260e8ec6 | 125 | |
5732d284 | 126 | use test_utilities::*; |
260e8ec6 | 127 | |
5732d284 RBR |
128 | #[test] |
129 | fn detects_title() { | |
8766e441 | 130 | assert!(Strategy::is_title("--- title: Hello!")); |
260e8ec6 RBR |
131 | } |
132 | ||
5732d284 RBR |
133 | #[test] |
134 | fn does_not_detect_other_keys_as_title() { | |
8766e441 | 135 | assert!(!Strategy::is_title("--- description: Hello!")); |
260e8ec6 RBR |
136 | } |
137 | ||
5732d284 RBR |
138 | #[test] |
139 | fn detects_description() { | |
8766e441 | 140 | assert!(Strategy::is_description("--- description: What is this?")); |
260e8ec6 RBR |
141 | } |
142 | ||
5732d284 RBR |
143 | #[test] |
144 | fn does_not_detect_other_keys_as_description() { | |
8766e441 | 145 | assert!(!Strategy::is_description("--- title: What is this?")); |
260e8ec6 RBR |
146 | } |
147 | ||
5732d284 RBR |
148 | #[test] |
149 | fn extracts_title() { | |
8766e441 | 150 | assert_eq!(Strategy::get_title("--- title: Hello!").trim(), "Hello!"); |
5732d284 | 151 | } |
260e8ec6 | 152 | |
5732d284 RBR |
153 | #[test] |
154 | fn extracts_description() { | |
5732d284 | 155 | assert_eq!( |
36a5c232 | 156 | Strategy::get_description("--- description: What is this?").trim(), |
5732d284 RBR |
157 | "What is this?" |
158 | ); | |
260e8ec6 RBR |
159 | } |
160 | ||
5732d284 RBR |
161 | #[test] |
162 | fn identifies_gemini_file() { | |
163 | let test_dir = setup_test_dir(); | |
164 | create_test_file(&test_dir.join("test.gmi"), ""); | |
165 | let strategy = Strategy {}; | |
166 | assert!(strategy.is(&test_dir.join("test.gmi"))); | |
167 | } | |
260e8ec6 | 168 | |
5732d284 RBR |
169 | #[test] |
170 | fn rejects_non_gemini_file() { | |
171 | let test_dir = setup_test_dir(); | |
172 | create_test_file(&test_dir.join("_layout.html"), ""); | |
173 | create_test_file(&test_dir.join("image.png"), ""); | |
174 | let strategy = Strategy {}; | |
175 | assert!(!strategy.is(&test_dir.join("_layout.html"))); | |
176 | assert!(!strategy.is(&test_dir.join("image.png"))); | |
177 | assert!(!strategy.is(&test_dir)); | |
178 | } | |
260e8ec6 | 179 | |
5732d284 RBR |
180 | #[test] |
181 | fn identifies_gemini_type() { | |
182 | let strategy = Strategy {}; | |
183 | assert!(matches!(strategy.identify(), FileType::Gemini)); | |
184 | } | |
260e8ec6 | 185 | |
5732d284 RBR |
186 | #[test] |
187 | fn handles_gemini_type() { | |
188 | let strategy = Strategy {}; | |
189 | assert!(strategy.can_handle(&FileType::Gemini)); | |
190 | } | |
260e8ec6 | 191 | |
5732d284 RBR |
192 | #[test] |
193 | fn rejects_non_gemini_types() { | |
194 | let strategy = Strategy {}; | |
195 | assert!(!strategy.can_handle(&FileType::Layout)); | |
196 | assert!(!strategy.can_handle(&FileType::File)); | |
197 | assert!(!strategy.can_handle(&FileType::Unknown)); | |
198 | } | |
260e8ec6 | 199 | |
5732d284 RBR |
200 | #[test] |
201 | fn handles_html_generation() { | |
202 | let test_dir = setup_test_dir(); | |
203 | let source_dir = test_dir.join("source"); | |
204 | let output_dir = test_dir.join("output"); | |
205 | create_dir_all(&source_dir).expect("Could not create source test directory"); | |
206 | create_dir_all(&output_dir).expect("Could not create output test directory"); | |
207 | let layout = "\ | |
208 | <html> | |
209 | <head> | |
210 | <title>{{ title }}</title> | |
211 | <meta name=\"description\" content=\"{{ description }}\"> | |
212 | </head> | |
213 | <body>{{ content }}</body> | |
214 | </html> | |
215 | "; | |
216 | create_test_file( | |
217 | &source_dir.join("test.gmi"), | |
218 | "\ | |
219 | --- title: Page Is Cool! | |
220 | --- description: My Description | |
221 | # Test | |
222 | Hello world | |
223 | ", | |
224 | ); | |
225 | ||
226 | let strategy = Strategy {}; | |
227 | let file = File { | |
228 | path: source_dir.join("test.gmi"), | |
229 | file_type: FileType::Gemini, | |
230 | }; | |
231 | ||
b0328413 | 232 | strategy.handle_html(&source_dir, &output_dir, &file, layout); |
5732d284 RBR |
233 | |
234 | let html_output = output_dir.join("test.html"); | |
235 | assert!(html_output.exists()); | |
236 | assert_file_contents( | |
237 | &html_output, | |
238 | "\ | |
239 | <html> | |
240 | <head> | |
241 | <title>Page Is Cool!</title> | |
242 | <meta name=\"description\" content=\"My Description\"> | |
243 | </head> | |
244 | <body><section class=\"h1\"> | |
245 | <h1> Test</h1> | |
246 | <p>Hello world</p> | |
247 | </section> | |
248 | </body> | |
249 | </html> | |
250 | ", | |
251 | ); | |
252 | } | |
260e8ec6 | 253 | |
5732d284 RBR |
254 | #[test] |
255 | fn handles_gemini_generation() { | |
256 | let test_dir = setup_test_dir(); | |
257 | let source_dir = test_dir.join("source"); | |
258 | let output_dir = test_dir.join("output"); | |
259 | create_dir_all(&source_dir).expect("Could not create source test directory"); | |
260 | create_dir_all(&output_dir).expect("Could not create output test directory"); | |
261 | create_test_file( | |
262 | &source_dir.join("test.gmi"), | |
263 | "\ | |
264 | --- title: Page Is Cool! | |
265 | --- description: My Description | |
266 | # Test | |
267 | Hello world | |
268 | ", | |
269 | ); | |
270 | ||
271 | let strategy = Strategy {}; | |
272 | let file = File { | |
273 | path: source_dir.join("test.gmi"), | |
274 | file_type: FileType::Gemini, | |
275 | }; | |
276 | ||
277 | strategy.handle_gemini(&source_dir, &output_dir, &file); | |
278 | ||
279 | let gemini_output = output_dir.join("test.gmi"); | |
280 | assert!(gemini_output.exists()); | |
281 | assert_file_contents( | |
282 | &gemini_output, | |
283 | "\ | |
284 | # Test | |
285 | Hello world | |
286 | ", | |
287 | ); | |
288 | } | |
260e8ec6 | 289 | |
5732d284 RBR |
290 | #[test] |
291 | fn handles_nested_structure() { | |
292 | let test_dir = setup_test_dir(); | |
293 | let source_dir = test_dir.join("source"); | |
294 | let output_dir = test_dir.join("output"); | |
b0328413 | 295 | create_dir_all(source_dir.join("nested")).expect("Could not create source test directory"); |
5732d284 RBR |
296 | create_dir_all(&output_dir).expect("Could not create output test directory"); |
297 | let layout = "\ | |
298 | <html> | |
299 | <head> | |
300 | <title>{{ title }}</title> | |
301 | <meta name=\"description\" content=\"{{ description }}\"> | |
302 | </head> | |
303 | <body>{{ content }}</body> | |
304 | </html> | |
305 | "; | |
306 | create_test_file( | |
307 | &source_dir.join("nested/test.gmi"), | |
308 | "\ | |
309 | --- title: Page Is Cool! | |
310 | --- description: My Description | |
311 | # Test | |
312 | Hello world | |
313 | ", | |
314 | ); | |
315 | ||
316 | let strategy = Strategy {}; | |
317 | let file = File { | |
318 | path: source_dir.join("nested/test.gmi"), | |
319 | file_type: FileType::Gemini, | |
320 | }; | |
321 | ||
b0328413 | 322 | strategy.handle_html(&source_dir, &output_dir, &file, layout); |
5732d284 RBR |
323 | |
324 | let html_output = output_dir.join("nested/test.html"); | |
325 | assert!(html_output.exists()); | |
326 | assert_file_contents( | |
327 | &html_output, | |
328 | "\ | |
329 | <html> | |
330 | <head> | |
331 | <title>Page Is Cool!</title> | |
332 | <meta name=\"description\" content=\"My Description\"> | |
333 | </head> | |
334 | <body><section class=\"h1\"> | |
335 | <h1> Test</h1> | |
336 | <p>Hello world</p> | |
337 | </section> | |
338 | </body> | |
339 | </html> | |
340 | ", | |
341 | ); | |
260e8ec6 RBR |
342 | } |
343 | } |