Module:Storm categories/demo: Difference between revisions

Content deleted Content added
m Anthony Appleyard moved page Module:Tropical cyclone categories/demo to Module:Storm categories/demo without leaving a redirect: Requested by Chlod at WP:RM/TR: Module's scope has now expanded to include other non-tropical storms (such as tornadoes). As such, the name "Tropical cyclone categories" is unfitting. This module includes many subpages (some are submodules, others are documentation pages) which require the page mover right to move (or at...
rm extra a
 
(8 intermediate revisions by the same user not shown)
Line 1:
--
-- This module handles demonstration and list of possible values for all storm
-- category templates! Please test this module on [[Module:Storm categories/demo/doc]]
-- before publishing to avoid errros. Thank you!
--
local colorRatio = require("Module:Color contrast")._ratio
local TableTools = require("Module:TableTools")
local tccstormcats = require("Module:Tropical cycloneStorm categories")
local cats = require("Module:Tropical cycloneStorm categories/categories").cats
local colors = require("Module:Tropical cycloneStorm categories/colors").colors
local icons = require("Module:Tropical cycloneStorm categories/icons").icons
local p = {}
 
Line 16 ⟶ 21:
TableTools.removeDuplicates(customKeys)
 
function inlineWarningtableEmpty(details, icon_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
 
--- Generates and renderds the demo table.
-- @param frame The Scribunto frame.
function p.demo(frame)
local plain = (frame.args["plain"] or frame:getParent().args["plain"] or "") ~= ""
local out = mw.html.create("table")
local verbose = (frame.args["verbose"] or frame:getParent().args["verbose"] or "") ~= ""
local legend = setmetatable({}, { refgroupname = "" })
local errors = setmetatable({}, { refgroupname = "E" })
local warnings = setmetatable({}, { refgroupname = "W" })
--- Builds the entire reflist
-- @param _group The group to build for. Uses `legend` by default.
function buildReflist(_group)
group = _group or legend
return frame:expandTemplate{ title = 'reflist', args = {
group = getmetatable(group)["refgroupname"]
} }
end
--- Returns a single <ref> tag containing the legend provided and
-- attaches the note definition to the list of notes.
-- @param details The details of the legend.
-- @param _group The group to write in. Uses `legend` by default.
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] = plain and "" or 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 plain and "" or frame:extensionTag{ name = 'ref', args = {
name = hash,
group = getmetatable(group)["refgroupname"]
} }
end
--- Checks for color contrast issues and tags accordingly.
-- Returns the refernce tag, so this should be used in conjunction with an
-- HTML node's :wikitext function.
function contrastCheck(color)
local catColorBlackRatio = colorRatio({ "#" .. color, "black" })
local catColorLinkRatio = colorRatio({ "#" .. color, "#0645ad" })
local catColorVisitedLinkRatio = colorRatio({ "#" .. color, "#0b0080" })
local finalWikitext = ""
if catColorBlackRatio == "?" or catColorLinkRatio == "?" or catColorVisitedLinkRatio == "?" then
finalWikitext = finalWikitext ..
createLegend("This color must be a hexadecimal color.", errors)
else
if catColorBlackRatio < 4.5 then
finalWikitext = finalWikitext ..
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
finalWikitext = finalWikitext ..
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
finalWikitext = finalWikitext ..
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
finalWikitext = finalWikitext ..
createLegend("This category is using a color reserved specifically for the \"unknown\" category.", warnings)
end
end
return finalWikitext
end
function colorInfo(color, verbose, extra)
local catColorBlackRatio = colorRatio({ "#" .. color, "black" })
local catColorLinkRatio = colorRatio({ "#" .. color, "#0645ad" })
local catColorVisitedLinkRatio = colorRatio({ "#" .. color, "#0b0080" })
local nc = tostring(mw.html.create("abbr")
:wikitext("NC")
:attr("title", "Does not satisfy the minimum WCAG 2.1 compliance level for color contrast (AA)")
);
local aa = tostring(mw.html.create("abbr")
:wikitext("AA")
:attr("title", "WCAG 2.1 Level AA: Acceptable compliance")
);
local aaa = tostring(mw.html.create("abbr")
:wikitext("AAA")
:attr("title", "WCAG 2.1 Level AAA: Optimal compliance")
);
function contrastLevel(contrast)
return contrast >= 7 and aaa or (contrast >= 4.5 and aa or nc)
end
return mw.html.create("td")
:attr("data-sort-value", math.min(catColorBlackRatio))
:wikitext(
"#" .. color .. (extra or "") .. contrastCheck(color) .. (verbose and ("<br/>"
.. tostring(
mw.html.create("abbr")
:attr("title", "Contrast to black")
:wikitext("CTB")
) .. ": " .. string.format("%.2f", catColorBlackRatio) .. " (" .. contrastLevel(catColorBlackRatio) .. ")<br/>"
.. tostring(
mw.html.create("abbr")
:attr("title", "Contrast to links")
:wikitext("CTL")
) .. ": " .. string.format("%.2f", catColorLinkRatio) .. " (" .. contrastLevel(catColorLinkRatio) .. ")<br/>"
.. tostring(
mw.html.create("abbr")
:attr("title", "Contrast to visited links")
:wikitext("CTVL")
) .. ": " .. string.format("%.2f", catColorVisitedLinkRatio) .. " (" .. contrastLevel(catColorVisitedLinkRatio) .. ")") or "")
)
end
local categoryTable = mw.html.create("table")
:addClass("wikitable")
:addClass("sortable")
:attr("style", "width: 100%")
categoryTable
out
:node(
mw.html.create("tr")
Line 60 ⟶ 179:
:attr("data-sort-type", "number"))
)
local legends = {}
for name, cat in TableTools.sortedPairs(cats) do
Line 67 ⟶ 184:
local row = rows[1]
local actualIcon = tccstormcats._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"] .. ")")
: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
local id = mw.html.create("td")
:wikitext(name)
local actualColor = tccstormcats._color(name)
local colorPreview = mw.html.create("td")
:attr("style", "background-color: #" .. actualColor .. "; padding: 0; width: 1.8em")
local color = mw.html.createcolorInfo("td")
:wikitext("#" .. actualColor),
verbose,
if actualColor ~= cat["color"] thenand createLegend(
"Overriden from original color ({{color box|#"
color
:node(.. cat["color"]
mw.html.create( "span}} #")
.. cat["color"]
:css("text-decoration", "underline")
.. ")"
:css("text-decoration-style", "dotted")
) or ""
:attr("title", "Overriden from original color (#" .. cat["color"] .. ")")
):css("width", "0"):css("white-space", "nowrap")
:wikitext("**")
)
legends["**"] = "Overriden from original 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(" ")
:node(inlineWarning("This color must be a hexadecimal color.", "E"))
legends["E"] = "Not a valid hexadecimal color code."
else
if catColorBlackRatio < 4.5 then
color
: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
if catColorLinkRatio < 4.5 then
color
: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
if catColorVisitedLinkRatio < 4.5 then
color
:wikitext(" ")
:node(inlineWarning("This color has contrast issues with visited links (not WCAG 2.0 AA-compatible).", "V"))
legends["V"] = "Color has contrast issues with visited links (#0b0080)."
end
end
local sortkeyCategory = mw.html.create("td")
Line 205 ⟶ 282:
for _, _row in TableTools.sortedPairs(rows) do
outcategoryTable:node(_row)
end
end
Line 213 ⟶ 290:
local row = mw.html.create("tr")
local icon = tccstormcats._icon(name, true)
row
:node(mw.html.create("td")
Line 223 ⟶ 300:
:node(mw.html.create("td"):wikitext(name))
local color = tccstormcats._color(name, true)
-- Add more conditions eventually
if color ~= nil then
Line 232 ⟶ 309:
:node(mw.html.create("td")
:attr("style", "background-color: #" .. color .. "; padding: 0; width: 1.8em"))
:node(mw.html.createcolorInfo("td"color, verbose))
:wikitextcss("#width", .. color)"0")
:css("white-space", "nowrap")
:node(mw.html.create("td")
:attr("colspan", "2")
Line 243 ⟶ 321:
:wikitext("''<span style=\"color:gray\">not available</span>''"))
end
outcategoryTable:node(row)
end
end
out = ""
local legendsCompiled = mw.html.create("ul")
if not plain and 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 plain and 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 plain and 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 changes that would require consensus."))
.. buildReflist(warnings)
end
return mw.text.trim(out)
return (showLegend and tostring(legendsCompiled) or "") .. tostring(out)
end