blob: 375e88504b7167a0a591bff8ee709e70a318d09a (
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
|
use std::{path::Path, sync::OnceLock};
use anyhow::{Result, anyhow};
use crate::{
config::Config,
helpers::read_doc_from_file,
render::{execute_lua, expand_template, process_markdown, process_syntax_highlighting},
};
static CONFIG: OnceLock<Config> = OnceLock::new();
pub fn get_config() -> &'static Config {
CONFIG.get_or_init(|| {
Config::load().unwrap_or_else(|e| {
eprintln!("Warning: Failed to load config: {e}");
eprintln!("Using default configuration");
Config::default()
})
})
}
pub fn serve_content(request_uri: &str) -> Result<String> {
let config = get_config();
let safe_path = Path::new(request_uri).strip_prefix("/")?;
let page_path = config.paths.pages.join(safe_path);
let doc = read_doc_from_file(page_path)?
.select_first("html")
.map_err(|()| anyhow!("failed to read doc"))?;
let full_doc = expand_template(doc.as_node().to_owned(), &config.paths.components, None)?;
let markdown_doc = process_markdown(full_doc)?;
let highlighted_doc = process_syntax_highlighting(markdown_doc)?;
let executed_doc = execute_lua(highlighted_doc)?;
Ok(executed_doc.to_string())
}
|