Jump to content

Module:WorkersVoices: Difference between revisions

Your free guide through every stage of migration
Initial WorkersVoices render module
 
No edit summary
Line 1: Line 1:
-- Module:WorkersVoices
-- Module:WorkersVoices (native-MediaWiki version)
-- Renders the "Worker's Voices" section with multiple tagged videos per
-- Outputs a sortable wikitable using only core wikitext markup. No custom
-- country, content warnings, curator notes, and a paired verified resource.
-- 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 = "Sector unclear",
other = "Other", unclear = "Unclear",
}
}


local SENTIMENT_LABELS = {
local SENTIMENT_LABELS = {
positive = { text = "Positive", color = "#1a7f37" },
positive = "Positive", mixed = "Mixed", negative = "Negative",
mixed = { text = "Mixed", color = "#9a6700" },
negative = { text = "Negative", color = "#b3261e" },
}
}


local function escapeHtml(s)
local function wikiEscape(s)
if not s then return "" end
if not s then return "" end
return mw.text.encode(s)
return (s:gsub("|", "|"):gsub("%[", "["):gsub("%]", "]"))
end
end


local function renderTag(text, bgColor)
local function genderLabel(g)
return mw.html.create("span")
if g == "man" then return "Man" end
:addClass("voice-tag")
if g == "woman" then return "Woman" end
:cssText("background:" .. bgColor .. ";")
return "Unclear"
:wikitext(escapeHtml(text))
end
end


local function renderVideo(video)
function p.main(frame)
local sentiment = SENTIMENT_LABELS[video.sentiment] or { text = "Unknown", color = "#888" }
local dataPageTitle = frame.args[1] or "Data:WorkersVoices.json"
local sectorLabel = SECTOR_LABELS[video.sector] or video.sector
local genderLabel = video.gender == "unclear" and "" or
(video.gender == "man" and "Man" or "Woman")


local card = mw.html.create("div")
local data
:addClass("video-card")
if mw.loadJsonData then
:attr("data-sector", video.sector)
data = mw.loadJsonData(dataPageTitle)
:attr("data-gender", video.gender)
else
:attr("data-sentiment", video.sentiment)
local content = mw.title.new(dataPageTitle):getContent()
 
data = mw.text.jsonDecode(content)
if video.contentWarning and video.contentWarning.flag then
card:tag("div")
:addClass("content-warning")
:wikitext("⚠ " .. escapeHtml(video.contentWarning.label))
end
end


card:tag("div")
local rows = {}
:addClass("thumb-wrap")
local noVideoLines = {}
:tag("div"):addClass("placeholder")
:tag("div"):addClass("play-btn"):wikitext("▶"):done()
:wikitext("YouTube embed")
:done()


local body = card:tag("div"):addClass("card-body")
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 tagRow = body:tag("div"):addClass("tag-row")
local resource = "—"
tagRow:node(renderTag(sectorLabel, "#e6e6e6"))
if video.verifiedResource and video.verifiedResource.url then
if genderLabel ~= "" then
resource = "[" .. video.verifiedResource.url .. " " ..
tagRow:node(renderTag(genderLabel, "#e6e6e6"))
wikiEscape(video.verifiedResource.label) .. "]"
end
end
tagRow:node(renderTag(sentiment.text, sentiment.color))


body:tag("div"):addClass("card-meta")
local note = video.curatorNote and video.curatorNote ~= ""
:wikitext("Added " .. escapeHtml(video.addedDate) .. " · ")
and ("''" .. wikiEscape(video.curatorNote) .. "''") or "—"
:tag("a")
:attr("href", video.sourceUrl)
:attr("target", "_blank")
:attr("rel", "noopener")
:wikitext("Source ↗")


if video.curatorNote and video.curatorNote ~= "" then
local sourceLink = "[" .. video.sourceUrl .. " Source]"
body:tag("div"):addClass("curator-note")
:wikitext("Why we picked this: " .. escapeHtml(video.curatorNote))
end
 
if video.verifiedResource and video.verifiedResource.url then
body:tag("div"):addClass("verified-resource")
:wikitext("✓ ")
:tag("a")
:attr("href", video.verifiedResource.url)
:attr("target", "_blank")
:attr("rel", "noopener")
:wikitext(escapeHtml(video.verifiedResource.label))
end
 
return card
end


local function renderCountry(country)
table.insert(rows, table.concat({
if #country.videos == 0 then
"|-",
return mw.html.create("div")
"| " .. wikiEscape(region.name),
:addClass("no-video")
"| " .. wikiEscape(country.flag) .. " " .. wikiEscape(country.name),
:tag("div"):cssText("font-size:1.6em;"):wikitext("🎥"):done()
"| " .. (SECTOR_LABELS[video.sector] or wikiEscape(video.sector)),
:wikitext("No account currently featured for<br><b>" ..
"| " .. genderLabel(video.gender),
escapeHtml(country.flag) .. " " .. escapeHtml(country.name) .. "</b>")
"| " .. (SENTIMENT_LABELS[video.sentiment] or "Unknown"),
"| " .. wikiEscape(video.addedDate),
"| " .. sourceLink,
"| " .. warning,
"| " .. note,
"| " .. resource,
}, "\n"))
end
end
end
end
end


local group = mw.html.create("div"):addClass("country-group")
local tableWikitext = table.concat({
group:tag("div"):addClass("country-heading")
'{| class="wikitable sortable"',
:wikitext(escapeHtml(country.flag) .. " " .. escapeHtml(country.name))
"! Region !! Country !! Sector !! Gender !! Sentiment !! Added !! Source !! Content warning !! Why we picked this !! Verified resource",
table.concat(rows, "\n"),
"|}",
}, "\n")


local cards = group:tag("div"):addClass("country-video-row")
local out = { tableWikitext }
for _, video in ipairs(country.videos) do
cards:node(renderVideo(video))
end
return group
end


local function renderRegion(region)
if #noVideoLines > 0 then
local wrapper = mw.html.create("div")
table.insert(out, "\n'''No account currently featured for:'''\n")
wrapper:tag("div")
table.insert(out, table.concat(noVideoLines, "\n"))
:addClass("region-title")
:cssText("color:" .. escapeHtml(region.color) .. ";")
:wikitext(escapeHtml(region.name))
 
local grid = wrapper:tag("div"):addClass("region-grid")
for _, country in ipairs(region.countries) do
grid:node(renderCountry(country))
end
return wrapper
end
 
local function renderFilterChips()
local bar = mw.html.create("div"):addClass("voice-filters")
bar:tag("span"):addClass("filter-label"):wikitext("Filter: ")
 
local sectors = { "construction", "domestic_work", "hospitality", "healthcare", "skilled_it", "agriculture" }
for _, sector in ipairs(sectors) do
bar:tag("button")
:addClass("filter-chip")
:attr("data-filter-type", "sector")
:attr("data-filter-value", sector)
:wikitext(SECTOR_LABELS[sector])
end
 
bar:tag("button")
:addClass("filter-chip")
:attr("data-filter-type", "gender")
:attr("data-filter-value", "woman")
:wikitext("Women's accounts")
 
bar:tag("button")
:addClass("filter-chip filter-chip-reset")
:attr("data-filter-type", "reset")
:wikitext("Clear filters")
 
return bar
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 out = mw.html.create("div"):addClass("wrap")
out:node(renderFilterChips())
for _, region in ipairs(data.regions) do
out:node(renderRegion(region))
end
end


return tostring(out)
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("|", "&#124;"):gsub("%[", "&#91;"):gsub("%]", "&#93;"))
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