local getArgs = require('Module:Arguments').getArgs
local p = {}
function p.main(frame)
local args = getArgs(frame)
return p._main(frame, args)
end
-- 生成表头样式
local function makeHeaderStyle(args, colIndex)
-- 颜色优先级:cT#-color > TA-color/TB-color > T-color
local cT_color = args["cT" .. colIndex .. "-color"]
local TA_color = args["TA-color"]
local TB_color = args["TB-color"]
local T_color = args["T-color"]
local bgColor = ""
if cT_color and cT_color ~= "" then
bgColor = cT_color
elseif colIndex <= 3 then
bgColor = TA_color or T_color or ""
else
bgColor = TB_color or T_color or ""
end
local Tshadow_color = args["Tshadow-color"] or args["shadow-color"] or ""
local style = {}
if Tshadow_color ~= "" then
table.insert(style, "box-shadow:0px 0px 1px 2px " .. Tshadow_color .. " inset")
end
table.insert(style, "background-color:" .. bgColor)
-- 宽度优先级:c#-width > T#-width (T1-width/T2-width/T3-width)
local width
if args["c" .. colIndex .. "-width"] and args["c" .. colIndex .. "-width"] ~= "" then
width = args["c" .. colIndex .. "-width"]
else
if colIndex == 1 or colIndex == 4 then
width = args["T1-width"] or "43px"
elseif colIndex == 2 or colIndex == 5 then
width = args["T2-width"] or ""
else -- 3 or 6
width = args["c3-width"] or args["T3-width"] or args["T1-width"] or "43px"
end
end
if width ~= "" then
table.insert(style, "width:" .. width)
end
local T_height = args["T-height"]
if colIndex == 1 and T_height and T_height ~= "" then
table.insert(style, "height:" .. T_height)
end
local T_style = args["T-style"]
if T_style and T_style ~= "" then
table.insert(style, T_style)
end
return table.concat(style, ";") .. ";"
end
-- 生成单元格样式
local function makeCellStyle(args, colIndex, rowIndex)
-- 行颜色优先级:c#-color > A/B-color > L-color
local c_color = args["c" .. colIndex .. "-color"]
local A_color = args["A-color"] or args["L-color"] or ""
local B_color = args["B-color"] or args["L-color"] or ""
local Lshadow_color = args["Lshadow-color"] or args["shadow-color"] or ""
local L_height = args["L-height"] or ""
local A_style = args["A-style"] or args["L-style"] or ""
local B_style = args["B-style"] or args["L-style"] or ""
local bgColor = ""
if c_color and c_color ~= "" then
bgColor = c_color
else
if rowIndex % 2 == 1 then
bgColor = A_color
else
bgColor = B_color
end
end
local style = {}
table.insert(style, "background-color:" .. bgColor)
if Lshadow_color ~= "" then
table.insert(style, "box-shadow:0px 0px 1px 2px " .. Lshadow_color .. " inset")
end
if L_height ~= "" then
table.insert(style, "height:" .. L_height)
end
if rowIndex % 2 == 1 then
table.insert(style, A_style)
else
table.insert(style, B_style)
end
return table.concat(style, ";") .. ";"
end
function p._main(frame, args)
if not frame then
frame = mw.getCurrentFrame()
end
-- 读取参数
local border_color = args["border-color"] or ""
local A_color = args["A-color"] or args["L-color"] or ""
local B_color = args["B-color"] or args["L-color"] or ""
local Lshadow_color = args["Lshadow-color"] or args["shadow-color"] or ""
local A_style = args["A-style"] or args["L-style"] or ""
local B_style = args["B-style"] or args["L-style"] or ""
local total = tonumber(args["total"]) or 0
-- 开始构造表格
local html = {}
local tableClass = border_color == "" and "wikitable" or ""
table.insert(html, string.format('<table style="text-align:center;background-color:%s" class="%s">', border_color, tableClass))
-- 表头
table.insert(html, "<tr>")
for colIndex = 1, 6 do
-- 标题名优先级:c#-name > T#-name > 默认值
local c_name = args["c" .. colIndex .. "-name"]
if not c_name or c_name == "" then
if colIndex == 1 or colIndex == 4 then
c_name = args["T1-name"] or "序号"
elseif colIndex == 2 or colIndex == 5 then
c_name = args["T2-name"] or "曲目名称"
else
c_name = args["T3-name"] or "示例"
end
end
local style = makeHeaderStyle(args, colIndex)
table.insert(html, string.format('<th style="%s">%s</th>', style, c_name))
end
table.insert(html, "</tr>")
-- 计算行数
local half = math.ceil(total / 2)
-- 生成行
for key = 1, half do
local key2 = half + key
table.insert(html, "<tr>")
-- 第一列
local n_key = args["n" .. key] or args[string.format("n%02d", key)] or tostring(key)
table.insert(html, string.format('<td style="%s">%s</td>', makeCellStyle(args, 1, key), n_key))
-- 第二列
local s_key = args["s" .. key] or args[string.format("s%02d", key)] or ""
table.insert(html, string.format('<td style="%s">%s</td>', makeCellStyle(args, 2, key), s_key))
-- 第三列
local m_key = args["m" .. key] or args[string.format("m%02d", key)] or ""
table.insert(html, string.format('<td style="%s">%s</td>', makeCellStyle(args, 3, key), m_key))
-- 第四列
local n_key2 = args["n" .. key2] or args[string.format("n%02d", key2)] or tostring(key2)
local style4_color = args["c4-color"]
local style4_bg, style4_style
if style4_color and style4_color ~= "" then
style4_bg = style4_color
style4_style = (key % 2 == 1) and B_style or A_style
else
style4_bg = (key % 2 == 1) and B_color or A_color
style4_style = (key % 2 == 1) and B_style or A_style
end
local style4 = string.format("background-color:%s;", style4_bg)
if Lshadow_color and Lshadow_color ~= "" then
style4 = style4 .. "box-shadow:0px 0px 1px 2px " .. Lshadow_color .. " inset;"
end
style4 = style4 .. style4_style
table.insert(html, string.format('<td style="%s">%s</td>', style4, n_key2))
-- 第五列
local s_key2 = args["s" .. key2] or args[string.format("s%02d", key2)] or ""
local style5_color = args["c5-color"]
local style5_bg, style5_style
if style5_color and style5_color ~= "" then
style5_bg = style5_color
style5_style = (key % 2 == 1) and B_style or A_style
else
style5_bg = (key % 2 == 1) and B_color or A_color
style5_style = (key % 2 == 1) and B_style or A_style
end
local style5 = string.format("background-color:%s;", style5_bg)
if Lshadow_color and Lshadow_color ~= "" then
style5 = style5 .. "box-shadow:0px 0px 1px 2px " .. Lshadow_color .. " inset;"
end
style5 = style5 .. style5_style
table.insert(html, string.format('<td style="%s">%s</td>', style5, s_key2))
-- 第六列
local m_key2 = args["m" .. key2] or args[string.format("m%02d", key2)] or ""
local style6_color = args["c6-color"]
local style6_bg, style6_style
if style6_color and style6_color ~= "" then
style6_bg = style6_color
style6_style = (key % 2 == 1) and B_style or A_style
else
style6_bg = (key % 2 == 1) and B_color or A_color
style6_style = (key % 2 == 1) and B_style or A_style
end
local style6 = string.format("background-color:%s;", style6_bg)
if Lshadow_color and Lshadow_color ~= "" then
style6 = style6 .. "box-shadow:0px 0px 1px 2px " .. Lshadow_color .. " inset;"
end
style6 = style6 .. style6_style
table.insert(html, string.format('<td style="%s">%s</td>', style6, m_key2))
table.insert(html, "</tr>")
end
table.insert(html, "</table>")
return table.concat(html, "\n")
end
return p