Modul:Collapsible list2

Izvor: Wikipedija
Prijeđi na navigaciju Prijeđi na pretragu

Dokumentaciju za ovaj modul možete napraviti na stranici Modul:Collapsible list2/dok

-- This module implements {{collapsible list2}}.

local p = {}

local function getListItem( data )
    if not type( data ) == 'string' then
        return ''
    end
    return mw.ustring.format( '<li style="line-height: inherit; margin: 0">%s</li>', data )
end

-- Returns an array containing the keys of all positional arguments
-- that contain data (i.e. non-whitespace values).
local function getArgNums( args )
    local nums = {}
    for k, v in pairs( args ) do
        if type( k ) == 'number' and
            k >= 1 and
            math.floor( k ) == k and
            type( v ) == 'string' and
            mw.ustring.match( v, '%S' ) then
                table.insert( nums, k )
        end
    end
    table.sort( nums )
    return nums
end

-- Formats a list of classes, styles or other attributes.
local function formatAttributes( attrType, ... )
    local attributes = { ... }
    local nums = getArgNums( attributes )
    local t = {}
    for i, num in ipairs( nums ) do
        table.insert( t, attributes[ num ] )
    end
    if #t == 0 then
        return '' -- Return the blank string so concatenation will work.
    end
    return mw.ustring.format( ' %s="%s"', attrType, table.concat( t, ' ' ) )
end

local function buildList( args )
    -- Get the list items.
    local listItems = {}
    local argNums = getArgNums( args )
    local szItems = ''
    for i, num in ipairs( argNums ) do
        szItems = mw.ustring.format('%s<tr><td>%s</td></tr>\n', szItems, args[num])
    end
   
    local title = args.title or 'List'
    local szTitle = mw.ustring.format('<tr><th style="text-align:left">%s</th></tr>\n', title)
    local szStart = '<div style="font-size:90%;">\n<table cellspacing="0" class="nowraplinks collapsible collapsed navbox-inner" style="border-spacing:0">\n'
    local szEnd = '</table>\n</div>\n'
 
    -- Build the list.
    return mw.ustring.format('%s%s%s%s', szStart, szTitle, szItems, szEnd)
end

function p.main( frame )
    local origArgs
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
        for k, v in pairs( frame.args ) do
            origArgs = frame.args
            break
        end
    else
        origArgs = frame
    end
    
    local args = {}
    for k, v in pairs( origArgs ) do
        if type( k ) == 'number' or v ~= '' then
            args[ k ] = v
        end
    end
    return buildList( args )
end

return p