From f9b2e74b4525eabb5ea7d1df84b4e570194429b8 Mon Sep 17 00:00:00 2001 From: gyoder <70408179+gyoder@users.noreply.github.com> Date: Mon, 7 Jul 2025 11:33:26 -0600 Subject: Fixed document vs fragment parsing --- htmlua-parser/src/render.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'htmlua-parser/src/render.rs') diff --git a/htmlua-parser/src/render.rs b/htmlua-parser/src/render.rs index fe0b64f..1ea99a0 100644 --- a/htmlua-parser/src/render.rs +++ b/htmlua-parser/src/render.rs @@ -1,14 +1,15 @@ 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; use anyhow::anyhow; -use kuchikiki::{NodeRef, traits::TendrilSink}; +use kuchikiki::NodeRef; use mlua::{Lua, Table}; +use crate::helpers::read_doc_from_file; + fn create_htmlua_stdlib(l: &Lua, stdout: &Rc>) -> mlua::Result { let t = l.create_table()?; @@ -79,13 +80,14 @@ pub fn expand_template(document: NodeRef, component_path: &PathBuf, include_from let exported = from_node .select_first(format!("exportelement.{name}").as_str()) .map_err(|()| anyhow!("Error finding exportelement"))?; - exported.as_node().children().for_each(|c| i.as_node().insert_after(c)); + exported.as_node().children().rev().for_each(|c| i.as_node().insert_after(c)); } } while let Ok(i) = document.select_first("includeelement") { i.as_node().detach(); } } + for i in document .select("include") .map_err(|()| anyhow!("Error finding include"))? @@ -97,10 +99,8 @@ pub fn expand_template(document: NodeRef, component_path: &PathBuf, include_from if let Some(include_path) = attrs.get("path") { let mut item_path = component_path.clone(); item_path.push(include_path); - let component_text = read_to_string(item_path)?; - 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()))?; + let new_node = read_doc_from_file(item_path)?; + let replaced_node = expand_template(new_node, component_path, Some(i.as_node()))?; i.as_node().insert_before(replaced_node); i.as_node().detach(); } -- cgit v1.3.1