Jump to content

Module:Storm categories/demo: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m top: Post-move cleanup, replaced: Tropical cyclone categories → Storm categories (4)
Switching to note-style legends
Line 16: Line 16:
TableTools.removeDuplicates(customKeys)
TableTools.removeDuplicates(customKeys)


function inlineWarning(details, icon)
function tableEmpty(_table)
for k, v in pairs(_table) do
return mw.html.create("span")
return false
:css("color", "darkred")
end
:wikitext("(")
return true
:node(
mw.html.create("span")
:attr("title", details)
:css("text-decoration", "underline")
:css("text-decoration-style", "dotted")
:wikitext(icon or "!")
)
:wikitext(")")
end
end


function p.demo(frame)
function p.demo(frame)
local out = mw.html.create("table")
local legend = setmetatable({}, { refgroupname = "" })
local errors = setmetatable({}, { refgroupname = "E" })
local warnings = setmetatable({}, { refgroupname = "W" })
function buildReflist(_group)
group = _group or legend
return frame:expandTemplate{ title = 'reflist', args = {
group = getmetatable(group)["refgroupname"]
} }
end
function createLegend(details, _group)
hash = string.sub(mw.hash.hashValue("md5", details), 0, 8)
group = _group or legend
if group[hash] == nil then
group[hash] = frame:extensionTag{
-- <ref name="hash">details</ref>
name = 'ref',
content = mw.ustring.gsub(details, "%[%[File:([^%|]+)[^%]]*%]%]", "[[:File:%1]]"),
args = {
name = hash,
group = getmetatable(group)["refgroupname"]
}
}
end
-- <ref name="hash"/>
return frame:extensionTag{ name = 'ref', args = {
name = hash,
group = getmetatable(group)["refgroupname"]
} }
end
local categoryTable = mw.html.create("table")
:addClass("wikitable")
:addClass("wikitable")
:addClass("sortable")
:addClass("sortable")
:attr("style", "width: 100%")
:attr("style", "width: 100%")
categoryTable
out
:node(
:node(
mw.html.create("tr")
mw.html.create("tr")
Line 60: Line 86:
:attr("data-sort-type", "number"))
:attr("data-sort-type", "number"))
)
)
local legends = {}
for name, cat in TableTools.sortedPairs(cats) do
for name, cat in TableTools.sortedPairs(cats) do
Line 71: Line 95:
:wikitext(actualIcon)
:wikitext(actualIcon)
if cat["icon"] ~= nil and actualIcon ~= cat["icon"] then
if cat["icon"] ~= nil and actualIcon ~= cat["icon"] then
icon
icon:wikitext(
createLegend("Overriden from original icon (" .. cat["icon"] .. ")")
:wikitext(" ")
:node(
)
mw.html.create("span")
:css("text-decoration", "underline")
:css("text-decoration-style", "dotted")
:attr("title", "Overriden from original icon (" .. cat["icon"] .. ")")
:wikitext("*")
)
legends["*"] = "Overriden from original icon."
end
end
Line 92: Line 109:
if actualColor ~= cat["color"] then
if actualColor ~= cat["color"] then
color
color:wikitext(createLegend(
"Overriden from original color ({{color box|#"
:node(
mw.html.create("span")
.. cat["color"]
.. "}} #"
:css("text-decoration", "underline")
.. cat["color"]
:css("text-decoration-style", "dotted")
.. ")"
:attr("title", "Overriden from original color (#" .. cat["color"] .. ")")
))
:wikitext("**")
)
legends["**"] = "Overriden from original color."
end
end
Line 108: Line 123:
if catColorBlackRatio == "?" or catColorLinkRatio == "?" or catColorVisitedLinkRatio == "?" then
if catColorBlackRatio == "?" or catColorLinkRatio == "?" or catColorVisitedLinkRatio == "?" then
color
color:wikitext(
createLegend("This color must be a hexadecimal color.", errors)
:wikitext(" ")
)
:node(inlineWarning("This color must be a hexadecimal color.", "E"))
legends["E"] = "Not a valid hexadecimal color code."
else
else
if catColorBlackRatio < 4.5 then
if catColorBlackRatio < 4.5 then
color
color:wikitext(
createLegend("This color has [[MOS:COLOR|contrast issues]] with black (not WCAG 2.0 AA-compatible). It will be unusable on all infoboxes and storm season summaries.", errors)
:wikitext(" ")
)
:node(inlineWarning("This color has contrast issues with black (not WCAG 2.0 AA-compatible).", "C"))
legends["C"] = "Color has contrast issues with black."
end
end
if catColorLinkRatio < 4.5 then
if catColorLinkRatio < 4.5 then
color
color:wikitext(
createLegend("This color has [[MOS:COLOR|contrast issues]] with links (not WCAG 2.0 AA-compatible). It should not be used in conjunction with a link.", warnings)
:wikitext(" ")
)
:node(inlineWarning("This color has contrast issues with links (not WCAG 2.0 AA-compatible).", "L"))
legends["L"] = "Color has contrast issues with links (#0645ad)."
end
end
if catColorVisitedLinkRatio < 4.5 then
if catColorVisitedLinkRatio < 4.5 then
color
color:wikitext(
createLegend("This color has [[MOS:COLOR|contrast issues]] with visited links (not WCAG 2.0 AA-compatible with #0b0080). It should not be used in conjunction with a visited link.", warnings)
:wikitext(" ")
)
:node(inlineWarning("This color has contrast issues with visited links (not WCAG 2.0 AA-compatible).", "V"))
end
legends["V"] = "Color has contrast issues with visited links (#0b0080)."
if actualCat == "c0c0c0" and cat[sortkey] ~= 0 then
color:wikitext(
createLegend("This category is using a color reserved specifically for the \"unknown\" category.", warnings)
)
end
end
end
end
Line 205: Line 221:
for _, _row in TableTools.sortedPairs(rows) do
for _, _row in TableTools.sortedPairs(rows) do
out:node(_row)
categoryTable:node(_row)
end
end
end
end
Line 243: Line 259:
:wikitext("''<span style=\"color:gray\">not available</span>''"))
:wikitext("''<span style=\"color:gray\">not available</span>''"))
end
end
out:node(row)
categoryTable:node(row)
end
end
end
end
out = ""
local legendsCompiled = mw.html.create("ul")
if not tableEmpty(errors) then
local showLegend = false
out = out
legendsCompiled
.. tostring(mw.html.create("h4"):wikitext("Error"))
:css("list-style-icon", "none")
.. tostring(mw.html.create("p"):wikitext("This table contains errors than need to be addressed immediately, as it may cause errors on a large amount of pages."))
:css("list-style-type", "none")
.. buildReflist(errors)
for legend, details in TableTools.sortedPairs(legends) do
end
showLegend = true
if not tableEmpty(legend) then
legendsCompiled:node(
out = out
mw.html.create("li")
.. tostring(mw.html.create("h4"):wikitext("Legend"))
-- en dash
.. buildReflist(legend)
:wikitext("'''" .. legend .. "''' – " .. details)
end
)
out = out .. tostring(categoryTable)
if not tableEmpty(warnings) then
out = out
.. tostring(mw.html.create("h4"):wikitext("Warnings"))
.. tostring(mw.html.create("p"):wikitext("This table contains warnings than should be addressed. Please note that some warnings cannot be fully addressed without consensus on related WikiProjects."))
.. buildReflist(warnings)
end
end
return mw.text.trim(out)
return (showLegend and tostring(legendsCompiled) or "") .. tostring(out)
end
end



Revision as of 13:41, 3 February 2022

local colorRatio = require("Module:Color contrast")._ratio
local TableTools = require("Module:TableTools")
local tcc = require("Module:Storm categories")
local cats = require("Module:Storm categories/categories").cats
local colors = require("Module:Storm categories/colors").colors
local icons = require("Module:Storm categories/icons").icons
local p = {}

local customKeys = {}
for k, v in pairs(colors) do
	table.insert(customKeys, k)
end
for k, v in pairs(icons) do
	table.insert(customKeys, k)
end
TableTools.removeDuplicates(customKeys)

function tableEmpty(_table)
	for k, v in pairs(_table) do
		return false
	end
	return true
end

function p.demo(frame)
	local legend = setmetatable({}, { refgroupname = "" })
	local errors = setmetatable({}, { refgroupname = "E" })
	local warnings = setmetatable({}, { refgroupname = "W" })
	
	function buildReflist(_group)
		group = _group or legend
		return frame:expandTemplate{ title = 'reflist', args = {
			group = getmetatable(group)["refgroupname"]
		} }
	end
	
	function createLegend(details, _group)
		hash = string.sub(mw.hash.hashValue("md5", details), 0, 8)
		group = _group or legend
		if group[hash] == nil then
			group[hash] = frame:extensionTag{ 
				-- <ref name="hash">details</ref>
				name = 'ref',
				content = mw.ustring.gsub(details, "%[%[File:([^%|]+)[^%]]*%]%]", "[[:File:%1]]"),
				args = { 
					name = hash, 
					group = getmetatable(group)["refgroupname"]
				}
			}
		end
		
		-- <ref name="hash"/>
		return frame:extensionTag{ name = 'ref', args = { 
			name = hash,
			group = getmetatable(group)["refgroupname"]
		} }
	end
	
	local categoryTable = mw.html.create("table")
		:addClass("wikitable")
		:addClass("sortable")
		:attr("style", "width: 100%")
		
	categoryTable
		:node(
			mw.html.create("tr")
				:node(mw.html.create("th"):wikitext("Icon")
					:attr("class", "unsortable")
					:attr("rowspan", "2")
					:css("width", "0"))
				:node(mw.html.create("th"):wikitext("ID")
					:attr("rowspan", "2"))
				:node(mw.html.create("th"):wikitext("Name")
					:attr("colspan", "2"))
				:node(mw.html.create("th"):wikitext("Color")
					:attr("rowspan", "2")
					:attr("colspan", "2"))
				:node(mw.html.create("th"):wikitext("Sortkey")
					:attr("colspan", "2"))
		):node(
			mw.html.create("tr")
				:node(mw.html.create("th"):wikitext("Basin"))
				:node(mw.html.create("th"):wikitext("Name"))
				:node(mw.html.create("th"):wikitext("Basin"))
				:node(mw.html.create("th"):wikitext("Sortkey")
					:attr("data-sort-type", "number"))
		)
	
	for name, cat in TableTools.sortedPairs(cats) do
		local rows = { mw.html.create("tr") }
		local row = rows[1]
		
		local actualIcon = tcc._icon(name)
		local icon = mw.html.create("td")
			:wikitext(actualIcon)
		if cat["icon"] ~= nil and actualIcon ~= cat["icon"] then
			icon:wikitext(
				createLegend("Overriden from original icon (" .. cat["icon"] .. ")")
			)
		end
			
		local id = mw.html.create("td")
			:wikitext(name)
		local actualColor = tcc._color(name)
		local colorPreview = mw.html.create("td")
			:attr("style", "background-color: #" .. actualColor .. "; padding: 0; width: 1.8em")
		local color = mw.html.create("td")
			:wikitext("#" .. actualColor)
			
		if actualColor ~= cat["color"] then
			color:wikitext(createLegend(
				"Overriden from original color ({{color box|#"
				.. cat["color"]
				.. "}} #"
				.. cat["color"]
				.. ")"
			))
		end
		
		local catColorBlackRatio = colorRatio({ "#" .. actualColor, "black" })
		local catColorLinkRatio = colorRatio({ "#" .. actualColor, "#0645ad" })
		local catColorVisitedLinkRatio = colorRatio({ "#" .. actualColor, "#0b0080" })
		
		if catColorBlackRatio == "?" or catColorLinkRatio == "?" or catColorVisitedLinkRatio == "?" then
			color:wikitext(
				createLegend("This color must be a hexadecimal color.", errors)
			)
		else
			if catColorBlackRatio < 4.5 then
				color:wikitext(
					createLegend("This color has [[MOS:COLOR|contrast issues]] with black (not WCAG 2.0 AA-compatible). It will be unusable on all infoboxes and storm season summaries.", errors)
				)
			end
			if catColorLinkRatio < 4.5 then
				color:wikitext(
					createLegend("This color has [[MOS:COLOR|contrast issues]] with links (not WCAG 2.0 AA-compatible). It should not be used in conjunction with a link.", warnings)
				)
			end
			if catColorVisitedLinkRatio < 4.5 then
				color:wikitext(
					createLegend("This color has [[MOS:COLOR|contrast issues]] with visited links (not WCAG 2.0 AA-compatible with #0b0080). It should not be used in conjunction with a visited link.", warnings)
				)
			end
			if actualCat == "c0c0c0" and cat[sortkey] ~= 0 then
				color:wikitext(
					createLegend("This category is using a color reserved specifically for the \"unknown\" category.", warnings)
				)
			end
		end
		
		local sortkeyCategory = mw.html.create("td")
			:attr("data-sort-value", cat["sortkey"])
		local sortkey = mw.html.create("td")
			:attr("data-sort-value", cat["sortkey"])
			:wikitext(cat["sortkey"])
			
		if cat["sortkey"] < 0 then
			sortkeyCategory:wikitext("Invalid")
		elseif cat["sortkey"] < 20000 then
			sortkeyCategory:wikitext("Global")
		elseif cat["sortkey"] < 30000 then
			sortkeyCategory:wikitext("Historical")
		elseif cat["sortkey"] < 40000 then
			sortkeyCategory:wikitext("SWIO")
		elseif cat["sortkey"] < 50000 then
			sortkeyCategory:wikitext("Aus/Fiji")
		elseif cat["sortkey"] < 60000 then
			sortkeyCategory:wikitext("NIO")
		elseif cat["sortkey"] < 80000 then
			sortkeyCategory:wikitext("WPAC")
		elseif cat["sortkey"] < 90000 then
			sortkeyCategory:wikitext("Atl/EPac/SAtl")
		elseif cat["sortkey"] < 100000 then
			sortkeyCategory:attr("style", "color: gray")
			sortkeyCategory:wikitext("''Global''")
		else
			sortkeyCategory:wikitext("Invalid")
		end
		
		if type(cat["name"]) == "string" then
			local name = mw.html.create("td")
				:attr("colspan", "2")
				:wikitext(cat["name"])
			row:node(icon)
			row:node(id)
			row:node(name)
		else
			local nameTableLength = TableTools.size(cat["name"])
			icon:attr("rowspan", nameTableLength)
			id:attr("rowspan", nameTableLength)
			colorPreview:attr("rowspan", nameTableLength)
			color:attr("rowspan", nameTableLength)
			sortkeyCategory:attr("rowspan", nameTableLength)
			sortkey:attr("rowspan", nameTableLength)
			
			row:node(icon)
			row:node(id)
			local firstDone = false
			for key, basinName in TableTools.sortedPairs(cat["name"]) do
				if firstDone then
					local nameRow = mw.html.create("tr")
					
					nameRow
						:node(mw.html.create("td"):wikitext(key))
						:node(mw.html.create("td"):wikitext(basinName))
						
					table.insert(rows, nameRow)	
				else
					firstDone = true
					row
						:node(mw.html.create("td"):wikitext(key))
						:node(mw.html.create("td"):wikitext(basinName))
				end
			end
		end
		
		row:node(colorPreview)
		row:node(color)
		row:node(sortkeyCategory)
		row:node(sortkey)
		
		for _, _row in TableTools.sortedPairs(rows) do
			categoryTable:node(_row)
		end
	end
	
	for name, _ in TableTools.sortedPairs(TableTools.listToSet(customKeys)) do
		if cats[name] == nil then
			local row = mw.html.create("tr")
			
			local icon = tcc._icon(name, true)
			row
				:node(mw.html.create("td")
					:wikitext(
						icon ~= nil
						and icon
						or "''<span style=\"color:gray\">N/A</span>''"
					))
				:node(mw.html.create("td"):wikitext(name))
			
			local color = tcc._color(name, true)
			-- Add more conditions eventually
			if color ~= nil then
				row
					:node(mw.html.create("td")
						:attr("colspan", "2")
						:wikitext("''<span style=\"color:gray\">not available</span>''"))
					:node(mw.html.create("td")
						:attr("style", "background-color: #" .. color .. "; padding: 0; width: 1.8em"))
					:node(mw.html.create("td")
						:wikitext("#" .. color))
					:node(mw.html.create("td")
						:attr("colspan", "2")
						:wikitext("''<span style=\"color:gray\">not available</span>''"))
			else
				row
					:node(mw.html.create("td")
						:attr("colspan", "6")
						:wikitext("''<span style=\"color:gray\">not available</span>''"))
			end
			categoryTable:node(row)
		end
	end
	
	out = ""
	if not tableEmpty(errors) then
		out = out
			.. tostring(mw.html.create("h4"):wikitext("Error"))
		    .. tostring(mw.html.create("p"):wikitext("This table contains errors than need to be addressed immediately, as it may cause errors on a large amount of pages."))
		    .. buildReflist(errors)
	end
	if not tableEmpty(legend) then
		out = out
			.. tostring(mw.html.create("h4"):wikitext("Legend"))
		    .. buildReflist(legend)
	end
	out = out .. tostring(categoryTable)
	if not tableEmpty(warnings) then
		out = out
			.. tostring(mw.html.create("h4"):wikitext("Warnings"))
		    .. tostring(mw.html.create("p"):wikitext("This table contains warnings than should be addressed. Please note that some warnings cannot be fully addressed without consensus on related WikiProjects."))
		    .. buildReflist(warnings)
	end
	
	return mw.text.trim(out)
end

return p