Модуль:Сущность/поля: различия между версиями
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 | ||