Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 201: | Строка 201: | ||
return entry[string.upper(first) .. tail] | return entry[string.upper(first) .. tail] | ||
end | |||
local function apply_pattern(s, pattern, repl) | |||
if not pattern or pattern == "" or not s then | |||
return s | |||
end | |||
local text = tostring(s) | |||
local replacement | |||
if repl and repl ~= "" then | |||
replacement = tostring(repl) | |||
replacement = replacement:gsub("\\(%d)", "%%%1") | |||
else | |||
replacement = "%1" | |||
end | |||
local patt = pattern | |||
if not patt:find("%^") and not patt:find("%$") then | |||
patt = "^" .. patt .. "$" | |||
end | |||
return (text:gsub(patt, replacement)) | |||
end | end | ||
| Строка 531: | Строка 553: | ||
local args = frame.args or {} | local args = frame.args or {} | ||
local pagePath = args[1] or "" | local pagePath = args[1] or "" | ||
local replace = mw.text.unstripNoWiki(args.replace or "") | |||
local pattern = mw.text.unstripNoWiki(args.pattern or "(.*)") | |||
if pagePath == "" then | if pagePath == "" then | ||
| Строка 553: | Строка 577: | ||
table.sort(ids) | table.sort(ids) | ||
if replace ~= "" then | |||
local out = {} | |||
for _, id in ipairs(ids) do | |||
local text = apply_pattern(id, pattern, replace) | |||
if text ~= "" then | |||
out[#out + 1] = text | |||
end | |||
end | |||
if #out == 0 then | |||
return "" | |||
end | |||
return preprocess_or_return(frame, table.concat(out, "\n")) | |||
end | |||
local ok, json = pcall(mw.text.jsonEncode, ids) | local ok, json = pcall(mw.text.jsonEncode, ids) | ||