Lua-Beispiel2

Die Dokumentation für dieses Modul kann unter Modul:Lua-Beispiel2/Doku erstellt werden

local p = {}

function p.listPortals( frame )
    local listitems = {}
    local portalPage = '';
    local mainpageTitle = mw.message.new( 'mainpage' );
    local listItem = ''
    local count = 0
    local numberOfColumns = frame.args['cols'] or 3

    -- Run over all namespaces and add a link to a portal page into our table
    -- object
    for k, v in pairs( mw.site.contentNamespaces ) do
    	if v['id'] > 3000 then
        	portalPage = v['name'] .. ':' .. tostring( mainpageTitle )
        	listItem = makeListItem( portalPage, v['name'] )
        	table.insert( listitems, listItem )
        	count = count + 1
        end
    end

    local itemsPerColumn = math.ceil( count / numberOfColumns )
    local rows = {}

	-- Sort in each column
	for i = 1,itemsPerColumn,1 do
		local row = {}
		for j = 0,numberOfColumns,1 do
			table.insert( row, listitems[ i + j * itemsPerColumn ] )
		end
		table.insert( rows, row )
	end
    
    return  makeCountBox( count ) .. makeColumns( rows ) 
end

-- Creates a WikiText list item with a link
function makeListItem( target, alias )
	return '* [[' .. target .. '|' .. alias .. ']]'
end

function makeCountBox( count )
	local div = mw.html.create( 'div' )
	div
	     :attr( 'class', 'portal-count' )
	     :css( 'width', '100%' )
	     :wikitext( 'Found \'\'\'' .. count .. '\'\'\' portals!' )
	     :tag( 'hr' )
	return tostring( div )
end

function makeColumns( rows )
	markup = '\n{| class="wikitable"'
	for index, row in pairs( rows ) do
		markup = markup .. '\n|-'
		for itemIndex, item in pairs( row ) do
			markup = markup .. '\n||\n' .. item
		end
	end
	markup = markup .. '\n|}\n'
	return markup
end

return p

Anhänge

Diskussionen