diff --git a/htmlua-parser/src/lib.rs b/htmlua-parser/src/lib.rs
index dcbd4b1..bf62208 100644
--- a/htmlua-parser/src/lib.rs
+++ b/htmlua-parser/src/lib.rs
@@ -1,5 +1,7 @@
use std::cell::RefCell;
use std::fmt::Write;
+use std::fs::read_to_string;
+use std::path::PathBuf;
use std::rc::Rc;
use anyhow::Result;
@@ -9,8 +11,7 @@ use kuchikiki::{
traits::TendrilSink,
};
use mlua::{Lua, Table};
-use tendril::fmt;
-use tendril::{Atomicity, Tendril};
+use tendril::Atomicity;
fn create_luahtml_stdlib(l: &Lua, stdout: &Rc>) -> mlua::Result {
let t = l.create_table()?;
@@ -37,14 +38,7 @@ fn create_luahtml_stdlib(l: &Lua, stdout: &Rc>) -> mlua::Result<
Ok(t)
}
-pub fn parse(to_parse: T) -> Result
-where
- F: fmt::Format,
- A: Atomicity,
- T: Into>,
- tendril::Tendril: std::convert::From>,
-{
- let document = kuchikiki::parse_html().one(to_parse.into());
+pub fn execute_lua(document: NodeRef) -> Result {
let lua = Lua::new();
let globals = lua.globals();
@@ -79,12 +73,32 @@ where
Ok(document)
}
+pub fn expand_template(document: NodeRef, component_path: &PathBuf) -> Result {
+ for i in document.select("include").map_err(|()| anyhow!("Error finding include"))? {
+ let attrs = match i.as_node().as_element() {
+ Some(e) => e.attributes.borrow(),
+ None => continue,
+ };
+ if let Some(include_path) = attrs.get("path") {
+ let mut item_path = component_path.clone();
+ item_path.push(include_path);
+ println!("{item_path:?}");
+ let component_text = read_to_string(item_path)?;
+ let new_node = kuchikiki::parse_html().one(component_text);
+ let replaced_node = expand_template(new_node, component_path)?;
+ i.as_node().insert_before(replaced_node);
+ i.as_node().detach();
+ }
+ }
+ Ok(document)
+}
+
#[cfg(test)]
mod tests {
use super::*;
#[test]
- fn base() {
+ fn basic_lua() {
let page = r#"
@@ -99,9 +113,59 @@ mod tests {