Модуль:Песочница/Pok: различия между версиями

Нет описания правки
мНет описания правки
Строка 182: Строка 182:
table.insert(switchKeyOrder[sw], key)
table.insert(switchKeyOrder[sw], key)
end
end
local param = sw
-- try to extract RHS for this key from template and replace placeholders with data
local ok, dp = pcall(require, "Module:Песочница/Pok/2")
local extra = ""
local extra = ""
local ok, dp = pcall(require, "Module:Песочница/Pok/2")
if ok and dp and dp.flattenComponent then
if ok and dp and dp.flattenComponent then
extra = dp.flattenComponent({ args = { id, tplPath } })
extra = dp.flattenComponent({ args = { id, tplPath } })
end
end
local tplStr = "{{" .. tplPath .. "|" .. param .. "|" .. key
local dataMap = {}
if extra and extra ~= "" then
if extra and extra ~= "" then
tplStr = tplStr .. "|" .. extra
for pair in string.gmatch(extra, "([^|]+)") do
local k, v = pair:match("^([^=]+)=(.*)$")
if k and v then
dataMap[k] = mw.text.decode(v)
end
end
end
-- extract RHS for this key from the inner switch block
local function extract_rhs(templateContent, swName, matchKey)
local lower = templateContent:lower()
local pos = lower:find("|%s*" .. swName:lower() .. "%s*=")
if not pos then return nil end
-- find inner switch start after pos
local innerStart = templateContent:find("{{", pos)
if not innerStart then return nil end
-- find inner switch end (simple heuristic: find matching '}}' for the first '{{')
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))
-- find the RHS for the given key
local pattern = "|%s*" .. mw.ustring.gsub(matchKey, "([^%w])", "%%%1") .. "%s*=%s*([^|%}]+)"
local rhs = region:match(pattern)
if rhs then return rhs end
return nil
end
local rhs = extract_rhs(content, sw, key)
if rhs then
-- replace placeholders {{{k}}} and {{{k|...}}} with values from dataMap
local rendered = rhs
for k, v in pairs(dataMap) do
-- exact match {{{k}}}
rendered = rendered:gsub("{{{" .. k .. "}}}", v)
-- with default {{{k|...}}}
rendered = rendered:gsub("{{{" .. k .. "|[^}]-}}}", v)
end
table.insert(switchKeyToTemplates[sw][key], trim(rendered))
else
local param = sw
local tplStr = "{{" .. tplPath .. "|" .. param .. "|" .. key
if extra and extra ~= "" then
tplStr = tplStr .. "|" .. extra
end
tplStr = tplStr .. "}}"
table.insert(switchKeyToTemplates[sw][key], tplStr)
end
end
tplStr = tplStr .. "}}"
table.insert(switchKeyToTemplates[sw][key], tplStr)
end
end
end
end