Modul:Person
Aus Star Citizen Wiki
Weitere Optionen
Diese Dokumentation wird von Modul:Person/doc aus übernommen. Änderungen können auf der Diskussionsseite vorgeschlagen werden.
| Function list |
|---|
| L 16 — splitList L 21 — flush L 58 — collectList L 84 — linkify L 96 — plainName L 109 — item L 118 — pack L 134 — cleanDate L 147 — getTitle L 161 — Person.infoBox |
Dieses Modul setzt die Vorlage:Person um. Anweisungen zur Verwendung findest du auf der Vorlagenseite.
require('strict')
--- Personen-Infobox für Artikel über Charaktere; Aufrufer: Vorlage:Person
--- ({{#invoke:Person|infoBox}}). Listen-Argumente (employer, occupation,
--- affiliation, related) sind kommagetrennt; nummerierte Alt-Argumente
--- (employer1, occupation2, ...) werden als Fallback weiter gelesen.
local Person = {}
local common = require('Module:Common')
--- Teilt eine kommaseparerte Liste; Kommas innerhalb von [[...]]-Links trennen nicht.
---
--- @param value string
--- @return string[]
local function splitList(value)
local out = {}
local buf = {}
local depth = 0
local function flush()
local entry = table.concat(buf):gsub('^%s*(.-)%s*$', '%1')
if entry ~= '' then
table.insert(out, entry)
end
buf = {}
end
local i, n = 1, mw.ustring.len(value)
while i <= n do
local c = mw.ustring.sub(value, i, i)
if c == '[' then
depth = depth + 1
table.insert(buf, c)
elseif c == ']' then
depth = depth - 1
table.insert(buf, c)
elseif c == ',' and depth == 0 then
flush()
else
table.insert(buf, c)
end
i = i + 1
end
flush()
return out
end
--- Liest ein Listen-Argument: kommagetrennter Wert, sonst nummerierte
--- Alt-Argumente (key1, key2, ...) in Reihenfolge.
---
--- @param args table
--- @param key string
--- @return string[]
local function collectList(args, key)
local value = args[key]
if value ~= nil and value ~= '' then
return splitList(value)
end
local out = {}
local i = 1
while args[key .. i] ~= nil do
local entry = args[key .. i]
if entry ~= nil and entry ~= '' then
for _, v in ipairs(splitList(entry)) do
table.insert(out, v)
end
end
i = i + 1
end
return out
end
--- Wickelt nur nackte Namen in Links; bereits verlinkte Einträge bleiben unverändert.
---
--- @param entry string
--- @return string
local function linkify(entry)
if mw.ustring.sub(entry, 1, 2) == '[[' then
return entry
end
return '[[' .. entry .. ']]'
end
--- SMW speichert wie bisher nackte Namen; aus Links wird das Ziel extrahiert.
---
--- @param entry string
--- @return string
local function plainName(entry)
if mw.ustring.sub(entry, 1, 2) == '[[' then
return mw.ustring.match(entry, '^%[%[([^|%]]+)')
end
return entry
end
--- Zeile nur bei vorhandenem Wert; hält die items-Arrays lückenlos (pack).
---
--- @param label string
--- @param content string|nil
--- @return table|nil
local function item(label, content)
if content == nil or content == '' then
return nil
end
return { label = label, content = content }
end
--- Lässt nil-Lücken weg, damit ipairs im Renderer nicht abbricht.
local function pack(...)
local out = {}
for i = 1, select('#', ...) do
local it = select(i, ...)
if it ~= nil then
table.insert(out, it)
end
end
return out
end
--- 'unbekannt' (Groß-/Kleinschreibung egal) ist kein Datum.
---
--- @param value string|nil
--- @return string|nil
local function cleanDate(value)
if value ~= nil and mw.ustring.lower(value) == 'unbekannt' then
return nil
end
return value
end
--- Erstellt den Kasten-Titel aus Titel-Argument (inkl. deutschem Fallback)
--- und Seitennamen.
---
--- @param title string|nil
--- @return string
local function getTitle(title)
local subpage = mw.title.getCurrentTitle().subpageText
if title ~= nil and title ~= '' then
return title .. ' ' .. subpage
end
return subpage
end
--- Personen-Infobox rendern.
---
--- @param frame table
--- @return string
function Person.infoBox(frame)
local infobox = require('Module:InfoboxLua')
local args = require('Module:Arguments').getArgs(frame)
local occupations = collectList(args, 'occupation')
local affiliations = collectList(args, 'affiliation')
local employers = collectList(args, 'employer')
local related = collectList(args, 'related')
local geburtsdatum = cleanDate(args['born'] or args['Geburtsdatum'])
local todesdatum = cleanDate(args['died'] or args['Todesdatum'])
local smwData = {
['Titel'] = args['title'] or args['Titel'],
['Volk'] = args['species'] or args['Spezies'],
['foaf:name'] = args['name'] or args['Name'],
['Geschlecht'] = args['gender'] or args['Geschlecht'],
['Geburtsdatum'] = geburtsdatum,
['Todesdatum'] = todesdatum,
}
if #related > 0 then
local known = {}
for _, person in ipairs(related) do
table.insert(known, plainName(person))
end
smwData['foaf:knows'] = known
end
if #occupations > 0 then
smwData['Beruf'] = occupations
end
if #affiliations > 0 then
smwData['Zugehörigkeit'] = affiliations
end
if #employers > 0 then
smwData['Arbeitgeber'] = employers
end
common.checkSmwResult(mw.smw.set(smwData))
-- Fehlende Skalarwerte entfallen: Der alte Kasten renderte '-' nur,
-- um es per removeEmpty sofort zu entfernen (sparsame Box).
local information = pack(
item('Titel', smwData['Titel']),
item('Name', smwData['foaf:name']),
item('Geschlecht', smwData['Geschlecht']),
item('Geboren', geburtsdatum),
item('Gestorben', todesdatum),
item('Spezies', smwData['Volk'])
)
if #employers > 0 then
table.insert(information, { label = 'Arbeitgeber', content = table.concat(employers, '<br>') })
end
if #affiliations > 0 then
table.insert(information, { label = 'Zugehörigkeit', content = table.concat(affiliations, '<br>') })
end
local steckbrief = {}
if #occupations > 0 then
table.insert(steckbrief, { label = 'Beruf', content = table.concat(occupations, '<br>') })
end
if #related > 0 then
local persons = {}
for _, person in ipairs(related) do
table.insert(persons, linkify(person))
end
table.insert(steckbrief, { label = 'Familie / Bekannte', content = table.concat(persons, '<br>') })
end
local sections = {
{
columns = 2,
items = information,
},
}
if #steckbrief > 0 then
table.insert(sections, { items = steckbrief })
end
local categories = { '[[Kategorie:Persönlichkeit]]' }
if todesdatum ~= nil then
table.insert(categories, '[[Kategorie:Verstorbene Persönlichkeit]]')
end
if smwData['Volk'] ~= nil then
table.insert(categories, '[[Kategorie:' .. smwData['Volk'] .. ']]')
end
return infobox.render({
title = getTitle(smwData['Titel']),
image = args['image'],
sections = sections,
}) .. table.concat(categories, '')
end
return Person
