diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..21ebfc1 --- /dev/null +++ b/.rustfmt.toml @@ -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 diff --git a/htmlua-apache-cgi/src/main.rs b/htmlua-apache-cgi/src/main.rs index ce86dc6..ab91cb1 100644 --- a/htmlua-apache-cgi/src/main.rs +++ b/htmlua-apache-cgi/src/main.rs @@ -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 kuchikiki::{ - NodeRef, - traits::TendrilSink, -}; +use std::env; fn main() { println!("Content-Type: text/html\n"); let request_uri = env::var("PATH_INFO").unwrap_or_else(|_| "".to_string()); - let page = serve_content(request_uri.as_str()).unwrap(); - - // Output the final expanded HTML println!("{page}"); } - -// Simple HTML escape function -fn html_escape(input: &str) -> String { - input - .replace('&', "&") - .replace('<', "<") - .replace('>', ">") - .replace('"', """) - .replace('\'', "'") -} diff --git a/htmlua-parser/src/lib.rs b/htmlua-parser/src/lib.rs index 75aac9c..03e66bd 100644 --- a/htmlua-parser/src/lib.rs +++ b/htmlua-parser/src/lib.rs @@ -1,4 +1,2 @@ - pub mod render; pub mod serve; - diff --git a/htmlua-parser/src/render.rs b/htmlua-parser/src/render.rs index d5b291f..58a8259 100644 --- a/htmlua-parser/src/render.rs +++ b/htmlua-parser/src/render.rs @@ -6,10 +6,7 @@ use std::rc::Rc; use anyhow::Result; use anyhow::anyhow; -use kuchikiki::{ - NodeRef, - traits::TendrilSink, -}; +use kuchikiki::{NodeRef, traits::TendrilSink}; use mlua::{Lua, Table}; fn create_luahtml_stdlib(l: &Lua, stdout: &Rc>) -> mlua::Result { @@ -43,8 +40,8 @@ pub fn execute_lua(document: NodeRef) -> Result { let stdout: Rc> = Rc::new(RefCell::new(String::new())); - let htmlua_table = create_luahtml_stdlib(&lua, &stdout) - .map_err(|e| anyhow!("Failed to create Lua stdlib: {}", e))?; + let htmlua_table = + create_luahtml_stdlib(&lua, &stdout).map_err(|e| anyhow!("Failed to create Lua stdlib: {}", e))?; globals .set("htmlua", htmlua_table) @@ -74,20 +71,25 @@ pub fn execute_lua(document: NodeRef) -> Result { pub fn expand_template(document: NodeRef, component_path: &PathBuf, include_from: Option<&NodeRef>) -> Result { if let Some(from_node) = include_from { - - - for i in document.select("includeelement").map_err(|()| anyhow!("Error finding includeelement"))? { + for i in document + .select("includeelement") + .map_err(|()| anyhow!("Error finding includeelement"))? + { 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)); } } while let Ok(i) = document.select_first("includeelement") { 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() { Some(e) => e.attributes.borrow(), None => continue, @@ -125,7 +127,7 @@ mod tests { "#; - let document = kuchikiki::parse_html().one(page); + let document = kuchikiki::parse_html().one(page); let d = execute_lua(document).unwrap(); let text = d.select_first("span").unwrap().as_node().text_contents(); assert_eq!(text, "Test from Lua!\n"); @@ -151,7 +153,7 @@ mod tests { "#; - let document = kuchikiki::parse_html().one(page); + let document = kuchikiki::parse_html().one(page); let d = execute_lua(document).unwrap(); let text = d.select_first("#ta").unwrap().as_node().text_contents(); assert_eq!(text, "Test from Lua!\n"); @@ -179,7 +181,7 @@ mod tests { "#; let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR")); 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 text = d.select_first("span").unwrap().as_node().text_contents(); assert_eq!(text, "included1"); @@ -201,7 +203,7 @@ mod tests { "#; let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR")); 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 text = d.select_first("span").unwrap().as_node().text_contents(); assert_eq!(text, "included_twice"); @@ -226,7 +228,7 @@ mod tests { "#; let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR")); 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 text = d.select_first("#ta").unwrap().as_node().text_contents(); assert_eq!(text, "element 1"); @@ -235,5 +237,4 @@ mod tests { assert!(d.select_first("exportelement").is_err()); assert!(d.select_first("includeelement").is_err()); } - } diff --git a/htmlua-parser/src/serve.rs b/htmlua-parser/src/serve.rs index 6bbf73a..1dfd306 100644 --- a/htmlua-parser/src/serve.rs +++ b/htmlua-parser/src/serve.rs @@ -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::expand_template; use anyhow::Result; -use kuchikiki::{ - NodeRef, - traits::TendrilSink, -}; +use kuchikiki::traits::TendrilSink; +use std::fs::read_to_string; +use std::path::{Path, PathBuf}; -pub fn serve_content(request_uri: &str) -> Result{ +pub fn serve_content(request_uri: &str) -> Result { // TODO: read from config let pages = PathBuf::from("/var/www/htmlua/pages"); let components = PathBuf::from("/var/www/htmlua/components"); @@ -28,12 +22,3 @@ pub fn serve_content(request_uri: &str) -> Result{ Ok(executed_doc.to_string()) } - -fn html_escape(input: &str) -> String { - input - .replace('&', "&") - .replace('<', "<") - .replace('>', ">") - .replace('"', """) - .replace('\'', "'") -}