模块:GFInfobox:修订间差异
来自OGAS数据中枢
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第7行: | 第7行: | ||
s = s:gsub('^%s*<p[^>]*>', ''):gsub('</p>%s*$', '') | s = s:gsub('^%s*<p[^>]*>', ''):gsub('</p>%s*$', '') | ||
return s | return s | ||
end | |||
local function expand(frame, s) | |||
if not s then return nil end | |||
return frame:preprocess(s) | |||
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)) | |||
) | |||
end | |||
return rarity | |||
end | end | ||
function p.main(frame) | function p.main(frame) | ||
local | |||
local | local parentFrame = frame:getParent() | ||
local args = parentFrame.args | |||
local | |||
local name = cleanParam(args['标题']) | |||
or cleanParam(args['名字']) | |||
or mw.title.getCurrentTitle().subpageText | |||
local function fmtColor(raw, cType) | |||
raw = cleanParam(raw) | |||
if not raw then return nil end | |||
return frame:preprocess('{{' .. cType .. '_color|' .. raw .. '}}') | |||
end | |||
local hairColor = fmtColor(args['多种发色'] or args['发色'], 'Hair') | |||
local eyeColor = fmtColor(args['多种瞳色'] or args['瞳色'], 'Eye') | |||
local rarity = p.formatRarity(cleanParam(args['稀有度'])) | |||
local function ep(key) | |||
local v = cleanParam(args[key]) | |||
if not v then return nil end | |||
return expand(frame, v) | |||
end | |||
local function fmtVoice() | |||
local raw = cleanParam(args['多位声优'] or args['声优']) | |||
if not raw then return nil end | |||
local expanded = expand(frame, raw) | |||
if expanded:find('%[%[') then | |||
return expanded | |||
end | |||
return '[[' .. expanded .. ']]' | |||
end | |||
local parts = {} | |||
local function push(s) parts[#parts + 1] = s end | |||
push('<div class="gf-infobox">') | |||
push('<div class="gf-title">' .. mw.text.nowiki(name):gsub('&', '&') .. '</div>') | |||
parts[#parts] = '<div class="gf-title">' .. expand(frame, name) .. '</div>' | |||
local image = cleanParam(args['image']) | local image = cleanParam(args['image']) | ||
push('<div class="gf-image-container">') | |||
if image then | if image then | ||
push('[[File:' .. image .. '|280px]]') | |||
local caption = cleanParam(args['图片说明']) | local caption = cleanParam(args['图片说明']) | ||
if caption then | if caption then | ||
push('<div style="font-size:0.9em;">' .. expand(frame, caption) .. '</div>') | |||
end | end | ||
end | end | ||
push('</div>') | |||
push('<div class="gf-section">基础资料</div>') | |||
push('<div class="gf-table">') | |||
local fields = { | local fields = { | ||
{'本名', | { '本名', ep('本名') }, | ||
{'别名', | { '别名', ep('别名') }, | ||
{'发色', | { '发色', hairColor }, | ||
{'瞳色', | { '瞳色', eyeColor }, | ||
{'声优', | { '声优', fmtVoice() }, | ||
{'萌点', | { '萌点', ep('萌点') }, | ||
{'类型', | { '类型', ep('类型') }, | ||
{'稀有度', | { '稀有度', rarity }, | ||
{'团体', | { '团体', ep('所属团体') }, | ||
{'状态', | { '状态', ep('个人状态') }, | ||
} | } | ||
for _, field in ipairs(fields) do | 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 | ||
end | end | ||
local related = | push('</div>') -- gf-table | ||
local related = ep('相关人士') | |||
if related then | if related then | ||
push('<div class="gf-section">亲属或相关人</div>') | |||
push('<div class="gf-related">' .. related .. '</div>') | |||
end | end | ||
push('</div>') | |||
return table.concat(parts, '\n') | |||
end | end | ||
return p | return p | ||
2026年5月12日 (二) 22:05的版本
此模块的文档可以在模块: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 expand(frame, s)
if not s then return nil end
return frame:preprocess(s)
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))
)
end
return rarity
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
local function fmtColor(raw, cType)
raw = cleanParam(raw)
if not raw then return nil end
return frame:preprocess('{{' .. cType .. '_color|' .. raw .. '}}')
end
local hairColor = fmtColor(args['多种发色'] or args['发色'], 'Hair')
local eyeColor = fmtColor(args['多种瞳色'] or args['瞳色'], 'Eye')
local rarity = p.formatRarity(cleanParam(args['稀有度']))
local function ep(key)
local v = cleanParam(args[key])
if not v then return nil end
return expand(frame, v)
end
local function fmtVoice()
local raw = cleanParam(args['多位声优'] or args['声优'])
if not raw then return nil end
local expanded = expand(frame, raw)
if expanded:find('%[%[') then
return expanded
end
return '[[' .. expanded .. ']]'
end
local parts = {}
local function push(s) parts[#parts + 1] = s end
push('<div class="gf-infobox">')
push('<div class="gf-title">' .. mw.text.nowiki(name):gsub('&', '&') .. '</div>')
parts[#parts] = '<div class="gf-title">' .. expand(frame, name) .. '</div>'
local image = cleanParam(args['image'])
push('<div class="gf-image-container">')
if image then
push('[[File:' .. image .. '|280px]]')
local caption = cleanParam(args['图片说明'])
if caption then
push('<div style="font-size:0.9em;">' .. expand(frame, caption) .. '</div>')
end
end
push('</div>')
push('<div class="gf-section">基础资料</div>')
push('<div class="gf-table">')
local fields = {
{ '本名', ep('本名') },
{ '别名', ep('别名') },
{ '发色', hairColor },
{ '瞳色', eyeColor },
{ '声优', fmtVoice() },
{ '萌点', ep('萌点') },
{ '类型', ep('类型') },
{ '稀有度', rarity },
{ '团体', ep('所属团体') },
{ '状态', ep('个人状态') },
}
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 = ep('相关人士')
if related then
push('<div class="gf-section">亲属或相关人</div>')
push('<div class="gf-related">' .. related .. '</div>')
end
push('</div>')
return table.concat(parts, '\n')
end
return p