Модуль:Сущность: различия между версиями

мНет описания правки
Нет описания правки
 
(не показано 29 промежуточных версий этого же участника)
Строка 75: Строка 75:
     end
     end


     -- типы (cardTag)
     -- типы  
     if merged.tags and #merged.tags > 0 then
     if merged.tags and #merged.tags > 0 then
         table.sort(merged.tags)
         table.sort(merged.tags)
Строка 120: Строка 120:
end
end


local function cardWrapper(frame, keyOrder, keyToTemplates, keySources, entityId)
local function cardWrapper(frame, keyOrder, keyToTemplates, keySources, entityId, noHeaders)
     local merged = {
     local merged = {
         sections = {},
         sections = {},
Строка 149: Строка 149:
                         table.insert(merged.sections, section)
                         table.insert(merged.sections, section)
                     end
                     end
                     merged.labelOverrides[compositeKey] = displayLabel
                     if displayLabel ~= "" and (not merged.labelOverrides[compositeKey] or merged.labelOverrides[compositeKey] == "") then
                     merged.contentByKey[compositeKey] = content
                        merged.labelOverrides[compositeKey] = displayLabel
                     end
                    if content ~= "" then
                        local prev = merged.contentByKey[compositeKey]
                        if prev and prev ~= "" then
                            merged.contentByKey[compositeKey] = prev .. "\n" .. content
                        else
                            merged.contentByKey[compositeKey] = content
                        end
                    end


                     merged.labelSets[section] = merged.labelSets[section] or {}
                     merged.labelSets[section] = merged.labelSets[section] or {}
Строка 159: Строка 168:
                         merged.labelLists[section] = cur
                         merged.labelLists[section] = cur
                     end
                     end
                end
                    if e.cardTag and e.cardTag ~= "" then
                if e.cardTag and e.cardTag ~= "" then
                        if not merged.tagSet[e.cardTag] then
                    if not merged.tagSet[e.cardTag] then
                            merged.tagSet[e.cardTag] = true
                        merged.tagSet[e.cardTag] = true
                            table.insert(merged.tags, e.cardTag)
                        table.insert(merged.tags, e.cardTag)
                        end
                     end
                     end
                 end
                 end
Строка 169: Строка 178:
         end
         end
     end
     end
    if noHeaders then
        local hasLabel = false
        for _, v in pairs(merged.labelOverrides or {}) do
            if v and v ~= "" then hasLabel = true break end
        end
        if not hasLabel then
            for _, lst in pairs(merged.labelLists or {}) do
                if #lst > 0 then hasLabel = true break end
            end
        end
        local hasContent = false
        for _, v in pairs(merged.contentByKey or {}) do
            if v and v ~= "" then hasContent = true break end
        end
        if not hasLabel and not hasContent then
            return ""
        end
    end
     return buildCardCall(merged, entityId)
     return buildCardCall(merged, entityId)
end
end
Строка 220: Строка 251:
         local cfg = configs[sw] or {}
         local cfg = configs[sw] or {}
         if cfg.fullCard and sw == "card" then
         if cfg.fullCard and sw == "card" then
             local outStr = cfg.wrapper(frame, keyOrder[sw], keyToTemplates[sw], keySources[sw], entityId)
             local outStr = cfg.wrapper(frame, keyOrder[sw], keyToTemplates[sw], keySources[sw], entityId, noHeaders)
             if outStr and outStr ~= "" then table.insert(outLocal, outStr) end
             if outStr and outStr ~= "" then table.insert(outLocal, outStr) end
         else
         else
Строка 260: Строка 291:
     local whitelist = parseListArg(args.whitelist or "")
     local whitelist = parseListArg(args.whitelist or "")
     local hasWhitelist = next(whitelist) ~= nil
     local hasWhitelist = next(whitelist) ~= nil
    local ignoreComponents = args.ignoreComponents or args.ignoreComponent or ""
    local ignorePrototypes = args.ignorePrototypes or args.ignorePrototype or ""


     local componentDefs = load_module_data("component.json")
     local componentDefs = load_module_data("component.json")
     local prototypeDefs = load_module_data("prototype.json")
     local prototypeStoreDefs = load_module_data("prototype_store.json")
     if not componentDefs or not prototypeDefs then return "" end
    local componentStoreDefs = load_module_data("component_store.json")
     if not componentDefs or not prototypeStoreDefs or not componentStoreDefs then return "" end


     local foundComponents, foundPrototypes = {}, {}
     local foundComponents, foundPrototypes = {}, {}
Строка 271: Строка 306:
             if type(v) == "string" then
             if type(v) == "string" then
                 foundComponents[v] = true
                 foundComponents[v] = true
            end
        end
    end
    local protoEntry = prototypeDefs[id]
    if type(protoEntry) == "table" then
        for protoName, _ in pairs(protoEntry) do
            if type(protoName) == "string" then
                foundPrototypes[protoName] = true
             end
             end
         end
         end
Строка 289: Строка 315:
             if componentDefs[n] ~= nil then
             if componentDefs[n] ~= nil then
                 foundComponents[n] = true
                 foundComponents[n] = true
             elseif prototypeDefs[n] ~= nil then
             elseif prototypeStoreDefs[n] ~= nil then
                 foundPrototypes[n] = true
                 foundPrototypes[n] = true
             end
             end
        end
    end
    if ignoreComponents ~= "" then
        for item in string.gmatch(ignoreComponents, "[^,]+") do
            local name = trim(item)
            if name ~= "" then foundComponents[name] = nil end
        end
    end
    if ignorePrototypes ~= "" then
        for item in string.gmatch(ignorePrototypes, "[^,]+") do
            local name = trim(item)
            if name ~= "" then foundPrototypes[name] = nil end
         end
         end
     end
     end
Строка 302: Строка 341:
     local ok, dp = pcall(require, "Module:GetField")
     local ok, dp = pcall(require, "Module:GetField")
     local errors = {}
     local errors = {}
     local function processEntity(kind, name)
     local function processEntity(kind, name, isStore)
         local pathName = lcfirst(name)
         local pathName = lcfirst(name)
         local tplPath = kind .. "/" .. pathName
         local tplPath = kind .. "/" .. pathName
        if isStore then
            tplPath = tplPath .. "/store"
        end
         local content = load_template_content(tplPath)
         local content = load_template_content(tplPath)
         if not content then
         if not content then
Строка 310: Строка 353:
                 return
                 return
             end
             end
             local classType = (kind and (kind:sub(1, 1):upper() .. kind:sub(2)) or "")
             local baseType = (kind and (kind:sub(1, 1):upper() .. kind:sub(2)) or "")
             local className = name .. classType
            local classType = baseType
            if isStore then classType = classType .. "Store" end
             local className = name .. baseType
             local tplLabel = "Template:" .. tplPath
             local tplLabel = "Template:" .. tplPath
             table.insert(errors,
             table.insert(errors,
                 "{{сущность/infobox|тип=" .. classType .. "|название=" .. className .. "|ссылка=" .. tplLabel .. "}}")
                 "{{сущность/infobox/base|тип=" .. classType .. "|название=" .. className .. "|ссылка=" .. tplLabel .. "}}")
             return
             return
         end
         end
         local parsed = getTemplateMeta(frame, tplPath)
         local parsed = getTemplateMeta(frame, tplPath)
         for _, sw in ipairs(switches) do
         for _, sw in ipairs(switches) do
Строка 377: Строка 423:
         end
         end
     end
     end
    for compName, _ in pairs(foundComponents) do processEntity("component", compName, false) end
    for protoName, _ in pairs(foundPrototypes) do processEntity("prototype", protoName, false) end


     local items = {}
     if type(componentStoreDefs) == "table" then
    for compName, _ in pairs(foundComponents) do table.insert(items, { kind = "component", name = compName }) end
        local compStore = componentStoreDefs[id]
     for protoName, _ in pairs(foundPrototypes) do table.insert(items, { kind = "prototype", name = protoName }) end
        if type(compStore) == "table" then
     for _, it in ipairs(items) do processEntity(it.kind, it.name) end
            for compName in pairs(compStore) do
                processEntity("component", compName, true)
            end
        end
     end
 
    if type(prototypeStoreDefs) == "table" then
        local protoStore = prototypeStoreDefs[id]
        if type(protoStore) == "table" then
            for protoName in pairs(protoStore) do
                processEntity("prototype", protoName, true)
            end
        end
     end


     local out = {}
     local out = {}
     for _, e in ipairs(errors) do table.insert(out, e) end
     if #errors > 0 then
        table.insert(out, '{{сущность/infobox|' .. table.concat(errors, "\n") .. '}}')
    end
 
     local blocks = renderBlocks(frame, switches, switchConfigs, switchKeyOrder, switchKeyToTemplates, switchKeySources,
     local blocks = renderBlocks(frame, switches, switchConfigs, switchKeyOrder, switchKeyToTemplates, switchKeySources,
         hasWhitelist, id)
         hasWhitelist, id)
     for _, b in ipairs(blocks) do table.insert(out, b) end
     for _, b in ipairs(blocks) do table.insert(out, b) end


     return frame:preprocess(table.concat(out, "\n"))
     return frame:preprocess(table.concat(out, "\n") .. "[[Категория:Сущности]]")
end
end


Строка 480: Строка 545:


     return (text:gsub(patt, replacement))
     return (text:gsub(patt, replacement))
end
function p.json(frame)
    local args = getArgs(frame, { removeBlanks = false })
    local jsonStr = mw.text.unstripNoWiki(trim(args[1] or args.json or ""))
    local tplPath = trim(args[2] or args.template or "")
    if jsonStr == "" or tplPath == "" then return "" end
    local ok, data = pcall(mw.text.jsonDecode, jsonStr)
    if not ok or type(data) ~= "table" then
        return ""
    end
    local calls = {}
    local function makeCall(id, obj)
        if type(id) ~= "string" then return end
        local parts = { "{{" .. tplPath, "id=" .. id }
        if type(obj) == "table" then
            for k, v in pairs(obj) do
                if v ~= nil then
                    parts[#parts + 1] = tostring(k) .. "=" .. tostring(v)
                end
            end
        end
        parts[#parts + 1] = "}}"
        calls[#calls + 1] = table.concat(parts, "|")
    end
    if is_array(data) then
        for _, item in ipairs(data) do
            if type(item) == "table" then
                for k, v in pairs(item) do
                    makeCall(k, v)
                end
            end
        end
    else
        for k, v in pairs(data) do
            makeCall(k, v)
        end
    end
    if #calls == 0 then
        return ""
    end
    local rendered = table.concat(calls, "\n")
    return frame:preprocess(rendered)
end
end


Строка 500: Строка 614:
     local valuePattern = mw.text.unstripNoWiki(args.value_pattern or "(.*)")
     local valuePattern = mw.text.unstripNoWiki(args.value_pattern or "(.*)")
     local valueReplace = mw.text.unstripNoWiki(args.value_replace or "\\1")
     local valueReplace = mw.text.unstripNoWiki(args.value_replace or "\\1")
    local pairPattern = mw.text.unstripNoWiki(args.pattern or "")
    local pairReplace = mw.text.unstripNoWiki(args.replace or "\\1")


     local out = {}
     local out = {}
Строка 525: Строка 642:
                 text = apply_pattern(text, patt, repl)
                 text = apply_pattern(text, patt, repl)


                local line
                 if outputType == "enum" then
                 if outputType == "enum" then
                     table.insert(out, text)
                     line = text
                 else
                 else
                     table.insert(out, bullet .. text)
                     line = bullet .. text
                 end
                 end
                if pairPattern ~= "" then
                    line = apply_pattern(line, pairPattern, pairReplace)
                end
                table.insert(out, line)
             end
             end
         end
         end
Строка 554: Строка 678:
             end
             end


             local keyStr = tostring(k)
             local baseKey = apply_pattern(tostring(k), keyPattern, "\\1")
             keyStr = apply_pattern(keyStr, keyPattern, keyReplace)
 
             vStr = apply_pattern(vStr, valuePattern, valueReplace)
            local MARK_KEY = "\31KEY\31"
             local vRepl = (valueReplace or "\\1"):gsub("\\2", MARK_KEY)
            local vStr0 = apply_pattern(vStr, valuePattern, vRepl)
            vStr0 = tostring(vStr0):gsub(MARK_KEY, baseKey)
 
            local MARK_VAL = "\31VAL\31"
            local kRepl = (keyReplace or "\\1"):gsub("\\2", MARK_VAL)
             local keyStr0 = apply_pattern(tostring(k), keyPattern, kRepl)
            local keyStr = tostring(keyStr0):gsub(MARK_VAL, vStr0)
 
            vStr = vStr0


             if vStr ~= "" then
             if vStr ~= "" then
                local line
                 if outputType == "enum" then
                 if outputType == "enum" then
                     table.insert(out, vStr .. " " .. keyStr)
                     line = vStr .. " " .. keyStr
                 else
                 else
                     table.insert(out, bullet .. keyStr .. sep .. vStr)
                     line = bullet .. keyStr .. sep .. vStr
                end
 
                if pairPattern ~= "" then
                    line = apply_pattern(line, pairPattern, pairReplace)
                 end
                 end
                table.insert(out, line)
             end
             end
         end
         end