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

мНет описания правки
Нет описания правки
Строка 40: Строка 40:
     local tplLabel = "Template:" .. s.tplPath
     local tplLabel = "Template:" .. s.tplPath
     return "[[" .. tplLabel .. "|" .. className .. "]]"
     return "[[" .. tplLabel .. "|" .. className .. "]]"
end
local function renderTitleBlock(key, tplCalls, sources, includeHeader)
    local parts = {}
    if includeHeader then table.insert(parts, "<h2>" .. mw.text.encode(key) .. "</h2>") end
    if tplCalls and #tplCalls > 0 then
        for i, tpl in ipairs(tplCalls) do
            local line = tpl
            local src = sources and sources[i]
            if src then line = line .. " " .. makeSourceLink(src) end
            table.insert(parts, "<p>" .. line .. "</p>")
        end
    end
    return table.concat(parts, "\n")
end
end


Строка 59: Строка 73:
     title = {
     title = {
         wrapper = function(key, tplCalls, sources)
         wrapper = function(key, tplCalls, sources)
             local parts = {}
             return renderTitleBlock(key, tplCalls, sources, true)
            table.insert(parts, "<h2>" .. mw.text.encode(key) .. "</h2>")
            if tplCalls and #tplCalls > 0 then
                for i, tpl in ipairs(tplCalls) do
                    local line = tpl
                    local src = sources and sources[i]
                    line = '<span>' .. line .. '</span><span class="ts-Сущность-field">' .. makeSourceLink(src) .. '</span>'
                    table.insert(parts, '<p class="ts-Сущность">' .. line .. '</p>')
                end
            end
            return table.concat(parts, "\n")
         end
         end
     }
     }
Строка 88: Строка 92:
end
end


local function renderBlocks(frame, switchesTbl, configs, keyOrder, keyToTemplates, keySources)
local function parseListArg(str)
    local res = {}
    if not str or str == "" then return res end
    for item in string.gmatch(str, "[^,]+") do
        local s = trim(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 function renderBlocks(frame, switchesTbl, configs, keyOrder, keyToTemplates, keySources, noHeaders)
     local outLocal = {}
     local outLocal = {}
     for _, sw in ipairs(switchesTbl) do
     for _, sw in ipairs(switchesTbl) do
Строка 106: Строка 126:
                 end
                 end
             end
             end
             if cfg.wrapper then
             if noHeaders and sw == "title" then
                 local outStr = cfg.wrapper(key, tplCalls, sources)
                 local outStr = renderTitleBlock(key, tplCalls, sources, false)
                 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)
                    if outStr and outStr ~= "" then table.insert(outLocal, outStr) end
                end
             end
             end
         end
         end
Строка 119: Строка 144:
     local id = args[1] or ""
     local id = args[1] or ""
     if id == "" then return "" end
     if id == "" then return "" end
    local function parseListArg(str)
 
        local res = {}
        if not str or str == "" then return res end
        for item in string.gmatch(str, "[^,]+") do
            local s = trim(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 blacklist = parseListArg(args.blacklist or "")
     local blacklist = parseListArg(args.blacklist or "")
     local whitelist = parseListArg(args.whitelist or "")
     local whitelist = parseListArg(args.whitelist or "")
Строка 226: Строка 237:


     local out = {}
     local out = {}
     local blocks = renderBlocks(frame, switches, switchConfigs, switchKeyOrder, switchKeyToTemplates, switchKeySources)
     local blocks = renderBlocks(frame, switches, switchConfigs, switchKeyOrder, switchKeyToTemplates, switchKeySources,
        hasWhitelist)
     for _, b in ipairs(blocks) do table.insert(out, b) end
     for _, b in ipairs(blocks) do table.insert(out, b) end
     for _, e in ipairs(errors) do table.insert(out, e) end
     for _, e in ipairs(errors) do table.insert(out, e) end
Строка 268: Строка 280:
     end
     end


    local whitelist = parseListArg(args.whitelist or "")
    local hasWhitelist = next(whitelist) ~= nil
     local out = {}
     local out = {}
     local blocks = renderBlocks(frame, switches, switchConfigs, switchKeyOrder, switchKeyToTemplates, switchKeySources)
     local blocks = renderBlocks(frame, switches, switchConfigs, switchKeyOrder, switchKeyToTemplates, switchKeySources,
        hasWhitelist)
     for _, b in ipairs(blocks) do table.insert(out, b) end
     for _, b in ipairs(blocks) do table.insert(out, b) end