applied suggestions from @kdkasad
This commit is contained in:
parent
d6a294c043
commit
013317c13c
3 changed files with 17 additions and 13 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
[workspace]
|
[workspace]
|
||||||
members = ["htmlua-parser", "htmlua-server"]
|
members = ["htmlua-parser", "htmlua-server"]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -8,3 +8,7 @@ anyhow = "1.0.98"
|
||||||
kuchikiki = "0.8.2"
|
kuchikiki = "0.8.2"
|
||||||
mlua = { version = "0.10.5", features = ["lua54", "vendored", "serialize"] }
|
mlua = { version = "0.10.5", features = ["lua54", "vendored", "serialize"] }
|
||||||
tendril = "0.4.3"
|
tendril = "0.4.3"
|
||||||
|
|
||||||
|
|
||||||
|
[lints.clippy]
|
||||||
|
pedantic = { level = "warn", priority = -1 }
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
#![warn(clippy::pedantic)]
|
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
@ -24,8 +22,7 @@ fn create_luahtml_stdlib(l: &Lua, stdout: &Rc<RefCell<String>>) -> mlua::Result<
|
||||||
"println",
|
"println",
|
||||||
l.create_function(move |_, text: String| {
|
l.create_function(move |_, text: String| {
|
||||||
let mut stdout_ref = stdout_println.borrow_mut();
|
let mut stdout_ref = stdout_println.borrow_mut();
|
||||||
writeln!(stdout_ref, "{text}").map_err(mlua::Error::external)?;
|
writeln!(stdout_ref, "{text}").map_err(mlua::Error::external)
|
||||||
Ok(())
|
|
||||||
})?,
|
})?,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
|
@ -34,15 +31,17 @@ fn create_luahtml_stdlib(l: &Lua, stdout: &Rc<RefCell<String>>) -> mlua::Result<
|
||||||
"print",
|
"print",
|
||||||
l.create_function(move |_, text: String| {
|
l.create_function(move |_, text: String| {
|
||||||
let mut stdout_ref = stdout_print.borrow_mut();
|
let mut stdout_ref = stdout_print.borrow_mut();
|
||||||
write!(stdout_ref, "{text}").map_err(mlua::Error::external)?;
|
write!(stdout_ref, "{text}").map_err(mlua::Error::external)
|
||||||
Ok(())
|
|
||||||
})?,
|
})?,
|
||||||
)?;
|
)?;
|
||||||
Ok(t)
|
Ok(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse<F: fmt::Format, A: Atomicity, T: Into<Tendril<F, A>>>(to_parse: T) -> Result<NodeRef>
|
pub fn parse<F, A, T>(to_parse: T) -> Result<NodeRef>
|
||||||
where
|
where
|
||||||
|
F: fmt::Format,
|
||||||
|
A: Atomicity,
|
||||||
|
T: Into<Tendril<F, A>>,
|
||||||
tendril::Tendril<tendril::fmt::UTF8>: std::convert::From<tendril::Tendril<F, A>>,
|
tendril::Tendril<tendril::fmt::UTF8>: std::convert::From<tendril::Tendril<F, A>>,
|
||||||
{
|
{
|
||||||
let document = kuchikiki::parse_html().one(to_parse.into());
|
let document = kuchikiki::parse_html().one(to_parse.into());
|
||||||
|
|
@ -52,11 +51,11 @@ where
|
||||||
let stdout: Rc<RefCell<String>> = Rc::new(RefCell::new(String::new()));
|
let stdout: Rc<RefCell<String>> = Rc::new(RefCell::new(String::new()));
|
||||||
|
|
||||||
let htmlua_table = create_luahtml_stdlib(&lua, &stdout)
|
let htmlua_table = create_luahtml_stdlib(&lua, &stdout)
|
||||||
.map_err(|e| anyhow::anyhow!("Failed to create Lua stdlib: {}", e))?;
|
.map_err(|e| anyhow!("Failed to create Lua stdlib: {}", e))?;
|
||||||
|
|
||||||
globals
|
globals
|
||||||
.set("htmlua", htmlua_table)
|
.set("htmlua", htmlua_table)
|
||||||
.map_err(|e| anyhow::anyhow!("Failed to set global: {}", e))?;
|
.map_err(|e| anyhow!("Failed to set global: {}", e))?;
|
||||||
|
|
||||||
let lua_elements: Vec<_> = match document.select("lua") {
|
let lua_elements: Vec<_> = match document.select("lua") {
|
||||||
Ok(e) => e.collect(),
|
Ok(e) => e.collect(),
|
||||||
|
|
@ -69,7 +68,7 @@ where
|
||||||
stdout.borrow_mut().clear();
|
stdout.borrow_mut().clear();
|
||||||
lua.load(lua_code.borrow().as_str())
|
lua.load(lua_code.borrow().as_str())
|
||||||
.exec()
|
.exec()
|
||||||
.map_err(|e| anyhow::anyhow!("Failed to execute Lua: {}", e))?;
|
.map_err(|e| anyhow!("Failed to execute Lua: {}", e))?;
|
||||||
node.as_node()
|
node.as_node()
|
||||||
.insert_before(NodeRef::new_text(stdout.borrow().as_str()));
|
.insert_before(NodeRef::new_text(stdout.borrow().as_str()));
|
||||||
node.as_node().detach();
|
node.as_node().detach();
|
||||||
|
|
@ -85,7 +84,7 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn base() -> Result<()> {
|
fn base() {
|
||||||
let page = r#"
|
let page = r#"
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
|
|
@ -100,10 +99,9 @@ mod tests {
|
||||||
</lua></span>
|
</lua></span>
|
||||||
</body>
|
</body>
|
||||||
</html>"#;
|
</html>"#;
|
||||||
let d = parse(page)?;
|
let d = parse(page).unwrap();
|
||||||
let text = d.select_first("span").unwrap().as_node().text_contents();
|
let text = d.select_first("span").unwrap().as_node().text_contents();
|
||||||
assert_eq!(text, "Test from Lua!\n");
|
assert_eq!(text, "Test from Lua!\n");
|
||||||
assert!(d.select_first("lua").is_err());
|
assert!(d.select_first("lua").is_err());
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue