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

мНет описания правки
Нет описания правки
 
(не показано 19 промежуточных версий этого же участника)
Строка 70: Строка 70:
     local parts = {}
     local parts = {}


     -- типы (cardTag)
     -- id сущности
    if entityId and entityId ~= "" then
        parts[#parts + 1] = "id=" .. mw.text.encode(entityId)
    end
 
    -- типы
     if merged.tags and #merged.tags > 0 then
     if merged.tags and #merged.tags > 0 then
         table.sort(merged.tags)
         table.sort(merged.tags)
Строка 106: Строка 111:
             parts[#parts + 1] = mw.text.encode(compositeKey) .. "=" .. content
             parts[#parts + 1] = mw.text.encode(compositeKey) .. "=" .. content
         end
         end
    end
    -- id сущности
    if entityId and entityId ~= "" and #parts > 0 then
        table.insert(parts, 1, "id=" .. mw.text.encode(entityId))
     end
     end


Строка 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 = {},
Строка 168: Строка 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
Строка 178: Строка 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
Строка 229: Строка 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
Строка 275: Строка 297:
     local componentDefs = load_module_data("component.json")
     local componentDefs = load_module_data("component.json")
     local prototypeStoreDefs = load_module_data("prototype_store.json")
     local prototypeStoreDefs = load_module_data("prototype_store.json")
     local componentStoreDefs = load_module_data("component_store.json") or {}
     local componentStoreDefs = load_module_data("component_store.json")
     if not componentDefs then return "" end
     if not componentDefs or not prototypeStoreDefs or not componentStoreDefs then return "" end
 
    local foundComponents = {}
    local foundComponentStores = {}
    local foundPrototypeStores = {}


    local foundComponents, foundPrototypes = {}, {}
     local compList = componentDefs[id]
     local compList = componentDefs[id]
     if type(compList) == "table" then
     if type(compList) == "table" then
Строка 287: Строка 306:
             if type(v) == "string" then
             if type(v) == "string" then
                 foundComponents[v] = true
                 foundComponents[v] = true
            end
        end
    end
    local compStoreEntry = componentStoreDefs[id]
    if type(compStoreEntry) == "table" then
        for compName, entityList in pairs(compStoreEntry) do
            if type(compName) == "string" and type(entityList) == "table" and #entityList > 0 then
                foundComponentStores[compName] = entityList
            end
        end
    end
    if type(prototypeStoreDefs) == "table" then
        local protoEntry = prototypeStoreDefs[id]
        if type(protoEntry) == "table" then
            for protoName, entityList in pairs(protoEntry) do
                if type(protoName) == "string" and type(entityList) == "table" and #entityList > 0 then
                    foundPrototypeStores[protoName] = entityList
                end
             end
             end
         end
         end
Строка 316: Строка 315:
             if componentDefs[n] ~= nil then
             if componentDefs[n] ~= nil then
                 foundComponents[n] = true
                 foundComponents[n] = true
             end
             elseif prototypeStoreDefs[n] ~= nil then
            if componentStoreDefs[n] and type(componentStoreDefs[n]) == "table" then
                 foundPrototypes[n] = true
                for compName, entityList in pairs(componentStoreDefs[n]) do
                    if type(compName) == "string" and type(entityList) == "table" and #entityList > 0 then
                        foundComponentStores[compName] = entityList
                    end
                end
            end
            if prototypeStoreDefs and prototypeStoreDefs[n] and type(prototypeStoreDefs[n]) == "table" then
                 for protoName, entityList in pairs(prototypeStoreDefs[n]) do
                    if type(protoName) == "string" and type(entityList) == "table" and #entityList > 0 then
                        foundPrototypeStores[protoName] = entityList
                    end
                end
             end
             end
         end
         end
Строка 337: Строка 324:
         for item in string.gmatch(ignoreComponents, "[^,]+") do
         for item in string.gmatch(ignoreComponents, "[^,]+") do
             local name = trim(item)
             local name = trim(item)
             if name ~= "" then
             if name ~= "" then foundComponents[name] = nil end
                foundComponents[name] = nil
                foundComponentStores[name] = nil
            end
         end
         end
     end
     end
Строка 346: Строка 330:
         for item in string.gmatch(ignorePrototypes, "[^,]+") do
         for item in string.gmatch(ignorePrototypes, "[^,]+") do
             local name = trim(item)
             local name = trim(item)
             if name ~= "" then foundPrototypeStores[name] = nil end
             if name ~= "" then foundPrototypes[name] = nil end
         end
         end
     end
     end
Строка 357: Строка 341:
     local ok, dp = pcall(require, "Module:GetField")
     local ok, dp = pcall(require, "Module:GetField")
     local errors = {}
     local errors = {}
    local function processEntity(kind, name, isStore)
        local pathName = lcfirst(name)
        local tplPath = kind .. "/" .. pathName
        if isStore then
            tplPath = tplPath .. "/store"
        end


    local function processEntity(kind, name, isStore, storeEntities)
        local pathName = lcfirst(name)
        local tplPath = kind .. "/" .. pathName .. (isStore and "/store" or "")
         local content = load_template_content(tplPath)
         local content = load_template_content(tplPath)
         if not content then
         if not content then
Строка 366: Строка 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)
        local dataPage = (kind .. "/" .. pathName) .. ".json"
         for _, sw in ipairs(switches) do
        local entities = isStore and storeEntities or { id }
        local switchesToProcess = isStore and { "card" } or switches
 
        if isStore then
            parsed = parsed or {}
            if not parsed.card or #parsed.card == 0 then
                parsed.card = { (kind == "component") and "ComponentStore" or "PrototypeStore" }
            end
        end
 
         for _, sw in ipairs(switchesToProcess) do
             local keys = parsed[sw] or {}
             local keys = parsed[sw] or {}
             for _, key in ipairs(keys) do
             for _, key in ipairs(keys) do
Строка 399: Строка 378:
                         table.insert(switchKeyOrder[sw], key)
                         table.insert(switchKeyOrder[sw], key)
                     end
                     end
                     for _, entityId in ipairs(entities) do
                     local extra = ""
                        local extra = ""
                    if ok and dp and dp.flattenField then
                        if ok and dp and dp.flattenField then
                        local dataPage = tplPath .. ".json"
                            extra = dp.flattenField({ args = { entityId, dataPage } }) or ""
                        extra = dp.flattenField({ args = { id, dataPage } })
                        end
                    end
                        local tplStr
                    local tplStr
                        local tplLabelStr, tplContentStr
                    local tplLabelStr, tplContentStr
                        if sw == "card" then
                    if sw == "card" then
                            tplLabelStr = makeTplCall(tplPath, "cardLabel", key, entityId, extra)
                        tplLabelStr = makeTplCall(tplPath, "cardLabel", key, id, extra)
                            tplContentStr = makeTplCall(tplPath, "cardContent", key, entityId, extra)
                        tplContentStr = makeTplCall(tplPath, "cardContent", key, id, extra)
                    else
                        tplStr = makeTplCall(tplPath, sw, key, id, extra)
                    end
                    local priority = 1
                    if parsed and parsed.priority ~= nil then
                        if type(parsed.priority) == "number" then
                            priority = parsed.priority
                         else
                         else
                             tplStr = makeTplCall(tplPath, sw, key, entityId, extra)
                             local pnum = tonumber(parsed.priority)
                            if pnum then priority = pnum end
                         end
                         end
                        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
                        if sw == "card" then
                            entry = {
                                tplLabel = tplLabelStr,
                                tplContent = tplContentStr,
                                cardTag = parsed.cardTag,
                                source = { kind = kind, name = name, pathName = pathName, tplPath = tplPath },
                                priority = priority,
                                idx = #switchKeyToTemplates[sw][key] + 1
                            }
                        else
                            entry = {
                                tpl = tplStr,
                                source = { kind = kind, name = name, pathName = pathName, tplPath = tplPath },
                                priority = priority,
                                idx = #switchKeyToTemplates[sw][key] + 1
                            }
                        end
                        table.insert(switchKeyToTemplates[sw][key], entry)
                     end
                     end
                    local entry
                    if sw == "card" then
                        entry = {
                            tplLabel = tplLabelStr,
                            tplContent = tplContentStr,
                            cardTag = parsed.cardTag,
                            source = { kind = kind, name = name, pathName = pathName, tplPath = tplPath },
                            priority = priority,
                            idx = #switchKeyToTemplates[sw][key] + 1
                        }
                    else
                        entry = {
                            tpl = tplStr,
                            source = { kind = kind, name = name, pathName = pathName, tplPath = tplPath },
                            priority = priority,
                            idx = #switchKeyToTemplates[sw][key] + 1
                        }
                    end
                    table.insert(switchKeyToTemplates[sw][key], entry)
                 end
                 end
             end
             end
         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


     for compName, _ in pairs(foundComponents) do
     if type(componentStoreDefs) == "table" then
        processEntity("component", compName, false)
        local compStore = componentStoreDefs[id]
        if type(compStore) == "table" then
            for compName in pairs(compStore) do
                processEntity("component", compName, true)
            end
        end
     end
     end
     for compName, entityList in pairs(foundComponentStores) do
 
        processEntity("component", compName, true, entityList)
     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
     end
     for protoName, entityList in pairs(foundPrototypeStores) do
 
         processEntity("prototype", protoName, true, entityList)
    local out = {}
     if #errors > 0 then
         table.insert(out, '{{сущность/infobox|' .. table.concat(errors, "\n") .. '}}')
     end
     end


    local out = {}
    for _, e in ipairs(errors) do table.insert(out, e) 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


Строка 622: Строка 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 = {}
Строка 647: Строка 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
 
                if pairPattern ~= "" then
                    line = apply_pattern(line, pairPattern, pairReplace)
                 end
                 end
                table.insert(out, line)
             end
             end
         end
         end
Строка 691: Строка 693:


             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