Modul:Geburtsort

Version vom 4. Oktober 2025, 09:49 Uhr von Bunnyfreak (Diskussion | Beiträge)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

Die Dokumentation für dieses Modul kann unter Modul:Geburtsort/Doku erstellt werden

-- Modul:Geburtsort
-- Benutzung in Vorlage: {{#invoke:Geburtsort|render|value={{{Geburtsort}}}|link=category}}
-- link=page (Standard) → auf Artikelseiten verlinken
-- link=category        → sichtbare Links zeigen direkt auf die Kategorien

local p = {}

local trim = mw.text.trim
local lc   = mw.ustring.lower

-- Link, wenn Seite existiert; sonst Plaintext
local function linkIfExists(titleText)
	if not titleText or titleText == "" then return "" end
	local title = mw.title.new(titleText)
	if title and title.exists then
		return string.format("[[%s]]", titleText)
	end
	return titleText
end

-- Länder-Normalisierung
local countryNormalize = (function()
	local map = {}
	local function add(canonical, variants)
		for _, v in ipairs(variants) do map[lc(v)] = canonical end
	end
	add("Deutschland",    {"Deutschland","BRD","deutschland"})
	add("USA",            {"USA","Vereinigte Staaten","United States","US","Amerika","US-Amerika","vereinigte staaten"})
	add("Großbritannien", {"Großbritannien","Grossbritannien","Vereinigtes Königreich","United Kingdom","UK","England","gb"})
	add("Österreich",     {"Österreich","Oesterreich","österreich","oesterreich"})
	add("Schweiz",        {"Schweiz"})
	add("Frankreich",     {"Frankreich"})
	add("Italien",        {"Italien"})
	add("Spanien",        {"Spanien"})
	add("Niederlande",    {"Niederlande","Holland"})
	add("Kanada",         {"Kanada"})
	add("Belgien",        {"Belgien"})
	add("Dänemark",       {"Dänemark","Daenemark"})
	add("Schweden",       {"Schweden"})
	add("Norwegen",       {"Norwegen"})
	return map
end)()

-- Länderkategorie (mit Beugung nach "Geboren in ")
local countryCatSuffix = {
	["Deutschland"]    = "Deutschland",
	["USA"]            = "den USA",
	["Großbritannien"] = "Großbritannien",
	["Österreich"]     = "Österreich",
	["Schweiz"]        = "der Schweiz",
	["Frankreich"]     = "Frankreich",
	["Italien"]        = "Italien",
	["Spanien"]        = "Spanien",
	["Niederlande"]    = "den Niederlanden",
	["Kanada"]         = "Kanada",
	["Belgien"]        = "Belgien",
	["Dänemark"]       = "Dänemark",
	["Schweden"]       = "Schweden",
	["Norwegen"]       = "Norwegen",
}

-- "Stadt, Land" am ersten Komma trennen
local function splitCityCountry(s)
	local parts = mw.text.split(s, ",", true)
	local city = trim(parts[1] or "")
	local country = ""
	if #parts >= 2 then
		local rest = {}
		for i = 2, #parts do rest[#rest+1] = parts[i] end
		country = trim(table.concat(rest, ","))
	end
	return city, country
end

function p.render(frame)
	-- Parameter lesen
	local value = frame.args.value
	if (not value or value == "") and frame.getParent then
		local parent = frame:getParent()
		if parent then value = parent.args.value end
	end
	value = value and trim(value) or ""
	if value == "" then return "" end

	local linkMode = (frame.args.link or ""):lower()  -- "page" (default) oder "category"

	-- Parsen & normalisieren
	local city, countryRaw = splitCityCountry(value)
	local countryNorm = nil
	if countryRaw ~= "" then
		countryNorm = countryNormalize[lc(countryRaw)] or countryRaw
	end

	-- Anzeige-Links
	local cityView, countryView
	if linkMode == "category" then
		cityView = (city ~= "") and string.format("[[:Kategorie:Geboren in %s|%s]]", city, city) or ""
		if countryNorm and countryNorm ~= "" then
			local suffix = countryCatSuffix[countryNorm] or countryNorm
			countryView = string.format("[[:Kategorie:Geboren in %s|%s]]", suffix, countryNorm)
		else
			countryView = ""
		end
	else
		cityView    = linkIfExists(city)
		countryView = (countryNorm and linkIfExists(countryNorm)) or ""
	end

	-- Ausgabe zusammensetzen
	local out = {}
	out[#out+1] = cityView
	if countryView ~= "" then
		out[#out+1] = ", " .. countryView
	end

	-- Kategorien setzen
	if city ~= "" then
		out[#out+1] = string.format("\n[[Kategorie:Geboren in %s]]", city)
	end
	if countryNorm and countryNorm ~= "" then
		local suffix = countryCatSuffix[countryNorm] or countryNorm
		out[#out+1] = string.format("\n[[Kategorie:Geboren in %s]]", suffix)
	end

	return table.concat(out)
end

return p