Module:沙盒
来自Vocawiki
更多操作
local p = {}
local REDIRECT_PREFIX = "/Special:Redirect/file/"
-- 只接受 “xxx.svg”,返回行内 CSS 变量赋值
local function buildIconVarStyle(icon)
if not icon or icon == "" then
return ""
end
icon = mw.text.trim(tostring(icon))
-- 只允许纯文件名且必须 .svg 结尾
-- 允许:字母数字、下划线、短横线、空格、点
if not icon:match("^[%w%-%_ %.]+%.svg$") then
return ""
end
local filename = icon:gsub(" ", "_")
local encoded = mw.uri.encode(filename, "PATH")
-- 生成 /Special:Redirect/file/xxx.svg
local url = REDIRECT_PREFIX .. encoded
return string.format("--notice-icon:url(%s);", url)
end
function p.main(frame)
local args = frame:getParent().args
local type_ = args.type or "info"
-- icon=xxx.svg -> style="--notice-icon:url(/Special:Redirect/file/xxx.svg);"
local iconVar = buildIconVarStyle(args.icon)
local iconAttr = ""
if iconVar ~= "" then
iconAttr = ' style="' .. iconVar .. '"'
end
-- text模式
local textContent = args.text or args[1]
if textContent and textContent ~= "" then
return string.format([[
<div class="notice notice-%s">
<div class="notice-icon"%s></div>
<div class="notice-text text-base">
<div class="notice-text-only text-base">%s</div>
</div>
</div>
]], type_, iconAttr, textContent)
end
-- 列表
local listItems = {}
for i = 1, 50 do
local key = "l" .. i
if args[key] and args[key] ~= "" then
table.insert(listItems, "<li>" .. args[key] .. "</li>")
end
end
local listHtml = ""
if #listItems > 0 then
listHtml = '<ul class="notice-list text-sm">' .. table.concat(listItems, "\n") .. "</ul>"
elseif args.list and args.list ~= "" then
listHtml = '<ul class="notice-list text-sm">' .. args.list .. "</ul>"
end
return string.format([[
<div class="notice notice-%s">
<div class="notice-icon"%s></div>
<div class="notice-text text-base">
<div class="notice-title text-base">%s</div>
%s
<div class="notice-detail text-sm">%s</div>
</div>
</div>
]], type_, iconAttr, args.title or "", listHtml, args.detail or "")
end
return p