Модуль:Песочница/Pok: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 74: | Строка 74: | ||
if not ok then return nil end | if not ok then return nil end | ||
return content | return content | ||
end | |||
local function extract_rhs(templateContent, swName, matchKey) | |||
if not templateContent then return nil end | |||
local lower = templateContent:lower() | |||
local pos = lower:find("|%s*" .. swName:lower() .. "%s*=") | |||
if not pos then return nil end | |||
local innerStart = templateContent:find("{{", pos) | |||
if not innerStart then return nil end | |||
local len = #templateContent | |||
local i = innerStart | |||
local depth = 0 | |||
local regionEnd = nil | |||
while i <= len - 1 do | |||
local two = templateContent:sub(i, i+1) | |||
if two == "{{" then depth = depth + 1; i = i + 2 | |||
elseif two == "}}" then depth = depth - 1; i = i + 2 | |||
if depth == 0 then regionEnd = i - 1; break end | |||
else i = i + 1 end | |||
end | |||
local region = templateContent:sub(innerStart, (regionEnd or #templateContent)) | |||
local esc = matchKey:gsub("([^%w])", "%%%1") | |||
local pattern = "|%s*" .. esc .. "%s*=%s*([^|%}]+)" | |||
local rhs = region:match(pattern) | |||
if rhs then return trim(rhs) end | |||
return nil | |||
end | end | ||
| Строка 188: | Строка 214: | ||
extra = dp.flattenComponent({ args = { id, tplPath } }) | extra = dp.flattenComponent({ args = { id, tplPath } }) | ||
end | end | ||
local dataMap = {} | local dataMap = {} | ||
if extra and extra ~= "" then | if extra and extra ~= "" then | ||
| Строка 194: | Строка 219: | ||
local k, v = pair:match("^([^=]+)=(.*)$") | local k, v = pair:match("^([^=]+)=(.*)$") | ||
if k and v then | if k and v then | ||
dataMap[k] = mw.text.decode(v) | dataMap[mw.text.decode(k)] = mw.text.decode(v) | ||
end | end | ||
end | end | ||
end | end | ||
local rhs = extract_rhs(content, sw, key) | local rhs = extract_rhs(content, sw, key) | ||
| Строка 227: | Строка 227: | ||
local rendered = rhs | local rendered = rhs | ||
for k, v in pairs(dataMap) do | for k, v in pairs(dataMap) do | ||
rendered = rendered:gsub("{{{" .. | local esc = k:gsub("([^%w])", "%%%1") | ||
rendered = rendered:gsub("{{{" .. | rendered = rendered:gsub("{{{" .. esc .. "}}}", v) | ||
rendered = rendered:gsub("{{{" .. esc .. "|[^}]-}}}", v) | |||
end | end | ||
table.insert(switchKeyToTemplates[sw][key], trim(rendered)) | table.insert(switchKeyToTemplates[sw][key], trim(rendered)) | ||