Module:BilibiliVideoIDValidator

From Moegirlpedia
Revision as of 05:09, 2 August 2021 by AnnAngela (talk | contribs) (Created page with "return { validation = function(frame) local result = true local id = mw.text.trim(frame.args.id or '') if mw.ustring.match(id, '^%d+$') then...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Template-info.svg Module Documentation  [Create] [Refresh]
return {
    validation = function(frame)
        local result = true
        local id = mw.text.trim(frame.args.id or '')
        if mw.ustring.match(id, '^%d+$') then
            -- 纯数字
            if not (string.sub(id, 1, 1) ~= '0' and string.len(id) <= 9) then -- 若id为纯数字时,当首位不为0且长度在1-9位时合法。
                result = false
            end
        elseif mw.ustring.match(id, '^[aA][vV]%d+$') then
            -- 若id为【av+纯数字】(不区分大小写)时,当且仅当纯数字部分首位不为0且长度在1-9位时合法,其余情况添加分类
            if not (string.sub(id, 3, 3) ~= '0' and string.len(id) <= 11) then
                result = false
            end
        elseif mw.ustring.match(id, '4.1.7..$') then
            if string.len(id) == 12 then
                -- 若id为【BV1】开头时,当且仅当id长度为12位,且除去【BV1】后的部分均不包含小写l大写I、大写O和数字0时合法。
                if not mw.ustring.match(id, '^[bB][vV]1[a-km-zA-HJ-NP-Z0-9]+$') then
                    result = false
                end
            elseif string.len(id) == 10 then
                -- 若id为【1】开头时,当且仅当id长度为10位,且除去【1】后的部分均不包含小写l大写I、大写O和数字0时合法。
                if not mw.ustring.match(id, '^1[a-km-zA-HJ-NP-Z0-9]+$') then
                    result = false
                end
            elseif string.len(id) == 9 then
                -- 若id不满足上述四种情况时,当且仅当id长度为9位,且不包含小写l大写I、大写O和数字0时合法。
                if not mw.ustring.match(id, '^[a-km-zA-HJ-NP-Z0-9]+$') then
                    result = false
                end
            else
                result = false
            end
        else
            result = false
        end

        if result then
            return ''
        else
            return frame:expandTemplate {title = 'ArticleCategory', args = {'Bilibili视频ID错误'}} ..
                    '<span class="BilibiliVideoIDValidatorArgument" style="display: none;">' ..
                        frame.args.id .. '</span>'
        end
    end
}