FANDOM


local LuaInfobox = {}
 
local expandedLimit = 2
 
function LuaInfobox.DataPI(frame)
    local value = isTrue(frame.args.Value)
    local label = isTrue(frame.args.Label)
 
    local _, _, dateD = string.find(value or '', '([0-9][0-9][0-9][0-9]%-[0-9]?[0-9]%-[0-9]?[0-9])')
    local _, _, dateM = string.find(value or '', '([0-9][0-9][0-9][0-9]%-[0-9]?[0-9])')
    local _, _, dateY = string.find(value or '', '([0-9][0-9][0-9][0-9])')
 
    local lang = mw.getLanguage( 'pl' )
    if dateD then
        value = lang:formatDate( frame.args.FormatDay or 'Y-m-d', dateD )
    elseif dateM then
        value = lang:formatDate( frame.args.FormatMonth or 'Y-m', dateM )
    elseif dateY then
        label = frame.args.LabelMore or frame.args.Label or nil
        value = lang:formatDate( frame.args.FormatYear or 'Y', dateY )
    end
 
    local data = mw.html.create('data')
    if label then data:tag('label'):wikitext(label) end
    if value then data:tag('default'):wikitext(value) end
 
    return tostring(data)
end
function LuaInfobox.ListaPI(frame)
    local value = frame.args.Value or nil
    local label = frame.args.Label or nil
 
    local lines = getList(value, frame.args.Order or 'asc', frame.args.Separator)
    if #lines == 0 then return nil end
 
    local data = mw.html.create('data')
    data:tag('default'):node(makeList(lines))
 
    label = ternary(#lines == 1, label, isTrue(frame.args.LabelMore) or label)
    if label then data:tag('label'):wikitext(label) end
    return tostring(data)
end
function LuaInfobox.MechanikiPI(frame)
    local value = frame.args.Value or nil
    local label = frame.args.Label or nil
    local cats  = not isTrue(frame.args.nocats)
 
    local lines = getList(value, frame.args.Order or false, frame.args.Separator)
    if #lines == 0 then return nil end
 
    local data = mw.html.create('data')
    for i, v in ipairs(lines) do
        v = "[[:Kategoria:" .. v .. "|" .. v .. "]]"
        if cats then v = v .. "[[Kategoria:" .. v .. "]]" end
    end
    data:tag('default'):node(makeList(lines))
 
    label = ternary(#lines == 1, label, isTrue(frame.args.LabelMore) or label)
    if label then data:tag('label'):wikitext(label) end
    return tostring(data)
end
 
function makeList(lines, expand)
    for i, v in ipairs(lines) do
        lines[i] = '* ' .. v
    end
    local list = mw.html.create('div')
 
    list:addClass('infobox-list')
    if not expand then list:attr('data-limit', expandedLimit) end
    list:wikitext('\n' .. table.concat(lines, '\n'))
    return list or nil
end
function getList(values, sort, separator)
    separator = '%s*' .. (separator or '[,;]') .. '%s*'
 
    local list = mw.text.split(values or '', separator)
 
    local lines = {}
    for i, v in ipairs(list) do
        v = isTrue(v)
        if v then
            table.insert(lines, v)
        end
    end
    if type(sort) == 'string' then sort = sort:lower() else sort = false end
    if sort == 'asc' then
        table.sort(lines)
    elseif sort == 'desc' then
        table.sort(lines, function(a, b) return a > b end)
    elseif sort == 'lastname' then
        for i, v in ipairs(lines) do
            local link, disp = deLink(v)
            local _, _, key = string.find(disp or link or v, '(%S+)$')
            lines[i] = {v, key}
        end
        table.sort(lines, function(a, b) return a[2] < b[2] end)
        for i, v in ipairs(lines) do
            lines[i] = v[1]
        end
    end
    return lines
end
function reverse(tbl) --https://gist.github.com/balaam/3122129#gistcomment-1680319
  for i=1, math.floor(#tbl / 2) do
    local tmp = tbl[i]
    tbl[i] = tbl[#tbl - i + 1]
    tbl[#tbl - i + 1] = tmp
  end
end
function deLink(text)
    local _, _, link, disp = string.find(text, '%[%[:?([^|^%]]*)%|?([^%]]*)%]%]')
    if link then
        if disp == '' then
            return link, nil
        end
        return link, disp or nil
    end
    return nil
end
function isTrue(test) -- MediaWiki-ish definition of true (empty/whitespace string == false) returns trimmed 'true' value or false
    if not test then return false end
    if type(test) == 'string' then
        test = mw.text.trim(test or '')
        if test == '' then return false end
    end
    return test
end
function ternary(cond, T, F)
    if cond then return T else return F end
end
----------------------------------------------------------------------------------------------------------------------
function LuaInfobox.Data(field, vars)
    local _, _, dateD = string.find(field.Value, '([0-9][0-9][0-9][0-9]%-[0-9]?[0-9]%-[0-9]?[0-9])')
    local _, _, dateM = string.find(field.Value, '([0-9][0-9][0-9][0-9]%-[0-9]?[0-9])')
    local _, _, dateY = string.find(field.Value, '([0-9][0-9][0-9][0-9])')
 
    local lang = mw.getLanguage( 'pl' )
    if dateD then
        return lang:formatDate( field.FormatDay or 'Y-m-d', dateD )
    elseif dateM then
        return lang:formatDate( field.FormatMonth or 'Y-m', dateM )
    elseif dateY then
        field.Label = field.LabelMore or field.Label
        return lang:formatDate( field.FormatYear or 'Y', dateY )
    end
    return field.Value
end
function LuaInfobox.Lista(field, vars)
    local lines = {}
    local expanded = not not (vars.expand or false)
    local separator = field.Separator or '[,;]'
    separator = '%s*' .. separator .. '%s*'
    local val = mw.text.split(field.Value or '', separator)
 
    for i, v in ipairs(val) do
        v = mw.text.trim(v or '')
        local line
        if v then
            line = "* " .. v
        end
        if line then table.insert(lines, line) end
    end
 
    table.sort(lines)
 
    field.Label = ternary(#lines == 1, field.Label, field.LabelMore or field.Label)
    local div = mw.html.create('div')
    div:wikitext('\n' .. mw.getCurrentFrame():preprocess('\n' .. collapsible(lines, expanded)))
    return tostring(div)
end
function LuaInfobox.Mechaniki(field, vars)
    local lines = {}
 
    local cats = not (vars.nocats or false)
    local expanded = not not (vars.expand or false)
    local separator = field.Separator or ';'
    local val = mw.text.split(field.Value or '', separator)
    local more = mw.text.split(field.ValueMore or '', separator)
 
 
    for i, v in ipairs(val) do
        v = mw.text.trim(v or '')
        local line
        if v then
            line = "* [[:Kategoria:" .. v .. "|" .. v .. "]]"
            if cats then line = line .. "[[Kategoria:" .. v .. "]]" end
        end
        if line then table.insert(lines, line) end
    end
 
    table.sort(lines)
 
    field.Label = ternary(#lines == 1, field.Label, field.LabelMore or field.Label)
    local div = mw.html.create('div')
    div:wikitext('\n' .. mw.getCurrentFrame():preprocess('\n' .. collapsible(lines, expanded)))
    return tostring(div)
end
function collapsible(lines, expand)
    if expand or #lines <= expandedLimit then return table.concat(lines, '\n') end
 
    local visible = {}
    local hidden = {}
    for i, value in ipairs(lines) do
        if i > expandedLimit then table.insert(hidden, value)
        else table.insert(visible, value) end
    end
    return '<div class="mw-collapsible mw-collapsed" data-expandtext="&#9660;" data-collapsetext="&#9650;">\n' .. table.concat(visible, '\n') .. '\n<div class="mw-collapsible-content">\n' .. table.concat(hidden, '\n') .. '\n</div></div>'
end
return LuaInfobox

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Więcej z Fandomu

Losowa wiki