模块:GFInfobox
来自OGAS数据中枢
更多操作
此模块的文档可以在模块:GFInfobox/doc创建
local p = {}
local function cleanParam(s)
if not s then return nil end
s = s:gsub('^[%s\n\r\t]+', ''):gsub('[%s\n\r\t]+$', '')
if s == '' then return nil end
s = s:gsub('^%s*<p[^>]*>', ''):gsub('</p>%s*$', '')
return s
end
-- 安全展开不含用户输入的模板(如颜色模板)
local function expandSafe(frame, s)
if not s then return nil end
return frame:preprocess(s)
end
-- 展开可能含用户输入模板的字段(如 ruby、黑幕等)
local function expandField(frame, key)
local text = cleanParam(frame:getParent().args[key])
if not text then return nil end
return frame:preprocess(text)
end
function p.formatRarity(rarity)
if not rarity then return nil end
local colors = {
[2] = '#777',
[3] = '#33566f',
[4] = '#7b813f',
[5] = '#a7753b',
[6] = '#ad4229',
}
local num = tonumber(rarity)
or (rarity:find('★') and select(2, rarity:gsub('★', '★')))
if num and colors[math.floor(num)] then
return string.format(
'<span style="color:%s">%s</span>',
colors[math.floor(num)],
string.rep('★', math.floor(num))
), false
end
return rarity, false
end
function p.main(frame)
local parentFrame = frame:getParent()
local args = parentFrame.args
local name = cleanParam(args['标题'])
or cleanParam(args['名字'])
or mw.title.getCurrentTitle().subpageText
-- 颜色字段:由 Lua 构造模板调用字符串,用 preprocess 展开,安全
local function fmtColor(rawColor, cType)
rawColor = cleanParam(rawColor)
if not rawColor then return nil end
return expandSafe(frame, '{{' .. cType .. '_color|' .. rawColor .. '}}')
end
local hairColor = fmtColor(args['多种发色'] or args['发色'], 'Hair')
local eyeColor = fmtColor(args['多种瞳色'] or args['瞳色'], 'Eye')
local rarity = p.formatRarity(cleanParam(args['稀有度']))
-- 声优字段
local function fmtVoice()
local v = cleanParam(args['多位声优'] or args['声优'])
if not v then return nil end
if v:find('%[%[') then return v end
return '[[' .. v .. ']]'
end
-- 分类:需要展开的字段(可能含模板)
local function raw(key)
return expandField(frame, key)
end
-- 分类:纯文本字段(不含用户输入模板)
local function rawPlain(key)
return cleanParam(args[key])
end
---------- 拼 wikitext + HTML 混合字符串 ----------
local parts = {}
local function push(s) parts[#parts + 1] = s end
push('<div class="gf-infobox">')
push('<div class="gf-title">' .. name .. '</div>')
local image = cleanParam(args['image'])
push('<div class="gf-image-container">')
if image then
push('[[File:' .. image .. '|280px]]')
local caption = raw('图片说明')
if caption then
push('<div style="font-size:0.9em;">' .. caption .. '</div>')
end
end
push('</div>')
push('<div class="gf-section">基础资料</div>')
push('<div class="gf-table">')
local fields = {
{ '本名', raw('本名') },
{ '别名', raw('别名') },
{ '发色', hairColor },
{ '瞳色', eyeColor },
{ '声优', fmtVoice() },
{ '萌点', raw('萌点') },
{ '类型', raw('类型') },
{ '稀有度', rarity },
{ '团体', raw('所属团体') },
{ '状态', raw('个人状态') },
}
for _, field in ipairs(fields) do
local label, value = field[1], field[2]
if value then
push('<div class="gf-row">')
push('<div class="gf-label">' .. label .. '</div>')
push('<div class="gf-value">' .. value .. '</div>')
push('</div>')
end
end
push('</div>') -- gf-table
local related = raw('相关人士')
if related then
push('<div class="gf-section">亲属或相关人</div>')
push('<div class="gf-related">' .. related .. '</div>')
end
push('</div>') -- gf-infobox
return table.concat(parts, '\n')
end
return p