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