Module:Uses TemplateStyles

From Weezerpedia

-- Note: This module has been modified from its original code to exclude its dependency on Module:TNT

local yesno = require('Module:Yesno')
local mList = require('Module:List')
local mTableTools = require('Module:TableTools')
local mMessageBox = require('Module:Message box')

local p = {}

local function getConfig()
	return mw.loadData('Module:Uses TemplateStyles/config')
end

local function renderBox(tStyles)
	local boxArgs = {
		type = 'notice',
		small = true,
		image = '[[File:CSS-Logo.png|32px|alt=CSS]]'
	}
	if #tStyles < 1 then
		boxArgs.text = '<strong class="error">Error: no TemplateStyles specified</strong>'
	else
		local cfg = getConfig()
		local tStylesLinks = {}
		for i, ts in ipairs(tStyles) do
			local link = string.format('[[:%s]]', ts)
			local sandboxLink = nil
			local tsTitle = mw.title.new(ts)
			if tsTitle and cfg['sandbox_title'] then
				local tsSandboxTitle = mw.title.new(string.format(
					'%s:%s/%s/%s', tsTitle.nsText, tsTitle.baseText, cfg['sandbox_title'], tsTitle.subpageText))
				if tsSandboxTitle and tsSandboxTitle.exists then
					sandboxLink = link .. ' ([[:' .. tsSandboxTitle.prefixedText .. '|sandbox]])'
				end
			end
			tStylesLinks[i] = sandboxLink or link
		end
		local tStylesList = mList.makeList('bulleted', tStylesLinks)
		if mw.title.getCurrentTitle():inNamespaces(828,829) then
			boxArgs.text = 'This module uses [[mediawikiwiki:Help:TemplateStyles|TemplateStyles]]:' .. '\n' .. tStylesList
		else
			boxArgs.text = 'This template uses [[mediawikiwiki:Help:TemplateStyles|TemplateStyles]]:' .. '\n' .. tStylesList 
		end
	end
	return mMessageBox.main('mbox', boxArgs)
end

function p._main(args)
	local cfg = getConfig()
	if #args == 0 then
		local prefixed = mw.title.getCurrentTitle().prefixedText
		prefixed = prefixed:gsub("/doc","")
		args[1] = prefixed .. "/" .. cfg["default_subpage_name"]
	end
	local tStyles = mTableTools.compressSparseArray(args)
	local box = renderBox(tStyles)
	return box
end

function p.main(frame)
	local origArgs = frame:getParent().args
	local args = {}
	for k, v in pairs(origArgs) do
		v = v:match('^%s*(.-)%s*$')
		if v ~= '' then
			args[k] = v
		end
	end
	return p._main(args)
end

return p