local getArgs = require("Module:Arguments").getArgs
local zhnum = require("Module:zhnum")
local p = {}
-- 字符串替换函数(简化版str replace功能)
local function strReplace(str, pattern, replacement, count)
if not str or not pattern then return str end
replacement = replacement or ""
count = count and tonumber(count) or nil
if count then
return string.gsub(str, pattern, replacement, count)
else
return string.gsub(str, pattern, replacement)
end
end
-- 生成用户链接
local function userLink(username, displayName, noLink)
if noLink then
return displayName or username
end
local display = displayName or username
local url = tostring(mw.uri.canonicalUrl('User:' .. username))
return string.format('<span class="plainlinks">[%s %s]</span>', url, display)
end
-- 生成日期标题
local function generateDateTitle(year, month)
if not year or not month then
return ""
end
-- 转换年份为中文数字并替换零为〇
local yearChinese = zhnum.zhnum4(year, false)
yearChinese = strReplace(yearChinese, "零", "〇")
-- 移除非中文数字字符
yearChinese = strReplace(yearChinese, "[^一二三四五六七八九〇]", "")
local monthChinese = zhnum.zhnum4(month, false)
return string.format(
'<div class="moegirlMonthlyReport-v202112-mainTitle">' ..
'<div class="moegirlMonthlyReport-v202112-letterSpacingMiddle">%s年</div>' ..
'<div><span class="moegirlMonthlyReport-v202112-letterSpacingMiddle">%s月</span>號</div>' ..
'</div>',
yearChinese, monthChinese
)
end
-- 生成作者信息
local function generateWriters(args, prefix, label)
local writers = {}
local i = 1
-- 收集所有作者
while args[prefix .. i] do
table.insert(writers, args[prefix .. i])
i = i + 1
end
if #writers == 0 then
return ""
end
-- 生成作者链接列表
local writerLinks = {}
for j, writer in ipairs(writers) do
table.insert(writerLinks, userLink(writer, nil, args.noLink))
end
return string.format(
'<div class="moegirlMonthlyReport-v202112-mainWriter">%s:%s</div>',
label,
table.concat(writerLinks, "、")
)
end
-- 生成内容行
local function generateRows(args)
local html = {}
local rowCount = tonumber(args["row count"]) or 0
for row = 1, rowCount do
local columnCount = tonumber(args[string.format("row%d count", row)]) or 0
if columnCount > 0 then
table.insert(html, '<div class="moegirlMonthlyReport-v202112-mainContainer">')
for col = 1, columnCount do
local title = args[string.format("row%d column%d title", row, col)] or ""
local content = args[string.format("row%d column%d content", row, col)] or ""
local width = string.format("%.2f%%", 100 / columnCount)
table.insert(html, string.format(
'<div class="moegirlMonthlyReport-v202112-mainBlockContainer" style="width: %s">' ..
'<div class="moegirlMonthlyReport-v202112-mainBlockTitle">%s</div>' ..
'<div class="moegirlMonthlyReport-v202112-mainBlockContent">\n%s\n</div>' ..
'</div>',
width, title, content
))
end
table.insert(html, '</div>')
end
end
return table.concat(html)
end
-- 主函数
function p.main(frame)
local args = getArgs(frame)
local result = {}
-- 生成日期标题
local dateTitle = generateDateTitle(args.year, args.month)
if dateTitle ~= "" then
table.insert(result, dateTitle)
end
-- 生成编写者信息
local writers = generateWriters(args, "writer", "本期主要编写者")
if writers ~= "" then
table.insert(result, writers)
end
-- 生成排版者信息
local layoutWriters = generateWriters(args, "layoutWriter", "本期主要排版者")
if layoutWriters ~= "" then
table.insert(result, layoutWriters)
end
-- 生成内容行
local rows = generateRows(args)
if rows ~= "" then
table.insert(result, rows)
end
-- 使用frame:preprocess来解析wikitext
local output = table.concat(result, "\n")
return frame:preprocess(output)
end
return p