grace/htmlua
HTML Templating Engine
git clone https://git.gae.moe/grace/htmlua
0213276
main
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 ( "root.zig" ) ; 10 11pub fn main ( init : std . process . Init ) ! void { 12const io = init . io ; 13 14var arena = std . heap . ArenaAllocator . init ( init . gpa ) ; 15defer arena . deinit () ; 16const alloc = arena . allocator () ; 17 18var state : HTMLua . Parser = . { 19. component_resolver = & resolver , 20. tags = & [ _ ] HTMLua . ParserTag { 21HTMLua . tags . Include , 22HTMLua . tags . Export , 23HTMLua . tags . Use , 24} , 25. tag_stack = try ArrayList ([] const u8 ) . initCapacity ( alloc , 256 ) , 26. exported_parts = try StringArrayHashMap ([] const u8 ) . init ( alloc , & [ 0 ][] const u8 {} , & [ 0 ][] const u8 {}) , 27} ; 28 29// I dont like this but this is temporary so its ok 30const args = try init . minimal . args . toSlice ( alloc ) ; 31const file_path = if ( args . len != 1 ) args [ 1 ] else null ; 32const file = if ( file_path ) | fp | 33try Io . Dir . cwd () . openFile ( io , fp , . { . mode = . read_only }) 34else 35null ; 36defer if ( file ) | f | f . close ( io ) ; 37 38const in_buf = try alloc . alloc ( u8 , 1024 ) ; 39var in = 40if ( file ) | f | f . reader ( io , in_buf ) else File . stdin () . reader ( io , in_buf ) ; 41 42const out_buf = try alloc . alloc ( u8 , 1024 ) ; 43var out = File . stdout () . writer ( io , out_buf ) ; 44 45try HTMLua . parseTemplate ( & arena , io , & state , & in . interface , & out . interface ) ; 46 47try out . flush () ; 48} 49 50// https://ziggit.dev/t/zig-0-15-1-reader-writer-dont-make-copies-of-fieldparentptr-based-interfaces/11719 51fn resolver ( arena : * ArenaAllocator , io : Io , path : [] const u8 ) anyerror !* Io . Reader { 52const read_buff = try arena . allocator () . alloc ( u8 , 1024 ) ; 53const cwd = std . Io . Dir . cwd () ; 54 55var file = try cwd . openFile ( io , path , . { . mode = . read_only }) ; 56const reader = try arena . allocator () . create ( Io . File . Reader ) ; 57reader .* = file . reader ( io , read_buff ) ; 58return & reader . interface ; 59}