summaryrefslogtreecommitdiff
path: root/htmlua-parser
diff options
context:
space:
mode:
authorgyoder <70408179+gyoder@users.noreply.github.com>2025-06-23 20:04:08 -0600
committergyoder <70408179+gyoder@users.noreply.github.com>2025-06-23 20:04:41 -0600
commit0c33e46b3055defe2baf37ce609cf6f1a7e2b370 (patch)
tree7f2ec91f81fa74b0e08538b7d0baa300f62040ee /htmlua-parser
parent013317c13c6389dd41ba79f4d244ff5a230df28c (diff)
added template expanding
Diffstat (limited to 'htmlua-parser')
-rw-r--r--htmlua-parser/src/lib.rs88
-rw-r--r--htmlua-parser/tests/components/comp1.html1
-rw-r--r--htmlua-parser/tests/components/comp2_level1.html4
-rw-r--r--htmlua-parser/tests/components/comp2_level2.html1
4 files changed, 82 insertions, 12 deletions
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<RefCell<String>>) -> mlua::Result<Table> {
let t = l.create_table()?;
@@ -37,14 +38,7 @@ fn create_luahtml_stdlib(l: &Lua, stdout: &Rc<RefCell<String>>) -> mlua::Result<
Ok(t)
}
-pub fn parse<F, A, T>(to_parse: T) -> Result<NodeRef>
-where
- F: fmt::Format,
- A: Atomicity,
- T: Into<Tendril<F, A>>,
- tendril::Tendril<tendril::fmt::UTF8>: std::convert::From<tendril::Tendril<F, A>>,
-{
- let document = kuchikiki::parse_html().one(to_parse.into());
+pub fn execute_lua(document: NodeRef) -> Result<NodeRef> {
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<NodeRef> {
+ 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#"
<!DOCTYPE html>
<html>
@@ -99,9 +113,59 @@ mod tests {
</lua></span>
</body>
</html>"#;
- let d = parse(page).unwrap();
+ let document = kuchikiki::parse_html().one(page);
+ let d = execute_lua(document).unwrap();
let text = d.select_first("span").unwrap().as_node().text_contents();
assert_eq!(text, "Test from Lua!\n");
assert!(d.select_first("lua").is_err());
}
+
+ #[test]
+ fn basic_include() {
+ let page = r#"
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>Basic HTML Page</title>
+ </head>
+ <body>
+ <h1>Hello World</h1>
+ <p>This is a paragraph.</p>
+ <include path="comp1.html" />
+ <span><lua>
+ htmlua.println("Test from Lua!")
+ </lua></span>
+ </body>
+ </html>"#;
+ let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
+ p.push("tests/components");
+ let document = kuchikiki::parse_html().one(page);
+ let d = expand_template(document, &p).unwrap();
+ let text = d.select_first("span").unwrap().as_node().text_contents();
+ assert_eq!(text, "included1");
+ assert!(d.select_first("include").is_err());
+ }
+
+ #[test]
+ fn recursive_include() {
+ let page = r#"
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>Basic HTML Page</title>
+ </head>
+ <body>
+ <h1>Hello World</h1>
+ <include path="comp2_level1.html" />
+ </body>
+ </html>"#;
+ let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
+ p.push("tests/components");
+ let document = kuchikiki::parse_html().one(page);
+ let d = expand_template(document, &p).unwrap();
+ let text = d.select_first("span").unwrap().as_node().text_contents();
+ assert_eq!(text, "included_twice");
+ assert!(d.select_first("include").is_err());
+ }
+
}
diff --git a/htmlua-parser/tests/components/comp1.html b/htmlua-parser/tests/components/comp1.html
new file mode 100644
index 0000000..516d00e
--- /dev/null
+++ b/htmlua-parser/tests/components/comp1.html
@@ -0,0 +1 @@
+<span>included1</span>
diff --git a/htmlua-parser/tests/components/comp2_level1.html b/htmlua-parser/tests/components/comp2_level1.html
new file mode 100644
index 0000000..086a939
--- /dev/null
+++ b/htmlua-parser/tests/components/comp2_level1.html
@@ -0,0 +1,4 @@
+<div>
+ <p>test</p>
+ <include path="comp2_level2.html"/>
+</div>
diff --git a/htmlua-parser/tests/components/comp2_level2.html b/htmlua-parser/tests/components/comp2_level2.html
new file mode 100644
index 0000000..5a8bced
--- /dev/null
+++ b/htmlua-parser/tests/components/comp2_level2.html
@@ -0,0 +1 @@
+<span>included_twice</span>