use std::io::Write;
use std::path::Path;
-use crate::file_handler::{File, FileHandlerStrategy, FileType};
-use crate::gemini_parser::parse;
-use crate::html_renderer::render_html;
+use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy};
+use gema_texto::{gemini_parser::parse, html_renderer::render_html};
impl Strategy {
- fn is_title(&self, line: &str) -> bool {
+ fn is_title(line: &str) -> bool {
line.starts_with("--- title:")
}
- fn is_description(&self, line: &str) -> bool {
+ fn is_description(line: &str) -> bool {
line.starts_with("--- description:")
}
- fn get_title<'a>(&self, line: &'a str) -> &'a str {
+ fn get_title(line: &str) -> &str {
line.split_once("--- title:").unwrap().1
}
- fn get_description<'a>(&self, line: &'a str) -> &'a str {
+ fn get_description(line: &str) -> &str {
line.split_once("--- description:").unwrap().1
}
}
let gemini_contents = read_to_string(&file.path).unwrap();
// Front matter extraction
- let lines: Vec<&str> = gemini_contents.split("\n").collect();
+ let lines: Vec<&str> = gemini_contents.split('\n').collect();
let mut lines_found = 0;
let mut title = "";
let mut description = "";
if let Some(slice) = lines.get(..2) {
- for line in slice.iter() {
- if self.is_title(line) {
- title = self.get_title(line).trim();
+ for line in slice {
+ if Strategy::is_title(line) {
+ title = Strategy::get_title(line).trim();
lines_found += 1;
continue;
}
- if self.is_description(line) {
- description = self.get_description(line).trim();
+ if Strategy::is_description(line) {
+ description = Strategy::get_description(line).trim();
lines_found += 1;
continue;
}
}
let gemini_source = lines[lines_found..].join("\n");
- let content_html = render_html(parse(&gemini_source[..]));
+ let content_html = render_html(&parse(&gemini_source[..]));
let generated_html = layout
.replace("{{ title }}", title)
let gemini_contents = read_to_string(&file.path).unwrap();
// Front matter extraction
- let lines: Vec<&str> = gemini_contents.split("\n").collect();
+ let lines: Vec<&str> = gemini_contents.split('\n').collect();
let mut lines_found = 0;
if let Some(slice) = lines.get(..2) {
- for line in slice.iter() {
- if self.is_title(line) {
+ for line in slice {
+ if Strategy::is_title(line) {
lines_found += 1;
continue;
}
- if self.is_description(line) {
+ if Strategy::is_description(line) {
lines_found += 1;
continue;
}
#[test]
fn detects_title() {
- let strategy = Strategy {};
- assert!(strategy.is_title("--- title: Hello!"));
+ assert!(Strategy::is_title("--- title: Hello!"));
}
#[test]
fn does_not_detect_other_keys_as_title() {
- let strategy = Strategy {};
- assert!(!strategy.is_title("--- description: Hello!"));
+ assert!(!Strategy::is_title("--- description: Hello!"));
}
#[test]
fn detects_description() {
- let strategy = Strategy {};
- assert!(strategy.is_description("--- description: What is this?"));
+ assert!(Strategy::is_description("--- description: What is this?"));
}
#[test]
fn does_not_detect_other_keys_as_description() {
- let strategy = Strategy {};
- assert!(!strategy.is_description("--- title: What is this?"));
+ assert!(!Strategy::is_description("--- title: What is this?"));
}
#[test]
fn extracts_title() {
- let strategy = Strategy {};
- assert_eq!(strategy.get_title("--- title: Hello!").trim(), "Hello!");
+ assert_eq!(Strategy::get_title("--- title: Hello!").trim(), "Hello!");
}
#[test]
fn extracts_description() {
- let strategy = Strategy {};
assert_eq!(
- strategy
- .get_description("--- description: What is this?")
- .trim(),
+ Strategy::get_description("--- description: What is this?").trim(),
"What is this?"
);
}