模組:MakeTable

本页使用了标题或全文手工转换
维基百科,自由的百科全书
文档图示 模块文档[查看] [编辑] [历史] [清除缓存]

这个模组是Lua模组生成HTML表的API。这个模组只应该,也只可能由其他Lua模组调用。

调用方法[编辑]

首先以require调用此模组:

local MakeTable = require("Module:MakeTable")

其返回一个函數,接受一个table为参数,

Module:MakeTable 的调用参数
范例
attrs 一个表,键为HTML键名,值为HTML键的值
{
    key = "value",
    key_two = "value_two"
}
css 一个表,键为CSS键名,值为CSS键的值
{
    color = "#fff",
    width = "100px"
}
class 一个表,值为HTML表的类
{"wikitable","sortable"}
caption 一个字串,值为HTML表的标题
"Caption"
titles 一个表,值为HTML表内部的标题栏
{"title1","title2","title3"}
content 必填,一个二维表,为HTML表的内容
{
    {"content1.1","content1.2","content1.3"},
    {"content2.1","content2.2","content2.3"}
}

例子[编辑]

basic

Content1.1Content1.2
Content2.1Content2.2

wikitable

Content1.1Content1.2
Content2.1Content2.2

wikitable_sortable_title

title1title2
Content1.1Content1.2
Content2.1Content2.2

wikitable_caption

标题
Content1.1Content1.2
Content2.1Content2.2

return function (def)
	if not def.content then
		error("Contents should be defined as a 2-dimision table, see [[Module:MakeTable]].")
	elseif not def.content[1] then
		error("Contents should be defined as a 2-dimision table, see [[Module:MakeTable]].")
	end
	local tb = mw.html.create( 'table' )
	if def.attrs then
		for x,y in pairs(def.attrs) do
			if x == "styles" or x == "class" then
				error(x .. " should be defined in def." .. x .. " in key-value table form, see [[Module:MakeTable]].")
			end
			tb:attr(x,y)
		end
	end
	if def.class then
		for _,y in pairs(def.class) do
			tb:addClass(y)
		end
	end
	if def.css then
		for x,y in pairs(def.css) do
			tb:css(x,y)
		end
	end
	if def.caption then
		tb:tag("caption"):wikitext(def.caption)
	end
	if def.titles then
		local tr = tb:tag("tr")
		for _,z in ipairs(def.titles) do
			local th = tr:tag("th")
			th:wikitext(z)
		end
	end
	for _,y in ipairs(def.content) do
		local tr = tb:tag("tr")
		for _,z in ipairs(y) do
			local td = tr:tag("td")
			td:wikitext(z)
		end
	end
	return tostring(tb)
end