Mô đun:Bảng tự đánh số

Tài liệu mô đun[tạo]
local p = {};
local yesno = require('Module:Yesno')

function IntKeyTable (t)
	r = {}
	for k,v in pairs(t) do
		i = tonumber(k)
		if i then r[i] = v	end
	end
	return r
end

function tableCount (t)
	c = 0
	for k,v in pairs(t) do
		c = c+ 1
	end
	return c
end

function p.main(frame)
	local oldArgs = frame.args[1] and frame.args or frame:getParent().args
	local args = IntKeyTable(oldArgs)
	local r = ""
	local c = tableCount(args)
	local row = oldArgs["start"] or 1
	local cycle = oldArgs["cycle"] or 1
	local col = 0
	local colName = ""
	local pfix = oldArgs["prefix"] or ""
	local sfix = oldArgs["suffix"] or ""
	local debugMode = oldArgs["debug"]

	for k, v in pairs(args) do
		firstChar = string.sub(v, 1, 1)
		lastChar = string.sub(v, #v)
		if lastChar == "\n" then
			if firstChar == "-" then
				if k ~= 1 then row = row + cycle end
				col = 0
				r = r .. string.format("|%s\n|(%s) [%s; %s] %s%s%s\n", v, k, row, col, pfix, row, sfix)
				-- r = r .. "|" .. v.. "\n|(" .. k.. ") [" .. row .. "; " .. col .. "]\n" .. row .. "\n"
			else
				col = col + 1
				colName = "col" .. col
				if oldArgs[colName] then
					r = r .. string.format("|%s|(%s) [%s; %s] %s", oldArgs[colName], k, row, col, v)
				else
					r = r .. string.format("|(%s) [%s; %s] %s", k, row, col, v)
					-- r = r .. "|(" .. k .. ") " .. v
				end
				if k ~= c then r = r .. "\n"	end
			end
		else
			if v == "-" then
				if k ~= 1 then row = row + cycle end
			else
				if oldArgs[colName] then
					r = r .. string.format("|(%s) %s %s", k, oldArgs[colName], v)
				else
					r = r .. string.format("|(%s) %s", k, v)
					-- r = r .. "|(" .. k .. ") " .. v
				end
			end
		end
		
	end
	
	if not yesno(debugMode) then r = string.gsub(r, "|%(%d+%)%s%[%d+;%s%d+]%s", "|") end
	
	return r

end

return p