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

мНет описания правки
мНет описания правки
Строка 20: Строка 20:
     if not ok then return nil end
     if not ok then return nil end
     return content
     return content
end
local function parse_keys_from_template(content, switches)
    if not content then return {} end
    local lower = content:lower()
    local result = {}
    for _, sw in ipairs(switches) do
        result[sw] = {}
        local swLower = sw:lower()
        local pos = lower:find("|%s*" .. swLower .. "%s*=")
        if pos then
            local startBrace = content:find("{{", pos)
            local region = nil
            if startBrace then
                local len = #content
                local i = startBrace
                local depth = 0
                while i <= len - 1 do
                    local two = content:sub(i, i + 1)
                    if two == "{{" then
                        depth = depth + 1; i = i + 2
                    elseif two == "}}" then
                        depth = depth - 1; i = i + 2
                        if depth == 0 then
                            region = content:sub(startBrace, i - 1); break
                        end
                    else
                        i = i + 1
                    end
                end
            end
            if not region then
                local substr = content:sub(pos)
                local endPos = substr:find("}}")
                if endPos then region = substr:sub(1, endPos) else region = substr end
            end
            for key in string.gmatch(region, "|%s*([^=|%}]-)%s*=") do
                local k = trim(key)
                if k ~= "" then table.insert(result[sw], k) end
            end
        end
    end
    return result
end
end


Строка 143: Строка 100:
         }
         }
     }
     }
    local function getTemplateMeta(frame, tplPath)
        local expanded = frame:expandTemplate {
            title = tplPath,
            args = { "json" }
        }
        local ok, data = pcall(mw.text.jsonDecode, expanded)
        if ok and type(data) == "table" then
            return data
        end
        return ""
    end


     local errors = {}
     local errors = {}
Строка 157: Строка 128:
             return
             return
         end
         end
         local parsed = parse_keys_from_template(content, switches)
         local parsed = getTemplateMeta(frame, tplPath)
         local ok, dp = pcall(require, "Module:GetField")
         local ok, dp = pcall(require, "Module:GetField")
         for _, sw in ipairs(switches) do
         for _, sw in ipairs(switches) do