“Module:Rainbow”的版本间差异

来自BanG Dream! Wiki
跳到导航 跳到搜索
(创建页面,内容为“local module = {} local getArgs = require('Module:Arguments').getArgs function module._main(colors, material) local rubySave = {} material = mw.ustring.gsub(material, '(<ruby[^>]*>.-</ruby>)', function(str) rubySave[#rubySave + 1] = str return 'Ÿ' end) local htmlEscapeSave = {} local htmlEscapeSaveFunc = function(str) htmlEscapeSave[#htmlEscapeSave + 1] = str return 'Æ' end material = mw.ust…”)
 
(没有差异)

2022年7月20日 (三) 23:17的最新版本

此模块的文档可以在Module:Rainbow/doc创建

local module = {}
 
local getArgs = require('Module:Arguments').getArgs
 
function module._main(colors, material)
    local rubySave = {}
    material = mw.ustring.gsub(material, '(<ruby[^>]*>.-</ruby>)', function(str)
        rubySave[#rubySave + 1] = str
        return 'Ÿ'
    end)
    
    local htmlEscapeSave = {}
    local htmlEscapeSaveFunc = function(str)
        htmlEscapeSave[#htmlEscapeSave + 1] = str
        return 'Æ'
    end
    material = mw.ustring.gsub(material, '&#%d+;', htmlEscapeSaveFunc)
    material = mw.ustring.gsub(material, '&[A-Za-z]+;', htmlEscapeSaveFunc)
    
    function lang(ptns)
        for i, v in ipairs(ptns) do
            material = mw.ustring.gsub(material, '(.*%-{)ῧ?(.-)(;?%s-'..v..':)(.-}%-)', function(str0, str1, str2, str3)
                -- 使用ῧ指示保持当前颜色
                -- 使用Ῥ指示这是下一个语言变种的开始,读取颜色
                return str0..'ῧ'..str1..mw.ustring.gsub(str2, '(.)', '‡%1')..'Ῥ'..str3
            end)
        end
    end
 
    lang({
        'zh%-hans', 'zh%-hant', 'zh%-cn', 'zh%-tw', 'zh%-hk', 'zh%-sg', 'zh%-mo', 'zh%-my', 'zh'
    })
 
    function flee(ptns)
        for i, v in ipairs(ptns) do
            material = mw.ustring.gsub(material, '('..v..')', function(str)
                return mw.ustring.gsub(str, '(.)', '‡%1')
            end)
        end
    end
 
    flee({
        -- html标签或mw的标记应当视为一个整体,不单独变色
        '<.->', '%-{', '}%-', '\'\"\`UNIQ%-.-%-QINU\`\"\''
    })
 
    local flag = 1            -- 变色计数
    local ZhConverter_old_flag = 1    -- 字词转换中上一个语言变种的flag
    local ruby = 1            -- ruby计数
    local htmlEscape = 1      -- HTML转义字符计数
    local mark = false        -- 标签跳过
    local rainbow = mw.ustring.gsub(material, '[^%s]', function(str1)
        if str1 == 'ῧ' then
            ZhConverter_old_flag = flag
            return ''
        end
        if str1 == 'Ῥ' then
            flag = ZhConverter_old_flag
            return ''
        end
        if str1 == '‡' then
            mark = true
            return ''
        end
        if str1 == 'Æ' then
            htmlEscape = htmlEscape + 1
            return htmlEscapeSave[htmlEscape - 1]
        end
        if mark then
            mark = false
            return str1
        end
 
        local colour = ''
        if str1 ~= 'Ÿ' then
            colour = '<span style="color:'..colors[flag]..'">'..str1..'</span>'
        else
            colour = string.gsub(rubySave[ruby], '<ruby([^>]*)>', function(str2)
                if string.find(str2, 'style=".-"') then
                    return '<ruby '..string.gsub(str2, 'style="(.-)"', function(str3)
                        return 'style="'..str3..'; color:'..colors[flag]..'"'
                    end)..'>'
                else
                    return '<ruby'..str2..' style="color:'..colors[flag]..'">'
                end
            end)
            ruby = ruby + 1
        end
 
        if flag < #colors then
            flag = flag + 1
        else
            flag = 1
        end
 
        return colour
    end)
 
    return rainbow
end
 
function module.main(frame)
    local args = getArgs(frame, {wrappers='Template:交叉颜色F'})
    local colorStr = args.c or args[1]
    local material = args.s or args[2]
    local colors = mw.text.split(colorStr, ',')
    return module._main(colors, material)
end
 
return module