cleaned up
This commit is contained in:
parent
010bb99c9b
commit
88b22b4e77
5 changed files with 35 additions and 61 deletions
11
.rustfmt.toml
Normal file
11
.rustfmt.toml
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
unstable_features = true
|
||||||
|
|
||||||
|
max_width = 120
|
||||||
|
fn_params_layout = "Compressed"
|
||||||
|
fn_single_line = true
|
||||||
|
fn_call_width = 120
|
||||||
|
where_single_line = true
|
||||||
|
brace_style = "PreferSameLine"
|
||||||
|
blank_lines_upper_bound = 5
|
||||||
|
combine_control_expr = false
|
||||||
|
wrap_comments = false
|
||||||
|
|
@ -1,32 +1,11 @@
|
||||||
use std::env;
|
|
||||||
use std::fs;
|
|
||||||
use std::fs::read_to_string;
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use std::str::FromStr;
|
|
||||||
use htmlua_parser::serve::serve_content;
|
use htmlua_parser::serve::serve_content;
|
||||||
use kuchikiki::{
|
use std::env;
|
||||||
NodeRef,
|
|
||||||
traits::TendrilSink,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Content-Type: text/html\n");
|
println!("Content-Type: text/html\n");
|
||||||
|
|
||||||
let request_uri = env::var("PATH_INFO").unwrap_or_else(|_| "".to_string());
|
let request_uri = env::var("PATH_INFO").unwrap_or_else(|_| "".to_string());
|
||||||
|
|
||||||
let page = serve_content(request_uri.as_str()).unwrap();
|
let page = serve_content(request_uri.as_str()).unwrap();
|
||||||
|
|
||||||
|
|
||||||
// Output the final expanded HTML
|
|
||||||
println!("{page}");
|
println!("{page}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple HTML escape function
|
|
||||||
fn html_escape(input: &str) -> String {
|
|
||||||
input
|
|
||||||
.replace('&', "&")
|
|
||||||
.replace('<', "<")
|
|
||||||
.replace('>', ">")
|
|
||||||
.replace('"', """)
|
|
||||||
.replace('\'', "'")
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,2 @@
|
||||||
|
|
||||||
pub mod render;
|
pub mod render;
|
||||||
pub mod serve;
|
pub mod serve;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,7 @@ use std::rc::Rc;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use kuchikiki::{
|
use kuchikiki::{NodeRef, traits::TendrilSink};
|
||||||
NodeRef,
|
|
||||||
traits::TendrilSink,
|
|
||||||
};
|
|
||||||
use mlua::{Lua, Table};
|
use mlua::{Lua, Table};
|
||||||
|
|
||||||
fn create_luahtml_stdlib(l: &Lua, stdout: &Rc<RefCell<String>>) -> mlua::Result<Table> {
|
fn create_luahtml_stdlib(l: &Lua, stdout: &Rc<RefCell<String>>) -> mlua::Result<Table> {
|
||||||
|
|
@ -43,8 +40,8 @@ pub fn execute_lua(document: NodeRef) -> Result<NodeRef> {
|
||||||
|
|
||||||
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 =
|
||||||
.map_err(|e| anyhow!("Failed to create Lua stdlib: {}", e))?;
|
create_luahtml_stdlib(&lua, &stdout).map_err(|e| anyhow!("Failed to create Lua stdlib: {}", e))?;
|
||||||
|
|
||||||
globals
|
globals
|
||||||
.set("htmlua", htmlua_table)
|
.set("htmlua", htmlua_table)
|
||||||
|
|
@ -74,20 +71,25 @@ pub fn execute_lua(document: NodeRef) -> Result<NodeRef> {
|
||||||
|
|
||||||
pub fn expand_template(document: NodeRef, component_path: &PathBuf, include_from: Option<&NodeRef>) -> Result<NodeRef> {
|
pub fn expand_template(document: NodeRef, component_path: &PathBuf, include_from: Option<&NodeRef>) -> Result<NodeRef> {
|
||||||
if let Some(from_node) = include_from {
|
if let Some(from_node) = include_from {
|
||||||
|
for i in document
|
||||||
|
.select("includeelement")
|
||||||
for i in document.select("includeelement").map_err(|()| anyhow!("Error finding includeelement"))? {
|
.map_err(|()| anyhow!("Error finding includeelement"))?
|
||||||
|
{
|
||||||
if let Some(name) = i.attributes.borrow().get("name") {
|
if let Some(name) = i.attributes.borrow().get("name") {
|
||||||
let exported = from_node.select_first(format!("exportelement.{name}").as_str()).map_err(|()| anyhow!("Error finding exportelement"))?;
|
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().for_each(|c| i.as_node().insert_after(c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while let Ok(i) = document.select_first("includeelement") {
|
while let Ok(i) = document.select_first("includeelement") {
|
||||||
i.as_node().detach();
|
i.as_node().detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
for i in document.select("include").map_err(|()| anyhow!("Error finding include"))? {
|
for i in document
|
||||||
|
.select("include")
|
||||||
|
.map_err(|()| anyhow!("Error finding include"))?
|
||||||
|
{
|
||||||
let attrs = match i.as_node().as_element() {
|
let attrs = match i.as_node().as_element() {
|
||||||
Some(e) => e.attributes.borrow(),
|
Some(e) => e.attributes.borrow(),
|
||||||
None => continue,
|
None => continue,
|
||||||
|
|
@ -125,7 +127,7 @@ mod tests {
|
||||||
</lua></span>
|
</lua></span>
|
||||||
</body>
|
</body>
|
||||||
</html>"#;
|
</html>"#;
|
||||||
let document = kuchikiki::parse_html().one(page);
|
let document = kuchikiki::parse_html().one(page);
|
||||||
let d = execute_lua(document).unwrap();
|
let d = execute_lua(document).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");
|
||||||
|
|
@ -151,7 +153,7 @@ mod tests {
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>"#;
|
</html>"#;
|
||||||
let document = kuchikiki::parse_html().one(page);
|
let document = kuchikiki::parse_html().one(page);
|
||||||
let d = execute_lua(document).unwrap();
|
let d = execute_lua(document).unwrap();
|
||||||
let text = d.select_first("#ta").unwrap().as_node().text_contents();
|
let text = d.select_first("#ta").unwrap().as_node().text_contents();
|
||||||
assert_eq!(text, "Test from Lua!\n");
|
assert_eq!(text, "Test from Lua!\n");
|
||||||
|
|
@ -179,7 +181,7 @@ mod tests {
|
||||||
</html>"#;
|
</html>"#;
|
||||||
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||||
p.push("tests/components");
|
p.push("tests/components");
|
||||||
let document = kuchikiki::parse_html().one(page);
|
let document = kuchikiki::parse_html().one(page);
|
||||||
let d = expand_template(document, &p, None).unwrap();
|
let d = expand_template(document, &p, None).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, "included1");
|
assert_eq!(text, "included1");
|
||||||
|
|
@ -201,7 +203,7 @@ mod tests {
|
||||||
</html>"#;
|
</html>"#;
|
||||||
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||||
p.push("tests/components");
|
p.push("tests/components");
|
||||||
let document = kuchikiki::parse_html().one(page);
|
let document = kuchikiki::parse_html().one(page);
|
||||||
let d = expand_template(document, &p, None).unwrap();
|
let d = expand_template(document, &p, None).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, "included_twice");
|
assert_eq!(text, "included_twice");
|
||||||
|
|
@ -226,7 +228,7 @@ mod tests {
|
||||||
</html>"#;
|
</html>"#;
|
||||||
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||||
p.push("tests/components");
|
p.push("tests/components");
|
||||||
let document = kuchikiki::parse_html().one(page);
|
let document = kuchikiki::parse_html().one(page);
|
||||||
let d = expand_template(document, &p, None).unwrap();
|
let d = expand_template(document, &p, None).unwrap();
|
||||||
let text = d.select_first("#ta").unwrap().as_node().text_contents();
|
let text = d.select_first("#ta").unwrap().as_node().text_contents();
|
||||||
assert_eq!(text, "element 1");
|
assert_eq!(text, "element 1");
|
||||||
|
|
@ -235,5 +237,4 @@ mod tests {
|
||||||
assert!(d.select_first("exportelement").is_err());
|
assert!(d.select_first("exportelement").is_err());
|
||||||
assert!(d.select_first("includeelement").is_err());
|
assert!(d.select_first("includeelement").is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,11 @@
|
||||||
use std::env;
|
|
||||||
use std::fs;
|
|
||||||
use std::fs::read_to_string;
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use std::str::FromStr;
|
|
||||||
use super::render::execute_lua;
|
use super::render::execute_lua;
|
||||||
use super::render::expand_template;
|
use super::render::expand_template;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use kuchikiki::{
|
use kuchikiki::traits::TendrilSink;
|
||||||
NodeRef,
|
use std::fs::read_to_string;
|
||||||
traits::TendrilSink,
|
use std::path::{Path, PathBuf};
|
||||||
};
|
|
||||||
|
|
||||||
pub fn serve_content(request_uri: &str) -> Result<String>{
|
pub fn serve_content(request_uri: &str) -> Result<String> {
|
||||||
// TODO: read from config
|
// TODO: read from config
|
||||||
let pages = PathBuf::from("/var/www/htmlua/pages");
|
let pages = PathBuf::from("/var/www/htmlua/pages");
|
||||||
let components = PathBuf::from("/var/www/htmlua/components");
|
let components = PathBuf::from("/var/www/htmlua/components");
|
||||||
|
|
@ -28,12 +22,3 @@ pub fn serve_content(request_uri: &str) -> Result<String>{
|
||||||
|
|
||||||
Ok(executed_doc.to_string())
|
Ok(executed_doc.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn html_escape(input: &str) -> String {
|
|
||||||
input
|
|
||||||
.replace('&', "&")
|
|
||||||
.replace('<', "<")
|
|
||||||
.replace('>', ">")
|
|
||||||
.replace('"', """)
|
|
||||||
.replace('\'', "'")
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue