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 |
||
| (Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt) | |||
| Zeile 1: | Zeile 1: | ||
-- Modul:Geburtsort | -- 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 | |||
-- 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 | ||
-- | -- 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 | -- Länder-Normalisierung | ||
local countryNormalize = (function() | local countryNormalize = (function() | ||
local | local map = {} | ||
local function add(canonical, variants) | local function add(canonical, variants) | ||
for _, v in ipairs(variants) do | 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 | return map | ||
end)() | end)() | ||
-- Länderkategorie | -- Länderkategorie (mit Beugung nach "Geboren in ") | ||
local countryCatSuffix = { | local countryCatSuffix = { | ||
["Deutschland"] = "Deutschland", | ["Deutschland"] = "Deutschland", | ||
| Zeile 62: | Zeile 60: | ||
} | } | ||
-- | -- "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 | ||
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 | -- 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 | ||
local linkMode = (frame.args.link or ""):lower() -- "page" (default) oder "category" | |||
-- | -- Parsen & normalisieren | ||
local city, countryRaw = splitCityCountry(value) | local city, countryRaw = splitCityCountry(value) | ||
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 | ||
-- 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 | ||
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 | ||
if countryNorm and countryNorm ~= "" then | if countryNorm and countryNorm ~= "" then | ||
local suffix = countryCatSuffix[countryNorm] or countryNorm | local suffix = countryCatSuffix[countryNorm] or countryNorm | ||