Модуль:Песочница/Pok: различия между версиями

Нет описания правки
Нет описания правки
 
(не показана 21 промежуточная версия этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}
local BASE_USER = "IanComradeBot/"
local getArgs = require('Module:Arguments').getArgs
local CURRENT_PROJECT
local JsonPaths = require('Module:JsonPaths')


local function normalize_value(v)
local dpOk, dpModule = pcall(require, "Module:GetField")
     if type(v) == "string" then
local dp = dpOk and dpModule or nil
        if v == "" then return nil end
 
        return v
local switchModeRegistry = {}
    elseif type(v) == "table" then
local switchModeOrder = {}
         if v[1] and v[1] ~= "" then return tostring(v[1]) end
 
         for _, val in pairs(v) do
local function trim(s)
            if type(val) == "string" and val ~= "" then return val end
     if not s then return s end
             if type(val) ~= "nil" then return tostring(val) end
    return (s:gsub("^%s*(.-)%s*$", "%1"))
end
 
local function ucfirst(s)
    if not s or s == "" then return s end
    return mw.ustring.upper(mw.ustring.sub(s, 1, 1)) .. mw.ustring.sub(s, 2)
end
 
local function each_csv_value(str, fn)
    if not str or str == "" then
         return
    end
    for item in string.gmatch(str, "[^,]+") do
         local value = trim(item)
        if value ~= "" then
             fn(value)
         end
         end
    end
end
local function load_module_data(page)
    local moduleName = JsonPaths.get(page)
    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(title.getContent, title)
    if not ok then
         return nil
         return nil
    elseif v ~= nil then
        return tostring(v)
     end
     end
     return nil
     return content
end
end


local function serial(obj, depth, seen)
local function load_entity_data(entityId)
     depth = depth or 0
     if not entityId or entityId == "" then
    seen = seen or {}
        return nil
    if type(obj) ~= "table" then return tostring(obj) end
     end
     if seen[obj] then return "<cycle>" end
 
     if depth > 5 then return "{...}" end
     local page = "prototype/Entity/" .. entityId .. ".json"
    seen[obj] = true
     local moduleName = JsonPaths.get(page)
     local parts = {}
    local ok, data = pcall(mw.loadData, moduleName)
    for k, v in pairs(obj) do
    if not ok or type(data) ~= "table" then
        local ks = tostring(k)
         return nil
        local vs = serial(v, depth + 1, seen)
         table.insert(parts, ks .. "=" .. vs)
     end
     end
     return "{" .. table.concat(parts, ", ") .. "}"
     return data
end
end


function p.set_path(frame)
local function normalize_component_name(name)
    local arg
     if type(name) ~= "string" then
    -- support: #invoke:GetField|set_path|VALUE  (frame.args[1])
         return nil
     if type(frame) == "table" and frame.args then
    end
        if frame.args[1] then arg = frame.args[1] end
 
        if (not arg or arg == "") then
    name = trim(name)
            local ok, a = pcall(function() return frame:getArgument(1) end)
    if name == "" then
            if ok and a and a ~= "" then arg = a end
         return nil
         end
        if (not arg or arg == "") then
            local ok, a = pcall(function() return frame:getArgument("path") end)
            if ok and a and a ~= "" then arg = a end
        end
        if (not arg or arg == "") then
            local ok, a = pcall(function() return frame:getArgument("GetFieldPath") end)
            if ok and a and a ~= "" then arg = a end
        end
    elseif type(frame) == "string" then
         arg = frame
    elseif frame ~= nil then
        -- maybe called without frame but with second arg: p.set_path(nil, "X")
        if arg == nil and type(frame) ~= "table" then
            arg = tostring(frame)
        end
     end
     end


     local val = normalize_value(arg)
     if name:sub(1, 5) == "type:" then
    if val and val ~= "" then
         return name:sub(6)
        CURRENT_PROJECT = val
         return "OK:" .. tostring(CURRENT_PROJECT)
     end
     end
    -- nothing set — return diagnostic snapshot to help debug
     if name:sub(1, 6) == "!type:" then
    local info = "NOT_SET"
         return name:sub(7)
     if type(frame) == "table" then
         local a = frame.args and serial(frame.args) or "(no frame.args)"
        info = info .. "; frame.args=" .. a
    else
        info = info .. "; frame_type=" .. type(frame) .. "; frame_val=" .. tostring(frame)
     end
     end
    return info
end


function p._debug_get_cached()
     return name
     if CURRENT_PROJECT == nil then return "(nil)" end
    return tostring(CURRENT_PROJECT)
end
end


function p.inspect(frame)
local function collect_entity_components(entity)
     local out = {}
     local out = {}
     local f = frame or mw.getCurrentFrame()
     local seen = {}
    if not f then return "NO_FRAME_AVAILABLE" end
    local function push(s) table.insert(out, s) end


     local depth = 0
     if type(entity) ~= "table" then
    while f and depth < 20 do
         return out
        local header = string.format("FRAME depth=%d", depth)
    end
        push(header)
        -- try to read positional args table if present
        if f.args then
            push("  .args = " .. serial(f.args))
        else
            push("  .args = (no args table)")
         end
        -- try reading some common named args via getArgument (safe)
        local ok1, a1 = pcall(function() return f:getArgument("1") end)
        local okn, an = pcall(function() return f:getArgument("name") end)
        local okN, aN = pcall(function() return f:getArgument("Название") end)
        push("  getArgument('1') = " .. (ok1 and tostring(a1) or "(err)"))
        push("  getArgument('name') = " .. (okn and tostring(an) or "(err)"))
        push("  getArgument('Название') = " .. (okN and tostring(aN) or "(err)"))


        -- also dump callable fields existence
    local comps = entity.components
        push("  has:getParent = " .. tostring(type(f.getParent) == "function"))
    if type(comps) ~= "table" then
         push("  has:callParserFunction = " .. tostring(type(f.callParserFunction) == "function"))
         return out
    end


         f = (type(f.getParent) == "function") and f:getParent() or nil
    if #comps > 0 then
        depth = depth + 1
         for _, v in ipairs(comps) do
            local name = normalize_component_name(v)
            if name and not seen[name] then
                seen[name] = true
                out[#out + 1] = name
            end
        end
    else
        for k in pairs(comps) do
            local name = normalize_component_name(k)
            if name and not seen[name] then
                seen[name] = true
                out[#out + 1] = name
            end
        end
     end
     end
     push("CURRENT_PROJECT = " .. tostring(CURRENT_PROJECT))
 
     return table.concat(out, "\n")
     table.sort(out)
     return out
end
end


function p.get_module_name(frame, pagePath)
local function load_entity_components_from_dp(entityId)
    local fp, pp = frame, pagePath
     if not dp then
     if type(frame) == "string" and (not pagePath) then
         return nil
         pp = frame
        fp = nil
     end
     end
     local project = pp and normalize_value(pp) or CURRENT_PROJECT
 
     if (not project or project == "") and fp then
     local getter = dp.getEntityComponents or dp.collectEntityComponents or dp.getComp
         local ok, res = pcall(function() return fp:callParserFunction{ name = "var", args = { "GetFieldPath" } } end)
     if type(getter) ~= "function" then
        if ok and res and res ~= "" then project = res end
         return nil
     end
     end
    if not project or project == "" then project = "default" end
    return "Module:" .. BASE_USER .. project .. "/data"
end


local function load_cached_data(moduleName)
     local ok, result = pcall(getter, { args = { entityId } })
     local ok, loaded = pcall(mw.loadData, moduleName)
     if not ok or result == nil or result == "" then
     if not ok or not loaded then
         return nil
         return nil
     end
     end
    return loaded
end


local function parse_indexed_part(part)
    if type(result) == "table" then
    local key, idx = string.match(part, "^(.-)%[(%d+)%]$")
         return result
    if key then
         return key, tonumber(idx)
     end
     end
     local num = tonumber(part)
 
    if num then
     if type(result) == "string" then
         return nil, num
        local okJson, decoded = pcall(mw.text.jsonDecode, result)
        if okJson and type(decoded) == "table" then
            return decoded
         end
     end
     end
     return part, nil
 
     return nil
end
end


local function get_by_path(tbl, path)
local function load_entity_components(entityId)
     if not tbl or path == "" then return nil end
    local viaDp = load_entity_components_from_dp(entityId)
    local cur = tbl
     if type(viaDp) == "table" and next(viaDp) ~= nil then
    for part in string.gmatch(path, "([^%.]+)") do
        local out = {}
        local key, idx = parse_indexed_part(part)
        local seen = {}
        if key and key ~= "" then
        if #viaDp > 0 then
             if type(cur) ~= "table" then return nil end
            for _, v in ipairs(viaDp) do
            local nextCur = cur[key]
                local name = normalize_component_name(v)
            if nextCur == nil then
                if name and not seen[name] then
                 nextCur = cur["!type:" .. key]
                    seen[name] = true
                    out[#out + 1] = name
                end
            end
        else
             for k in pairs(viaDp) do
                local name = normalize_component_name(k)
                if name and not seen[name] then
                    seen[name] = true
                    out[#out + 1] = name
                 end
             end
             end
            cur = nextCur
        end
        if idx then
            if type(cur) ~= "table" then return nil end
            cur = cur[idx]
         end
         end
         if cur == nil then return nil end
         table.sort(out)
        return out
     end
     end
     return cur
 
    local entity = load_entity_data(entityId)
     return collect_entity_components(entity)
end
end


local function format_value(v)
p.loadEntityData = load_entity_data
    local okJson, json = pcall(mw.text.jsonEncode, v)
p.collectEntityComponents = collect_entity_components
     if okJson and json == "null" then
p.loadEntityComponents = load_entity_components
         return "null"
p.entityHasComponent = function(entityOrId, compName)
     if not compName or compName == "" then
         return false
     end
     end


     if v == nil then return "" end
     if type(entityOrId) == "string" then
        local entity = load_entity_data(entityOrId)
        if not entity then
            return false
        end
        local comps = collect_entity_components(entity)
        for _, v in ipairs(comps) do
            if v == compName then
                return true
            end
        end
        return false
    end


     local t = type(v)
     local comps = collect_entity_components(entityOrId)
     if t == "string" or t == "number" or t == "boolean" then
     for _, v in ipairs(comps) do
        return tostring(v)
         if v == compName then
    elseif t == "table" then
             return true
        local ok, json2 = pcall(mw.text.jsonEncode, v)
         if ok and json2 then
             return json2
         end
         end
        return ""
    else
        return tostring(v)
     end
     end
    return false
end
end


local function to_nowiki(v)
local function makeTplCall(tplPath, sw, key, id, extra)
     return "<nowiki>" .. v .. "</nowiki>"
     local tplStr = "{{" .. tplPath .. "|" .. sw .. "|" .. key
    tplStr = tplStr .. "|id=" .. tostring(id)
    if extra and extra ~= "" then tplStr = tplStr .. "|" .. extra end
    tplStr = tplStr .. "}}"
    return tplStr
end
end


local function is_array(tbl)
local function add_template_param(params, seen, raw)
     local max = 0
     local param = trim(raw or "")
    local count = 0
    if param == "" or param == "id" or param:match("^%d+$") then
    for k in pairs(tbl) do
        return
        if type(k) ~= "number" then
    end
            return false
    if not seen[param] then
        end
        seen[param] = true
        if k > max then max = k end
         params[#params + 1] = param
         count = count + 1
     end
     end
    return count > 0 and max == count
end
end


local function deep_copy(src)
local function collect_template_params(content)
     local dst = {}
     local params = {}
     for k, v in pairs(src) do
     local seen = {}
        if type(v) == "table" then
 
            dst[k] = deep_copy(v)
    if not content or content == "" then
        else
        return params
            dst[k] = v
    end
         end
 
    for param in content:gmatch("{{{%s*([^|}]+)%s*|") do
        add_template_param(params, seen, param)
    end
    for param in content:gmatch("{{{%s*([^|}]+)%s*}}") do
         add_template_param(params, seen, param)
     end
     end
     return dst
 
     return params
end
end


local function deep_merge(dst, src)
local function get_template_params(tplPath, content)
     for k, v in pairs(src) do
     return collect_template_params(content)
         if type(v) == "table" and type(dst[k]) == "table" then
end
            deep_merge(dst[k], v)
 
        elseif type(v) == "table" then
local function sort_entries_by_priority(entries)
            dst[k] = deep_copy(v)
    table.sort(entries, function(a, b)
         else
         if a.priority == b.priority then return a.idx < b.idx end
            dst[k] = v
        return a.priority > b.priority
         end
    end)
end
 
local function make_source(kind, name, pathName, tplPath)
    return { kind = kind, name = name, pathName = pathName, tplPath = tplPath }
end
 
local function register_switch_mode(name, cfg)
    switchModeRegistry[name] = cfg or {}
    switchModeOrder[#switchModeOrder + 1] = name
end
 
local function new_switch_state()
    local state = { keyOrder = {}, keyToTemplates = {}, keySources = {} }
    for _, sw in ipairs(switchModeOrder) do
        state.keyOrder[sw] = {}
         state.keyToTemplates[sw] = {}
         state.keySources[sw] = {}
     end
     end
    return state
end
end


local function resolve_entry(data, id)
local function ensure_switch_key(state, sw, key)
     if type(data) ~= "table" then
    local byKey = state.keyToTemplates[sw]
         return nil
     if not byKey[key] then
        byKey[key] = {}
         state.keyOrder[sw][#state.keyOrder[sw] + 1] = key
     end
     end
    return byKey[key]
end


     if id and id ~= "" then
local function add_switch_entry(state, sw, key, entry)
        local direct = data[id]
     local bucket = ensure_switch_key(state, sw, key)
         if direct ~= nil then
    entry.idx = #bucket + 1
             return direct
    bucket[#bucket + 1] = entry
end
 
local function collect_tpl_calls(entries)
    local tplCalls = {}
    local sources = {}
    if #entries > 0 then
         sort_entries_by_priority(entries)
        for _, e in ipairs(entries) do
            tplCalls[#tplCalls + 1] = e.tpl
             sources[#sources + 1] = e.source
         end
         end
    end
    return tplCalls, sources
end


        local idsTable = data.id
local function makeSourceLink(s)
         if type(idsTable) == "table" then
    local className =
             local specific = idsTable[id]
        (s.name:sub(1, 1):upper() .. s.name:sub(2)) ..
             if type(specific) == "table" then
         (s.kind and (s.kind:sub(1, 1):upper() .. s.kind:sub(2)) or "")
                 local base = data["default"]
 
                 if type(base) == "table" then
    local tplLabel = "Template:" .. s.tplPath
                    local merged = deep_copy(base)
    return "[[" .. tplLabel .. "|" .. className .. "]]"
                    deep_merge(merged, specific)
end
                    return merged
 
local function renderTitleBlock(key, tplCalls, sources, includeHeader, frame, showSource)
    local parts = {}
    if tplCalls and #tplCalls > 0 then
        for i, tpl in ipairs(tplCalls) do
             local add = true
             if frame then
                local expanded = frame:preprocess(tpl)
                add = expanded and trim(expanded) ~= ""
            end
            if add then
                 local src = sources and sources[i]
                local line = '<div>' .. tpl .. '</div>'
                 if showSource and src then
                    line = line .. '<div class="ts-Сущность-field">' .. makeSourceLink(src) .. '</div>'
                 end
                 end
                 return deep_copy(specific)
                 parts[#parts + 1] = '<div class="ts-Сущность">' .. line .. '</div>'
             end
             end
        end
        if #parts == 0 then
            return ""
        end
        if includeHeader then
            table.insert(parts, 1, "<h2>" .. mw.text.encode(key) .. "</h2>")
         end
         end
     end
     end
    return table.concat(parts, "\n")
end


     local base = data["default"]
local function split_title_key(key)
     if type(base) == "table" then
     local main, sub = (key or ""):match("^([^_]+)_(.+)$")
         return deep_copy(base)
     if main and sub then
         return main, sub
     end
     end
     return nil
     return key, nil
end
end


local function collect_id_keys(data)
local function renderGroupedTitleBlocks(frame, keyOrder, keyToTemplates, noHeaders, showSource)
     if type(data) ~= "table" then
     local groups = {}
        return {}
     local groupOrder = {}
     end


     local idsTable = data.id
     for _, key in ipairs(keyOrder or {}) do
    local ids = {}
        local mainTitle, subTitle = split_title_key(key)
        if mainTitle and mainTitle ~= "" then
            local group = groups[mainTitle]
            if not group then
                group = { blocks = {} }
                groups[mainTitle] = group
                groupOrder[#groupOrder + 1] = mainTitle
            end


    if type(idsTable) == "table" then
             group.blocks[#group.blocks + 1] = {
        for k in pairs(idsTable) do
                subTitle = subTitle,
             ids[#ids + 1] = k
                entries = keyToTemplates[key] or {}
            }
         end
         end
        return ids
     end
     end


     for k in pairs(data) do
    local out = {}
         if k ~= "default" and k ~= "id" then
     for _, mainTitle in ipairs(groupOrder) do
             ids[#ids + 1] = k
         local group = groups[mainTitle]
        local parts = {}
 
        if not noHeaders then
             parts[#parts + 1] = "<h2>" .. mw.text.encode(mainTitle) .. "</h2>"
         end
         end
    end
    return ids
end


local function contains_target(v, target)
         for _, block in ipairs(group.blocks or {}) do
    if type(v) == "table" then
            local tplCalls, sources = collect_tpl_calls(block.entries or {})
         if is_array(v) then
            local blockText = renderTitleBlock(block.subTitle or mainTitle, tplCalls, sources, false, frame, showSource)
            for _, item in ipairs(v) do
            if blockText ~= "" then
                if tostring(item) == target then
                if block.subTitle and not noHeaders then
                     return true
                     parts[#parts + 1] = "<h3>" .. mw.text.encode(block.subTitle) .. "</h3>"
                 end
                 end
                parts[#parts + 1] = blockText
             end
             end
            return false
         end
         end


         for _, item in pairs(v) do
         if #parts > 0 then
             if tostring(item) == target then
             out[#out + 1] = table.concat(parts, "\n")
                return true
            end
         end
         end
        return false
     end
     end


     return tostring(v) == target
     return table.concat(out, "\n")
end
end


local function is_nonempty_value(v)
local function normalizeFilterKey(s)
     if v == nil then return false end
     s = trim(s or "")
     if type(v) == "table" then
    s = s:gsub("%s*_%s*", "_")
         return next(v) ~= nil
    return s
end
 
local function matches_card_list(list, callKey, compositeKey)
     if not list then
         return false
     end
     end
     return true
    callKey = normalizeFilterKey(callKey)
    compositeKey = normalizeFilterKey(compositeKey)
     return list[callKey] or list[compositeKey] or false
end
end


local function find_matching_ids(idsTable, keyPath, searchValue)
local function buildCardCall(merged, entityId)
     local target = tostring(searchValue)
     local parts = {}
     local matches = {}
 
    if entityId and entityId ~= "" then
        parts[#parts + 1] = "id=" .. mw.text.encode(entityId)
     end
 
    if merged.tags and #merged.tags > 0 then
        table.sort(merged.tags)
        parts[#parts + 1] = "тип=" .. table.concat(merged.tags, ", ")
    end


     for idKey, entry in pairs(idsTable) do
     if merged.sections and #merged.sections > 0 then
        if type(entry) == "table" then
        table.sort(merged.sections, function(a, b)
             local v = get_by_path(entry, keyPath)
            if a == "Сущность" then return true end
             if v ~= nil and contains_target(v, target) then
            if b == "Сущность" then return false end
                 matches[#matches + 1] = idKey
             return a < b
        end)
        parts[#parts + 1] = "sections=" .. table.concat(merged.sections, ", ")
        for _, section in ipairs(merged.sections) do
            local labels = merged.labelLists[section]
             if labels and #labels > 0 then
                local enc = {}
                for i = 1, #labels do
                    enc[i] = mw.text.encode(labels[i])
                 end
                parts[#parts + 1] = mw.text.encode(section) .. "=" .. table.concat(enc, ", ")
             end
             end
         end
         end
     end
     end


     return matches
     for compositeKey, displayLabel in pairs(merged.labelOverrides or {}) do
end
        if displayLabel and displayLabel ~= "" then
            parts[#parts + 1] = mw.text.encode(compositeKey .. "_label") .. "=" .. displayLabel
        end
    end
    for compositeKey, content in pairs(merged.contentByKey or {}) do
        if content and content ~= "" then
            parts[#parts + 1] = mw.text.encode(compositeKey) .. "=" .. content
        end
    end


local function preprocess_or_return(frame, text)
     if #parts == 0 then
     if type(frame.preprocess) == "function" then
         return ""
         return frame:preprocess(text)
     end
     end
     return text
 
     return "{{карточка/сущность|" .. table.concat(parts, "|") .. "}}"
end
end


local function get_field_loose(entry, fieldId)
local function cardWrapper(frame, keyOrder, keyToTemplates, keySources, entityId, noHeaders, cardFilter)
     local value = entry[fieldId]
     local merged = {
    if value ~= nil then return value end
        sections = {},
    if fieldId == "" then return nil end
        sectionsMap = {},
        labelLists = {},
        labelSets = {},
        labelOverrides = {},
        contentByKey = {},
        tags = {},
        tagSet = {}
    }
    local rawContentParts = {}
    for _, callKey in ipairs(keyOrder or {}) do
        local entries = keyToTemplates[callKey] or {}
        if #entries > 0 then
            sort_entries_by_priority(entries)
            for _, e in ipairs(entries) do
                local displayLabel = trim(frame:preprocess(e.tplLabel or "") or "")
                local content = trim(frame:preprocess(e.tplContent or "") or "")
                local tagText = ""
                if e.tplTag then
                    tagText = trim(frame:preprocess(e.tplTag or "") or "")
                end
                local compositeKey = (callKey:find("_", 1, true)) and callKey or ("Сущность_" .. callKey)
                local section = (callKey:find("_", 1, true)) and callKey:match("^([^_]+)") or "Сущность"


    local first = string.sub(fieldId, 1, 1)
                local isWhitelisted = cardFilter and matches_card_list(cardFilter.whitelist, callKey, compositeKey) or
    local tail = string.sub(fieldId, 2)
                    false
    value = entry[string.lower(first) .. tail]
                local isBlacklisted = cardFilter and matches_card_list(cardFilter.blacklist, callKey, compositeKey) or
    if value ~= nil then return value end
                    false
                if isWhitelisted and content ~= "" then
                    rawContentParts[#rawContentParts + 1] = content
                end


    return entry[string.upper(first) .. tail]
                local allowCardEntry = not isWhitelisted and not isBlacklisted and
end
                    ((not cardFilter) or (not cardFilter.hasWhitelist) or
                        matches_card_list(cardFilter.cardWhitelist, callKey, compositeKey))
 
                if allowCardEntry and (displayLabel ~= "" or content ~= "") then
                    if not merged.sectionsMap[section] then
                        merged.sectionsMap[section] = true
                        merged.sections[#merged.sections + 1] = section
                    end
                    if displayLabel ~= "" and (not merged.labelOverrides[compositeKey] or merged.labelOverrides[compositeKey] == "") then
                        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 {}
                    if not merged.labelSets[section][compositeKey] then
                        merged.labelSets[section][compositeKey] = true
                        local cur = merged.labelLists[section] or {}
                        cur[#cur + 1] = compositeKey
                        merged.labelLists[section] = cur
                    end
                end


local function apply_pattern(s, pattern, repl)
                if allowCardEntry and tagText ~= "" then
    if not pattern or pattern == "" or not s then
                    if not merged.tagSet[tagText] then
         return s
                        merged.tagSet[tagText] = true
                        merged.tags[#merged.tags + 1] = tagText
                    end
                end
            end
         end
     end
     end


     local text = tostring(s)
     each_csv_value(frame.args.cardTag or "", function(extraTag)
    local replacement
        if not merged.tagSet[extraTag] then
    if repl and repl ~= "" then
            merged.tagSet[extraTag] = true
        replacement = tostring(repl)
            merged.tags[#merged.tags + 1] = extraTag
        replacement = replacement:gsub("\\(%d)", "%%%1")
         end
    else
     end)
         replacement = "%1"
     end


     local patt = pattern
     local out = {}
     if not patt:find("%^") and not patt:find("%$") then
     if #rawContentParts > 0 then
         patt = "^" .. patt .. "$"
         out[#out + 1] = table.concat(rawContentParts, "\n")
     end
     end


     return (text:gsub(patt, replacement))
     local cardCall = buildCardCall(merged, entityId)
end


local function flatten_parts(entry)
     if noHeaders then
     if type(entry) ~= "table" then return {} end
        local hasLabel = false
 
         for _, v in pairs(merged.labelOverrides or {}) do
    local parts = {}
            if v and v ~= "" then
    local function append_table_json(key, value)
                hasLabel = true
         local ok, json = pcall(mw.text.jsonEncode, value)
                break
        if ok and json then
             end
             parts[#parts + 1] = key .. "=" .. to_nowiki(json)
         end
         end
    end
         if not hasLabel then
 
            for _, lst in pairs(merged.labelLists or {}) do
    local function walk(tbl, prefix)
                 if #lst > 0 then
         local keys = {}
                     hasLabel = true
        for k in pairs(tbl) do keys[#keys + 1] = k end
                     break
        table.sort(keys, function(a, b) return tostring(a) < tostring(b) end)
        for _, k in ipairs(keys) do
            local v = tbl[k]
            local kStr = tostring(k)
            local key = (prefix == "" and kStr or prefix .. "." .. kStr)
            if type(v) == "table" then
                 if next(v) == nil then
                else
                    append_table_json(key, v)
                     if is_array(v) then
                        local first = v[1]
                        if type(first) == "table" then
                            walk(first, key)
                        end
                    else
                        walk(v, key)
                     end
                 end
                 end
            else
                parts[#parts + 1] = key .. "=" .. tostring(v)
             end
             end
         end
         end
    end


    walk(entry, "")
        local hasContent = false
        for _, v in pairs(merged.contentByKey or {}) do
            if v and v ~= "" then
                hasContent = true
                break
            end
        end


    return parts
        if not hasLabel and not hasContent then
end
            return table.concat(out, "\n")
        end
    end


local function flatten_entry(entry)
     if cardCall ~= "" then
    local parts = flatten_parts(entry)
         out[#out + 1] = cardCall
     if #parts == 0 then
         return ""
     end
     end
     return table.concat(parts, "|")
 
     return table.concat(out, "\n")
end
end


function p.findInGenerator(frame)
register_switch_mode("card", {
    local args = frame.args or {}
    full = true,
    local searchId = args[1] or ""
    build_entry = function(ctx, key)
    local kind = (args[2] or ""):lower()
        return {
    local fieldId = args[3] or ""
            tplLabel = makeTplCall(ctx.tplPath, "cardLabel", key, ctx.id, ctx.extra),
 
            tplContent = makeTplCall(ctx.tplPath, "cardContent", key, ctx.id, ctx.extra),
     if searchId == "" or fieldId == "" then
            tplTag = makeTplCall(ctx.tplPath, "cardTag", key, ctx.id, ctx.extra),
         return ""
            source = ctx.source,
     end
            priority = ctx.priority
     if kind ~= "prototype" and kind ~= "component" then
        }
         return ""
    end,
     build_preview_entry = function(ctx, key)
        return {
            tplLabel = makeTplCall(ctx.tplPath, "cardLabel", key, ""),
            tplContent = makeTplCall(ctx.tplPath, "cardContent", key, ""),
            source = ctx.source,
            priority = ctx.priority
         }
     end,
     add_entity_extras = function(state, parsed, ctx)
        if type(parsed) == "table" and parsed.cardTag and parsed.cardTag ~= "" then
            add_switch_entry(state, "card", "cardTag", {
                tplLabel = "",
                tplContent = "",
                tplTag = makeTplCall(ctx.tplPath, "cardTag", "cardTag", ctx.id, ctx.extra),
                source = ctx.source,
                priority = ctx.priority
            })
        end
    end,
    render_full = function(frame, keyOrder, keyToTemplates, keySources, entityId, noHeaders, showSource, cardFilter)
         return cardWrapper(frame, keyOrder, keyToTemplates, keySources, entityId, noHeaders, cardFilter)
     end
     end
})


     local storeName = (kind == "prototype") and "prototype_store.json" or "component_store.json"
register_switch_mode("title", {
     local moduleName = get_module_name(storeName)
    full = true,
    local data = load_cached_data(moduleName)
     build_entry = function(ctx, key)
     if not data then
        return {
         return ""
            tpl = makeTplCall(ctx.tplPath, "title", key, ctx.id, ctx.extra),
            source = ctx.source,
            priority = ctx.priority
        }
    end,
     build_preview_entry = function(ctx, key)
        return {
            tpl = makeTplCall(ctx.tplPath, "title", key, ""),
            source = ctx.source,
            priority = ctx.priority
        }
     end,
    render_full = function(frame, keyOrder, keyToTemplates, keySources, entityId, noHeaders, showSource)
         return renderGroupedTitleBlocks(frame, keyOrder, keyToTemplates, noHeaders, showSource)
     end
     end
})


     local entry = data[searchId]
local function getTemplateMeta(frame, tplPath)
    if type(entry) ~= "table" then
     local expanded = frame:expandTemplate {
         return ""
        title = tplPath,
     end
         args = { "json" }
     }


     local value = get_field_loose(entry, fieldId)
     local ok, data = pcall(mw.text.jsonDecode, expanded)
     if value == nil then
     if not ok or type(data) ~= "table" then
         return ""
         return ""
     end
     end


     local out = {}
     if data.card == nil then
    local t = type(value)
        local cardKeys = {}
    if t == "table" then
        local seen = {}
        for _, v in ipairs(value) do
        for base, labels in pairs(data) do
            out[#out + 1] = v
            if type(base) == "string" and base ~= "card" and base:sub(1, 4) == "card" and type(labels) == "table" then
                for _, lab in ipairs(labels) do
                    if not seen[lab] then
                        seen[lab] = true
                        cardKeys[#cardKeys + 1] = lab
                    end
                end
            end
         end
         end
    else
         data.card = cardKeys
         out[1] = value
     end
     end


     return mw.text.jsonEncode(out)
     return data
end
end


function p.flattenField(frame)
local function parseListArg(str)
     local args = frame.args or {}
     local res = {}
     local id = args[1] or ""
     if not str or str == "" then return res end
     local pagePath = args[2] or ""
     for item in string.gmatch(str, "[^,]+") do
    if id == "" or pagePath == "" then return "" end
        local s = normalizeFilterKey(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


     local moduleName = get_module_name(pagePath)
local function build_key_filter(args)
     local data = load_cached_data(moduleName)
     local filter = {}
     if not data then return "" end
    filter.blacklist = parseListArg(args.blacklist or "")
     filter.whitelist = parseListArg(args.whitelist or "")
    filter.hasWhitelist = false
    for _, sw in ipairs(switchModeOrder) do
        if filter.whitelist[sw] and next(filter.whitelist[sw]) ~= nil then
            filter.hasWhitelist = true
            break
        end
    end
     if not filter.hasWhitelist and filter.whitelist.cardContent and next(filter.whitelist.cardContent) ~= nil then
        filter.hasWhitelist = true
    end
    return filter
end


     local entry = resolve_entry(data, id) or {}
local function build_render_options(filter)
     return flatten_entry(entry)
     return {
        noHeaders = false,
        cardFilter = {
            hasWhitelist = filter.whitelist.cardContent and next(filter.whitelist.cardContent) ~= nil or false,
            cardWhitelist = filter.whitelist.card or {},
            blacklist = filter.blacklist.cardContent or {},
            whitelist = filter.whitelist.cardContent or {}
        }
     }
end
end


function p.get(frame)
local function should_include_key(filter, sw, key)
     local args = frame.args or {}
     if filter.hasWhitelist then
    local id = args[1] or ""
        if filter.whitelist[sw] and filter.whitelist[sw][key] then
     local pagePath = args[2] or ""
            return true
    local keyPath = args[3] or ""
        end
        if sw == "card" and filter.whitelist.cardContent and filter.whitelist.cardContent[key] then
            return true
        end
        return false
    end
     return not (filter.blacklist[sw] and filter.blacklist[sw][key])
end


     if pagePath == "" then return "" end
local function parse_csv_set(str)
     local res = {}
    each_csv_value(str, function(name)
        res[name] = true
    end)
    return res
end


    local moduleName = get_module_name(pagePath)
local function apply_entity_set_filters(foundSet, whitelistSet, blacklistSet)
     local data = load_cached_data(moduleName)
     local hasWhitelist = next(whitelistSet or {}) ~= nil
    if not data then return "" end


     local entry = resolve_entry(data, id)
     if hasWhitelist then
    if entry == nil then return "" end
        for name in pairs(foundSet) do
            if not whitelistSet[name] then
                foundSet[name] = nil
            end
        end
    end


     if keyPath == "" then
     for name in pairs(blacklistSet or {}) do
         return format_value(entry)
         foundSet[name] = nil
     end
     end
    local value = get_by_path(entry, keyPath)
    return format_value(value)
end
end


function p.getId(frame)
local function collect_entity_sets(id, prototypeStoreDefs, componentWhitelist, componentBlacklist, prototypeWhitelist, prototypeBlacklist)
     local args = frame.args or {}
     local foundComponents, foundPrototypes = {}, {}
    local searchValue = args[1] or ""
    local pagePath = args[2] or ""
    local keyPath = args[3] or ""
    local searchType = (args.searchType or ""):lower()


     if searchValue == "" or pagePath == "" or keyPath == "" then
    local compList = load_entity_components(id)
         return ""
     if type(compList) == "table" then
        for _, v in ipairs(compList) do
            if type(v) == "string" and v ~= "" then
                foundComponents[v] = true
            end
         end
     end
     end
     if searchType == "" then
 
         searchType = "value"
    local protoStore = prototypeStoreDefs and prototypeStoreDefs[id]
     if type(protoStore) == "table" then
         for protoName in pairs(protoStore) do
            if type(protoName) == "string" and protoName ~= "" then
                foundPrototypes[protoName] = true
            end
        end
     end
     end


     local moduleName = get_module_name(pagePath)
     apply_entity_set_filters(foundComponents, parse_csv_set(componentWhitelist), parse_csv_set(componentBlacklist))
     local data = load_cached_data(moduleName)
     apply_entity_set_filters(foundPrototypes, parse_csv_set(prototypeWhitelist), parse_csv_set(prototypeBlacklist))
    if not data then return "[]" end


     local ids = collect_id_keys(data)
     return foundComponents, foundPrototypes
    if #ids == 0 then
end
        return ""
    end


    local matches
local function resolve_priority(parsed)
    if searchType == "key" then
    local basePriority = 1
        local target = tostring(searchValue)
    if type(parsed) == "table" and parsed.priority ~= nil then
        matches = {}
        if type(parsed.priority) == "number" then
        for _, idKey in ipairs(ids) do
            basePriority = parsed.priority
            local entry = resolve_entry(data, idKey)
         else
            if type(entry) == "table" then
             local pnum = tonumber(parsed.priority)
                local v = get_by_path(entry, keyPath)
             if pnum then
                if type(v) == "table" and v[target] ~= nil then
                 basePriority = pnum
                    matches[#matches + 1] = idKey
                end
            end
         end
    else
        local target = tostring(searchValue)
        matches = {}
        for _, idKey in ipairs(ids) do
             local entry = resolve_entry(data, idKey)
             if type(entry) == "table" then
                 local v = get_by_path(entry, keyPath)
                if v ~= nil and contains_target(v, target) then
                    matches[#matches + 1] = idKey
                end
             end
             end
         end
         end
     end
     end
    return basePriority
end


     if #matches == 0 then
local function get_selective_extra(id, dataPage, paramNames)
     if not dp or type(dp.flattenFieldSelectiveDirect) ~= "function" then
         return ""
         return ""
     end
     end
 
     if type(paramNames) ~= "table" or #paramNames == 0 then
     local ok, json = pcall(mw.text.jsonEncode, matches)
         return ""
    if ok and json then
         return json
     end
     end


     return ""
     return dp.flattenFieldSelectiveDirect(id, dataPage, paramNames) or ""
end
end


function p.getTplId(frame)
local function add_card_tag_value(tags, seen, value)
     local args = frame.args or {}
     value = trim(value or "")
    local searchValue = args[1] or ""
     if value == "" or seen[value] then
    local pagePath = args[2] or ""
         return
    local keyPath = args[3] or ""
    local tplPath = mw.text.unstripNoWiki(args[4] or "")
    local searchType = (args.searchType or ""):lower()
 
     if searchType == "" then
        searchType = "value"
    end
    if searchType == "path" then
        searchValue = ""
        pagePath = args[1] or ""
        keyPath = args[2] or ""
        tplPath = mw.text.unstripNoWiki(args[3] or "")
    end
    if pagePath == "" or keyPath == "" or tplPath == "" then
         return ""
    end
    if searchType ~= "path" and searchValue == "" then
        return ""
     end
     end
    seen[value] = true
    tags[#tags + 1] = value
end


    local moduleName = get_module_name(pagePath)
local function merge_card_tag_text(...)
     local data = load_cached_data(moduleName)
     local tags = {}
     if not data then return "" end
     local seen = {}


     local ids = collect_id_keys(data)
     for i = 1, select("#", ...) do
    if #ids == 0 then
        each_csv_value(select(i, ...), function(value)
         return ""
            add_card_tag_value(tags, seen, value)
         end)
     end
     end


     local matches
     return table.concat(tags, ", ")
    if searchType == "path" then
end
        matches = {}
 
        for _, idKey in ipairs(ids) do
local function add_entries_from_meta(state, parsed, ctx, filter, isPreview)
            local entry = resolve_entry(data, idKey)
    for _, sw in ipairs(switchModeOrder) do
            if type(entry) == "table" then
        local mode = switchModeRegistry[sw] or {}
                local v = get_by_path(entry, keyPath)
        local keys
                if is_nonempty_value(v) then
        if type(mode.get_keys) == "function" then
                    matches[#matches + 1] = idKey
            keys = mode.get_keys(parsed)
                end
        else
            end
            keys = (type(parsed) == "table" and parsed[sw]) or {}
         end
         end
    elseif searchType == "key" then
 
        local target = tostring(searchValue)
        if type(keys) == "table" then
        matches = {}
            for _, key in ipairs(keys) do
        for _, idKey in ipairs(ids) do
                if (not filter) or should_include_key(filter, sw, key) then
            local entry = resolve_entry(data, idKey)
                    local buildFn = isPreview and (mode.build_preview_entry or mode.build_entry) or mode.build_entry
            if type(entry) == "table" then
                    if type(buildFn) == "function" then
                local v = get_by_path(entry, keyPath)
                        local entry = buildFn(ctx, key)
                if type(v) == "table" and v[target] ~= nil then
                        if entry then
                     matches[#matches + 1] = idKey
                            add_switch_entry(state, sw, key, entry)
                        end
                     end
                 end
                 end
             end
             end
         end
         end
    else
 
         local target = tostring(searchValue)
         if (not isPreview) and type(mode.add_entity_extras) == "function" then
        matches = {}
            mode.add_entity_extras(state, parsed, ctx)
        for _, idKey in ipairs(ids) do
            local entry = resolve_entry(data, idKey)
            if type(entry) == "table" then
                local v = get_by_path(entry, keyPath)
                if v ~= nil and contains_target(v, target) then
                    matches[#matches + 1] = idKey
                end
            end
         end
         end
     end
     end
end


     if #matches == 0 then
local function extract_whitelist_search_strings(keyFilter)
         return ""
     if not keyFilter or not keyFilter.hasWhitelist then
         return nil
     end
     end


     local out = {}
     local strings = {}
     for _, idKey in ipairs(matches) do
     for sw, keys in pairs(keyFilter.whitelist) do
         local tpl = p.getTpl({ args = { idKey, pagePath, tplPath }, data = data })
         if type(keys) == "table" then
        if tpl ~= "" then
             for key in pairs(keys) do
             out[#out + 1] = tpl
                strings[#strings + 1] = key
            end
         end
         end
     end
     end


     if #out == 0 then
     if #strings == 0 then
         return ""
         return nil
     end
     end


    local result = table.concat(out, " ")
     return strings
     return preprocess_or_return(frame, result)
end
end


function p.getTpl(frame)
local function content_matches_whitelist(content, searchStrings)
    local args = frame.args or {}
     if not searchStrings then
    local id = args[1] or ""
         return true
    local pagePath = args[2] or ""
    local tplPath = mw.text.unstripNoWiki(args[3] or "")
 
     if id == "" or pagePath == "" or tplPath == "" then
         return ""
     end
     end
 
     if not content then
    local moduleName = get_module_name(pagePath)
         return false
    local data = frame.data
    if not data then
        data = load_cached_data(moduleName)
    end
     if not data then
         return ""
     end
     end


     local entry = resolve_entry(data, id)
     for _, s in ipairs(searchStrings) do
    local extra = flatten_entry(entry)
        if string.find(content, s, 1, true) then
    local tplStr = "{{" .. tostring(tplPath) .. "|id=" .. tostring(id)
            return true
    if extra ~= "" then
         end
         tplStr = tplStr .. "|" .. extra
     end
     end
    tplStr = tplStr .. "}}"


     return preprocess_or_return(frame, tplStr)
     return false
end
end


function p.getTplGenerator(frame)
local function each_entity_data(frame, id, onEntity, onMissing, keyFilter)
     local args = frame.args or {}
     local componentWhitelist = frame.args.componentWhitelist or frame.args.componentwhitelist or ""
    local searchId = args[1] or ""
     local componentBlacklist = frame.args.componentBlacklist or frame.args.componentblacklist or ""
     local kind = (args[2] or ""):lower()
     local prototypeWhitelist = frame.args.prototypeWhitelist or frame.args.prototypewhitelist or ""
     local generatorId = args[3] or ""
     local prototypeBlacklist = frame.args.prototypeBlacklist or frame.args.prototypeblacklist or ""
     local tplPath = mw.text.unstripNoWiki(args[4] or "")


     if searchId == "" or generatorId == "" or tplPath == "" then
     local prototypeStoreDefs = load_module_data("prototype_store.json")
        return ""
     if not prototypeStoreDefs then
    end
         return false
     if kind ~= "prototype" and kind ~= "component" then
         return ""
     end
     end


     local dir = (kind == "prototype") and "prototype/" or "component/"
     local foundComponents, foundPrototypes = collect_entity_sets(id, prototypeStoreDefs,
     local pagePath = dir .. generatorId .. ".json"
        componentWhitelist, componentBlacklist, prototypeWhitelist, prototypeBlacklist)
 
    local compWhitelistSet = parse_csv_set(componentWhitelist)
    local compBlacklistSet = parse_csv_set(componentBlacklist)
    local protoWhitelistSet = parse_csv_set(prototypeWhitelist)
    local protoBlacklistSet = parse_csv_set(prototypeBlacklist)
 
    local compHasWhitelist = next(compWhitelistSet) ~= nil
    local protoHasWhitelist = next(protoWhitelistSet) ~= nil
    local anyEntityWhitelist = compHasWhitelist or protoHasWhitelist
 
     local whitelistSearchStrings = extract_whitelist_search_strings(keyFilter)


     local idsJson = p.findInGenerator({ args = { searchId, kind, generatorId } })
     local function processEntity(kind, name, isStore)
    local ok, ids = pcall(mw.text.jsonDecode, idsJson or "")
        local pathName = ucfirst(name)
    if not ok or type(ids) ~= "table" or #ids == 0 then
        local tplPath = kind .. "/" .. pathName
         return ""
        if isStore then
    end
            tplPath = tplPath .. "/store"
         end


    local moduleName = get_module_name(pagePath)
        local content = load_template_content(tplPath)
    local data = load_cached_data(moduleName)
        if not content then
    if not data then
            if onMissing then
        return ""
                onMissing(kind, name, isStore, tplPath)
    end
            end
            return
        end


    local out = {}
         if not content_matches_whitelist(content, whitelistSearchStrings) then
    for _, id in ipairs(ids) do
             return
         local tpl = p.getTpl({ args = { id, pagePath, tplPath }, data = data })
        if tpl ~= "" then
             out[#out + 1] = tpl
         end
         end
    end


    local result = table.concat(out, " ")
        local parsed = getTemplateMeta(frame, tplPath)
    return preprocess_or_return(frame, result)
        if type(parsed) ~= "table" then
end
            parsed = {}
        end


function p.flattenParams(entry)
        local extra = ""
    return flatten_parts(entry)
        local paramNames = get_template_params(tplPath, content)
end
        if dp then
            local dataPage = tplPath .. ".json"
            extra = get_selective_extra(id, dataPage, paramNames)
        end


function p.getGenerator(frame)
        onEntity(parsed, {
    local args = frame.args or {}
            tplPath = tplPath,
    local searchId = args[1] or ""
            id = id,
    local kind = (args[2] or ""):lower()
            extra = extra,
     local generatorId = args[3] or ""
            source = make_source(kind, name, pathName, tplPath),
            priority = resolve_priority(parsed)
        })
     end


     if searchId == "" or generatorId == "" then
     for compName in pairs(foundComponents) do
         return ""
        if not anyEntityWhitelist or compHasWhitelist then
            processEntity("component", compName, false)
         end
     end
     end
     if kind ~= "prototype" and kind ~= "component" then
     for protoName in pairs(foundPrototypes) do
         return ""
        if not anyEntityWhitelist or protoHasWhitelist then
            processEntity("prototype", protoName, false)
         end
     end
     end


     local idsJson = p.findInGenerator({ args = { searchId, kind, generatorId } })
     local componentStoreDefs = load_module_data("component_store.json")
     local ok, ids = pcall(mw.text.jsonDecode, idsJson or "")
     if type(componentStoreDefs) == "table" and (not anyEntityWhitelist or compHasWhitelist) then
    if not ok or type(ids) ~= "table" or #ids == 0 then
        local compStore = componentStoreDefs[id]
        return ""
        if type(compStore) == "table" then
            for compName in pairs(compStore) do
                local allowed = true
                if compBlacklistSet[compName] then
                    allowed = false
                elseif compHasWhitelist and not compWhitelistSet[compName] then
                    allowed = false
                end
                if allowed then
                    processEntity("component", compName, true)
                end
            end
        end
     end
     end


     local okOut, outJson = pcall(mw.text.jsonEncode, ids)
     if type(prototypeStoreDefs) == "table" and (not anyEntityWhitelist or protoHasWhitelist) then
    if okOut and outJson then
        local protoStore = prototypeStoreDefs[id]
         return outJson
        if type(protoStore) == "table" then
            for protoName in pairs(protoStore) do
                local allowed = true
                if protoBlacklistSet[protoName] then
                    allowed = false
                elseif protoHasWhitelist and not protoWhitelistSet[protoName] then
                    allowed = false
                end
                if allowed then
                    processEntity("prototype", protoName, true)
                end
            end
         end
     end
     end


     return ""
     return true
end
end


function p.hasComp(frame)
local function collect_card_tag_text(frame, args, id)
     local args = frame.args or {}
     local filter = build_key_filter(args)
    local entityId = args[1] or ""
    local cardFilter = build_render_options(filter).cardFilter
    local compName = args[2] or ""
    local entries = {}
 
    local ok = each_entity_data(frame, id, function(parsed, ctx)
        local keys = parsed.card or {}
 
        if type(keys) == "table" then
            for _, key in ipairs(keys) do
                local compositeKey = (key:find("_", 1, true)) and key or ("Сущность_" .. key)
                local isWhitelisted = matches_card_list(cardFilter.whitelist, key, compositeKey)
                local isBlacklisted = matches_card_list(cardFilter.blacklist, key, compositeKey)
                local allowCardEntry = not isWhitelisted and not isBlacklisted and
                    ((not cardFilter.hasWhitelist) or matches_card_list(cardFilter.cardWhitelist, key, compositeKey))


    if entityId == "" or compName == "" then
                if allowCardEntry then
         return "false"
                    entries[#entries + 1] = {
    end
                        tplTag = makeTplCall(ctx.tplPath, "cardTag", key, ctx.id, ctx.extra),
                        priority = ctx.priority,
                        idx = #entries + 1
                    }
                end
            end
         end


    local moduleName = get_module_name("component.json")
        if parsed.cardTag and parsed.cardTag ~= "" then
    local data = load_cached_data(moduleName)
            entries[#entries + 1] = {
    if not data then
                tplTag = makeTplCall(ctx.tplPath, "cardTag", "cardTag", ctx.id, ctx.extra),
         return "false"
                priority = ctx.priority,
     end
                idx = #entries + 1
            }
         end
     end)


     if type(data) ~= "table" then
     if not ok then
         return "false"
         return trim(args.tag or "")
     end
     end


     local entry = data[entityId]
     sort_entries_by_priority(entries)
    if type(entry) ~= "table" then
        return "false"
    end


     local target = tostring(compName)
     local tags = {}
     for _, v in ipairs(entry) do
    local seen = {}
         if tostring(v) == target then
     for _, entry in ipairs(entries) do
            return "true"
         local tagText = trim(frame:preprocess(entry.tplTag or "") or "")
         end
         add_card_tag_value(tags, seen, tagText)
     end
     end


     return "false"
     return merge_card_tag_text(table.concat(tags, ", "), args.tag)
end
end


function p.GeneratorId(frame)
p.mergeCardTagText = merge_card_tag_text
    local args = frame.args or {}
p.collectCardTagText = collect_card_tag_text
    local pagePath = args[1] or ""
    local replace = mw.text.unstripNoWiki(args.replace or "")
    local pattern = mw.text.unstripNoWiki(args.pattern or "(.*)")


     if pagePath == "" then
local function build_missing_template_error(kind, name, isStore, tplPath)
         return ""
     local baseType = (kind and (kind:sub(1, 1):upper() .. kind:sub(2)) or "")
    local classType = baseType
    if isStore then
         classType = classType .. "Store"
     end
     end
    local className = name .. baseType
    local tplLabel = "Template:" .. tplPath
    return "{{сущность/infobox/base|тип=" .. classType .. "|название=" .. className .. "|ссылка=" .. tplLabel .. "}}"
end


     local moduleName = get_module_name(pagePath)
local function renderBlocks(frame, state, renderOptions, entityId, showSource)
    local data = load_cached_data(moduleName)
    local outLocal = {}
    if not data then
    local noHeaders = renderOptions and renderOptions.noHeaders
         return ""
     local cardFilter = renderOptions and renderOptions.cardFilter
    for _, sw in ipairs(switchModeOrder) do
        local mode = switchModeRegistry[sw] or {}
        if mode.full then
            local outStr = ""
            if type(mode.render_full) == "function" then
                outStr = mode.render_full(frame, state.keyOrder[sw], state.keyToTemplates[sw], state.keySources[sw],
                    entityId, noHeaders, showSource, cardFilter)
            end
            if outStr and outStr ~= "" then outLocal[#outLocal + 1] = outStr end
         else
            for _, key in ipairs(state.keyOrder[sw] or {}) do
                local entries = state.keyToTemplates[sw][key] or {}
                if type(mode.render_key) == "function" then
                    local outStr = mode.render_key(frame, key, entries, noHeaders, showSource)
                    if outStr and outStr ~= "" then outLocal[#outLocal + 1] = outStr end
                end
            end
        end
     end
     end
    return outLocal
end


     local idsTable = data.id
function p.get(frame)
     if type(idsTable) ~= "table" then
     local args = getArgs(frame, { removeBlanks = false })
        return ""
    local id = args[1] or ""
    end
     if id == "" then return "" end


     local ids = {}
     local showSource = trim(args.showSource or "") == ""
    for k in pairs(idsTable) do
        ids[#ids + 1] = k
    end


     table.sort(ids)
     local filter = build_key_filter(args)
    local renderOptions = build_render_options(filter)


     if replace ~= "" then
     local state = new_switch_state()
        local out = {}
    local errors = {}
        for _, id in ipairs(ids) do
    local ok = each_entity_data(frame, id, function(parsed, ctx)
            local text = apply_pattern(id, pattern, replace)
        add_entries_from_meta(state, parsed, ctx, filter, false)
            if text ~= "" then
    end, function(kind, name, isStore, tplPath)
                out[#out + 1] = text
        if not filter.hasWhitelist then
            end
            errors[#errors + 1] = build_missing_template_error(kind, name, isStore, tplPath)
         end
         end
        if #out == 0 then
    end, filter)
            return ""
    if not ok then return "" end
        end
 
         return preprocess_or_return(frame, table.concat(out, "\n"))
    local out = {}
 
    if #errors > 0 then
         out[#out + 1] = '{{сущность/infobox|' .. table.concat(errors, "\n") .. '}}'
     end
     end


     local ok, json = pcall(mw.text.jsonEncode, ids)
    renderOptions.noHeaders = filter.hasWhitelist
     if ok and json then
 
         return json
     local blocks = renderBlocks(frame, state, renderOptions, id, showSource)
     for _, b in ipairs(blocks) do
         out[#out + 1] = b
     end
     end


     return ""
     return frame:preprocess(table.concat(out, "\n"))
end
end


function p.GeneratorTplId(frame)
function p.preview(frame)
     local args = frame.args or {}
     local args = getArgs(frame, { removeBlanks = false })
     local pagePath = args[1] or ""
     local tplPath = args[1] or ""
     local tplPath = args[2] or ""
     if tplPath == "" then return "" end


     if pagePath == "" or tplPath == "" then
     local showSource = trim(args.nosource or "") == ""
    local previewFilter = build_key_filter(args)
    local renderOptions = build_render_options(previewFilter)
 
    local content = load_template_content(tplPath)
    if not content then
         return ""
         return ""
     end
     end


     local moduleName = get_module_name(pagePath)
     local parsed = getTemplateMeta(frame, tplPath) or {}
     local data = load_cached_data(moduleName)
     if type(parsed) ~= "table" then
    if not data then
         parsed = {}
         return ""
     end
     end


     local idsTable = data.id
     local state = new_switch_state()
     if type(idsTable) ~= "table" then
     add_entries_from_meta(state, parsed, {
         return ""
        tplPath = tplPath,
     end
        id = "",
        extra = "",
         source = make_source("", tplPath, tplPath, tplPath),
        priority = 1
    }, nil, true)
 
    local hasWhitelist = previewFilter.hasWhitelist
     renderOptions.noHeaders = hasWhitelist


     local out = {}
     local out = {}
 
     local blocks = renderBlocks(frame, state, renderOptions, "", showSource)
     for idKey in pairs(idsTable) do
    for _, b in ipairs(blocks) do
        local tpl = p.getTpl({ args = { idKey, pagePath, tplPath }, data = data })
         out[#out + 1] = b
         if tpl ~= "" then
            out[#out + 1] = tpl
        end
     end
     end


     table.sort(out)
     return frame:preprocess(table.concat(out, "\n"))
 
    local result = table.concat(out, " ")
    return preprocess_or_return(frame, result)
end
end


return p
return p