Module:Uses TemplateStyles

From Miraheze Developers Wiki
-- This module implements the {{Uses TemplateStyles}} template.
local mMessageBox = require('Module:Message box')

local p = {}

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

function p._main(args)
	return p.renderBox(args)
end

function p.renderBox(tStyles)
	local boxArgs = {}
	if #tStyles < 1 then
		boxArgs.text = '<strong class="error">Error: no TemplateStyles specified</strong>'
	else
		local tStylesLinks = {}
		for i, ts in ipairs(tStyles) do
			local sandboxLink = nil
			local tsTitle = mw.title.new(ts)
			if tsTitle then
				local tsSandboxTitle = mw.title.new(string.format('%s:%s/sandbox/%s', tsTitle.nsText, tsTitle.baseText, tsTitle.subpageText))
				if tsSandboxTitle and tsSandboxTitle.exists then
					sandboxLink = string.format(' ([[:%s|sandbox]])', tsSandboxTitle.prefixedText)
				end
			end
			tStylesLinks[i] = string.format('[[:%s]]%s', ts, sandboxLink or '')
		end
		local frame = mw.getCurrentFrame()
		local tStylesList = mw.text.listToText(tStylesLinks)
		boxArgs.text = 'This ' .. 
			(mw.title.getCurrentTitle():inNamespaces(828,829) and 'module' or 'template') ..
			' uses [[mw:Extension:TemplateStyles|TemplateStyles]]:\n' .. tStylesList
		local installed
		local wiki = ""
		if frame:preprocess("{{SITENAME}}") == "Miraheze Developers Wiki" then
			installed = "may"
			wiki = " on your wiki "
		else
			local cssTag = frame:extensionTag("templatestyles", "", {src=tStyles[1]})
			if mw.text.killMarkers(cssTag) ~= "" then
				installed = "will"
			end
		end
		if installed then
			boxArgs.text = boxArgs.text .. "\n You " .. installed .. " need to enable the TemplateStyles extension at [[Special:ManageWiki/extensions#mw-section-parserhooks]] " .. wiki .. "for template this to work properly."
		end
	end
	boxArgs.type = 'notice'
	boxArgs.small = true
	boxArgs.image = '[[File:Farm-Fresh css add.svg|32px|alt=CSS]]'
	return mMessageBox.main('mbox', boxArgs)
end

return p