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

мНет описания правки
мНет описания правки
 
(не показано 6 промежуточных версий этого же участника)
Строка 49: Строка 49:
             local line = tpl
             local line = tpl
             local src = sources and sources[i]
             local src = sources and sources[i]
             line = '<span>' .. line .. '</span><span class="ts-Сущность-field">' .. makeSourceLink(src) .. '</span>'
             line = '<div>' .. line .. '</div><div class="ts-Сущность-field">' .. makeSourceLink(src) .. '</div>'
             table.insert(parts, '<p class="ts-Сущность">' .. line .. '</p>')
             table.insert(parts, '<div class="ts-Сущность">' .. line .. '</div>')
         end
         end
     end
     end
Строка 254: Строка 254:
     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\n"))
     return frame:preprocess(table.concat(out, "\n"))
end
end


Строка 299: Строка 299:
     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\n"))
     return frame:preprocess(table.concat(out, "\n"))
end
 
local function is_array(tbl)
    local max = 0
    local count = 0
    for k in pairs(tbl) do
        if type(k) ~= "number" then
            return false
        end
        if k > max then max = k end
        count = count + 1
    end
    return count > 0 and max == count
end
 
local function apply_pattern(s, pattern, repl)
    if not pattern or pattern == "" or not s then
        return s
    end
 
    local text = tostring(s)
    local replacement
    if repl and repl ~= "" then
        replacement = tostring(repl)
        replacement = replacement:gsub("\\(%d)", "%%%1")
    else
        replacement = "%1"
    end
 
    local patt = pattern
    if not patt:find("%^") and not patt:find("%$") then
        patt = "^" .. patt .. "$"
    end
 
    return (text:gsub(patt, replacement))
end
end


function p.jsonList(frame)
function p.jsonList(frame)
     local args = getArgs(frame, { removeBlanks = false })
     local args = getArgs(frame, { removeBlanks = false })
     local jsonStr = trim(args[1] or args.json or "")
     local jsonStr = mw.text.unstripNoWiki(trim(args[1] or args.json or ""))
     if jsonStr == "" then return "" end
     if jsonStr == "" then return "" end


Строка 312: Строка 347:
     end
     end


     local bullet = args.prefix or "* "
     local bullet = mw.text.unstripNoWiki(args.prefix or "* ")
     local sep = args.sep or ": "
     local sep = mw.text.unstripNoWiki(args.sep or ": ")
    local keyPattern = mw.text.unstripNoWiki(args.key_pattern or "(.*)")
    local keyReplace = mw.text.unstripNoWiki(args.key_replace or "\\1")
    local valuePattern = mw.text.unstripNoWiki(args.value_pattern or "(.*)")
    local valueReplace = mw.text.unstripNoWiki(args.value_replace or "\\1")


     local keys = {}
     local out = {}
    for k in pairs(data) do
        keys[#keys + 1] = k
    end
    table.sort(keys, function(a, b) return tostring(a) < tostring(b) end)


     local out = {}
     if is_array(data) then
    for _, k in ipairs(keys) do
        for _, v in ipairs(data) do
        local v = data[k]
            local text = ""
        local vStr
            if type(v) == "table" then
        if type(v) == "table" then
                if is_array(v) then
            local okJson, jsonVal = pcall(mw.text.jsonEncode, v)
                    if #v == 1 then
            if okJson and jsonVal then
                        text = tostring(v[1])
                vStr = jsonVal
                    elseif #v > 1 then
                        local parts = {}
                        for i = 1, #v do
                            parts[#parts + 1] = tostring(v[i])
                        end
                        text = table.concat(parts, ", ")
                    end
                else
                    local okJson, jsonVal = pcall(mw.text.jsonEncode, v)
                    if okJson and jsonVal then
                        text = jsonVal
                    end
                end
             else
             else
                 vStr = ""
                 text = tostring(v)
            end
            if text ~= "" then
                local patt = valuePattern ~= "" and valuePattern or keyPattern
                local repl = valueReplace ~= "" and valueReplace or keyReplace
                text = apply_pattern(text, patt, repl)
                table.insert(out, bullet .. text)
             end
             end
        else
            vStr = tostring(v)
         end
         end
         if vStr ~= "" then
    else
            table.insert(out, bullet .. tostring(k) .. sep .. vStr)
        local keys = {}
        for k in pairs(data) do
            keys[#keys + 1] = k
        end
         table.sort(keys, function(a, b) return tostring(a) < tostring(b) end)
 
        for _, k in ipairs(keys) do
            local v = data[k]
            local vStr
            if type(v) == "table" then
                local okJson, jsonVal = pcall(mw.text.jsonEncode, v)
                if okJson and jsonVal then
                    vStr = jsonVal
                else
                    vStr = ""
                end
            else
                vStr = tostring(v)
            end
            local keyStr = tostring(k)
            keyStr = apply_pattern(keyStr, keyPattern, keyReplace)
            vStr = apply_pattern(vStr, valuePattern, valueReplace)
            if vStr ~= "" then
                table.insert(out, bullet .. keyStr .. sep .. vStr)
            end
         end
         end
     end
     end