FANDOM


--| Module:ModularCSS (v1.3.2)
--  v1.3.0 – importlax for "strict" and "less strict" config page control
--  v1.3.2 – Special pages will no longer use expensive functions
--by "The JoTS"
 
--- Lua module that simplifies clutter on wiki's CSS pages
--- by dynamically generating modular CSS code.
 
-- e.g. CSS that changes the link color of every wiki admin
-- e.g. Infoboxes that change color on a page by page basis
 
-- <nowiki>
 
-- [==[ Module Start ]==] ------
local module  = {}
local imports = {}
local configs = {}
 
-- Helper functions
local function parse(pgName)
    -- Avoid incrementing expensive function count
    -- if using a special page, return empty table
    if pgName:sub(1,8):lower() == "special:" then return {} end
 
    -- Find cached parsed results and return
    if configs[pgName] then return configs[pgName] end
 
    -- BEGIN PARSING TASK
    -- Find page and get content
    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() or "") .. '\n'
 
    -- 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 = {
                parent = parent_obj,
                getValue = function(t) return next(t) and next(t).self end,
                self = val
            }
        })
 
        last_index = key
        curr_lvl = lvl
    end
 
    -- cache
    configs[pgName] = parsed;
 
    -- return newly parsed results
    return parsed
end
 
-- Invoked wikitext functions
local function _render()
    assert(#imports > 0, "No ModularCSS 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 in module is invalid.')
        assert(cssMod.main and type(cssMod.main) == "function",
            err_header .. '"main" function provided in module is invalid.')
 
        local success,css_vars = pcall(parse, cssMod.page)
        assert(success, err_header .. tostring(css_vars))
 
        local success,modCompiledCSS = pcall(cssMod.main, css_vars)
        renderedCSS = renderedCSS
            .. assert(
                assert(success, modCompiledCSS) and modCompiledCSS,
                "The CSS module did not return a value."
            )
    end
 
    return renderedCSS
end
 
local render = function(frame)
    local success,compiled = pcall(_render)
    if (not success) then return "/*" .. compiled .. "*/" end
 
    return frame:preprocess('/*<syntaxhighlight lang="css" enclose="div" '
        .. 'style="overflow:auto;">*/'
        .. compiled
        .. '/*</syntaxhighlight>*/')
end
 
-- Meta-module functions
local function import_wrap(lax)
    return function(moduleName)
        assert(type(moduleName) == "string", "Invalid module request! Not a string.")
        local success,reqModule = pcall(require, moduleName)
        assert(success, "Non-existent module requested: " .. moduleName)
 
        table.insert(imports, reqModule)
        reqModule.page = lax and reqModule.public_page or reqModule.page
 
        return module
    end
end
 
local import    = import_wrap(false)
local importlax = import_wrap(true)
 
-- en
module.import = import
module.render = render
module.importlax = importlax
 
return module

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.