Sari la conținut

Modul:Stackify

De la Wikipedia, enciclopedia liberă

Documentația acestui modul poate fi creată la Modul:Stackify/doc

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

local function isTrue(str)
	if str == nil then return false end
	if type(str) == 'boolean' then return str end
	if type(str) == 'number' then return str ~= 0 end
	if type(str) == 'string' then return str == 'y' or str == 'yes' or str == 'da' or str == 'd' or str == 'true' or str == '1' end
	return false
end

p.fromConfig = function(cfg, content)
	if not content then return nil end
	local mainDiv = mw.html.create('div')
						:addClass('stack')
	local classElementsList = { 'stack' }
	if isTrue(cfg.margin) then 
		table.insert(classElementsList, 'margin')
	end
	if cfg.clear == 'true' or cfg.clear == cfg.float then table.insert(classElementsList, 'clear') end
	table.insert(classElementsList, cfg.float)
	mainDiv:addClass(table.concat(classElementsList, '-'))
	local secondDiv = mainDiv:tag('div')
	if cfg.cellspacing then
		secondDiv:css('margin', cfg.cellspacing .. 'px')
	end
	for _,eachContent in ipairs(content) do
		secondDiv:wikitext(eachContent)
	end
	return mw.getCurrentFrame():extensionTag{ name = 'templatestyles', args = { src = 'Stack/style.css'} } .. tostring(mainDiv)
end
p.fromArgs = function(args)
	local cfg = {}
	cfg.float = args['float'] or 'right'
	cfg.margin = args['margin']
	cfg.clear = args['clear']
	cfg.cellspacing = args['cellspacing']
	
	local compressedArgs = TableTools.compressSparseArray(args)
	local content = {}
	for idx = 1,table.maxn(compressedArgs) do
		table.insert(content, compressedArgs[idx])
	end
	
	return p.fromConfig(cfg, content)
end


p.fromFrame = function(frame)
	local args = getArgs(frame)
	return p.fromArgs(args)
end

return p