summaryrefslogtreecommitdiff
path: root/htmlua-parser/src/serve.rs
blob: 6bbf73a9fe2c9fc8cf58e916ee28c717295b8b61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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,
};

pub fn serve_content(request_uri: &str) -> Result<String>{
    // TODO: read from config
    let pages = PathBuf::from("/var/www/htmlua/pages");
    let components = PathBuf::from("/var/www/htmlua/components");

    let safe_path = Path::new(request_uri).strip_prefix("/")?;
    let page_path = pages.join(safe_path);

    let page_string = read_to_string(&page_path)?;

    let doc = kuchikiki::parse_html().one(page_string);
    let full_doc = expand_template(doc, &components, None)?;

    let executed_doc = execute_lua(full_doc)?;

    Ok(executed_doc.to_string())
}

fn html_escape(input: &str) -> String {
    input
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&#x27;")
}