Jump to content

Module:WorkersVoices: Difference between revisions

Your free guide through every stage of migration
Initial WorkersVoices render module
 
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:WorkersVoices
-- Renders the "Worker's Voices" section with multiple tagged videos per
-- country, content warnings, curator notes, and a paired verified resource.
local p = {}
local p = {}


Line 9: Line 5:
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",
general_worker_life = "General worker life", employment_advice = "Employment advice",
driver_transport = "Driver / transport", general_expat_life = "General expat life",
study_work_settlement = "Study, work & settlement",
other = "Other",
}
}


local SENTIMENT_LABELS = {
local SENTIMENT_ICONS = {
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 htmlEscape(s)
if not s then return "" end
if not s then return "" end
return mw.text.encode(s)
return (s:gsub("&", "&amp;"):gsub("<", "&lt;"):gsub(">", "&gt;"):gsub('"', "&quot;"))
end
end


local function renderTag(text, bgColor)
local function tlen(t)
return mw.html.create("span")
local n = 0
:addClass("voice-tag")
for _ in ipairs(t) do n = n + 1 end
:cssText("background:" .. bgColor .. ";")
return n
:wikitext(escapeHtml(text))
end
end


local function renderVideo(video)
local function genderLabel(g)
local sentiment = SENTIMENT_LABELS[video.sentiment] or { text = "Unknown", color = "#888" }
if g == "man" then return "Man" end
local sectorLabel = SECTOR_LABELS[video.sector] or video.sector
if g == "woman" then return "Woman" end
local genderLabel = video.gender == "unclear" and "" or
return "Not specified"
(video.gender == "man" and "Man" or "Woman")
end


local card = mw.html.create("div")
local function sectorLabel(s)
:addClass("video-card")
if not s or s == "unclear" then return "Not yet confirmed" end
:attr("data-sector", video.sector)
return SECTOR_LABELS[s] or htmlEscape(s)
:attr("data-gender", video.gender)
end
:attr("data-sentiment", video.sentiment)


if video.contentWarning and video.contentWarning.flag then
local function sentimentLabel(s)
card:tag("div")
return SENTIMENT_ICONS[s] or "Unknown"
:addClass("content-warning")
end
:wikitext("⚠ " .. escapeHtml(video.contentWarning.label))
end


card:tag("div")
local function warningHtml(video)
:addClass("thumb-wrap")
if not (video.contentWarning and video.contentWarning.flag) then
:tag("div"):addClass("placeholder")
return ""
:tag("div"):addClass("play-btn"):wikitext("▶"):done()
:wikitext("YouTube embed")
:done()
 
local body = card:tag("div"):addClass("card-body")
 
local tagRow = body:tag("div"):addClass("tag-row")
tagRow:node(renderTag(sectorLabel, "#e6e6e6"))
if genderLabel ~= "" then
tagRow:node(renderTag(genderLabel, "#e6e6e6"))
end
end
tagRow:node(renderTag(sentiment.text, sentiment.color))
local label = video.contentWarning.label or ""
 
if label:find("automated discovery") then
body:tag("div"):addClass("card-meta")
return '<p>🔍 <i>Pending review</i></p>'
:wikitext("Added " .. escapeHtml(video.addedDate) .. " · ")
:tag("a")
:attr("href", video.sourceUrl)
:attr("target", "_blank")
:attr("rel", "noopener")
:wikitext("Source ↗")
 
if video.curatorNote and video.curatorNote ~= "" then
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
end
 
return '<p>⚠️ <b>' .. htmlEscape(label) .. '</b></p>'
return card
end
end


local function renderCountry(country)
local function youtubeThumbnail(url)
if #country.videos == 0 then
if not url then return nil end
return mw.html.create("div")
local id = url:match("[?&]v=([%w_%-]+)")
:addClass("no-video")
if not id then
:tag("div"):cssText("font-size:1.6em;"):wikitext("🎥"):done()
id = url:match("youtu%.be/([%w_%-]+)")
:wikitext("No account currently featured for<br><b>" ..
escapeHtml(country.flag) .. " " .. escapeHtml(country.name) .. "</b>")
end
end
 
if id then
local group = mw.html.create("div"):addClass("country-group")
return "https://img.youtube.com/vi/" .. id .. "/mqdefault.jpg"
group:tag("div"):addClass("country-heading")
:wikitext(escapeHtml(country.flag) .. " " .. escapeHtml(country.name))
 
local cards = group:tag("div"):addClass("country-video-row")
for _, video in ipairs(country.videos) do
cards:node(renderVideo(video))
end
end
return group
return nil
end
end


local function renderRegion(region)
local function buildCard(region, country, video)
local wrapper = mw.html.create("div")
local resource = ""
wrapper:tag("div")
if video.verifiedResource and video.verifiedResource.url then
:addClass("region-title")
resource = ' &middot; [' .. video.verifiedResource.url .. ' ' ..
:cssText("color:" .. escapeHtml(region.color) .. ";")
htmlEscape(video.verifiedResource.label) .. ']'
:wikitext(escapeHtml(region.name))
end


local grid = wrapper:tag("div"):addClass("region-grid")
local note = video.curatorNote and video.curatorNote ~= ""
for _, country in ipairs(region.countries) do
and ('<p><i>' .. htmlEscape(video.curatorNote) .. '</i></p>') or ""
grid:node(renderCountry(country))
end
return wrapper
end


local function renderFilterChips()
local sourceLink = '[' .. video.sourceUrl .. ' ▶ Watch source]'
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" }
local thumb = ""
for _, sector in ipairs(sectors) do
local thumbUrl = youtubeThumbnail(video.sourceUrl)
bar:tag("button")
if thumbUrl then
:addClass("filter-chip")
thumb = '<p>[' .. video.sourceUrl .. ' ' .. thumbUrl .. ']</p>'
:attr("data-filter-type", "sector")
:attr("data-filter-value", sector)
:wikitext(SECTOR_LABELS[sector])
end
end


bar:tag("button")
return table.concat({
:addClass("filter-chip")
'<h4>' .. country.flag .. ' ' .. htmlEscape(country.name) .. ' — ' .. sentimentLabel(video.sentiment) .. '</h4>',
:attr("data-filter-type", "gender")
thumb,
:attr("data-filter-value", "woman")
'<p><b>Sector:</b> ' .. sectorLabel(video.sector) ..
:wikitext("Women's accounts")
' &middot; <b>Gender:</b> ' .. genderLabel(video.gender) ..
 
' &middot; <b>Added:</b> ' .. htmlEscape(video.addedDate) .. '</p>',
bar:tag("button")
note,
:addClass("filter-chip filter-chip-reset")
warningHtml(video),
:attr("data-filter-type", "reset")
'<p>' .. sourceLink .. resource .. '</p>',
:wikitext("Clear filters")
}, "\n")
 
return bar
end
end


Line 162: Line 105:
end
end


local out = mw.html.create("div"):addClass("wrap")
local out = {}
out:node(renderFilterChips())
 
for _, region in ipairs(data.regions) do
for _, region in ipairs(data.regions) do
out:node(renderRegion(region))
local cardTexts = {}
local noVideoLines = {}
 
for _, country in ipairs(region.countries) do
if tlen(country.videos) == 0 then
table.insert(noVideoLines, "🔸 " .. country.flag .. " " .. htmlEscape(country.name))
else
for _, video in ipairs(country.videos) do
table.insert(cardTexts, buildCard(region, country, video))
end
end
end
 
if #cardTexts > 0 or #noVideoLines > 0 then
table.insert(out, '<h2>' .. htmlEscape(region.name) .. '</h2>')
 
if #cardTexts > 0 then
local tableRows = {}
local i = 1
while i <= #cardTexts do
local left = cardTexts[i]
local right = cardTexts[i + 1]
local row = '<tr>\n<td valign="top" width="50%">\n' .. left .. '\n</td>\n'
if right then
row = row .. '<td valign="top" width="50%">\n' .. right .. '\n</td>\n'
else
row = row .. '<td valign="top" width="50%">&nbsp;</td>\n'
end
row = row .. '</tr>'
table.insert(tableRows, row)
i = i + 2
end
table.insert(out, '<table border="1" cellpadding="10" cellspacing="0" width="100%">\n' ..
table.concat(tableRows, "\n") .. '\n</table>')
end
 
if #noVideoLines > 0 then
table.insert(out, '<p><b>No account yet:</b><br>' .. table.concat(noVideoLines, "<br>") .. '</p>')
end
end
end
end


return tostring(out)
return table.concat(out, "\n")
end
end


return p
return p

Latest revision as of 14:50, 27 September 2026

Documentation for this module may be created at Module:WorkersVoices/doc

local p = {}

local SECTOR_LABELS = {
	construction = "Construction", domestic_work = "Domestic work",
	hospitality = "Hospitality", healthcare = "Healthcare",
	skilled_it = "Skilled / IT", agriculture = "Agriculture",
	general_worker_life = "General worker life", employment_advice = "Employment advice",
	driver_transport = "Driver / transport", general_expat_life = "General expat life",
	study_work_settlement = "Study, work & settlement",
	other = "Other",
}

local SENTIMENT_ICONS = {
	positive = "🟢 Positive", mixed = "🟡 Mixed", negative = "🔴 Negative",
}

local function htmlEscape(s)
	if not s then return "" end
	return (s:gsub("&", "&amp;"):gsub("<", "&lt;"):gsub(">", "&gt;"):gsub('"', "&quot;"))
end

local function tlen(t)
	local n = 0
	for _ in ipairs(t) do n = n + 1 end
	return n
end

local function genderLabel(g)
	if g == "man" then return "Man" end
	if g == "woman" then return "Woman" end
	return "Not specified"
end

local function sectorLabel(s)
	if not s or s == "unclear" then return "Not yet confirmed" end
	return SECTOR_LABELS[s] or htmlEscape(s)
end

local function sentimentLabel(s)
	return SENTIMENT_ICONS[s] or "Unknown"
end

local function warningHtml(video)
	if not (video.contentWarning and video.contentWarning.flag) then
		return ""
	end
	local label = video.contentWarning.label or ""
	if label:find("automated discovery") then
		return '<p>🔍 <i>Pending review</i></p>'
	end
	return '<p>⚠️ <b>' .. htmlEscape(label) .. '</b></p>'
end

local function youtubeThumbnail(url)
	if not url then return nil end
	local id = url:match("[?&]v=([%w_%-]+)")
	if not id then
		id = url:match("youtu%.be/([%w_%-]+)")
	end
	if id then
		return "https://img.youtube.com/vi/" .. id .. "/mqdefault.jpg"
	end
	return nil
end

local function buildCard(region, country, video)
	local resource = ""
	if video.verifiedResource and video.verifiedResource.url then
		resource = ' &middot; [' .. video.verifiedResource.url .. ' ' ..
			htmlEscape(video.verifiedResource.label) .. ']'
	end

	local note = video.curatorNote and video.curatorNote ~= ""
		and ('<p><i>' .. htmlEscape(video.curatorNote) .. '</i></p>') or ""

	local sourceLink = '[' .. video.sourceUrl .. ' ▶ Watch source]'

	local thumb = ""
	local thumbUrl = youtubeThumbnail(video.sourceUrl)
	if thumbUrl then
		thumb = '<p>[' .. video.sourceUrl .. ' ' .. thumbUrl .. ']</p>'
	end

	return table.concat({
		'<h4>' .. country.flag .. ' ' .. htmlEscape(country.name) .. ' — ' .. sentimentLabel(video.sentiment) .. '</h4>',
		thumb,
		'<p><b>Sector:</b> ' .. sectorLabel(video.sector) ..
			' &middot; <b>Gender:</b> ' .. genderLabel(video.gender) ..
			' &middot; <b>Added:</b> ' .. htmlEscape(video.addedDate) .. '</p>',
		note,
		warningHtml(video),
		'<p>' .. sourceLink .. resource .. '</p>',
	}, "\n")
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 = {}

	for _, region in ipairs(data.regions) do
		local cardTexts = {}
		local noVideoLines = {}

		for _, country in ipairs(region.countries) do
			if tlen(country.videos) == 0 then
				table.insert(noVideoLines, "🔸 " .. country.flag .. " " .. htmlEscape(country.name))
			else
				for _, video in ipairs(country.videos) do
					table.insert(cardTexts, buildCard(region, country, video))
				end
			end
		end

		if #cardTexts > 0 or #noVideoLines > 0 then
			table.insert(out, '<h2>' .. htmlEscape(region.name) .. '</h2>')

			if #cardTexts > 0 then
				local tableRows = {}
				local i = 1
				while i <= #cardTexts do
					local left = cardTexts[i]
					local right = cardTexts[i + 1]
					local row = '<tr>\n<td valign="top" width="50%">\n' .. left .. '\n</td>\n'
					if right then
						row = row .. '<td valign="top" width="50%">\n' .. right .. '\n</td>\n'
					else
						row = row .. '<td valign="top" width="50%">&nbsp;</td>\n'
					end
					row = row .. '</tr>'
					table.insert(tableRows, row)
					i = i + 2
				end
				table.insert(out, '<table border="1" cellpadding="10" cellspacing="0" width="100%">\n' ..
					table.concat(tableRows, "\n") .. '\n</table>')
			end

			if #noVideoLines > 0 then
				table.insert(out, '<p><b>No account yet:</b><br>' .. table.concat(noVideoLines, "<br>") .. '</p>')
			end
		end
	end

	return table.concat(out, "\n")
end

return p