Модуль:Сущность/поля: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 55: | Строка 55: | ||
end | end | ||
local function | local function get_raw_arg(sources, key) | ||
local field = | for _, source in ipairs(sources) do | ||
local label = | if source and source[key] ~= nil then | ||
return source[key] | |||
end | |||
end | |||
return nil | |||
end | |||
local function each_raw_compound_arg(sources, fn) | |||
for _, source in ipairs(sources) do | |||
if source then | |||
for k in pairs(source) do | |||
if type(k) == 'string' and k:find('_', 1, true) then | |||
local base, rem = split_first_underscore(k) | |||
if base and rem then | |||
fn(source, k, base, rem) | |||
end | |||
end | |||
end | |||
end | |||
end | |||
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 '' | field = field and trim(field) or '' | ||
label = label and trim(label) or '' | label = label and trim(label) or '' | ||
if field ~= '' and label ~= '' then | if field ~= '' and label ~= '' then | ||
local | local result = nil | ||
if base then | each_raw_compound_arg(sources, function(source, key, base, rem) | ||
if base == field and trim(strip_trailing_digits(rem)) == label then | |||
local value = tostring(source[key]) | |||
if result then | |||
result = result .. '\n' .. value | |||
else | |||
result = value | |||
end | |||
end | |||
end) | |||
if result then return result end | |||
end | end | ||
if field ~= '' | if field ~= '' then | ||
local value = get_raw_arg(sources, field) | |||
if value ~= nil then | |||
return trim(tostring(value)) | |||
end | |||
end | end | ||
| Строка 87: | Строка 113: | ||
function p.main(frame) | function p.main(frame) | ||
local | local field = frame.args[1] | ||
if field == "json" then | if field == "json" then | ||
local args = getArgs(frame, { removeBlanks = false }) | |||
local json = collect_labels_from_args(args) | local json = collect_labels_from_args(args) | ||
return mw.text.jsonEncode(json) | return mw.text.jsonEncode(json) | ||
end | end | ||
return | return render_from_frame(frame) | ||
end | end | ||
return p | return p | ||
Версия от 18:32, 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 each_raw_compound_arg(sources, fn)
for _, source in ipairs(sources) do
if source then
for k in pairs(source) do
if type(k) == 'string' and k:find('_', 1, true) then
local base, rem = split_first_underscore(k)
if base and rem then
fn(source, k, base, rem)
end
end
end
end
end
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 result = nil
each_raw_compound_arg(sources, function(source, key, base, rem)
if base == field and trim(strip_trailing_digits(rem)) == label then
local value = tostring(source[key])
if result then
result = result .. '\n' .. value
else
result = value
end
end
end)
if result then return result 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 field = frame.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