Jump to content

Module:User:SD0001/Code page header

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SD0001 (talk | contribs) at 08:24, 20 January 2024 (Created page with 'local gadgets = require('Module:User:SD0001/Gadgets') local function arr_contains(array, val) for _, value in ipairs(array) do if value == val then return true end end return false end local p = {} p.header = function(frame) return p.headerForTitle(mw.title.getCurrentTitle()) end p.headerForTitle = function(title) local model = title.contentModel if model ~= 'javascript' and model ~= 'css' and model ~= 'json' t...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
local gadgets = require('Module:User:SD0001/Gadgets')

local function arr_contains(array, val)
    for _, value in ipairs(array) do
        if value == val then
            return true
        end
    end
    return false
end

local p = {}

p.header = function(frame)
	return p.headerForTitle(mw.title.getCurrentTitle())
end

p.headerForTitle = function(title)
	local model = title.contentModel
	if model ~= 'javascript' and model ~= 'css' and model ~= 'json' then
		return ''
	end
	
	if title.namespace == 8 and title.text:find('^Gadget-') ~= nil then
		local gadget = p.belongsToGadget(title)
		if gadget then
			return 'Part of '..gadget.name..' gadget ' .. 
			(gadget.isDefault and '<b>enabled by default</b>' or 
				'used by '..gadget.usage..' users ')
		end
	elseif title.namespace == 2 then
		-- if model == 'javascript' then
		-- 	return 'User js'
		-- elseif model == 'css' then
		-- 	return 'User css'
		-- elseif model == 'json' then
		-- 	return 'User json'
		-- end
	end
end

p.belongsToGadget = function(title)
	local repo = gadgets.parse()
	local shortName = title.text:gsub('^Gadget%-', '')
	for name, config in pairs(repo) do
		if arr_contains(config.pages, shortName) then
			return {
				name = name,
				usage = gadgets.get_usage(name),
				isDefault = config.options.default ~= nil
			}
		end
	end
	return nil
end

return p