grace/htmlua
HTML Templating Engine
git clone https://git.gae.moe/grace/htmlua
55a1697
1.0.0-alpha
1const std = @import ( "std" ) ; 2const Io = std . Io ; 3const File = Io . File ; 4const Allocator = std . mem . Allocator ; 5const ArenaAllocator = std . heap . ArenaAllocator ; 6const ArrayList = std . ArrayList ; 7const StringArrayHashMap = std . StringArrayHashMapUnmanaged ; 8 9const HTMLua = @import ( "htmlua" ) ; 10 11const Config = struct { 12component_path : [] const u8 , 13template_path : [] const u8 , 14} ; 15 16var component_dir : Io . Dir = undefined ; 17 18pub fn main ( init : std . process . Init ) ! void { 19const io = init . io ; 20 21var arena = std . heap . ArenaAllocator . init ( init . gpa ) ; 22defer arena . deinit () ; 23const alloc = arena . allocator () ; 24 25var args = init . minimal . args . iterate () ; 26 27const cwd = std . Io . Dir . cwd () ; 28 29if ( ! args . skip ()) unreachable ; 30const config_file_path = args . next () .? ; 31const config_buf = try cwd . readFileAllocOptions ( io , config_file_path , alloc , . unlimited , std . mem . Alignment . @"1" , ' \x00 ' ) ; 32const config = try std . zon . parse . fromSliceAlloc ( 33Config , 34alloc , 35config_buf , 36null , 37. {} , 38) ; 39 40component_dir = try cwd . openDir ( io , config . component_path , . {}) ; 41const template_dir = try cwd . openDir ( io , config . template_path , . {}) ; 42 43var state : HTMLua . Parser = . { 44. tags = & [ _ ] HTMLua . ParserTag { 45HTMLua . tags . Include , 46HTMLua . tags . Export , 47HTMLua . tags . Use , 48HTMLua . tags . Syntax , 49} , 50. tag_stack = try ArrayList ([] const u8 ) . initCapacity ( alloc , 256 ) , 51. exported_parts = try StringArrayHashMap ([] const u8 ) . init ( alloc , & [ 0 ][] const u8 {} , & [ 0 ][] const u8 {}) , 52. component_resolver = struct { 53pub fn resolve ( arena_inner : * ArenaAllocator , io_inner : Io , path : [] const u8 ) anyerror !* Io . Reader { 54const read_buff = try arena_inner . allocator () . alloc ( u8 , 16384 ) ; 55const clean_path = try HTMLua . utils . sanitizePath ( arena_inner . allocator () , path ) ; 56var file = try component_dir . openFile ( io_inner , clean_path , . { . mode = . read_only }) ; 57const reader = try arena_inner . allocator () . create ( File . Reader ) ; 58reader .* = file . reader ( io_inner , read_buff ) ; 59return & reader . interface ; 60} 61} . resolve , 62} ; 63 64const request_path = init . environ_map . get ( "PATH_INFO" ) .? ; 65const clean_request_path = try HTMLua . utils . sanitizePath ( alloc , request_path ) ; 66const file = try template_dir . openFile ( io , clean_request_path , . { . mode = . read_only }) ; 67defer file . close ( io ) ; 68 69const in_buf = try alloc . alloc ( u8 , 16384 ) ; 70var in = file . reader ( io , in_buf ) ; 71 72const out_buf = try alloc . alloc ( u8 , 16384 ) ; 73var out = File . stdout () . writer ( io , out_buf ) ; 74 75try out . interface . writeAll ( "Content-type: text/html \n " ) ; 76try HTMLua . parseTemplate ( & arena , io , & state , & in . interface , & out . interface ) ; 77 78try out . flush () ; 79}