模組:MakeTable
这个模组是Lua模组生成HTML表的API。这个模组只应该,也只可能由其他Lua模组调用。
调用方法[编辑]
首先以require
调用此模组:
local MakeTable = require("Module:MakeTable")
其返回一个函數,接受一个table
为参数,
键 | 值 | 范例 |
---|---|---|
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.1 | Content1.2 |
Content2.1 | Content2.2 |
wikitable
Content1.1 | Content1.2 |
Content2.1 | Content2.2 |
wikitable_sortable_title
title1 | title2 |
---|---|
Content1.1 | Content1.2 |
Content2.1 | Content2.2 |
Content1.1 | Content1.2 |
Content2.1 | Content2.2 |
上述文档嵌入自Module:MakeTable/doc。 (编辑 | 历史) 编者可以在本模块的沙盒 (创建 | 镜像)和测试样例 (创建)页面进行实验。 本模块的子页面。 |
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