started htmlua-parser
This commit is contained in:
commit
d6a294c043
7 changed files with 1230 additions and 0 deletions
21
.gitignore
vendored
Normal file
21
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
debug
|
||||||
|
target
|
||||||
|
|
||||||
|
# These are backup files generated by rustfmt
|
||||||
|
**/*.rs.bk
|
||||||
|
|
||||||
|
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||||
|
*.pdb
|
||||||
|
|
||||||
|
# Generated by cargo mutants
|
||||||
|
# Contains mutation testing data
|
||||||
|
**/mutants.out*/
|
||||||
|
|
||||||
|
# RustRover
|
||||||
|
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||||
|
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||||
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
|
#.idea/
|
||||||
1078
Cargo.lock
generated
Normal file
1078
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
3
cargo.toml
Normal file
3
cargo.toml
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
[workspace]
|
||||||
|
members = ["htmlua-parser", "htmlua-server"]
|
||||||
|
resolver = "2"
|
||||||
10
htmlua-parser/Cargo.toml
Normal file
10
htmlua-parser/Cargo.toml
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
[package]
|
||||||
|
name = "htmlua-parser"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
anyhow = "1.0.98"
|
||||||
|
kuchikiki = "0.8.2"
|
||||||
|
mlua = { version = "0.10.5", features = ["lua54", "vendored", "serialize"] }
|
||||||
|
tendril = "0.4.3"
|
||||||
109
htmlua-parser/src/lib.rs
Normal file
109
htmlua-parser/src/lib.rs
Normal file
|
|
@ -0,0 +1,109 @@
|
||||||
|
#![warn(clippy::pedantic)]
|
||||||
|
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::fmt::Write;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use anyhow::anyhow;
|
||||||
|
use kuchikiki::{
|
||||||
|
NodeRef,
|
||||||
|
traits::TendrilSink,
|
||||||
|
};
|
||||||
|
use mlua::{Lua, Table};
|
||||||
|
use tendril::fmt;
|
||||||
|
use tendril::{Atomicity, Tendril};
|
||||||
|
|
||||||
|
fn create_luahtml_stdlib(l: &Lua, stdout: &Rc<RefCell<String>>) -> mlua::Result<Table> {
|
||||||
|
let t = l.create_table()?;
|
||||||
|
|
||||||
|
// This cannot be the best way to do this
|
||||||
|
|
||||||
|
let stdout_println = stdout.clone();
|
||||||
|
t.set(
|
||||||
|
"println",
|
||||||
|
l.create_function(move |_, text: String| {
|
||||||
|
let mut stdout_ref = stdout_println.borrow_mut();
|
||||||
|
writeln!(stdout_ref, "{text}").map_err(mlua::Error::external)?;
|
||||||
|
Ok(())
|
||||||
|
})?,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let stdout_print = stdout.clone();
|
||||||
|
t.set(
|
||||||
|
"print",
|
||||||
|
l.create_function(move |_, text: String| {
|
||||||
|
let mut stdout_ref = stdout_print.borrow_mut();
|
||||||
|
write!(stdout_ref, "{text}").map_err(mlua::Error::external)?;
|
||||||
|
Ok(())
|
||||||
|
})?,
|
||||||
|
)?;
|
||||||
|
Ok(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse<F: fmt::Format, A: Atomicity, T: Into<Tendril<F, A>>>(to_parse: T) -> Result<NodeRef>
|
||||||
|
where
|
||||||
|
tendril::Tendril<tendril::fmt::UTF8>: std::convert::From<tendril::Tendril<F, A>>,
|
||||||
|
{
|
||||||
|
let document = kuchikiki::parse_html().one(to_parse.into());
|
||||||
|
let lua = Lua::new();
|
||||||
|
let globals = lua.globals();
|
||||||
|
|
||||||
|
let stdout: Rc<RefCell<String>> = Rc::new(RefCell::new(String::new()));
|
||||||
|
|
||||||
|
let htmlua_table = create_luahtml_stdlib(&lua, &stdout)
|
||||||
|
.map_err(|e| anyhow::anyhow!("Failed to create Lua stdlib: {}", e))?;
|
||||||
|
|
||||||
|
globals
|
||||||
|
.set("htmlua", htmlua_table)
|
||||||
|
.map_err(|e| anyhow::anyhow!("Failed to set global: {}", e))?;
|
||||||
|
|
||||||
|
let lua_elements: Vec<_> = match document.select("lua") {
|
||||||
|
Ok(e) => e.collect(),
|
||||||
|
Err(()) => return Err(anyhow!("Unable to find Lua")),
|
||||||
|
};
|
||||||
|
|
||||||
|
for node in lua_elements {
|
||||||
|
if let Some(text_node) = node.as_node().first_child() {
|
||||||
|
if let Some(lua_code) = text_node.as_text() {
|
||||||
|
stdout.borrow_mut().clear();
|
||||||
|
lua.load(lua_code.borrow().as_str())
|
||||||
|
.exec()
|
||||||
|
.map_err(|e| anyhow::anyhow!("Failed to execute Lua: {}", e))?;
|
||||||
|
node.as_node()
|
||||||
|
.insert_before(NodeRef::new_text(stdout.borrow().as_str()));
|
||||||
|
node.as_node().detach();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(document)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn base() -> Result<()> {
|
||||||
|
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>
|
||||||
|
</body>
|
||||||
|
</html>"#;
|
||||||
|
let d = parse(page)?;
|
||||||
|
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());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
6
htmlua-server/Cargo.toml
Normal file
6
htmlua-server/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "htmlua-server"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
3
htmlua-server/src/main.rs
Normal file
3
htmlua-server/src/main.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue