1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
pub struct Strategy {}
use std::path::PathBuf;
use std::io::Write;
use std::fs::{create_dir_all, read_to_string, File as IOFile};
use crate::file_handler::{File, FileType, FileHandlerStrategy};
use crate::gemini_parser::parse;
impl Strategy {
fn is_title(&self, line: &str) -> bool {
line.starts_with("--- title:")
}
fn is_description(&self, line: &str) -> bool {
line.starts_with("--- description:")
}
fn get_title<'a>(&self, line: &'a str) -> &'a str {
line.split_once("--- title:").unwrap().1
}
fn get_description<'a>(&self, line: &'a str) -> &'a str {
line.split_once("--- description:").unwrap().1
}
}
impl FileHandlerStrategy for Strategy {
fn is(&self, path: &PathBuf) -> bool {
if let Some(extension) = path.extension() {
return !path.is_dir() && extension == "gmi"
}
false
}
fn identify(&self) -> FileType {
FileType::Gemini
}
fn can_handle(&self, file_type: &FileType) -> bool {
match file_type {
FileType::Gemini => true,
_ => false,
}
}
fn handle_html(&self, source: &PathBuf, destination: &PathBuf, file: &File, layout: &String) {
let gemini_contents = read_to_string(&file.path).unwrap();
// Front matter extraction
let lines: Vec<&str> = gemini_contents.split("\n").collect();
let mut lines_found = 0;
let mut title = "";
let mut description = "";
for line in lines[..2].iter() {
if self.is_title(&line) {
title = self.get_title(&line).trim();
lines_found = lines_found + 1;
continue;
}
if self.is_description(&line) {
description = self.get_description(&line).trim();
lines_found = lines_found + 1;
continue;
}
}
let gemini_source = lines[lines_found..].join("\n");
let content_html = parse(&gemini_source[..]);
let generated_html = layout
.replace("{{ title }}", title)
.replace("{{ description }}", description)
.replace("{{ content }}", &content_html[..]);
let relative_path = file.path.strip_prefix(&source).unwrap();
let mut complete_destination = destination.join(relative_path);
complete_destination.set_extension("html");
let destination_parent = complete_destination.parent().unwrap();
create_dir_all(destination_parent).unwrap();
let mut destination_file = IOFile::create(&complete_destination).unwrap();
destination_file.write_all(generated_html.as_bytes()).unwrap();
}
fn handle_gemini(&self, source: &PathBuf, destination: &PathBuf, file: &File) {
let gemini_contents = read_to_string(&file.path).unwrap();
// Front matter extraction
let lines: Vec<&str> = gemini_contents.split("\n").collect();
let mut lines_found = 0;
for line in lines[..2].iter() {
if self.is_title(&line) {
lines_found = lines_found + 1;
continue;
}
if self.is_description(&line) {
lines_found = lines_found + 1;
continue;
}
}
let gemini_source = lines[lines_found..].join("\n");
let relative_path = file.path.strip_prefix(&source).unwrap();
let complete_destination = destination.join(relative_path);
let destination_parent = complete_destination.parent().unwrap();
create_dir_all(destination_parent).unwrap();
let mut destination_file = IOFile::create(&complete_destination).unwrap();
destination_file.write_all(gemini_source.as_bytes()).unwrap();
}
}
|