Modul:Címlap

A Wikikönyvekből, a szabad elektronikus könyvtárból.

A modult a Modul:Címlap/doc lapon tudod dokumentálni

local getArgs = require('Modul:Arguments').getArgs
local p = {}

local function prepareArgs(args)
	local prepared, other = {}, {}
	for n, v in pairs(args) do
		if (type(n) == 'number') then
			local n2 = math.floor( (n+1) / 2)
			local nmod = n % 2
			if (nmod == 0) then
				nmod = 2
			end
			if prepared[n2] then
				prepared[n2][nmod] = v
			else
				prepared[n2] = { [nmod] = v }
			end
		else
			other[n] = v
		end
	end
	local sorted = {}
	local isContent = false
	for n, v in ipairs(prepared) do
		if v[1] ~= nil then
			table.insert( sorted, v )
			isContent = true
		end
	end
	if not isContent then
		sorted = nil
	end
	return sorted, other
end

local function text(name, extra, css, desc)
	local frame = mw.getCurrentFrame()
	local function ucfirst(t)
		return mw.language.getContentLanguage():ucfirst(t)
	end
	local function special(prop)
		return frame:expandTemplate { title = 'Portál/speciális', args = { name, prop } }
	end
	local file, txt
	local specialImg = special('ikon')
	local subpage = mw.title.new( 'Címlap:' .. ucfirst(name) .. '/ikon' )
	if (specialImg and specialImg ~= '') then
		file = specialImg
	elseif (subpage.exists) then
		file = frame:expandTemplate{ title = subpage }
	elseif (mw.title.new( 'Media:Portál ' .. name .. '.png' ).exists) then
		file = 'Portál ' .. name .. '.png'
	else
		file = 'Portal.svg'
	end
	file = '[[Fájl:' .. file .. '|25x25px|' .. name .. ']]'
	local specialTxt = special('név')
	if (specialTxt and specialTxt ~= '') then
		txt = specialTxt
	else
		txt = ucfirst(name) .. (extra or '') .. 'portál'
	end
	txt = ("%s '''[[Címlap:%s|%s]]'''"):format(file, name, txt)
	if desc then
		txt = txt .. ' ' .. frame:expandTemplate{ title = 'Portál/leírások', args = { name } }
	end
	local li = mw.html.create( 'li' )
	li
		:css(css)
		:wikitext(txt)
	return tostring(li)
end

local function getUl(args)
	local desc = (#args == 1)
	local function one(n, baseCss, moreCss)
		baseCss['float']	= moreCss.float
		baseCss['width']	= moreCss.width
		baseCss['margin-left']	= moreCss.mleft
		baseCss['margin-right']	= moreCss.mright
		return text(args[n][1], args[n][2], baseCss, desc)
	end
	local ul = mw.html.create( 'ul' )
	local css = {
		['display']	= 'block',
		['clear']	= 'both',
		['list-style-type']	= 'none',
		['list-style-image']	= 'none',
		['width']	= '100%',
		['vertical-align']	= 'middle',
		['margin']	= '0 0 1em',
		['padding']	= '0',
		['min-height'] = '27px'
	}
	ul:css(css)
	local liCss = {
		['height']	= '27px',
		['line-height']	= '25px',
		['margin-bottom']	= '0',
		['margin-top']	= '0.5em',
		['padding'] = '0',
		['border']	= '1px solid #ccf',
		['background-color']	= '#f0eeff'
	}
	for n, v in pairs(args) do
		local pos = n % 3
		local moreCss = {}
		if pos == 1 then
			moreCss.float	= 'left'
			if (args[n+2]) then
				moreCss.width	= '33%'
				moreCss.mleft	= '-4px'
				moreCss.mright	= '1%'
			elseif (args[n+1]) then
				moreCss.width	= '49%'
				moreCss.mleft	= '-3px'
				moreCss.mright	= '1%'
			else
				moreCss.width	= '100%'
				moreCss.mleft	= '0'
				moreCss.mright	= '0'
			end
		elseif pos == 2 then
			if (args[n+1]) then
				moreCss.float	= 'left'
				moreCss.width	= '32%'
				moreCss.mleft	= '0'
				moreCss.mright	= '1%'
			else
				moreCss.float	= 'right'
				moreCss.width	= '49%'
				moreCss.mleft	= '1%'
				moreCss.mright	= '-3px'
			end
		else
			moreCss.float	= 'right'
			moreCss.width	= '33%'
			moreCss.mleft	= '0'
			moreCss.mright	= '-4px'
		end
		ul:wikitext( one(n, liCss, moreCss) )
	end
	return tostring(ul)
end

function p._main(args)
	local configs
	args, configs = prepareArgs(args)
	local div = mw.html.create( 'div' )
	local divCss = {
		['margin-left']  = '0',
		['margin-right'] = '2px'
	}
	if (args) then
		div:wikitext( getUl(args) )
		if (args[3]) then
			divCss['margin-left']  = '4px'
			divCss['margin-right'] = '4px'
		elseif (args[2]) then
			divCss['margin-left']  = '3px'
			divCss['margin-right'] = '3px'
		end
	else
		-- ha a usernek baja van
		local errDiv = mw.html.create( 'div' )
		errDiv
			:addClass( 'error' )
			:css( 'font-size', 'larger' )
			:wikitext( "A {{portál}} sablon számára legalább egy paramétert meg kell adnod!" )
		div:wikitext( tostring( errDiv ) )
	end
	div
		:addClass( 'noprint' )
		:css(divCss)
		:tag( 'div' )
			:css( 'clear', 'both' )
	return tostring( div )
end

function p.main(frame)
	local args = getArgs( frame, { trim = false } )
	return p._main( args )
end

return p