style: Minor style modifications
This commit is contained in:
+22
-29
@@ -1,6 +1,5 @@
|
||||
use std::fs;
|
||||
|
||||
// Make sure your models match your struct definitions
|
||||
use crate::compiler::models::{Page, PageRoute, Project, Template};
|
||||
|
||||
pub fn compile() {
|
||||
@@ -26,11 +25,9 @@ fn list_all_paths() -> Vec<String> {
|
||||
for entry in entries {
|
||||
let entry = entry.unwrap();
|
||||
let path = entry.path();
|
||||
|
||||
|
||||
if path.is_dir() {
|
||||
let dir_name = path.file_name()
|
||||
.and_then(|n| n.to_str())
|
||||
.unwrap_or("");
|
||||
let dir_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
||||
|
||||
if !dir_name.starts_with('.') && dir_name != "target" {
|
||||
paths.push(path.to_str().unwrap().to_string());
|
||||
@@ -42,7 +39,6 @@ fn list_all_paths() -> Vec<String> {
|
||||
}
|
||||
|
||||
fn construct_page(page_path: &str) -> Page {
|
||||
// --- 1. Route Resolution ---
|
||||
let suffixes = [".regexpr", ".js", ""];
|
||||
let mut route_option: Option<PageRoute> = None;
|
||||
|
||||
@@ -52,33 +48,32 @@ fn construct_page(page_path: &str) -> Page {
|
||||
if let Ok(content) = fs::read_to_string(&file_path) {
|
||||
route_option = Some(match suffix {
|
||||
".regexpr" => PageRoute::Regexpr(content),
|
||||
".js" => PageRoute::CustomScript(content),
|
||||
"" => PageRoute::Static(content),
|
||||
_ => unreachable!(),
|
||||
".js" => PageRoute::CustomScript(content),
|
||||
"" => PageRoute::Static(content),
|
||||
_ => unreachable!(),
|
||||
});
|
||||
break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let route = route_option.unwrap_or_else(|| {
|
||||
panic!("No valid route file found (route.regexpr, route.js, or route) in '{}'", page_path)
|
||||
});
|
||||
panic!(
|
||||
"No valid route file found (route.regexpr, route.js, or route) in '{}'",
|
||||
page_path
|
||||
)
|
||||
});
|
||||
|
||||
// --- 2. Page-level Style ---
|
||||
let style_path = format!("{}/style.css", page_path);
|
||||
let style = fs::read_to_string(&style_path).ok();
|
||||
|
||||
// --- 3. Parse Templates ---
|
||||
let mut content = Vec::new();
|
||||
let templates_dir = format!("{}/templates", page_path);
|
||||
|
||||
// Check if the templates directory exists for this page
|
||||
if let Ok(entries) = fs::read_dir(&templates_dir) {
|
||||
for entry in entries {
|
||||
let entry = entry.expect("Failed to read template entry");
|
||||
let path = entry.path();
|
||||
|
||||
// Only process folders inside the 'templates' directory
|
||||
|
||||
if path.is_dir() {
|
||||
let template_path = path.to_str().unwrap();
|
||||
let template = construct_template(template_path);
|
||||
@@ -95,40 +90,38 @@ fn construct_page(page_path: &str) -> Page {
|
||||
}
|
||||
|
||||
fn construct_template(template_path: &str) -> Template {
|
||||
// --- 1. Template-level Style ---
|
||||
let style_path = format!("{}/style.css", template_path);
|
||||
let style = fs::read_to_string(&style_path).ok();
|
||||
|
||||
// --- 2. Template Content Resolution ---
|
||||
let script_path = format!("{}/script.js", template_path);
|
||||
let template_html_path = format!("{}/template.html", template_path);
|
||||
|
||||
|
||||
if let Ok(script_content) = fs::read_to_string(&script_path) {
|
||||
Template::CustomScript {
|
||||
script: script_content,
|
||||
style,
|
||||
}
|
||||
}
|
||||
else if let Ok(template_content) = fs::read_to_string(&template_html_path) {
|
||||
} else if let Ok(template_content) = fs::read_to_string(&template_html_path) {
|
||||
let scraper_path = format!("{}/scrape.js", template_path);
|
||||
let scraper = fs::read_to_string(&scraper_path).ok();
|
||||
|
||||
let replace_selector_path = format!("{}/replace_selector", template_path);
|
||||
let replace_selector = fs::read_to_string(&replace_selector_path).unwrap_or_else(|_| {
|
||||
panic!("replace_selector file not found in '{}': Required for template injection", template_path);
|
||||
panic!(
|
||||
"replace_selector file not found in '{}': Required for template injection",
|
||||
template_path
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
Template::TemplateInjector {
|
||||
template: template_content,
|
||||
// .trim() prevents trailing newlines in text files from breaking CSS selectors
|
||||
replace_selector: replace_selector.trim().to_string(),
|
||||
replace_selector: replace_selector.trim().to_string(),
|
||||
scraper,
|
||||
style,
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
panic!(
|
||||
"Template content not found in '{}': Directory must contain either script.js or template.html",
|
||||
"Template content not found in '{}': Directory must contain either script.js or template.html",
|
||||
template_path
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ pub struct Project {
|
||||
|
||||
pub struct Page {
|
||||
pub route: PageRoute,
|
||||
pub style: Option<String>,
|
||||
pub style: Option<String>, // TODO: Actually use this lmao
|
||||
pub content: Vec<Template>,
|
||||
}
|
||||
|
||||
|
||||
@@ -139,7 +139,6 @@ impl Template {
|
||||
const parsedData = eval({scraper} || "(() => ({}))");
|
||||
const template = Handlebars.compile({template});
|
||||
const rendered = template(parsedData);
|
||||
console.log("Rendered template:", rendered);
|
||||
const targetElement = document.querySelector({replace_selector});
|
||||
|
||||
const temp = document.createElement('template');
|
||||
|
||||
Reference in New Issue
Block a user