Modul:Geburtsort: Unterschied zwischen den Versionen

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
Zeile 1: Zeile 1:
-- 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 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
-- Normalisierung von Ländernamen (Eingabe → kanonischer Name)
local countryNormalize = (function()
local t = {}
local function add(canonical, variants)
for _, v in ipairs(variants) do t[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 t
end)()
-- Länderkategorie-Text nach kanonischem Namen (mit korrekter Beugung)
-- Wert ist der komplette sichtbare Text 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",
}
-- Split "Stadt, Land" (erstes Komma trennt)
local function splitCityCountry(s)
local parts = mw.text.split(s, ",", true)
local city = trim(parts[1] or "")
local country = ""
if #parts >= 2 then
-- alles nach dem ersten Komma wieder zusammenfügen (falls jemand mehr Kommas nutzt)
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)
function p.render(frame)
local value = frame.args.value or frame:getParent() and frame:getParent().args.value
local value = frame.args.value or (frame:getParent() and frame:getParent().args.value)
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
-- Stadt/Land parsen
local city, countryRaw = splitCityCountry(value)
local city, countryRaw = splitCityCountry(value)


-- Anzeige-Strings (mit Autolink)
-- Land normalisieren
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-Links erzeugen
local cityView, countryView
 
if linkMode == "category" then
-- Sichtbare Links direkt auf die Kategorien setzen (mit führendem :)
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
-- Standard: auf Artikelseiten verlinken (nur wenn existieren)
cityView = linkIfExists(city)
countryView = (countryNorm and linkIfExists(countryNorm)) or ""
end


-- Anzeige zusammensetzen
-- Anzeige zusammensetzen
Zeile 103: Zeile 40:
end
end


-- Kategorien
-- Kategorien immer setzen (unsichtbar im Fließtext)
-- 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
Zeile 117: Zeile 51:
return table.concat(out)
return table.concat(out)
end
end
return p