模块:层级表:修订间差异
| 小 (debug) | 小 (debug) | ||
| 第1行: | 第1行: | ||
| local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
| local p = {} | local p = {} | ||
| function maxn(t) | |||
| 	local n, k = 0, nil | |||
| 	repeat | |||
| 		k = next(t, k) | |||
| 		if type(k) == 'number' and k > n then | |||
| 			maxn = k | |||
| 		end | |||
| 	until not k | |||
| 	return n | |||
| end | |||
| function p._main(args) | function p._main(args) | ||
| 第11行: | 第22行: | ||
| 	local last_col = 0 | 	local last_col = 0 | ||
| 	local max_col = 1 | 	local max_col = 1 | ||
| 	s=s.. "\n|+DEBUG: args | 	s=s.. "\n|+DEBUG: maxn(args) = " .. maxn(args) | ||
| 	for i = 1,  | 	for i = 1, maxn(args) do | ||
| 		local arg = args[i] | 		local arg = args[i] | ||
| 		if arg and arg ~= "" then | 		if arg and arg ~= "" then | ||
2025年3月19日 (三) 23:32的版本
可在模块:层级表/doc创建此模块的帮助文档
local getArgs = require('Module:Arguments').getArgs
local p = {}
function maxn(t)
	local n, k = 0, nil
	repeat
		k = next(t, k)
		if type(k) == 'number' and k > n then
			maxn = k
		end
	until not k
	return n
end
function p._main(args)
	local s = "{|"
	if args.class then
		s=s.. 'class="' .. mw.text.nowiki(args.class) .. '"'
	end
	local cells = {}
	local row = 1
	local last_col = 0
	local max_col = 1
	s=s.. "\n|+DEBUG: maxn(args) = " .. maxn(args)
	for i = 1, maxn(args) do
		local arg = args[i]
		if arg and arg ~= "" then
			if arg:sub(1, 1) == "+" then
				s=s.. "\n|" .. arg
			elseif arg:sub(1, 1) == "!" then
				s=s.. "\n" .. arg
			else
				local stars, text = arg:match("(%**)(.+)")
				local col = #stars + 1
				if col > max_col then
					max_col = col
				elseif col <= last_col then
					row = row + 1
				end
				table.insert(cells, { row=row, col=col, text=text })
				last_col = col
			end
		end
	end
	local max_row = row
	for i, cell in ipairs(cells) do
		local row = cell.row
		local col = cell.col
		local row2 = max_row
		local col2 = max_col
		if i < #cells and cells[i + 1].row == row then
			col2 = cells[i + 1].col - 1
		end
		for j = i + 1, #cells do
			if cells[j].row > row and cells[j].col <= col2 then
				row2 = cells[j].row - 1
				break
			end
		end
		local rowspan = row2 - row + 1
		local colspan = col2 - col + 1
		s=s.. "\n|"
		if rowspan ~= 1 then
			s=s.. " rowspan=" .. rowspan
		end
		if colspan ~= 1 then
			s=s.. " colspan=" .. colspan
		end
		s=s.. "|" .. cell.text
	end
	s=s.. "\n|}"
	return s
end
function p.main(frame)
	local args = getArgs(frame, { wrappers={ "模板:层级表" } })
	return p._main(args)
end
return p