打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

模块:GFInfobox:修订间差异

来自OGAS数据中枢
弃权者留言 | 贡献
无编辑摘要
弃权者留言 | 贡献
无编辑摘要
 
(未显示同一用户的2个中间版本)
第9行: 第9行:
end
end


local function expand(frame, s)
-- 递归展开模板和wikitext
local function expandWikitext(frame, text)
    if not text then return nil end
   
    -- 先展开所有模板
    local expanded = text
   
    -- 处理 {{ruby|文字|注音}} 模板
    expanded = expanded:gsub('{{ruby|([^|]+)|([^}]+)}}', function(base, ruby)
        return frame:expandTemplate{ title = 'ruby', args = { base, ruby } }
    end)
   
    -- 处理 {{黑幕|文字}} 模板
    expanded = expanded:gsub('{{黑幕|([^}]+)}}', function(content)
        return frame:expandTemplate{ title = '黑幕', args = { content } }
    end)
   
    -- 处理 {{lang|语言|文字}} 模板(如果有)
    expanded = expanded:gsub('{{lang|([^|]+)|([^}]+)}}', function(lang, text)
        return frame:expandTemplate{ title = 'lang', args = { lang, text } }
    end)
   
    -- 如果有其他常用模板,可以继续添加
   
    return expanded
end
 
-- 安全展开不含用户输入的模板(如颜色模板)
local function expandSafe(frame, s)
     if not s then return nil end
     if not s then return nil end
     return frame:preprocess(s)
     return frame:preprocess(s)
第30行: 第58行:
             colors[math.floor(num)],
             colors[math.floor(num)],
             string.rep('★', math.floor(num))
             string.rep('★', math.floor(num))
         )
         ), false
     end
     end
     return rarity
     return rarity, false
end
end


function p.main(frame)
function p.main(frame)
local parentFrame = frame:getParent()
    local args = parentFrame.args
   
    -- 调试:输出关键信息
    local debug = {}
    debug[#debug+1] = 'frame title: ' .. tostring(frame:getTitle())
    debug[#debug+1] = 'parent title: ' .. tostring(parentFrame:getTitle())
    debug[#debug+1] = '相关人士 raw: ' .. tostring(args['相关人士'])
   
    -- 测试 preprocess
    local raw = args['相关人士']
    if raw then
        local ok, result = pcall(function()
            return frame:preprocess(raw)
        end)
        debug[#debug+1] = 'preprocess ok: ' .. tostring(ok)
        debug[#debug+1] = 'preprocess result: ' .. tostring(result)
    end
   
    return table.concat(debug, '<br/>')
end
function p.main2(frame)
     local parentFrame = frame:getParent()
     local parentFrame = frame:getParent()
     local args = parentFrame.args
     local args = parentFrame.args
第67行: 第71行:
         or mw.title.getCurrentTitle().subpageText
         or mw.title.getCurrentTitle().subpageText


     local function fmtColor(raw, cType)
    -- 颜色字段:由 Lua 构造模板调用字符串,用 preprocess 展开,安全
         raw = cleanParam(raw)
     local function fmtColor(rawColor, cType)
         if not raw then return nil end
         rawColor = cleanParam(rawColor)
         return frame:preprocess('{{' .. cType .. '_color|' .. raw .. '}}')
         if not rawColor then return nil end
         return expandSafe(frame, '{{' .. cType .. '_color|' .. rawColor .. '}}')
     end
     end


     local hairColor = fmtColor(args['多种发色'] or args['发色'], 'Hair')
     local hairColor = fmtColor(args['多种发色'] or args['发色'], 'Hair')
     local eyeColor   = fmtColor(args['多种瞳色'] or args['瞳色'], 'Eye')
     local eyeColor = fmtColor(args['多种瞳色'] or args['瞳色'], 'Eye')
     local rarity     = p.formatRarity(cleanParam(args['稀有度']))
     local rarity   = p.formatRarity(cleanParam(args['稀有度']))


     local function ep(key)
    -- 声优字段
         local v = cleanParam(args[key])
     local function fmtVoice()
         local v = cleanParam(args['多位声优'] or args['声优'])
         if not v then return nil end
         if not v then return nil end
         return expand(frame, v)
         if v:find('%[%[') then return v end
        return '[[' .. v .. ']]'
     end
     end


     local function fmtVoice()
    -- 获取字段值并展开其中的模板
         local raw = cleanParam(args['多位声优'] or args['声优'])
     local function getField(key)
         local raw = cleanParam(args[key])
         if not raw then return nil end
         if not raw then return nil end
         local expanded = expand(frame, raw)
         return expandWikitext(frame, raw)
        if expanded:find('%[%[') then
            return expanded
        end
        return '[[' .. expanded .. ']]'
     end
     end


 
    ---------- 拼 wikitext + HTML 混合字符串 ----------
     local parts = {}
     local parts = {}
     local function push(s) parts[#parts + 1] = s end
     local function push(s) parts[#parts + 1] = s end


     push('<div class="gf-infobox">')
     push('<div class="gf-infobox">')
 
     push('<div class="gf-title">' .. name .. '</div>')
     push('<div class="gf-title">' .. mw.text.nowiki(name):gsub('&amp;', '&') .. '</div>')
    parts[#parts] = '<div class="gf-title">' .. expand(frame, name) .. '</div>'


     local image = cleanParam(args['image'])
     local image = cleanParam(args['image'])
第106行: 第108行:
     if image then
     if image then
         push('[[File:' .. image .. '|280px]]')
         push('[[File:' .. image .. '|280px]]')
         local caption = cleanParam(args['图片说明'])
         local caption = getField('图片说明')
         if caption then
         if caption then
             push('<div style="font-size:0.9em;">' .. expand(frame, caption) .. '</div>')
             push('<div style="font-size:0.9em;">' .. caption .. '</div>')
         end
         end
     end
     end
第117行: 第119行:


     local fields = {
     local fields = {
         { '本名',  ep('本名') },
         { '本名',  getField('本名') },
         { '别名',  ep('别名') },
         { '别名',  getField('别名') },
         { '发色',  hairColor },
         { '发色',  hairColor },
         { '瞳色',  eyeColor },
         { '瞳色',  eyeColor },
         { '声优',  fmtVoice() },
         { '声优',  fmtVoice() },
         { '萌点',  ep('萌点') },
         { '萌点',  getField('萌点') },
         { '类型',  ep('类型') },
         { '类型',  getField('类型') },
         { '稀有度', rarity },
         { '稀有度', rarity },
         { '团体',  ep('所属团体') },
         { '团体',  getField('所属团体') },
         { '状态',  ep('个人状态') },
         { '状态',  getField('个人状态') },
     }
     }


第141行: 第143行:
     push('</div>') -- gf-table
     push('</div>') -- gf-table


     local related = ep('相关人士')
     local related = getField('相关人士')
     if related then
     if related then
         push('<div class="gf-section">亲属或相关人</div>')
         push('<div class="gf-section">亲属或相关人</div>')
第147行: 第149行:
     end
     end


     push('</div>')  
     push('</div>') -- gf-infobox


     return table.concat(parts, '\n')
     return table.concat(parts, '\n')

2026年5月13日 (三) 09:56的最新版本

此模块的文档可以在模块: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

-- 递归展开模板和wikitext
local function expandWikitext(frame, text)
    if not text then return nil end
    
    -- 先展开所有模板
    local expanded = text
    
    -- 处理 {{ruby|文字|注音}} 模板
    expanded = expanded:gsub('{{ruby|([^|]+)|([^}]+)}}', function(base, ruby)
        return frame:expandTemplate{ title = 'ruby', args = { base, ruby } }
    end)
    
    -- 处理 {{黑幕|文字}} 模板
    expanded = expanded:gsub('{{黑幕|([^}]+)}}', function(content)
        return frame:expandTemplate{ title = '黑幕', args = { content } }
    end)
    
    -- 处理 {{lang|语言|文字}} 模板(如果有)
    expanded = expanded:gsub('{{lang|([^|]+)|([^}]+)}}', function(lang, text)
        return frame:expandTemplate{ title = 'lang', args = { lang, text } }
    end)
    
    -- 如果有其他常用模板,可以继续添加
    
    return expanded
end

-- 安全展开不含用户输入的模板(如颜色模板)
local function expandSafe(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))
        ), 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 getField(key)
        local raw = cleanParam(args[key])
        if not raw then return nil end
        return expandWikitext(frame, raw)
    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 = getField('图片说明')
        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 = {
        { '本名',   getField('本名') },
        { '别名',   getField('别名') },
        { '发色',   hairColor },
        { '瞳色',   eyeColor },
        { '声优',   fmtVoice() },
        { '萌点',   getField('萌点') },
        { '类型',   getField('类型') },
        { '稀有度', rarity },
        { '团体',   getField('所属团体') },
        { '状态',   getField('个人状态') },
    }

    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 = getField('相关人士')
    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