Module:WorkersVoices: Difference between revisions
Appearance
Initial WorkersVoices render module |
No edit summary |
||
| Line 1: | Line 1: | ||
-- Module:WorkersVoices | -- Module:WorkersVoices (native-MediaWiki version) | ||
-- | -- Outputs a sortable wikitable using only core wikitext markup. No custom | ||
-- | -- CSS or JS required — "class=wikitable sortable" and its click-to-sort | ||
-- columns ship with every standard MediaWiki install. | |||
local p = {} | local p = {} | ||
| Line 9: | Line 10: | ||
hospitality = "Hospitality", healthcare = "Healthcare", | hospitality = "Hospitality", healthcare = "Healthcare", | ||
skilled_it = "Skilled / IT", agriculture = "Agriculture", | skilled_it = "Skilled / IT", agriculture = "Agriculture", | ||
other = "Other", unclear = " | other = "Other", unclear = "Unclear", | ||
} | } | ||
local SENTIMENT_LABELS = { | local SENTIMENT_LABELS = { | ||
positive | positive = "Positive", mixed = "Mixed", negative = "Negative", | ||
} | } | ||
local function | local function wikiEscape(s) | ||
if not s then return "" end | if not s then return "" end | ||
return | return (s:gsub("|", "|"):gsub("%[", "["):gsub("%]", "]")) | ||
end | end | ||
local function | local function genderLabel(g) | ||
return | if g == "man" then return "Man" end | ||
if g == "woman" then return "Woman" end | |||
return "Unclear" | |||
end | end | ||
function p.main(frame) | |||
local | local dataPageTitle = frame.args[1] or "Data:WorkersVoices.json" | ||
local | local data | ||
if mw.loadJsonData then | |||
data = mw.loadJsonData(dataPageTitle) | |||
else | |||
local content = mw.title.new(dataPageTitle):getContent() | |||
data = mw.text.jsonDecode(content) | |||
end | end | ||
local rows = {} | |||
local noVideoLines = {} | |||
for _, region in ipairs(data.regions) do | |||
for _, country in ipairs(region.countries) do | |||
if #country.videos == 0 then | |||
table.insert(noVideoLines, | |||
"* " .. wikiEscape(country.flag) .. " " .. wikiEscape(country.name)) | |||
else | |||
for _, video in ipairs(country.videos) do | |||
local warning = "—" | |||
if video.contentWarning and video.contentWarning.flag then | |||
warning = "'''Warning:''' " .. wikiEscape(video.contentWarning.label) | |||
end | |||
local resource = "—" | |||
if video.verifiedResource and video.verifiedResource.url then | |||
resource = "[" .. video.verifiedResource.url .. " " .. | |||
wikiEscape(video.verifiedResource.label) .. "]" | |||
end | |||
local note = video.curatorNote and video.curatorNote ~= "" | |||
and ("''" .. wikiEscape(video.curatorNote) .. "''") or "—" | |||
local sourceLink = "[" .. video.sourceUrl .. " Source]" | |||
table.insert(rows, table.concat({ | |||
"|-", | |||
"| " .. wikiEscape(region.name), | |||
"| " .. wikiEscape(country.flag) .. " " .. wikiEscape(country.name), | |||
"| " .. (SECTOR_LABELS[video.sector] or wikiEscape(video.sector)), | |||
"| " .. genderLabel(video.gender), | |||
"| " .. (SENTIMENT_LABELS[video.sentiment] or "Unknown"), | |||
"| " .. wikiEscape(video.addedDate), | |||
"| " .. sourceLink, | |||
"| " .. warning, | |||
"| " .. note, | |||
"| " .. resource, | |||
}, "\n")) | |||
end | |||
end | |||
end | |||
end | end | ||
local | local tableWikitext = table.concat({ | ||
'{| class="wikitable sortable"', | |||
"! Region !! Country !! Sector !! Gender !! Sentiment !! Added !! Source !! Content warning !! Why we picked this !! Verified resource", | |||
table.concat(rows, "\n"), | |||
"|}", | |||
}, "\n") | |||
local | local out = { tableWikitext } | ||
if #noVideoLines > 0 then | |||
table.insert(out, "\n'''No account currently featured for:'''\n") | |||
table.insert(out, table.concat(noVideoLines, "\n")) | |||
end | end | ||
return | return table.concat(out, "\n") | ||
end | end | ||
return p | return p | ||
Revision as of 05:50, 27 September 2026
Documentation for this module may be created at Module:WorkersVoices/doc
-- Module:WorkersVoices (native-MediaWiki version)
-- Outputs a sortable wikitable using only core wikitext markup. No custom
-- CSS or JS required — "class=wikitable sortable" and its click-to-sort
-- columns ship with every standard MediaWiki install.
local p = {}
local SECTOR_LABELS = {
construction = "Construction", domestic_work = "Domestic work",
hospitality = "Hospitality", healthcare = "Healthcare",
skilled_it = "Skilled / IT", agriculture = "Agriculture",
other = "Other", unclear = "Unclear",
}
local SENTIMENT_LABELS = {
positive = "Positive", mixed = "Mixed", negative = "Negative",
}
local function wikiEscape(s)
if not s then return "" end
return (s:gsub("|", "|"):gsub("%[", "["):gsub("%]", "]"))
end
local function genderLabel(g)
if g == "man" then return "Man" end
if g == "woman" then return "Woman" end
return "Unclear"
end
function p.main(frame)
local dataPageTitle = frame.args[1] or "Data:WorkersVoices.json"
local data
if mw.loadJsonData then
data = mw.loadJsonData(dataPageTitle)
else
local content = mw.title.new(dataPageTitle):getContent()
data = mw.text.jsonDecode(content)
end
local rows = {}
local noVideoLines = {}
for _, region in ipairs(data.regions) do
for _, country in ipairs(region.countries) do
if #country.videos == 0 then
table.insert(noVideoLines,
"* " .. wikiEscape(country.flag) .. " " .. wikiEscape(country.name))
else
for _, video in ipairs(country.videos) do
local warning = "—"
if video.contentWarning and video.contentWarning.flag then
warning = "'''Warning:''' " .. wikiEscape(video.contentWarning.label)
end
local resource = "—"
if video.verifiedResource and video.verifiedResource.url then
resource = "[" .. video.verifiedResource.url .. " " ..
wikiEscape(video.verifiedResource.label) .. "]"
end
local note = video.curatorNote and video.curatorNote ~= ""
and ("''" .. wikiEscape(video.curatorNote) .. "''") or "—"
local sourceLink = "[" .. video.sourceUrl .. " Source]"
table.insert(rows, table.concat({
"|-",
"| " .. wikiEscape(region.name),
"| " .. wikiEscape(country.flag) .. " " .. wikiEscape(country.name),
"| " .. (SECTOR_LABELS[video.sector] or wikiEscape(video.sector)),
"| " .. genderLabel(video.gender),
"| " .. (SENTIMENT_LABELS[video.sentiment] or "Unknown"),
"| " .. wikiEscape(video.addedDate),
"| " .. sourceLink,
"| " .. warning,
"| " .. note,
"| " .. resource,
}, "\n"))
end
end
end
end
local tableWikitext = table.concat({
'{| class="wikitable sortable"',
"! Region !! Country !! Sector !! Gender !! Sentiment !! Added !! Source !! Content warning !! Why we picked this !! Verified resource",
table.concat(rows, "\n"),
"|}",
}, "\n")
local out = { tableWikitext }
if #noVideoLines > 0 then
table.insert(out, "\n'''No account currently featured for:'''\n")
table.insert(out, table.concat(noVideoLines, "\n"))
end
return table.concat(out, "\n")
end
return p