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 gema_texto::{gemini_parser::parse, html_renderer::render_html};
11 fn is_title(line: &str) -> bool {
12 line.starts_with("--- title:")
15 fn is_description(line: &str) -> bool {
16 line.starts_with("--- description:")
19 fn get_title(line: &str) -> &str {
20 line.split_once("--- title:").unwrap().1
23 fn get_description(line: &str) -> &str {
24 line.split_once("--- description:").unwrap().1
28 impl FileHandlerStrategy for Strategy {
29 fn is(&self, path: &Path) -> bool {
30 if let Some(extension) = path.extension() {
31 return !path.is_dir() && extension == "gmi";
36 fn identify(&self) -> FileType {
40 fn can_handle(&self, file_type: &FileType) -> bool {
41 matches!(file_type, FileType::Gemini)
44 fn handle_html(&self, source: &Path, destination: &Path, file: &File, layout: &str) {
45 let gemini_contents = read_to_string(&file.path).unwrap();
47 // Front matter extraction
48 let lines: Vec<&str> = gemini_contents.split('\n').collect();
49 let mut lines_found = 0;
51 let mut description = "";
52 if let Some(slice) = lines.get(..2) {
54 if Strategy::is_title(line) {
55 title = Strategy::get_title(line).trim();
59 if Strategy::is_description(line) {
60 description = Strategy::get_description(line).trim();
67 let gemini_source = lines[lines_found..].join("\n");
68 let content_html = render_html(&parse(&gemini_source[..]));
70 let generated_html = layout
71 .replace("{{ title }}", title)
72 .replace("{{ description }}", description)
73 .replace("{{ content }}", &content_html[..]);
75 let relative_path = file.path.strip_prefix(source).unwrap();
76 let mut complete_destination = destination.join(relative_path);
77 complete_destination.set_extension("html");
78 let destination_parent = complete_destination.parent().unwrap();
79 create_dir_all(destination_parent).unwrap();
81 let mut destination_file = IOFile::create(&complete_destination).unwrap();
83 .write_all(generated_html.as_bytes())
87 fn handle_gemini(&self, source: &Path, destination: &Path, file: &File) {
88 let gemini_contents = read_to_string(&file.path).unwrap();
90 // Front matter extraction
91 let lines: Vec<&str> = gemini_contents.split('\n').collect();
92 let mut lines_found = 0;
93 if let Some(slice) = lines.get(..2) {
95 if Strategy::is_title(line) {
99 if Strategy::is_description(line) {
106 let gemini_source = lines[lines_found..].join("\n");
108 let relative_path = file.path.strip_prefix(source).unwrap();
109 let complete_destination = destination.join(relative_path);
110 let destination_parent = complete_destination.parent().unwrap();
111 create_dir_all(destination_parent).unwrap();
113 let mut destination_file = IOFile::create(&complete_destination).unwrap();
115 .write_all(gemini_source.as_bytes())
122 use std::fs::create_dir_all;
126 use test_utilities::*;
130 assert!(Strategy::is_title("--- title: Hello!"));
134 fn does_not_detect_other_keys_as_title() {
135 assert!(!Strategy::is_title("--- description: Hello!"));
139 fn detects_description() {
140 assert!(Strategy::is_description("--- description: What is this?"));
144 fn does_not_detect_other_keys_as_description() {
145 assert!(!Strategy::is_description("--- title: What is this?"));
149 fn extracts_title() {
150 assert_eq!(Strategy::get_title("--- title: Hello!").trim(), "Hello!");
154 fn extracts_description() {
156 Strategy::get_description("--- description: What is this?").trim(),
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")));
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));
181 fn identifies_gemini_type() {
182 let strategy = Strategy {};
183 assert!(matches!(strategy.identify(), FileType::Gemini));
187 fn handles_gemini_type() {
188 let strategy = Strategy {};
189 assert!(strategy.can_handle(&FileType::Gemini));
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));
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");
210 <title>{{ title }}</title>
211 <meta name=\"description\" content=\"{{ description }}\">
213 <body>{{ content }}</body>
217 &source_dir.join("test.gmi"),
219 --- title: Page Is Cool!
220 --- description: My Description
226 let strategy = Strategy {};
228 path: source_dir.join("test.gmi"),
229 file_type: FileType::Gemini,
232 strategy.handle_html(&source_dir, &output_dir, &file, layout);
234 let html_output = output_dir.join("test.html");
235 assert!(html_output.exists());
236 assert_file_contents(
241 <title>Page Is Cool!</title>
242 <meta name=\"description\" content=\"My Description\">
244 <body><section class=\"h1\">
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");
262 &source_dir.join("test.gmi"),
264 --- title: Page Is Cool!
265 --- description: My Description
271 let strategy = Strategy {};
273 path: source_dir.join("test.gmi"),
274 file_type: FileType::Gemini,
277 strategy.handle_gemini(&source_dir, &output_dir, &file);
279 let gemini_output = output_dir.join("test.gmi");
280 assert!(gemini_output.exists());
281 assert_file_contents(
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");
295 create_dir_all(source_dir.join("nested")).expect("Could not create source test directory");
296 create_dir_all(&output_dir).expect("Could not create output test directory");
300 <title>{{ title }}</title>
301 <meta name=\"description\" content=\"{{ description }}\">
303 <body>{{ content }}</body>
307 &source_dir.join("nested/test.gmi"),
309 --- title: Page Is Cool!
310 --- description: My Description
316 let strategy = Strategy {};
318 path: source_dir.join("nested/test.gmi"),
319 file_type: FileType::Gemini,
322 strategy.handle_html(&source_dir, &output_dir, &file, layout);
324 let html_output = output_dir.join("nested/test.html");
325 assert!(html_output.exists());
326 assert_file_contents(
331 <title>Page Is Cool!</title>
332 <meta name=\"description\" content=\"My Description\">
334 <body><section class=\"h1\">