style: Minor style modifications

This commit is contained in:
2026-05-21 20:50:06 +02:00
parent b47d99ba2b
commit beabd7b1f6
3 changed files with 23 additions and 31 deletions
+14 -21
View File
@@ -1,6 +1,5 @@
use std::fs; use std::fs;
// Make sure your models match your struct definitions
use crate::compiler::models::{Page, PageRoute, Project, Template}; use crate::compiler::models::{Page, PageRoute, Project, Template};
pub fn compile() { pub fn compile() {
@@ -28,9 +27,7 @@ fn list_all_paths() -> Vec<String> {
let path = entry.path(); let path = entry.path();
if path.is_dir() { if path.is_dir() {
let dir_name = path.file_name() let dir_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
.and_then(|n| n.to_str())
.unwrap_or("");
if !dir_name.starts_with('.') && dir_name != "target" { if !dir_name.starts_with('.') && dir_name != "target" {
paths.push(path.to_str().unwrap().to_string()); 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 { fn construct_page(page_path: &str) -> Page {
// --- 1. Route Resolution ---
let suffixes = [".regexpr", ".js", ""]; let suffixes = [".regexpr", ".js", ""];
let mut route_option: Option<PageRoute> = None; 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) { if let Ok(content) = fs::read_to_string(&file_path) {
route_option = Some(match suffix { route_option = Some(match suffix {
".regexpr" => PageRoute::Regexpr(content), ".regexpr" => PageRoute::Regexpr(content),
".js" => PageRoute::CustomScript(content), ".js" => PageRoute::CustomScript(content),
"" => PageRoute::Static(content), "" => PageRoute::Static(content),
_ => unreachable!(), _ => unreachable!(),
}); });
break; break;
} }
} }
let route = route_option.unwrap_or_else(|| { 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_path = format!("{}/style.css", page_path);
let style = fs::read_to_string(&style_path).ok(); let style = fs::read_to_string(&style_path).ok();
// --- 3. Parse Templates ---
let mut content = Vec::new(); let mut content = Vec::new();
let templates_dir = format!("{}/templates", page_path); 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) { if let Ok(entries) = fs::read_dir(&templates_dir) {
for entry in entries { for entry in entries {
let entry = entry.expect("Failed to read template entry"); let entry = entry.expect("Failed to read template entry");
let path = entry.path(); let path = entry.path();
// Only process folders inside the 'templates' directory
if path.is_dir() { if path.is_dir() {
let template_path = path.to_str().unwrap(); let template_path = path.to_str().unwrap();
let template = construct_template(template_path); let template = construct_template(template_path);
@@ -95,11 +90,9 @@ fn construct_page(page_path: &str) -> Page {
} }
fn construct_template(template_path: &str) -> Template { fn construct_template(template_path: &str) -> Template {
// --- 1. Template-level Style ---
let style_path = format!("{}/style.css", template_path); let style_path = format!("{}/style.css", template_path);
let style = fs::read_to_string(&style_path).ok(); let style = fs::read_to_string(&style_path).ok();
// --- 2. Template Content Resolution ---
let script_path = format!("{}/script.js", template_path); let script_path = format!("{}/script.js", template_path);
let template_html_path = format!("{}/template.html", template_path); let template_html_path = format!("{}/template.html", template_path);
@@ -108,25 +101,25 @@ fn construct_template(template_path: &str) -> Template {
script: script_content, script: script_content,
style, 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_path = format!("{}/scrape.js", template_path);
let scraper = fs::read_to_string(&scraper_path).ok(); let scraper = fs::read_to_string(&scraper_path).ok();
let replace_selector_path = format!("{}/replace_selector", template_path); let replace_selector_path = format!("{}/replace_selector", template_path);
let replace_selector = fs::read_to_string(&replace_selector_path).unwrap_or_else(|_| { 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::TemplateInjector {
template: template_content, 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, scraper,
style, style,
} }
} } else {
else {
panic!( 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 template_path
+1 -1
View File
@@ -4,7 +4,7 @@ pub struct Project {
pub struct Page { pub struct Page {
pub route: PageRoute, pub route: PageRoute,
pub style: Option<String>, pub style: Option<String>, // TODO: Actually use this lmao
pub content: Vec<Template>, pub content: Vec<Template>,
} }
-1
View File
@@ -139,7 +139,6 @@ impl Template {
const parsedData = eval({scraper} || "(() => ({}))"); const parsedData = eval({scraper} || "(() => ({}))");
const template = Handlebars.compile({template}); const template = Handlebars.compile({template});
const rendered = template(parsedData); const rendered = template(parsedData);
console.log("Rendered template:", rendered);
const targetElement = document.querySelector({replace_selector}); const targetElement = document.querySelector({replace_selector});
const temp = document.createElement('template'); const temp = document.createElement('template');