robin/maivi
a tiny cute lua web framework
git clone https://git.robinwobin.dev/robin/maivi
ae085a5
push-srxvuswpkqxx
1-- selene: allow(unscoped_variables, global_usage) 2_G . maivi =_G . maivi or {} --[[@as table]] 3 4maivi . data ={} 5 6-- util ======================================================================= 7 8-- https://github.com/neovim/neovim/blob/04f802a2f6387028e3389334e6e04abc28d92587/runtime/lua/vim/F.lua#L58-L66 9 10--- like {...} except preserve the length explicitly 11function maivi . pack_len ( ... ) 12return { n =select ( "#" , ... ), ... } 13end 14 15--- like unpack() but use the length set by F.pack_len if present 16function maivi . unpack_len ( t ) 17return unpack ( t , 1 , t . n ) 18end 19 20function maivi . ( ... ) 21local t =maivi . pack_len ( ... ) 22for i =1 , t . n do 23( maivi . inspect ( t [ i ])) 24end 25end 26 27---@generic T 28---@param t table<string, T> 29---@param f fun(k: string): T 30---@param mt metatable 31function maivi . defaulttable ( t , f , mt ) 32local mt_ ={ 33__index =function ( self , k ) 34local v =f ( k ) 35rawset ( self , k , v ) 36return rawget ( self , k ) 37end , 38} 39if mt then 40for k , v in pairs ( mt ) do 41mt_ [ k ] =v 42end 43end 44return setmetatable ( t , mt_ ) 45end 46 47-- elem ======================================================================= 48 49---@alias Displayable string | number | boolean | fun(): Displayable 50---@alias Props table<string, Displayable> 51---@alias Child Displayable | VNode 52---@alias Children Child[] 53---@alias PropsOrChildren { [string]: Displayable, [integer]: Child }|Child 54 55---@alias VNode { type: ElementType?, props: Props, children: Children } 56---@alias ElementType string | Component 57---@alias Element fun(args: PropsOrChildren): VNode 58---@alias Component fun(props: Props, children: Children): VNode 59 60---@class ElementConfig 61---@field close? boolean 62---@field empty? boolean 63 64---@class ParsedArgs 65---@field props Props 66---@field children Children 67---@field boolean_props string[] 68 69---@param etype ElementType? 70---@param props Props 71---@param children Children 72---@return VNode 73function maivi . h ( etype , props , children ) 74if type ( etype ) == "function" then 75return etype ( props , children ) 76end 77return { type =etype , props =props , children =children } 78end 79 80---@param etype? ElementType 81---@param config? ElementConfig 82---@return Element 83function maivi . elem ( etype , config ) 84return function ( args ) 85local parsed =maivi . parse ( args ) 86return setmetatable ( maivi . h ( etype , parsed . props , parsed . children ), { 87config =config , 88} ) 89end 90end 91 92-- components ================================================================= 93 94--- minimized version of `maivi.elem` since components dont use `.config` 95--- @param f Component 96--- @return Element 97function maivi . component ( f ) 98return function ( args ) 99local parsed =maivi . parse ( args ) 100return maivi . h ( f , parsed . props , parsed . children ) 101end 102end 103 104---@alias Slots table<string, VNode> 105---@param f fun(slots: Slots): Element 106---@return fun(page: Component): VNode 107function maivi . layout ( f ) 108return function ( page ) 109return function ( args ) 110local parsed =maivi . parse ( args ) 111local slots =page ( parsed . props , parsed . children ) 112return f ( slots )( parsed . props , parsed . children ) 113end 114end 115end 116 117-- props & children ============================================================ 118 119---@param args PropsOrChildren? 120---@return ParsedArgs 121function maivi . parse ( args ) 122---@type Props 123local props ={} 124---@type Children 125local children ={} 126---@type string[] 127local boolean_props ={} 128 129if type ( args ) == "table" and getmetatable ( args ) == nil then 130for k , v in pairs ( args ) do 131if type ( k ) == "number" then 132table . insert ( children , v ) 133elseif k == "_" and type ( v ) == "table" then 134for _ , prop in ipairs ( v ) do 135table . insert ( boolean_props , prop ) 136end 137elseif k == "data" and type ( v ) == "table" then 138for kdata , prop in pairs ( v ) do 139props [ "data-" .. kdata ] =maivi . display . attr ( prop ) 140end 141else 142props [ k ] =maivi . display . attr ( v ) 143end 144end 145elseif args ~= nil then 146table . insert ( children , args ) 147end 148 149return { 150props =props , 151children =children , 152boolean_props =boolean_props , 153} 154end 155 156-- modules ==================================================================== 157 158maivi . mod =setmetatable ( { 159graph ={} , 160} , { 161---@param t table 162---@param modname string 163__call =function ( t , modname ) 164local info =debug . getinfo ( 2 , "Sln" ) 165local source =info . source 166t . graph [ source ] =t . graph [ source ] or {} 167table . insert ( t . graph [ source ], modname ) 168 169return function ( ... ) 170if this . dev then 171package . loaded [ modname ] =nil 172end 173 174local ok , result =pcall ( require , modname ) 175if not ok then 176error ( 177( [[%s:%s error while loading module '%s':\t\n%s]] ): format ( 178info . short_src , 179info . currentline , 180modname , 181result 182) 183) 184end 185if type ( result ) ~= "function" then 186error ( 187( [[%s:%s return value from module '%s' is not a function]] ): format ( 188info . short_src , 189info . currentline , 190modname 191) 192) 193end 194return result ( ... ) 195end 196end , 197} ) 198 199-- hooks ======================================================================= 200 201---@alias ctx table<string, any?> 202---@alias props table<string, any?> 203---@alias hook fun(ctx: ctx) 204 205maivi . _hooks ={} 206 207---@param event string 208---@param props props 209---@param fn hook 210function maivi . hook ( event , props , fn ) 211maivi . _hooks [ event ] =maivi . _hooks [ event ] or {} 212table . insert ( maivi . _hooks [ event ], { props =props , fn =fn } ) 213end 214 215---@param ref props 216---@param props props 217---@return boolean 218local function matchhook ( ref , props ) 219for k , v in pairs ( props ) do 220if k == "pattern" and ref . pattern then 221return string . match ( ref . pattern , v ) ~= nil 222elseif ref [ k ] ~= v then 223return false 224end 225end 226 227return true 228end 229 230---@param ctx ctx 231---@param event string 232---@param props props 233function maivi . trigger ( ctx , event , props ) 234local hooks =maivi . _hooks [ event ] 235if not hooks then 236return 237end 238 239for _ , hook in ipairs ( hooks ) do 240-- fallen can be nil 241if ctx . fallen ~= true and matchhook ( props , hook . props ) then 242hook . fn ( ctx ) 243end 244end 245end 246 247return maivi