robin/maivi
a tiny cute lua web framework
git clone https://git.robinwobin.dev/robin/maivi
1be4c9c
push-srxvuswpkqxx
1maivi . router : add ( "/" , maivi . mod ( "pages.index" ), {} ) 2 3---@diagnostic disable-next-line: global-in-non-module 4H =require ( "helpers" ) 5 6---@class Section 7---@field stub string 8---@field path string 9---@field url string 10---@field header boolean 11---@field section string 12---@field id string 13---@field content? string 14 15---@type Section[] 16maivi . data . sections ={} 17 18for file , data in pairs ( this . data ) do 19if string . sub ( file , 1 , 1 ) == "/" then 20file =string . sub ( file , 2 ) 21end 22 23assert ( string . sub ( file , 1 , 4 ) == "data" ) 24 25-- file: data/path/to/00-file.ext 26-- url: <-------------> 27local url =H . stripend ( string . sub ( file , # "data/" ), "%." ) 28 29-- url: /path/to/00-url 30-- stub: <----> 31data . stub =H . subafter ( url , "/" ) 32 33data . path =file 34data . url =url 35 36data . header =false 37-- stub: 00--url-- 38-- section: <-> 39if string . sub ( data . stub , 4 , 4 ) == "-" then 40data . header =true 41end 42 43-- stub: 00-url 44-- section: <-> 45if not data . section then 46if data . header then 47data . section =string . sub ( data . stub , 5 , - 3 ) 48else 49data . section =string . sub ( data . stub , 4 ) 50end 51---@diagnostic disable-next-line: cast-type-mismatch 52---@cast data.section string 53data . section =string . gsub ( data . section , "%-" , " " ) 54end 55 56data . id =string . gsub ( string . sub ( url , 2 ), "/" , "-" ) 57 58if string . sub ( data . path , - 4 ) ~= ".txt" then 59---@diagnostic disable-next-line: undefined-global 60local contents =read ( data . path ) 61 62if string . sub ( contents , 0 , 3 ) == "+++" then 63contents =string . gsub ( contents , "^+++.*+++" , "" ) 64end 65 66if string . sub ( data . path , - 4 ) == ".lua" then 67local f =loadstring ( contents , data . path ) 68if not f then 69return 70end 71_ , contents =pcall ( f ) 72end 73 74data . content =contents 75end 76 77-- maivi.print(data) 78 79data . pos =tonumber ( string . sub ( data . stub , 0 , 2 )) 80table . insert ( maivi . data . sections , data ) 81end 82 83table . sort ( maivi . data . sections , function ( a , b ) 84if not a . pos or not b . pos then 85return a . pos ~= nil 86end 87 88return a . pos < b . pos 89end )