Modul:Geburtsort: Unterschied zwischen den Versionen

Zur Navigation springen Zur Suche springen
Die Seite wurde neu angelegt: „-- Modul:Geburtsort -- Verarbeitet ein Feld "Stadt, Land" zu: -- Anzeige: Stadt[, Land] (beides automatisch verlinkt, falls Seite existiert) -- Kategorien: [[Kategorie:Geboren in <Stadt>]] + [[Kategorie:Geboren in <Land (gebeugt)>]] -- Benutzung in Vorlage: {{#invoke:Geburtsort|render|value={{{Geburtsort|}}}}} local p = {} local trim = mw.text.trim local lc = mw.ustring.lower -- Helper: Link, wenn Seite existiert, sonst Plaintext local function linkI…“
 
Keine Bearbeitungszusammenfassung
 
(Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt)
Zeile 1: Zeile 1:
-- Modul:Geburtsort
-- Modul:Geburtsort
-- Verarbeitet ein Feld "Stadt, Land" zu:
-- Benutzung in Vorlage: {{#invoke:Geburtsort|render|value={{{Geburtsort}}}|link=category}}
-- Anzeige: Stadt[, Land] (beides automatisch verlinkt, falls Seite existiert)
-- link=page (Standard) → auf Artikelseiten verlinken
-- Kategorien: [[Kategorie:Geboren in <Stadt>]] + [[Kategorie:Geboren in <Land (gebeugt)>]]
-- link=category        → sichtbare Links zeigen direkt auf die Kategorien
-- Benutzung in Vorlage: {{#invoke:Geburtsort|render|value={{{Geburtsort|}}}}}


local p = {}
local p = {}
Zeile 10: Zeile 9:
local lc  = mw.ustring.lower
local lc  = mw.ustring.lower


-- Helper: Link, wenn Seite existiert, sonst Plaintext
-- Link, wenn Seite existiert; sonst Plaintext
local function linkIfExists(titleText)
local function linkIfExists(titleText)
if not titleText or titleText == "" then return "" end
if not titleText or titleText == "" then return "" end
Zeile 20: Zeile 19:
end
end


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


-- Länderkategorie-Text nach kanonischem Namen (mit korrekter Beugung)
-- Länderkategorie (mit Beugung nach "Geboren in ")
-- Wert ist der komplette sichtbare Text NACH "Geboren in "
local countryCatSuffix = {
local countryCatSuffix = {
["Deutschland"]    = "Deutschland",
["Deutschland"]    = "Deutschland",
Zeile 62: Zeile 60:
}
}


-- Split "Stadt, Land" (erstes Komma trennt)
-- "Stadt, Land" am ersten Komma trennen
local function splitCityCountry(s)
local function splitCityCountry(s)
local parts = mw.text.split(s, ",", true)
local parts = mw.text.split(s, ",", true)
Zeile 68: Zeile 66:
local country = ""
local country = ""
if #parts >= 2 then
if #parts >= 2 then
-- alles nach dem ersten Komma wieder zusammenfügen (falls jemand mehr Kommas nutzt)
local rest = {}
local rest = {}
for i = 2, #parts do rest[#rest+1] = parts[i] end
for i = 2, #parts do rest[#rest+1] = parts[i] end
Zeile 77: Zeile 74:


function p.render(frame)
function p.render(frame)
local value = frame.args.value or frame:getParent() and frame:getParent().args.value
-- 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 ""
value = value and trim(value) or ""
if value == "" then
if value == "" then return "" end
return "" -- Nichts anzeigen, wenn leer
 
end
local linkMode = (frame.args.link or ""):lower()  -- "page" (default) oder "category"


-- Stadt/Land parsen
-- Parsen & normalisieren
local city, countryRaw = splitCityCountry(value)
local city, countryRaw = splitCityCountry(value)
-- Anzeige-Strings (mit Autolink)
local cityView = linkIfExists(city)
-- Land normalisieren + verlinkte Anzeige
local countryNorm = nil
local countryNorm = nil
if countryRaw ~= "" then
if countryRaw ~= "" then
countryNorm = countryNormalize[lc(countryRaw)] or countryRaw
countryNorm = countryNormalize[lc(countryRaw)] or countryRaw
end
end
local countryView = countryNorm and linkIfExists(countryNorm) or ""


-- Anzeige zusammensetzen
-- 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 = {}
local out = {}
out[#out+1] = cityView
out[#out+1] = cityView
Zeile 103: Zeile 114:
end
end


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

Aktuelle Version vom 4. Oktober 2025, 09:49 Uhr

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