Przejdź do zawartości

Moduł:Klad

Z Wikipedii, wolnej encyklopedii
 Dokumentacja modułu [stwórz] [odśwież]
function loadTree(data)
	local tree = {}
	
	for k, v in pairs(data) do
		if v and (#v > 0) then
			local keys = mw.text.split(k,'.',true)
			local index = keys[#keys]
			if #keys > 1 then
				for i = 1, #keys-1 do
					keys[i] = tonumber(string.match(keys[i], "^[0-9][0-9]?$"))
					if not keys[i] or (keys[i] == 0) then
						index = false
						break
					end
				end
			end
			
			if index then
				local node = tree
				for i = 1, #keys-1 do
					local k = keys[i]
					local child = node[k]
					if not child then
						child = {}
						node[k] = child
					elseif type(child) == "string" then
						local labelName = "label"..k
						if not node[labelName] then
							node[labelName] = child
						end
						
						child = {}
						node[k] = child
					end
					
					node = child
				end
				
				local childIndex = tonumber(index)
				if childIndex then
					if type(node[childIndex]) == "table" then
						local labelName = "label"..index
						if not node[labelName] then
							node[labelName] = v
						end
					else
						node[childIndex] = v
					end
				else
					node[index] = v
				end
			end
		end
	end
	
	return tree
end

function printTree(tree)
	local result = {}
	local style = tree.style or ""
	table.insert(result, '{| cellspacing=0 cellpadding=0 border=0 style="'..style..'"\n')
	local thickness = tostring(tonumber(tree.thickness) or 1)
	for i, v in ipairs(tree) do
		local text
		local labelName = "label"..tostring(i)
		local label = tree[labelName] or "<br />"
		if type(v) == "string" then
			-- Czy klad zagnieżdżony (v jest tabelą)
			if string.sub(mw.text.trim(v),1,2) == '{|' then
				--mw.log('\nt '..v..'\n')
				text = v
			else
				--mw.log('\n! '..v..'\n')
				text = '<span style="margin-left:5px;">'..v..'</span>'
			end
		elseif type(v) == "table" then
			text = printTree(v)
		end

		if i == 1 then
			table.insert(result, '| style="width:1.5em;border-bottom:'..thickness..'px solid black; padding:0px 5px 0px 5px;" valign=bottom align=center | '..label..' || rowspan=2 |\n')
			table.insert(result, text or "{{{1}}}")
			table.insert(result, '\n|-\n')
		elseif text and #text > 0 then
			table.insert(result, '| style="border-left:'..thickness..'px solid black;" valign=top |<br/>\n')
			table.insert(result, '|-\n')
			table.insert(result, '| style="border-left:'..thickness..'px solid black; border-bottom:'..thickness..'px solid black; padding:0px 5px 0px 5px;" valign=bottom align=center | '..label..' || rowspan=2 |\n')
			table.insert(result, text)
			table.insert(result, '\n|-\n')
		end
	end
	
	table.insert(result, '| valign=top | <br/>\n')
	table.insert(result, '|}')
	return table.concat(result)
end


return {
	
["Pokaż"] = function(frame)
	local tree = loadTree(frame:getParent().args)
	if tree and (#tree > 0) then
		return printTree(tree)
	end
end

}