|
|
| (не показано 29 промежуточных версий этого же участника) |
| Строка 1: |
Строка 1: |
| local p = {} | | local p = {} |
| | local getArgs = require('Module:Arguments').getArgs |
|
| |
|
| local function trim(s) | | function p.main(frame) |
| if not s then return s end
| | local args = getArgs(frame, { removeBlanks = false }) |
| return (s:gsub("^%s*(.-)%s*$", "%1"))
| | local name = args[1] or "" |
| end
| | local attributes = args[2] or "" |
| | if name == "" then |
| | return "<span class=\"error\">Ошибка: не указано имя файла.</span>" |
| | end |
| | local ext = (args["ext"] or "png"):gsub("^%.", "") |
| | local namespace = args["namespace"] or "Файл" |
| | local max = tonumber(args["max"]) or 50 |
| | local include_base = (args["base"] ~= "no") |
|
| |
|
| local function parse_keys_from_template(content, switches) | | local found = {} |
| if not content then return {} end
| |
| local lower = content:lower()
| |
| local result = {}
| |
| for _, sw in ipairs(switches) do
| |
| result[sw] = {}
| |
| local swLower = sw:lower()
| |
| local pos = lower:find("|%s*" .. swLower .. "%s*=")
| |
| if not pos then
| |
| -- no keys for this switch
| |
| else
| |
| local startBrace = content:find("{{", pos)
| |
| local region = nil
| |
| if startBrace then
| |
| local len = #content
| |
| local i = startBrace
| |
| local depth = 0
| |
| while i <= len - 1 do
| |
| local two = content: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
| |
| region = content:sub(startBrace, i-1)
| |
| break
| |
| end
| |
| else
| |
| i = i + 1
| |
| end
| |
| end
| |
| end
| |
| if not region then
| |
| local substr = content:sub(pos)
| |
| local endPos = substr:find("}}")
| |
| if endPos then
| |
| region = substr:sub(1, endPos)
| |
| else
| |
| region = substr
| |
| end
| |
| end
| |
| for key in string.gmatch(region, "|%s*([^=|%}]-)%s*=") do
| |
| local k = trim(key)
| |
| if k ~= "" then
| |
| table.insert(result[sw], k)
| |
| end
| |
| end
| |
| end
| |
| end
| |
| return result
| |
| end
| |
|
| |
|
| local function load_module_data(page)
| | if include_base then |
| local baseUser = "IanComradeBot/"
| | local t = mw.title.new("Файл:" .. name .. "." .. ext) |
| local moduleName = "Module:" .. baseUser .. page .. "/data"
| | if t and t.exists then |
| local ok, data = pcall(mw.loadData, moduleName)
| | table.insert(found, "") |
| if not ok then return nil end
| | end |
| return data
| | end |
| end | |
|
| |
|
| local function load_template_content(path)
| | for i = 1, max do |
| local title = mw.title.new("Template:" .. path)
| | local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext) |
| if not title then return nil end
| | if t and t.exists then |
| local ok, content = pcall(function() return title:getContent() end)
| | table.insert(found, "-" .. i) |
| if not ok then return nil end
| | end |
| return content
| | end |
| end | |
|
| |
|
| function p.get(frame)
| | if #found == 0 then |
| local args = frame.args or {}
| | return "" |
| local id = args[1] or ""
| | end |
| if id == "" then return "" end
| |
|
| |
|
| local componentDefs = load_module_data("component.json")
| | local before = "[[" .. namespace .. ":" .. name |
| local prototypeDefs = load_module_data("prototype.json")
| | local after = "." .. ext .. "|" .. attributes .. "]]" |
| if not componentDefs or not prototypeDefs then
| |
| return ""
| |
| end
| |
|
| |
|
| local foundComponents = {}
| | local parts = {} |
| local foundPrototypes = {}
| | table.insert(parts, "<choose before=\"" .. before .. "\" after=\"" .. after .. "\">") |
| local compList = componentDefs[id]
| | for _, suf in ipairs(found) do |
| if type(compList) == "table" then
| | table.insert(parts, "<option>" .. suf .. "</option>") |
| for _, v in ipairs(compList) do
| | end |
| if type(v) == "string" then
| | table.insert(parts, "</choose>") |
| foundComponents[v] = true
| |
| end
| |
| end
| |
| end
| |
|
| |
|
| local protoList = prototypeDefs[id]
| | return frame:preprocess(table.concat(parts, "\n")) |
| if type(protoList) == "table" then
| |
| for _, v in ipairs(protoList) do
| |
| if type(v) == "string" then
| |
| foundPrototypes[v] = true
| |
| end
| |
| end
| |
| end
| |
| | |
| for name in string.gmatch(id, "[^,]+") do
| |
| local n = trim(name)
| |
| if n ~= "" then
| |
| if componentDefs[n] ~= nil then
| |
| foundComponents[n] = true
| |
| end
| |
| if prototypeDefs[n] ~= nil then
| |
| foundPrototypes[n] = true
| |
| end
| |
| if componentDefs[n] == nil and prototypeDefs[n] == nil then
| |
| foundComponents[n] = true
| |
| end
| |
| end
| |
| end
| |
| | |
| local out = {}
| |
| local errors = {}
| |
| local switches = { "card", "title" }
| |
| local switchKeyOrder = {}
| |
| local switchKeyToTemplates = {}
| |
| for _, sw in ipairs(switches) do
| |
| switchKeyOrder[sw] = {}
| |
| switchKeyToTemplates[sw] = {}
| |
| end
| |
| local switchConfigs = {
| |
| card = {
| |
| wrapper = function(key, tplCalls)
| |
| if not tplCalls or #tplCalls == 0 then return "" end
| |
| return "{{card|" .. mw.text.encode(key) .. "|" .. table.concat(tplCalls, " ") .. "}}"
| |
| end
| |
| },
| |
| title = {
| |
| wrapper = function(key, tplCalls)
| |
| local parts = {}
| |
| table.insert(parts, "<h2>" .. mw.text.encode(key) .. "</h2>")
| |
| if tplCalls and #tplCalls > 0 then
| |
| for _, tpl in ipairs(tplCalls) do
| |
| table.insert(parts, "<p>" .. tpl .. "</p>")
| |
| end
| |
| end
| |
| return table.concat(parts, "\n")
| |
| end
| |
| }
| |
| }
| |
| | |
| local function lcfirst(s)
| |
| if not s or s == "" then return s end
| |
| return string.lower(s:sub(1,1)) .. (s:sub(2) or "")
| |
| end
| |
| | |
| local items = {}
| |
| for compName,_ in pairs(foundComponents) do
| |
| table.insert(items, { kind = "component", name = compName })
| |
| end
| |
| for protoName,_ in pairs(foundPrototypes) do
| |
| table.insert(items, { kind = "prototype", name = protoName })
| |
| end
| |
| | |
| for _, item in ipairs(items) do
| |
| local kind = item.kind
| |
| local name = item.name
| |
| local pathName = lcfirst(name)
| |
| local tplPath = kind .. "/" .. pathName
| |
| local content = load_template_content(tplPath)
| |
| if not content then
| |
| table.insert(errors, "Ошибка: не найден шаблон " .. kind .. "/" .. pathName)
| |
| else
| |
| local parsed = parse_keys_from_template(content, switches)
| |
| for _, sw in ipairs(switches) do
| |
| local keys = parsed[sw] or {}
| |
| for _, key in ipairs(keys) do
| |
| if not switchKeyToTemplates[sw][key] then
| |
| switchKeyToTemplates[sw][key] = {}
| |
| table.insert(switchKeyOrder[sw], key)
| |
| end
| |
| local param = sw
| |
| local extra = ""
| |
| local ok, dp = pcall(require, "Module:Песочница/Pok/2")
| |
| if ok and dp and dp.flattenComponent then
| |
| extra = dp.flattenComponent({ args = { id, tplPath } })
| |
| end
| |
| -- build data map from extra (format: k=val|k2=val2)
| |
| local dataMap = {}
| |
| if extra and extra ~= "" then
| |
| 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
| |
| -- try to extract RHS for this key from the template content and substitute placeholders
| |
| 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
| |
| 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 rhs end
| |
| return nil
| |
| end
| |
| local rhs = extract_rhs(content, sw, key)
| |
| if rhs then
| |
| local rendered = rhs
| |
| for k, v in pairs(dataMap) do
| |
| rendered = rendered:gsub("{{{" .. k .. "}}}", v)
| |
| rendered = rendered:gsub("{{{" .. k .. "|[^}]-}}}", 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
| |
| end
| |
| end
| |
| end
| |
| | |
| for _, e in ipairs(errors) do
| |
| table.insert(out, "<div class=\"error\">" .. mw.text.encode(e) .. "</div>")
| |
| end
| |
| | |
| for _, sw in ipairs(switches) do
| |
| local cfg = switchConfigs[sw] or {}
| |
| for _, key in ipairs(switchKeyOrder[sw]) do
| |
| local tplCalls = switchKeyToTemplates[sw][key] or {}
| |
| if cfg.wrapper then
| |
| local outStr = cfg.wrapper(key, tplCalls)
| |
| if outStr and outStr ~= "" then
| |
| table.insert(out, outStr)
| |
| end
| |
| end
| |
| end
| |
| end
| |
| | |
| return frame:preprocess(table.concat(out, "\n\n"))
| |
| end | | end |
|
| |
|
| return p | | return p |