Модуль:Песочница/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


Строка 212: Строка 186:
local ok, dp = pcall(require, "Module:Песочница/Pok/2")
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 } })
local dataPage = kind .. "/" .. pathName .. ".json"
extra = dp.flattenComponent({ args = { id, dataPage } })
end
end
local dataMap = {}
local tplStr = "{{" .. tplPath .. "|" .. param .. "|" .. key
if extra and extra ~= "" then
if extra and extra ~= "" then
for pair in string.gmatch(extra, "([^|]+)") do
tplStr = tplStr .. "|" .. extra
local k, v = pair:match("^([^=]+)=(.*)$")
if k and v then
dataMap[mw.text.decode(k)] = mw.text.decode(v)
end
end
end
local rhs = extract_rhs(content, sw, key)
if rhs then
local rendered = rhs
for k, v in pairs(dataMap) do
local esc = k:gsub("([^%w])", "%%%1")
rendered = rendered:gsub("{{{" .. esc .. "}}}", v)
rendered = rendered:gsub("{{{" .. esc .. "|[^}]-}}}", v)
end
table.insert(switchKeyToTemplates[sw][key], trim(rendered))
else
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