Модуль:Сущность/поля: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 62: | Строка 62: | ||
end | end | ||
return nil | return nil | ||
end | end | ||
| Строка 88: | Строка 73: | ||
if field ~= '' and label ~= '' then | if field ~= '' and label ~= '' then | ||
local | local value = get_raw_arg(sources, field .. '_' .. label) | ||
if value ~= nil then | |||
return tostring(value) | |||
end | |||
end | end | ||
Текущая версия от 18:38, 17 июня 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 pos = k:find('_', 1, true)
if not pos or pos == 1 or pos == #k then
return nil, nil
end
return k:sub(1, pos - 1), k:sub(pos + 1)
end
local function each_compound_arg(args, fn)
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
fn(base, rem, v)
end
end
end
end
local function collect_labels_from_args(args)
local meta = {}
local seen = {}
each_compound_arg(args, function(base, rem)
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
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 get_raw_arg(sources, key)
for _, source in ipairs(sources) do
if source and source[key] ~= nil then
return source[key]
end
end
return nil
end
local function render_from_frame(frame)
local parent = frame:getParent()
local sources = { frame.args, parent and parent.args }
local field = get_raw_arg(sources, 1)
local label = get_raw_arg(sources, 2)
field = field and trim(field) or ''
label = label and trim(label) or ''
if field ~= '' and label ~= '' then
local value = get_raw_arg(sources, field .. '_' .. label)
if value ~= nil then
return tostring(value)
end
end
if field ~= '' then
local value = get_raw_arg(sources, field)
if value ~= nil then
return trim(tostring(value))
end
end
return ''
end
function p.main(frame)
local parent = frame:getParent()
local field = get_raw_arg({ frame.args, parent and parent.args }, 1)
if field == "json" then
local args = getArgs(frame, { removeBlanks = false })
local json = collect_labels_from_args(args)
return mw.text.jsonEncode(json)
end
return render_from_frame(frame)
end
return p