|
|
| (не показано 15 промежуточных версий этого же участника) |
| Строка 1: |
Строка 1: |
| local p = {}
| |
|
| |
|
| local function trim(s)
| |
| if not s then return s end
| |
| return (s:gsub("^%s*(.-)%s*$", "%1"))
| |
| end
| |
|
| |
| local function split(s, sep)
| |
| if s == nil then return {} end
| |
| local parts = {}
| |
| sep = sep or "%."
| |
| for part in string.gmatch(s, "([^" .. sep .. "]+)") do
| |
| table.insert(parts, part)
| |
| end
| |
| return parts
| |
| end
| |
|
| |
| local function parse_keys_from_template(content)
| |
| if not content then return {} end
| |
| local keys = {}
| |
| local titlePos = string.find(content, "|%s*title%s*=")
| |
| if not titlePos then
| |
| return keys
| |
| end
| |
| local substr = content:sub(titlePos)
| |
| local endPos = substr:find("}}")
| |
| local region = substr
| |
| if endPos then
| |
| region = substr:sub(1, endPos)
| |
| end
| |
| for key in string.gmatch(region, "|%s*([^=|%}]-)%s*=") do
| |
| local k = trim(key)
| |
| if k ~= "" then
| |
| table.insert(keys, k)
| |
| end
| |
| end
| |
| return keys
| |
| end
| |
|
| |
| local function load_module_data(page)
| |
| local baseUser = "IanComradeBot/"
| |
| local moduleName = "Module:" .. baseUser .. page .. "/data"
| |
| local ok, data = pcall(mw.loadData, moduleName)
| |
| if not ok then return nil end
| |
| return data
| |
| end
| |
|
| |
| local function load_template_content(path)
| |
| local title = mw.title.new("Template:" .. path)
| |
| if not title then return nil end
| |
| local ok, content = pcall(function() return title:getContent() end)
| |
| if not ok then return nil end
| |
| return content
| |
| end
| |
|
| |
| function p.get(frame)
| |
| local args = frame.args or {}
| |
| local id = args[1] or ""
| |
| if id == "" then return "" end
| |
|
| |
| local componentDefs = load_module_data("component.json")
| |
| local prototypeDefs = load_module_data("prototype.json")
| |
| if not componentDefs or not prototypeDefs then
| |
| return ""
| |
| end
| |
|
| |
| local foundComponents = {}
| |
| local foundPrototypes = {}
| |
| -- If the component.json contains a list for this id, use it (entity -> components)
| |
| local compList = componentDefs[id]
| |
| if type(compList) == "table" then
| |
| for _, v in ipairs(compList) do
| |
| if type(v) == "string" then
| |
| foundComponents[v] = true
| |
| end
| |
| end
| |
| end
| |
|
| |
| -- If the prototype.json contains a list for this id, use it (entity -> prototypes)
| |
| local protoList = prototypeDefs[id]
| |
| if type(protoList) == "table" then
| |
| for _, v in ipairs(protoList) do
| |
| if type(v) == "string" then
| |
| foundPrototypes[v] = true
| |
| end
| |
| end
| |
| end
| |
|
| |
| -- Also support passing comma-separated component/prototype names directly
| |
| for name in string.gmatch(id, "[^,]+") do
| |
| local n = trim(name)
| |
| if n ~= "" then
| |
| -- if it's a known component/prototype name, include it
| |
| if componentDefs[n] ~= nil then
| |
| foundComponents[n] = true
| |
| end
| |
| if prototypeDefs[n] ~= nil then
| |
| foundPrototypes[n] = true
| |
| end
| |
| -- also allow using the name directly even if not present in defs
| |
| -- treat it as a component name
| |
| if componentDefs[n] == nil and prototypeDefs[n] == nil then
| |
| foundComponents[n] = true
| |
| end
| |
| end
| |
| end
| |
|
| |
| local out = {}
| |
| local errors = {}
| |
| local keyOrder = {}
| |
| local keyToTemplates = {}
| |
|
| |
| 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
| |
|
| |
| for compName,_ in pairs(foundComponents) do
| |
| local compPathName = lcfirst(compName)
| |
| local tplPath = "component/" .. compPathName
| |
| local content = load_template_content(tplPath)
| |
| if not content then
| |
| table.insert(errors, "Ошибка: не найден шаблон component/" .. compPathName)
| |
| else
| |
| local keys = parse_keys_from_template(content)
| |
| for _, key in ipairs(keys) do
| |
| if not keyToTemplates[key] then
| |
| keyToTemplates[key] = {}
| |
| table.insert(keyOrder, key)
| |
| end
| |
| table.insert(keyToTemplates[key], "{{" .. tplPath .. "|title|" .. key .. "}}")
| |
| end
| |
| end
| |
| end
| |
|
| |
| for protoName,_ in pairs(foundPrototypes) do
| |
| local protoPathName = lcfirst(protoName)
| |
| local tplPath = "prototype/" .. protoPathName
| |
| local content = load_template_content(tplPath)
| |
| if not content then
| |
| table.insert(errors, "Ошибка: не найден шаблон prototype/" .. protoPathName)
| |
| else
| |
| local keys = parse_keys_from_template(content)
| |
| for _, key in ipairs(keys) do
| |
| if not keyToTemplates[key] then
| |
| keyToTemplates[key] = {}
| |
| table.insert(keyOrder, key)
| |
| end
| |
| table.insert(keyToTemplates[key], "{{" .. tplPath .. "|title|" .. key .. "}}")
| |
| end
| |
| end
| |
| end
| |
|
| |
| -- Append errors first
| |
| for _, e in ipairs(errors) do
| |
| table.insert(out, "<div class=\"error\">" .. mw.text.encode(e) .. "</div>")
| |
| end
| |
|
| |
| -- Then for each key output header and all template invocations for that key
| |
| for _, key in ipairs(keyOrder) do
| |
| table.insert(out, "<h2>" .. mw.text.encode(key) .. "</h2>")
| |
| for _, tplCall in ipairs(keyToTemplates[key]) do
| |
| table.insert(out, tplCall)
| |
| end
| |
| end
| |
|
| |
| return table.concat(out, "\n\n")
| |
| end
| |
|
| |
| return p
| |