summaryrefslogtreecommitdiff
path: root/htmlua-parser/src/render.rs
diff options
context:
space:
mode:
authorQuin <trypsynth@gmail.com>2025-07-05 22:07:28 -0600
committerQuin <trypsynth@gmail.com>2025-07-05 22:07:28 -0600
commitc663ae27a8894afe8dc27586ecfc5f94b8731363 (patch)
treee85b9c4192e4d338999c5c2065601c7c8e568f73 /htmlua-parser/src/render.rs
parent8e25531af188b89acd9f3283b6925f06789248af (diff)
parent9a0038dacde3bcf1a10de51278d72f0f322c3817 (diff)
Fix merge.
Diffstat (limited to 'htmlua-parser/src/render.rs')
-rw-r--r--htmlua-parser/src/render.rs40
1 files changed, 36 insertions, 4 deletions
diff --git a/htmlua-parser/src/render.rs b/htmlua-parser/src/render.rs
index 21f66ba..eb50c2c 100644
--- a/htmlua-parser/src/render.rs
+++ b/htmlua-parser/src/render.rs
@@ -5,7 +5,7 @@ use kuchikiki::{NodeRef, traits::TendrilSink};
use mlua::{Lua, Table};
use pulldown_cmark::{Options, Parser, html};
-fn create_luahtml_stdlib(l: &Lua, stdout: &Rc<RefCell<String>>) -> mlua::Result<Table> {
+fn create_htmlua_stdlib(l: &Lua, stdout: &Rc<RefCell<String>>) -> mlua::Result<Table> {
let t = l.create_table()?;
// This cannot be the best way to do this
@@ -37,7 +37,7 @@ pub fn execute_lua(document: NodeRef) -> Result<NodeRef> {
let stdout: Rc<RefCell<String>> = Rc::new(RefCell::new(String::new()));
let htmlua_table =
- create_luahtml_stdlib(&lua, &stdout).map_err(|e| anyhow!("Failed to create Lua stdlib: {}", e))?;
+ create_htmlua_stdlib(&lua, &stdout).map_err(|e| anyhow!("Failed to create Lua stdlib: {}", e))?;
globals
.set("htmlua", htmlua_table)
@@ -118,8 +118,9 @@ pub fn expand_template(document: NodeRef, component_path: &PathBuf, include_from
let mut item_path = component_path.clone();
item_path.push(include_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, Some(i.as_node()))?;
+ let new_doc = kuchikiki::parse_html().one(component_text);
+ let new_node = new_doc.select_first("body").map_err(|()| anyhow!("Failed to get body"))?;
+ let replaced_node = expand_template(new_node.as_node().clone(), component_path, Some(i.as_node()))?;
i.as_node().insert_before(replaced_node);
i.as_node().detach();
}
@@ -209,6 +210,37 @@ mod tests {
}
#[test]
+ fn basic_lua_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>
+ <span><lua>
+ htmlua.println("Test from Lua!")
+ </lua></span>
+ <include path="comp1.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 = execute_lua(expand_template(document, &p, None).unwrap()).unwrap();
+ let text = d.select_first("span").unwrap().as_node().text_contents();
+ println!("{}", d.to_string());
+ assert_eq!(text, "Test from Lua!\n");
+ assert!(d.select_first("lua").is_err());
+ let text = d.select_first("#inc1").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>