|
|
| (не показано 119 промежуточных версий этого же участника) |
| Строка 1: |
Строка 1: |
| local p = {} | | local p = {} |
| local getArgs = require('Module:Arguments').getArgs | | local getArgs = require('Module:Arguments').getArgs |
| | local entityData = require('Module:Сущность/data') |
|
| |
|
| local function trim(s) | | local function trim(s) |
| if not s then return s end | | if not s then return "" end |
| return (s:gsub("^%s*(.-)%s*$", "%1"))
| | return mw.text.trim(tostring(s)) |
| 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
| |
| | |
| 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 function makeTplCall(tplPath, sw, key, id, extra)
| |
| local tplStr = "{{" .. tplPath .. "|" .. sw .. "|" .. key
| |
| tplStr = tplStr .. "|id=" .. tostring(id)
| |
| if extra and extra ~= "" then tplStr = tplStr .. "|" .. extra end
| |
| tplStr = tplStr .. "}}"
| |
| return tplStr
| |
| end
| |
| | |
| local function makeSourceLink(s)
| |
| local className = s.name .. (s.kind and (s.kind:sub(1, 1):upper() .. s.kind:sub(2)) or "")
| |
| local tplLabel = "Template:" .. s.tplPath
| |
| return "[[" .. tplLabel .. "|" .. className .. "]]"
| |
| end
| |
| | |
| local switches = { "card", "title" }
| |
| local switchConfigs = {
| |
| card = {
| |
| wrapper = function(key, tplCalls, sources)
| |
| if not tplCalls or #tplCalls == 0 then return "" end
| |
| local calls = table.concat(tplCalls, " ")
| |
| local srcStr = ""
| |
| if sources and #sources > 0 then
| |
| local srcParts = {}
| |
| for _, s in ipairs(sources) do table.insert(srcParts, makeSourceLink(s)) end
| |
| srcStr = " " .. table.concat(srcParts, " ")
| |
| end
| |
| return "{{карточка/Сущность|" .. mw.text.encode(key) .. "|" .. calls .. srcStr .. "}}"
| |
| end
| |
| },
| |
| title = {
| |
| wrapper = function(key, tplCalls, sources)
| |
| local parts = {}
| |
| table.insert(parts, "<h2>" .. mw.text.encode(key) .. "</h2>")
| |
| if tplCalls and #tplCalls > 0 then
| |
| for i, tpl in ipairs(tplCalls) do
| |
| local line = tpl
| |
| local src = sources and sources[i]
| |
| line = '<span>' .. line .. '</span><span class"ts-Сущность-field">' .. makeSourceLink(src) .. '</span>'
| |
| table.insert(parts, '<p class="ts-Сущность">' .. line .. '</p>')
| |
| end
| |
| end
| |
| return table.concat(parts, "\n")
| |
| end
| |
| }
| |
| }
| |
| | |
| local function getTemplateMeta(frame, tplPath)
| |
| local expanded = frame:expandTemplate {
| |
| title = tplPath,
| |
| args = { "json" }
| |
| }
| |
| | |
| local ok, data = pcall(mw.text.jsonDecode, expanded)
| |
| if ok and type(data) == "table" then
| |
| return data
| |
| end
| |
| | |
| return ""
| |
| end
| |
| | |
| local function renderBlocks(frame, switchesTbl, configs, keyOrder, keyToTemplates, keySources)
| |
| local outLocal = {}
| |
| for _, sw in ipairs(switchesTbl) do
| |
| local cfg = configs[sw] or {}
| |
| for _, key in ipairs(keyOrder[sw] or {}) do
| |
| local entries = keyToTemplates[sw][key] or {}
| |
| local tplCalls = {}
| |
| local sources = {}
| |
| if #entries > 0 then
| |
| table.sort(entries, function(a, b)
| |
| if a.priority == b.priority then return a.idx < b.idx end
| |
| return a.priority > b.priority
| |
| end)
| |
| for _, e in ipairs(entries) do
| |
| table.insert(tplCalls, e.tpl)
| |
| table.insert(sources, e.source)
| |
| end
| |
| end
| |
| if cfg.wrapper then
| |
| local outStr = cfg.wrapper(key, tplCalls, sources)
| |
| if outStr and outStr ~= "" then table.insert(outLocal, outStr) end
| |
| end
| |
| end
| |
| end
| |
| return outLocal
| |
| end | | end |
|
| |
|
| Строка 118: |
Строка 11: |
| local args = getArgs(frame, { removeBlanks = false }) | | local args = getArgs(frame, { removeBlanks = false }) |
| local id = args[1] or "" | | local id = args[1] or "" |
| if id == "" then return "" end | | if id == "" then |
| local function parseListArg(str)
| | return "" |
| local res = {} | |
| if not str or str == "" then return res end
| |
| for item in string.gmatch(str, "[^,]+") do
| |
| local s = trim(item)
| |
| if s ~= "" then
| |
| local a, b = s:match("^([^_]+)_(.+)$")
| |
| if a and b then
| |
| res[a] = res[a] or {}
| |
| res[a][b] = true
| |
| end
| |
| end
| |
| end
| |
| return res
| |
| end | | end |
| local blacklist = parseListArg(args.blacklist or "")
| |
| local whitelist = parseListArg(args.whitelist or "")
| |
| local hasWhitelist = next(whitelist) ~= nil
| |
|
| |
|
| local componentDefs = load_module_data("component.json") | | local params = "" |
| local prototypeDefs = load_module_data("prototype.json") | | local hasExtraArgs = false |
| if not componentDefs or not prototypeDefs then return "" end
| | |
| | for k, v in pairs(args) do |
| | if k ~= 1 and k ~= "cardTag" and k ~= "tag" and v ~= nil and v ~= "" then |
| | params = params .. "|" .. k .. "=" .. v |
|
| |
|
| local foundComponents, foundPrototypes = {}, {}
| | if k ~= "blacklist" then |
| local compList = componentDefs[id]
| | hasExtraArgs = true |
| if type(compList) == "table" then for _, v in ipairs(compList) do if type(v) == "string" then foundComponents[v] = true end end end
| | end |
| 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
| |
| 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 |
| end | | end |
|
| |
|
| local switchKeyOrder, switchKeyToTemplates, switchKeySources = {}, {}, {} | | local ajax = "{{ajax|<nowiki>{{#invoke:Сущность/data|get|" .. id .. params .. "}}</nowiki>|auto}}" |
| for _, sw in ipairs(switches) do
| | local text = ajax |
| switchKeyOrder[sw] = {}; switchKeyToTemplates[sw] = {}; switchKeySources[sw] = {}
| |
| end | |
|
| |
|
| local errors = {} | | if not hasExtraArgs then |
| local function processEntity(kind, name)
| | local cardTagText = entityData.collectCardTagText(frame, args, id) |
| local pathName = lcfirst(name)
| | if cardTagText ~= "" then |
| local tplPath = kind .. "/" .. pathName
| | params = params .. "|cardTag=" .. cardTagText |
| local content = load_template_content(tplPath)
| |
| if not content then
| |
| if hasWhitelist then
| |
| return
| |
| end
| |
| local classType = (kind and (kind:sub(1, 1):upper() .. kind:sub(2)) or "")
| |
| local className = name .. classType
| |
| local tplLabel = "Template:" .. tplPath
| |
| table.insert(errors,
| |
| "{{сущность/infobox|тип=" .. classType .. "|название=" .. className .. "|ссылка=" .. tplLabel .. "}}")
| |
| return
| |
| end
| |
| local parsed = getTemplateMeta(frame, tplPath)
| |
| local ok, dp = pcall(require, "Module:GetField")
| |
| for _, sw in ipairs(switches) do | |
| local keys = parsed[sw] or {}
| |
| for _, key in ipairs(keys) do
| |
| local skip = false
| |
| if next(whitelist) ~= nil then
| |
| if not (whitelist[sw] and whitelist[sw][key]) then skip = true end
| |
| else
| |
| if blacklist[sw] and blacklist[sw][key] then skip = true end
| |
| end
| |
| if not skip then
| |
| if not switchKeyToTemplates[sw][key] then
| |
| switchKeyToTemplates[sw][key] = {}
| |
| table.insert(switchKeyOrder[sw], key)
| |
| end
| |
| local extra = ""
| |
| if ok and dp and dp.flattenField then
| |
| local dataPage = tplPath .. ".json"
| |
| extra = dp.flattenField({ args = { id, dataPage } })
| |
| end
| |
| local tplStr = makeTplCall(tplPath, sw, key, id, extra)
| |
| local priority = 1
| |
| if parsed and parsed.priority ~= nil then
| |
| if type(parsed.priority) == "number" then
| |
| priority = parsed.priority
| |
| else
| |
| local pnum = tonumber(parsed.priority)
| |
| if pnum then priority = pnum end
| |
| end
| |
| end
| |
| local entry = {
| |
| tpl = tplStr,
| |
| source = { kind = kind, name = name, pathName = pathName, tplPath = tplPath },
| |
| priority = priority,
| |
| idx = #switchKeyToTemplates[sw][key] + 1
| |
| }
| |
| table.insert(switchKeyToTemplates[sw][key], entry)
| |
| end
| |
| end
| |
| end | | end |
| | text = |
| | "{{#if:{{NAMESPACE}}|{{переключатель_проекта|[[Категория:{{NAMESPACE}}:Сущности]]{{NAMESPACE}}}}|[[Категория:Сущности]]{{переключатель_проекта}}}}" .. |
| | '<span style="display:none;">{{#invoke:CategoryFormatter|main|' .. cardTagText .. '}}</span>' .. ajax |
| end | | end |
|
| |
|
| local items = {}
| | return frame:preprocess(text) |
| 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 _, it in ipairs(items) do processEntity(it.kind, it.name) end
| |
| | |
| local out = {}
| |
| local blocks = renderBlocks(frame, switches, switchConfigs, switchKeyOrder, switchKeyToTemplates, switchKeySources)
| |
| for _, b in ipairs(blocks) do table.insert(out, b) end
| |
| for _, e in ipairs(errors) do table.insert(out, e) end
| |
| | |
| return frame:preprocess(table.concat(out, "\n\n")) | |
| end | | end |
|
| |
|
| function p.preview(frame) | | function p.preview(frame) |
| local args = getArgs(frame, { removeBlanks = false }) | | local args = getArgs(frame, { removeBlanks = false }) |
| local tplPath = args[1] or "" | | local id = args[1] or "" |
| if tplPath == "" then return "" end | | if id == "" then return "" end |
|
| |
|
| local content = load_template_content(tplPath) | | local params = "" |
| if not content then
| | for k, v in pairs(args) do |
| return ""
| | if k ~= 1 and k ~= "cardTag" and k ~= "tag" then |
| | params = params .. "|" .. k .. "=" .. v |
| | end |
| end | | end |
|
| |
|
| local parsed = getTemplateMeta(frame, tplPath) or {} | | local cardTagText = entityData.mergeCardTagText(args.cardTag or "", args.tag or "") |
| | | if cardTagText ~= "" then |
| local switchKeyOrder, switchKeyToTemplates, switchKeySources = {}, {}, {}
| | params = params .. "|cardTag=" .. trim(cardTagText) |
| for _, sw in ipairs(switches) do
| |
| switchKeyOrder[sw] = {}; switchKeyToTemplates[sw] = {}; switchKeySources[sw] = {}
| |
| end | |
| | |
| for _, sw in ipairs(switches) do
| |
| local keys = parsed[sw] or {}
| |
| for idx, key in ipairs(keys) do | |
| if not switchKeyToTemplates[sw][key] then
| |
| switchKeyToTemplates[sw][key] = {}
| |
| table.insert(switchKeyOrder[sw], key)
| |
| end
| |
| local tplStr = makeTplCall(tplPath, sw, key, "")
| |
| local entry = {
| |
| tpl = tplStr,
| |
| source = { kind = "", name = tplPath, pathName = tplPath, tplPath = tplPath },
| |
| priority = 1,
| |
| idx = #switchKeyToTemplates[sw][key] + 1
| |
| }
| |
| table.insert(switchKeyToTemplates[sw][key], entry)
| |
| end
| |
| end | | end |
|
| |
|
| local out = {} | | local text = "{{#invoke:Сущность/data|preview|" .. id .. params .. "}}" |
| local blocks = renderBlocks(frame, switches, switchConfigs, switchKeyOrder, switchKeyToTemplates, switchKeySources)
| |
| for _, b in ipairs(blocks) do table.insert(out, b) end
| |
|
| |
|
| return frame:preprocess(table.concat(out, "\n\n")) | | return frame:preprocess(text) |
| end | | end |
|
| |
|
| return p | | return p |