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

мНет описания правки
мНет описания правки
Строка 65: Строка 65:
     end
     end
     return table.concat(parts, "\n")
     return table.concat(parts, "\n")
end
local function buildCardCall(merged)
    if not merged.sections or #merged.sections == 0 then return "" end
    table.sort(merged.sections)
    local parts = { "sections=" .. table.concat(merged.sections, ", ") }
    for _, section in ipairs(merged.sections) do
        local labels = merged.labelLists[section]
        if labels and #labels > 0 then
            parts[#parts + 1] = mw.text.encode(section) .. "=" .. table.concat(labels, ", ")
        end
    end
    for label, content in pairs(merged.contentByLabel or {}) do
        if content and content ~= "" then
            parts[#parts + 1] = mw.text.encode(label) .. "=" .. mw.text.encode(content)
        end
    end
    return "{{карточка/сущность|" .. table.concat(parts, "|") .. "}}"
end
local function cardWrapper(frame, keyOrder, keyToTemplates, keySources)
    local merged = { sections = {}, sectionsMap = {}, labelLists = {}, contentByLabel = {} }
    for _, section in ipairs(keyOrder or {}) do
        local entries = keyToTemplates[section] or {}
        if #entries > 0 then
            table.sort(entries, function(a, b)
                if a.priority == b.priority then return a.idx < b.idx end
                return a.priority > b.priority
            end)
            for _, e in ipairs(entries) do
                local label = trim(frame:preprocess(e.tplLabel or "") or "")
                local content = trim(frame:preprocess(e.tplContent or "") or "")
                if label ~= "" then
                    if not merged.sectionsMap[section] then
                        merged.sectionsMap[section] = true
                        table.insert(merged.sections, section)
                    end
                    local cur = merged.labelLists[section] or {}
                    table.insert(cur, label)
                    merged.labelLists[section] = cur
                    merged.contentByLabel[label] = content
                end
            end
        end
    end
    return buildCardCall(merged)
end
end


Строка 70: Строка 116:
local switchConfigs = {
local switchConfigs = {
     card = {
     card = {
         wrapper = function(key, tplCalls, sources)
         wrapper = cardWrapper,
            if not tplCalls or #tplCalls == 0 then return "" end
        fullCard = true
            local calls = table.concat(tplCalls, " ")
            local srcStr = ""
            if sources and #sources > 0 then
                local srcParts = {}
                for _, s in ipairs(sources) do table.insert(srcParts, makeSourceLink(s)) end
                srcStr = " " .. table.concat(srcParts, " ")
            end
            return "{{карточка/Сущность|" .. mw.text.encode(key) .. "|" .. calls .. srcStr .. "}}"
        end
     },
     },
     title = {
     title = {
Строка 123: Строка 160:
     for _, sw in ipairs(switchesTbl) do
     for _, sw in ipairs(switchesTbl) do
         local cfg = configs[sw] or {}
         local cfg = configs[sw] or {}
         for _, key in ipairs(keyOrder[sw] or {}) do
         if cfg.fullCard and sw == "card" then
            local entries = keyToTemplates[sw][key] or {}
            local outStr = cfg.wrapper(frame, keyOrder[sw], keyToTemplates[sw], keySources[sw])
            local tplCalls = {}
            if outStr and outStr ~= "" then table.insert(outLocal, outStr) end
            local sources = {}
        else
            if #entries > 0 then
            for _, key in ipairs(keyOrder[sw] or {}) do
                table.sort(entries, function(a, b)
                local entries = keyToTemplates[sw][key] or {}
                    if a.priority == b.priority then return a.idx < b.idx end
                local tplCalls = {}
                    return a.priority > b.priority
                local sources = {}
                end)
                if #entries > 0 then
                for _, e in ipairs(entries) do
                    table.sort(entries, function(a, b)
                    table.insert(tplCalls, e.tpl)
                        if a.priority == b.priority then return a.idx < b.idx end
                    table.insert(sources, e.source)
                        return a.priority > b.priority
                    end)
                    for _, e in ipairs(entries) do
                        table.insert(tplCalls, e.tpl)
                        table.insert(sources, e.source)
                    end
                 end
                 end
            end
                if noHeaders and sw == "title" then
            if noHeaders and sw == "title" then
                    local outStr = renderTitleBlock(key, tplCalls, sources, false, frame)
                local outStr = renderTitleBlock(key, tplCalls, sources, false, frame)
                if outStr and outStr ~= "" then table.insert(outLocal, outStr) end
            else
                if cfg.wrapper then
                    local outStr = cfg.wrapper(key, tplCalls, sources, frame)
                     if outStr and outStr ~= "" then table.insert(outLocal, outStr) end
                     if outStr and outStr ~= "" then table.insert(outLocal, outStr) end
                else
                    if cfg.wrapper then
                        local outStr = cfg.wrapper(key, tplCalls, sources, frame)
                        if outStr and outStr ~= "" then table.insert(outLocal, outStr) end
                    end
                 end
                 end
             end
             end
Строка 195: Строка 237:
     end
     end


    local ok, dp = pcall(require, "Module:GetField")
     local errors = {}
     local errors = {}
     local function processEntity(kind, name)
     local function processEntity(kind, name)
Строка 212: Строка 255:
         end
         end
         local parsed = getTemplateMeta(frame, tplPath)
         local parsed = getTemplateMeta(frame, tplPath)
        local ok, dp = pcall(require, "Module:GetField")
         for _, sw in ipairs(switches) do
         for _, sw in ipairs(switches) do
             local keys = parsed[sw] or {}
             local keys = parsed[sw] or {}
Строка 232: Строка 274:
                         extra = dp.flattenField({ args = { id, dataPage } })
                         extra = dp.flattenField({ args = { id, dataPage } })
                     end
                     end
                     local tplStr = makeTplCall(tplPath, sw, key, id, extra)
                     local tplStr
                    local tplLabelStr, tplContentStr
                    if sw == "card" then
                        tplLabelStr = makeTplCall(tplPath, "cardLabel", key, id, extra)
                        tplContentStr = makeTplCall(tplPath, "cardContent", key, id, extra)
                    else
                        tplStr = makeTplCall(tplPath, sw, key, id, extra)
                    end
                     local priority = 1
                     local priority = 1
                     if parsed and parsed.priority ~= nil then
                     if parsed and parsed.priority ~= nil then
Строка 242: Строка 291:
                         end
                         end
                     end
                     end
                     local entry = {
                     local entry
                         tpl = tplStr,
                    if sw == "card" then
                        source = { kind = kind, name = name, pathName = pathName, tplPath = tplPath },
                        entry = {
                        priority = priority,
                            tplLabel = tplLabelStr,
                        idx = #switchKeyToTemplates[sw][key] + 1
                            tplContent = tplContentStr,
                     }
                            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)
                     table.insert(switchKeyToTemplates[sw][key], entry)
                 end
                 end
Строка 260: Строка 320:


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


Строка 292: Строка 352:
                 table.insert(switchKeyOrder[sw], key)
                 table.insert(switchKeyOrder[sw], key)
             end
             end
             local tplStr = makeTplCall(tplPath, sw, key, "")
             local entry
             local entry = {
            if sw == "card" then
                tpl = tplStr,
                entry = {
                source = { kind = "", name = tplPath, pathName = tplPath, tplPath = tplPath },
                    tplLabel = makeTplCall(tplPath, "cardLabel", key, ""),
                priority = 1,
                    tplContent = makeTplCall(tplPath, "cardContent", key, ""),
                idx = #switchKeyToTemplates[sw][key] + 1
                    source = { kind = "", name = tplPath, pathName = tplPath, tplPath = tplPath },
             }
                    priority = 1,
                    idx = #switchKeyToTemplates[sw][key] + 1
                }
             else
                entry = {
                    tpl = makeTplCall(tplPath, sw, key, ""),
                    source = { kind = "", name = tplPath, pathName = tplPath, tplPath = tplPath },
                    priority = 1,
                    idx = #switchKeyToTemplates[sw][key] + 1
                }
             end
             table.insert(switchKeyToTemplates[sw][key], entry)
             table.insert(switchKeyToTemplates[sw][key], entry)
         end
         end