Module:ModularCSS

-- Module:DynamicCSS v0.9.0 (indev testing) -- Lua "magical girl" thing that simplifies clutter on wiki CSS pages. -- No, this is not the permanent description of the module. local module = {} local imports = {}

-- Helper functions local function parse(pgName) local page = mw.title.new(pgName) assert(page, "Invalid page title!") assert(page.exists, 'The config page "' .. pgName .. '" does not exist on this wiki.') page = page:getContent -- Flags... and stuff local parsed = {} local parent_obj = parsed local last_index local curr_lvl = 1 -- Parse bulleted list for ast,val in page:gmatch("(\*+)%s*(.-)\n") do       local lvl = #ast local key = val:gsub(" ","_")

if lvl > curr_lvl then parent_obj = parent_obj[last_index] elseif lvl < curr_lvl then parent_obj = parent_obj.parent end assert(not parent_obj[key], "Duplicate value in the same bullet level!") parent_obj[key] = setmetatable({},{           __index = function(t, i)                if i == "parent" then                    return parent_obj                elseif i == "value" then                    -- this does not guarantee that only one value is "available"                    -- only gets a single value, at "random"                    return next(t)                end            end        }) last_index = val curr_lvl = lvl end return parsed end

-- Invoked wikitext functions local function _render assert(#imports > 0, "No DynamicCSS modules were imported!") local renderedCSS = "" for modNum,cssMod in pairs(imports) do       local err_header = "CSS Module #" .. modNum .. ": "       -- CSS module format errors assert(cssMod.page and type(cssMod.page) == "string",           err_header .. '"page" string provided is invalid.') assert(cssMod.main and type(cssMod.main) == "function",           err_header .. '"main" function provided is invalid.') local css_vars,err = pcall(parse, cssMod.page) assert(css_vars, err_header .. err) -- yes, I'm just gonna assume that it will succesfully return a string -- and yes, this is the one part I actually *should* check for errors. renderedCSS = renderedCSS .. cssMod.main(css_vars) end return renderedCSS end

local render = function local success,err = pcall(_render) if (not success) then return "/*" .. err .. "*/" end return success end

-- Meta-module functions local function import(moduleName) assert(type(moduleName) == "string", "Invalid module request! Not a string.") local reqModule,err = pcall(require, moduleName) assert(reqModule, "Non-existent module requested: " .. moduleName) table.insert(imports, reqModule) return module end

-- en module.import = import module.render = render

return module