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 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('\'', "'")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,2 @@
|
|||
|
||||
pub mod render;
|
||||
pub mod serve;
|
||||
|
||||
|
|
|
|||
|
|
@ -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<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 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<NodeRef> {
|
|||
|
||||
pub fn expand_template(document: NodeRef, component_path: &PathBuf, include_from: Option<&NodeRef>) -> Result<NodeRef> {
|
||||
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,
|
||||
|
|
@ -235,5 +237,4 @@ mod tests {
|
|||
assert!(d.select_first("exportelement").is_err());
|
||||
assert!(d.select_first("includeelement").is_err());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,9 @@
|
|||
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<String> {
|
||||
// TODO: read from config
|
||||
|
|
@ -28,12 +22,3 @@ pub fn serve_content(request_uri: &str) -> Result<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