模块:层级表:修订间差异
| 小 (debug) | 无编辑摘要 | ||
| (未显示同一用户的3个中间版本) | |||
| 第1行: | 第1行: | ||
| local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
| local p = {} | local p = {} | ||
| function p._main(args) | function p._main(args) | ||
| 第22行: | 第11行: | ||
| 	local last_col = 0 | 	local last_col = 0 | ||
| 	local max_col = 1 | 	local max_col = 1 | ||
| 	for _, arg in ipairs(args) do | |||
| 	for  | |||
| 		if arg and arg ~= "" then | 		if arg and arg ~= "" then | ||
| 			if arg:sub(1, 1) == "+" then | 			if arg:sub(1, 1) == "+" then | ||
| 第44行: | 第31行: | ||
| 	end | 	end | ||
| 	local max_row = row | 	local max_row = row | ||
| 	local last_row = 0 | |||
| 	for i, cell in ipairs(cells) do | 	for i, cell in ipairs(cells) do | ||
| 		local row = cell.row | 		local row = cell.row | ||
| 第60行: | 第48行: | ||
| 		local rowspan = row2 - row + 1 | 		local rowspan = row2 - row + 1 | ||
| 		local colspan = col2 - col + 1 | 		local colspan = col2 - col + 1 | ||
| 		if row > last_row then | |||
| 			s=s.. '\n|-style="vertical-align:top"' | |||
| 		end | |||
| 		s=s.. "\n|" | 		s=s.. "\n|" | ||
| 		if rowspan ~= 1 then | 		if rowspan ~= 1 then | ||
| 第68行: | 第59行: | ||
| 		end | 		end | ||
| 		s=s.. "|" .. cell.text | 		s=s.. "|" .. cell.text | ||
| 		last_row = row | |||
| 	end | 	end | ||
| 	s=s.. "\n|}" | 	s=s.. "\n|}" | ||
| 第74行: | 第66行: | ||
| function p.main(frame) | function p.main(frame) | ||
| 	local args = getArgs(frame, { wrappers={ "模板:层级表" } }) | 	local args = getArgs(frame, { | ||
| 		removeBlanks=false, | |||
| 		wrappers={ "模板:层级表" }, | |||
| 	}) | |||
| 	return p._main(args) | 	return p._main(args) | ||
| end | end | ||
| return p | return p | ||
2025年3月19日 (三) 23:46的最新版本
可在模块:层级表/doc创建此模块的帮助文档
local getArgs = require('Module:Arguments').getArgs
local p = {}
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
	for _, arg in ipairs(args) do
		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
	local last_row = 0
	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
		if row > last_row then
			s=s.. '\n|-style="vertical-align:top"'
		end
		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
		last_row = row
	end
	s=s.. "\n|}"
	return s
end
function p.main(frame)
	local args = getArgs(frame, {
		removeBlanks=false,
		wrappers={ "模板:层级表" },
	})
	return p._main(args)
end
return p