grace/htmlua

HTML Templating Engine

git clone https://git.gae.moe/grace/htmlua

gyoderfixed parser bugbfa5d30

render_fixes
1.2 KiB35 linesraw
1use std::{path::Path, sync::OnceLock};
2
3use anyhow::{Result, anyhow};
4
5use crate::{
6    config::Config,
7    helpers::read_doc_from_file,
8    render::{execute_lua, expand_template, process_markdown, process_syntax_highlighting},
9};
10
11static CONFIG: OnceLock<Config> = OnceLock::new();
12
13pub fn get_config() -> &'static Config {
14    CONFIG.get_or_init(|| {
15        Config::load().unwrap_or_else(|e| {
16            eprintln!("Warning: Failed to load config: {e}");
17            eprintln!("Using default configuration");
18            Config::default()
19        })
20    })
21}
22
23pub fn serve_content(request_uri: &str) -> Result<String> {
24    let config = get_config();
25    let safe_path = Path::new(request_uri).strip_prefix("/")?;
26    let page_path = config.paths.pages.join(safe_path);
27    let doc = read_doc_from_file(page_path)?
28        .select_first("html")
29        .map_err(|()| anyhow!("failed to read doc"))?;
30    let full_doc = expand_template(doc.as_node().to_owned(), &config.paths.components, None)?;
31    let markdown_doc = process_markdown(full_doc)?;
32    let highlighted_doc = process_syntax_highlighting(markdown_doc)?;
33    let executed_doc = execute_lua(highlighted_doc)?;
34    Ok(executed_doc.to_string())
35}