Модуль:Сущность/поля: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Отмена версии 125845, сделанной Pok (обсуждение) Метка: отмена |
Pok (обсуждение | вклад) мНет описания правки Метка: ручная отмена |
||
| (не показаны 2 промежуточные версии этого же участника) | |||
| Строка 31: | Строка 31: | ||
table.insert(meta[base], label) | table.insert(meta[base], label) | ||
seen[base][label] = true | seen[base][label] = true | ||
end | |||
if base == 'cardLabel' or base == 'cardContent' then | |||
meta.card = meta.card or {} | |||
seen.card = seen.card or {} | |||
if not seen.card[label] then | |||
table.insert(meta.card, label) | |||
seen.card[label] = true | |||
end | |||
end | end | ||
end | end | ||
Текущая версия от 16:01, 1 марта 2026
Для документации этого модуля может быть создана страница Модуль:Сущность/поля/doc
local p = {}
local getArgs = require('Module:Arguments').getArgs
local function trim(s)
if not s then return s end
return (s:gsub('^%s*(.-)%s*$', '%1'))
end
local function strip_trailing_digits(s)
return s:gsub('%d+$', '')
end
local function split_first_underscore(k)
local a, b = k:match('^([^_]+)_(.+)$')
return a, b
end
local function collect_labels_from_args(args)
local meta = {}
local seen = {}
for k, v in pairs(args) do
if type(k) == 'string' and k:find('_', 1, true) then
local base, rem = split_first_underscore(k)
if base and rem then
local label = trim(strip_trailing_digits(rem))
if label ~= '' then
meta[base] = meta[base] or {}
if not seen[base] then seen[base] = {} end
if not seen[base][label] then
table.insert(meta[base], label)
seen[base][label] = true
end
if base == 'cardLabel' or base == 'cardContent' then
meta.card = meta.card or {}
seen.card = seen.card or {}
if not seen.card[label] then
table.insert(meta.card, label)
seen.card[label] = true
end
end
end
end
end
end
for k, v in pairs(args) do
if type(k) == 'string' and not k:find('_', 1, true) then
meta[k] = tostring(v)
end
end
return meta
end
local function render_from_args(args)
local field = args[1]
local label = args[2]
field = field and trim(field) or ''
label = label and trim(label) or ''
-- map base -> label -> value
local map = {}
for k, v in pairs(args) do
if type(k) == 'string' and k:find('_', 1, true) then
local base, rem = split_first_underscore(k)
if base and rem then
local lab = strip_trailing_digits(rem)
lab = trim(lab)
map[base] = map[base] or {}
local cur = map[base][lab]
if cur then
map[base][lab] = cur .. '\n' .. tostring(v)
else
map[base][lab] = tostring(v)
end
end
end
end
if field ~= '' and label ~= '' then
local base = map[field]
if base then return base[label] or '' end
return ''
end
if args[1] and args[2] then
local f = trim(args[1]); local l = trim(args[2])
local base = map[f]
if base then return base[l] or '' end
end
return ''
end
function p.main(frame)
local args = getArgs(frame, { removeBlanks = false })
local field = args[1]
if field == "json" then
local json = collect_labels_from_args(args)
return mw.text.jsonEncode(json)
end
return render_from_args(args)
end
return p