robin/maivi
a tiny cute lua web framework
git clone https://git.robinwobin.dev/robin/maivi
fdcf906
push-srxvuswpkqxx
1const std =@import ( "std" ); 2 3const fern =@import ( "fern" ); 4const md4c =@import ( "md4c" ); 5const zlua =@import ( "zlua" ); 6const Lua =zlua . Lua ; 7 8const maivi =@import ( "../root.zig" ); 9 10pub fn lua_openmd4c ( l : * Lua ) ! i32 { 11l . createTable ( 0 , 1 ); 12 13l . pushFunction ( zlua . wrap ( tohtml )); 14l . setField ( - 2 , "tohtml" ); 15 16return 1 ; 17} 18 19const FLAGS : c_int =0 | 20// md4c.MD_FLAG_HIGHLIGHT | 21// md4c.MD_FLAG_INSERT | 22md4c . MD_FLAG_NOINDENTEDCODEBLOCKS | 23// md4c.MD_FLAG_SPOILERS | 24md4c . MD_FLAG_STRIKETHROUGH | 25// md4c.MD_FLAG_SUBSCRIPTS | 26// md4c.MD_FLAG_SUPERSCRIPTS | 27md4c . MD_FLAG_TABLES | 28md4c . MD_FLAG_TASKLISTS ; 29 30pub fn tohtml ( l : * Lua ) ! i32 { 31l . argCheck ( l . isString ( 1 ), 1 , "string" ); 32 33const Output =struct { 34var buf : std . ArrayList ( u8 ) =. empty ; 35 36pub fn process_output ( chunk : [ * c ] const md4c . MD_CHAR , len : md4c . MD_SIZE , _ : ?* anyopaque ) callconv (. c ) void { 37buf . appendSlice ( maivi . allocator , chunk [ 0 .. len ]) catch | err | fern . err (). err ( err ). log (); 38} 39}; 40Output . buf = try . initCapacity ( maivi . allocator , 0x400 ); 41defer Output . buf . deinit ( maivi . allocator ); 42 43const input =try l . toString ( 1 ); 44const ret =md4c . md_html ( 45input , 46@intCast ( input . len ), 47& Output . process_output , 48null , 49FLAGS , 500 , 51); 52if ( ret == - 1 ) l . raiseErrorStr ( "error while parsing markdown input" , .{}); 53 54const buf =try Output . buf . toOwnedSlice ( maivi . allocator ); 55defer maivi . allocator . free ( buf ); 56_ = l . pushString ( buf ); 57 58return 1 ; 59}