Undertale社区维基
欢迎来到Undertale社区维基(*`∀´*)ノ亻,如果想要参与条目创建或编辑,请先登录哦

此模块的文档可以在模块:赋值/doc创建

local p = {}
local b = require("Module:Basic")
local cfg = mw.loadData('Module:赋值/config')

        -- 生成反查链接图标(跳转到分类:AU 筛选页)
-- 显示/隐藏由小工具 Gadget-AU-drilldown 的 CSS 控制
function p._search_icon(param, value)
    return '<span style="display:none" class="plainlinks au-drilldown-icon">[{{fullurl:分类:AU|' .. param .. '=' .. mw.uri.encode(value) .. '}} [[File:Search.svg|16px|link=]]]</span>'
end
function p.ref(text, mode)
    local ref_list = b.str_split(text, ";")
    local result = ""
    for _, value in ipairs(ref_list) do
        if b.empty(value) then
            if mode == "source" then
                if mw.ustring.find(value, "<ref") or mw.ustring.find(value, "{{#tag:ref|") then
                    result = result .. value
                else
                    result = result .. "{{#tag:ref|" .. value .. "}}"
                end
            elseif mode == "note" then
                if mw.ustring.find(value, "group=[\"']注") or mw.ustring.find(value, "group=注") then
                    result = result .. value
                else
                    result = result .. "{{#tag:ref|" .. value .. "|group=注}}"
                end
            end
        end
    end
    return result
end

function p.id2link_person(args)
    local dict = {}
    for key, plat in pairs(cfg.platforms) do
        local user = args[key] or ''
        local urls = {}
        for _, tpl in ipairs(plat.person) do
            urls[#urls + 1] = (mw.ustring.gsub(tpl, '{user}', user))
        end
        dict[key] = { key, urls }
    end
    return dict
end

function p.id2link_homepage()
    local dict = {}
    for key, plat in pairs(cfg.platforms) do
        local home = {}
        for _, h in ipairs(plat.home) do
            table.insert(home, h)
        end
        dict[key] = { key, home }
    end
    return dict
end

function p.https_re(link)
    if not mw.ustring.find(mw.ustring.lower(link), "^https?://") then
        local result = "https://" .. link
        return result
    else
        return link
    end
end

function p.maxnum(num_list)
    local maxnum = num_list[1]
    for i = 2, #num_list do
        if num_list[i] > maxnum then
            maxnum = num_list[i]
        end
    end
    return maxnum
end

function p.person(frame)
    local be_id2link_dict = p.id2link_person(frame.args)
    local args = {}
    local ICON = false
    local result_init = "<div style='display: none;'>模板:人物</div>"
    local result = ""
    local result_link = ""
    local result_set = ""
    result = result .. (frame.args[1] or "")
    for key, value in pairs(frame.args) do
        if b.empty(value) then
            args[key] = value
        end
    end

    for key, value in pairs(be_id2link_dict) do
        if b.is_include_key(key, args) then
            for _, link_match_string in pairs(value[2]) do
                local smwset_set_table = {
                    '人物链接组=' .. (frame.args[1] or "") .. ";" .. "https://" .. link_match_string .. ";" .. value[1] .. ";;",
                }
                mw.smw.set(smwset_set_table)
                if not ICON then result_link = result_link .. "<span class='supimg'>" end
                result_link = result_link .. "[[file:" .. value[1] .. ".png|15px|link=" .. "https://" .. link_match_string .. "]]"
                ICON = true
                break
            end
        end
    end

    local link_list = b.str_split(frame.args["url"], "、")

    local be_set_list = cfg.person_sets
    local set_list = b.str_split(frame.args["set"], "、")
    for key, value in ipairs(set_list) do
        for _, s_value in ipairs(be_set_list) do
            if mw.ustring.find(value, s_value) then
                set_list[key] = s_value
                break
            end
        end
    end

    local term_list = b.str_split(frame.args["代数"], "、")

    local link_source_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["链接来源"]))), "、")
    local link_note_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["链接注释"]))), "、")
    local set_source_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["职能来源"]))), "、")
    local set_note_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["职能注释"]))), "、")
    for key, value in ipairs(link_source_list) do
        link_source_list[key] = p.ref(value, "source")
    end
    for key, value in ipairs(link_note_list) do
        link_note_list[key] = p.ref(value, "note")
    end
    for key, value in ipairs(set_source_list) do
        set_source_list[key] = p.ref(value, "source")
    end
    for key, value in ipairs(set_note_list) do
        set_note_list[key] = p.ref(value, "note")
    end

    local site_list = {}
    local NOW_ICON = false
    local maxnum = p.maxnum({ #link_list, #set_list, #term_list, #link_source_list, #link_note_list, #set_source_list, #
    set_note_list })
    local IS_CAN_BREAK_LOOP = false


    for _, value in ipairs(link_list) do
        IS_CAN_BREAK_LOOP = false
        for _, s_value in pairs(be_id2link_dict) do
            if IS_CAN_BREAK_LOOP then
                break
            else
                NOW_ICON = false
            end
            for _, link_match_string in ipairs(s_value[2]) do
                link_match_string = b.escape_pattern_string(link_match_string)
                if mw.ustring.find(value, link_match_string) then
                    table.insert(site_list, s_value[1])
                    NOW_ICON = true
                    IS_CAN_BREAK_LOOP = true
                    break
                end
            end
        end
        if not NOW_ICON then table.insert(site_list, "") end
    end

    for i = 1, maxnum do
        local NOW = false
        if link_list[i] then
            if b.empty(link_list[i]) then
                link_list[i] = p.https_re(link_list[i])
            end
            local smwset_link_table2 = {
                '人物链接组=' ..
                (frame.args[1] or "") ..
                ";" ..
                (link_list[i] or "") ..
                ";" ..
                (site_list[i] or "") ..
                ";" .. ((link_source_list[i]) or "") .. ";" .. ((link_note_list[i]) or ""),
            }
            mw.smw.set(smwset_link_table2)
            if not ICON then result_link = result_link .. "<span class='supimg'>" end
            if b.empty(site_list[i]) then
                result_link = result_link ..
                    "[[file:" .. site_list[i] .. ".png|15px|link=" .. "https://" .. link_list[i] .. "]]"
            end
            ICON = true
        end

        if set_list[i] then
            if set_list[i] == "创始者" then
                set_list[i] = "持有者"
                term_list[i] = "1"
            end

            if set_list[i] == "现持有者" then
                NOW = true
                set_list[i] = "持有者"
            end

            local smwset_set_table1 = {
                '人物职能组=' ..
                (frame.args[1] or "") .. ";" ..
                (set_list[i] or "") ..
                ";" ..
                (term_list[i] or "") ..
                ";" .. (set_source_list[i] or "") .. ";" .. (set_note_list[i] or ""),
            }
            mw.smw.set(smwset_set_table1)

            if NOW then
                local smwset_set_table1 = {
                    '人物职能组=' ..
                    (frame.args[1] or "") .. ";" ..
                    "现持有者" ..
                    ";" ..
                    (term_list[i] or "") ..
                    ";" .. (set_source_list[i] or "") .. ";" .. (set_note_list[i] or ""),
                }
                mw.smw.set(smwset_set_table1)
            end

            if set_list[i] then
                if tonumber(b.empty(term_list[i])) then
                    if set_list[i] == "持有者" and term_list[i] == "1" then
                        result_set = result_set .. "(创始者)"
                    else
                        result_set = result_set ..
                            "(第" .. tonumber(b.empty(term_list[i])) .. "代" .. b.empty(set_list[i]) .. ")"
                    end
                elseif not NOW then
                    result_set = result_set .. "(" .. (set_list[i] or "") .. ")"
                end
                if NOW then result_set = result_set .. "(现持有者)" end
            end
        end
        result = result ..
            (link_source_list[i] or "") ..
            (link_note_list[i] or "") .. (set_source_list[i] or "") .. (set_note_list[i] or "") .. p._search_icon("person", frame.args[1] or "")
    end
    if ICON then result_link = result_link .. "</span>" end
    result = frame:preprocess(b.trim(tostring(result_init .. result .. result_link .. result_set)))
    return result
end

function p.parsedatetime(time)
    local patterns = {
        { "^(%d%d%d%d)-(%d?%d)-(%d?%d) (%d?%d):(%d?%d):(%d?%d)$", "%04d-%02d-%02d %02d:%02d:%02d" },
        { "^(%d%d%d%d)-(%d?%d)-(%d?%d) (%d?%d):(%d?%d)$",         "%04d-%02d-%02d %02d:%02d" },
        { "^(%d%d%d%d)-(%d?%d)-(%d?%d) (%d?%d)$",                 "%04d-%02d-%02d %02d" },
        { "^(%d%d%d%d)-(%d?%d)-(%d?%d)$",                         "%04d-%02d-%02d" },
        { "^(%d%d%d%d)-(%d?%d)$",                                 "%04d-%02d" },
        { "^(%d%d%d%d)$",                                         "%04d" }
    }

    for _, pattern in ipairs(patterns) do
        local matches = { string.match(time, pattern[1]) }
        if #matches > 0 then
            local formatted = string.format(pattern[2], unpack(matches))
            local y, m, d, h, min, s = unpack(matches)
            if m and (tonumber(m) < 1 or tonumber(m) > 12) then return nil end
            if d and (tonumber(d) < 1 or tonumber(d) > 31) then return nil end
            if h and (tonumber(h) < 0 or tonumber(h) > 23) then return nil end
            if min and (tonumber(min) < 0 or tonumber(min) > 59) then return nil end
            if s and (tonumber(s) < 0 or tonumber(s) > 59) then return nil end
            return formatted
        end
    end

    return nil
end

function p.date(frame)
    local date_list = b.str_split(frame.args[1], "、")
    local set_list = b.str_split(frame.args["set"], "、")
    local timenum_list = b.str_split(frame.args["次数"], "、")

    local source_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["来源"]))), "、")
    local note_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["注释"]))), "、")
    for key, value in ipairs(source_list) do
        source_list[key] = p.ref(value, "source")
    end
    for key, value in ipairs(note_list) do
        note_list[key] = p.ref(value, "note")
    end

    local result_init = "<div style='display: none;'>模板:日期</div>"
    local result = ""
    local new_args = {}
    local new_frame = {}

    for key, value in ipairs(date_list) do
        date_list[key] = p.parsedatetime(value)
    end

    local maxnum = p.maxnum({ #date_list, #set_list, #timenum_list, #source_list, #note_list })

    for i = 1, maxnum do
        if date_list[i] then
            if set_list[i] == "发布日期" then
                set_list[i] = "发布"
            elseif set_list[i] == "重置日期" then
                set_list[i] = "重置"
            elseif set_list[i] == "重制日期" then
                set_list[i] = "重制"
            else
                if set_list[i] then
                    local index = mw.ustring.find(set_list[i], "日期")
                    if index then
                        set_list[i] = mw.ustring.sub(set_list[i], 1, index - 1)
                    end
                end
            end
            new_args[1] = set_list[i] or ""
            new_args["time"] = date_list[i]
            new_args["来源"] = source_list[i] or ""
            new_args["注释"] = note_list[i] or ""
            new_frame = mw.getCurrentFrame()
            new_frame.args = new_args
            result = result .. p.status(new_frame)
        end
    end
    result = frame:preprocess(b.trim(tostring(result_init .. result)))
    return result
end

function p.name(frame)
    local result_init = "<div style='display: none;'>模板:名称</div>"
    local result = ""

    local name_list = b.str_split(frame.args[1], "、")

    local be_set_list = cfg.name_sets
    local set_list = b.str_split(frame.args["set"], "、")

    for key, value in ipairs(set_list) do
        for _, s_value in ipairs(be_set_list) do
            if mw.ustring.find(value, s_value) then
                set_list[key] = s_value
                break
            end
        end
    end

    local term_list = {}
    local term1_list = {}
    local term2_list = {}
    if b.empty(frame.args["前者代数"]) or b.empty(frame.args["后者代数"]) then
        term1_list = b.str_split(frame.args["前者代数"], "、")
        term2_list = b.str_split(frame.args["后者代数"], "、")
    else
        term_list = b.str_split(frame.args["代数"], "、")
    end

    local source_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["来源"]))), "、")
    local note_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["注释"]))), "、")
    for key, value in ipairs(source_list) do
        source_list[key] = p.ref(value, "source")
    end
    for key, value in ipairs(note_list) do
        note_list[key] = p.ref(value, "note")
    end

    local smwset_set_table = {}

    for i = 1, #name_list do
        local NOW = false

        if mw.ustring.find((set_list[i] or ""), "现") then
            NOW = true
            set_list[i] = mw.ustring.gsub(set_list[i], "现", "")
        end

        if NOW then
            smwset_set_table = {
                '名称组=' ..
                (name_list[i] or "") ..
                ";" ..
                "现" .. set_list[i] ..
                ";" ..
                (term_list[i] or "") ..
                ";;" .. (source_list[i] or "") .. ";" .. (note_list[i] or ""),
            }
            mw.smw.set(smwset_set_table)
        end

        if b.empty(term_list) then
            smwset_set_table = {
                '名称组=' ..
                (name_list[i] or "") ..
                ";" ..
                (set_list[i] or "") ..
                ";" ..
                (term_list[i] or "") ..
                ";;" .. (source_list[i] or "") .. ";" .. (note_list[i] or ""),
            }
        else
            smwset_set_table = {
                '名称组=' ..
                (name_list[i] or "") ..
                ";" ..
                (set_list[i] or "") ..
                ";" ..
                (term1_list[i] or "") ..
                ";" ..
                (term2_list[i] or "") ..
                ";" ..
                (source_list[i] or "") .. ";" .. (note_list[i] or ""),
            }
        end

        mw.smw.set(smwset_set_table)

        if #result ~= 0 then
            result = result .. "<br>"
        end

        result = result .. name_list[i]

        local _role_map = {["原名"]="role-original", ["官方译名"]="role-official", ["正式译名"]="role-formal", ["民间译名"]="role-folk"}
        local _role_param = _role_map[set_list[i] or ""]

        if b.empty(term_list) then
            if b.empty(set_list[i]) then
                if tonumber(term_list[i]) then
                    result = result .. "(第" .. tonumber(term_list[i]) .. "代" .. set_list[i] .. ")"
                elseif not NOW then
                    result = result .. "(" .. set_list[i] .. ")"
                end
            end
            if NOW then result = result .. "(现" .. set_list[i] .. ")" end
        elseif tonumber(term1_list[i]) and tonumber(term2_list[i]) then
            result = result ..
                "(第" ..
                tonumber(term1_list[i]) ..
                "代原名的第" .. tonumber(term2_list[i]) .. "代" .. set_list[i] .. ")"
        elseif b.empty(set_list[i]) then
            if not NOW then result = result .. "(" .. set_list[i] .. ")" end
            if NOW then result = result .. "(现" .. set_list[i] .. ")" end
        end

        result = result .. (source_list[i] or "") .. (note_list[i] or "")
        if _role_param then
            result = result .. p._search_icon(_role_param, name_list[i] or "")
        end
    end

    result = frame:preprocess(b.trim(tostring(result_init .. result)))
    return result
end

function p.type(frame)
    local type_list = b.str_split(frame.args[1], "、")
    local time_list = b.str_split(frame.args["time"], "、")
    local source_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["来源"]))), "、")
    local note_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["注释"]))), "、")
    for key, value in ipairs(source_list) do
        source_list[key] = p.ref(value, "source")
    end
    for key, value in ipairs(note_list) do
        note_list[key] = p.ref(value, "note")
    end

    local smwset_table = {
    }

    local be_a_type_list = cfg.type_categories.a
    local be_b_type_list = cfg.type_categories.b
    local be_c_type_list = cfg.type_categories.c

    local result_init = "<div style='display: none;'>模板:类型</div>"
    local result = ""

    for i = 1, #type_list do
        local matched = false
        for key, value in ipairs(be_a_type_list) do
            if type_list[i]:find(value) then
                matched = true
                if #result ~= 0 then
                    result = result .. "<br>"
                end
                result = result .. be_a_type_list[key] .. p._search_icon("type", value)
                table.insert(smwset_table,
                    '类型组=' ..
                    value ..
                    ";" ..
                    (time_list[i] or "") ..
                    ";" .. (source_list[i] or "") .. ";" .. (note_list[i] or ""))
            end
        end
        for key, value in ipairs(be_b_type_list) do
            if type_list[i]:find(value) then
                matched = true
                if #result ~= 0 then
                    result = result .. "<br>"
                end
                result = result .. be_b_type_list[key] .. p._search_icon("type", value)
                table.insert(smwset_table,
                    '类型组=' ..
                    value ..
                    ";" ..
                    (time_list[i] or "") ..
                    ";" .. (source_list[i] or "") .. ";" .. (note_list[i] or ""))
            end
        end
        for key, value in ipairs(be_c_type_list) do
            if type_list[i]:find(value) then
                matched = true
                if #result ~= 0 then
                    result = result .. "<br>"
                end
                if key == 1 then
                    result = result .. be_c_type_list[key] .. p._search_icon("type", value)
                else
                    result = result .. "交叉同人 - " .. be_c_type_list[key] .. p._search_icon("type", value)
                end
                table.insert(smwset_table,
                    '类型组=' ..
                    value ..
                    ";" ..
                    (time_list[i] or "") ..
                    ";" .. (source_list[i] or "") .. ";" .. (note_list[i] or ""))
            end
        end

        if not matched then
            if #result ~= 0 then result = result .. "<br>" end
            result = result .. type_list[i] .. p._search_icon("type", type_list[i])
            table.insert(smwset_table,
                '类型组=' .. type_list[i] .. ";" .. (time_list[i] or "") .. ";" .. (source_list[i] or "") .. ";" .. (note_list[i] or ""))
        end

        if b.empty(time_list[i]) then
            result = result .. " (" .. time_list[i] .. ")" .. (source_list[i] or "") .. (note_list[i] or "")
        else
            result = result .. (source_list[i] or "") .. (note_list[i] or "")
        end
    end

    mw.smw.set(smwset_table)
    result = frame:preprocess(b.trim(tostring(result_init .. result)))
    return result
end

function p.status(frame)
    local smwset_set_table = {}
    local result_init = "<div style='display: none;'>模板:状态</div>"
    local result = ""

    local status_list = b.str_split(frame.args[1], "、")
    local time_list = b.str_split(frame.args["time"], "、")

    local source_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["来源"]))), "、")
    local note_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["注释"]))), "、")
    for key, value in ipairs(source_list) do
        source_list[key] = p.ref(value, "source")
    end
    for key, value in ipairs(note_list) do
        note_list[key] = p.ref(value, "note")
    end

    local maxnum = p.maxnum({ #status_list, #time_list, #source_list, #note_list })

    for i = 1, maxnum do
        table.insert(smwset_set_table,
            '状态组=' ..
            (status_list[i] or "") ..
            ";" ..
            (time_list[i] or "") ..
            ";" .. (source_list[i] or "") .. ";" .. (note_list[i] or ""))
        if #result ~= 0 then
            result = result .. "<br>"
        end

        if b.empty(time_list[i]) then
            result = result ..
                (status_list[i] or "") .. " (" .. (time_list[i] or "") .. ")" .. (source_list[i] or "") .. (note_list[i] or "") .. p._search_icon("status", status_list[i] or "")
        else
            result = result .. (status_list[i] or "") .. (source_list[i] or "") .. (note_list[i] or "") .. p._search_icon("status", status_list[i] or "")
        end
    end

    mw.smw.set(smwset_set_table)
    result = frame:preprocess(b.trim(tostring(result_init .. result)))
    return result
end

function p.style(frame)
    local smwset_set_table = {}
    local result_init = "<div style='display: none;'>模板:风格</div>"
    local result = ""

    local style_list = b.str_split(frame.args[1], "、")
    local time_list = b.str_split(frame.args["time"], "、")

    local source_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["来源"]))), "、")
    local note_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["注释"]))), "、")
    for key, value in ipairs(source_list) do
        source_list[key] = p.ref(value, "source")
    end
    for key, value in ipairs(note_list) do
        note_list[key] = p.ref(value, "note")
    end

    local maxnum = p.maxnum({ #style_list, #time_list, #source_list, #note_list })

    for i = 1, maxnum do
        table.insert(smwset_set_table,
            '风格组=' ..
            (style_list[i] or "") ..
            ";" ..
            (time_list[i] or "") ..
            ";" .. (source_list[i] or "") .. ";" .. (note_list[i] or ""))
        if #result ~= 0 then
            result = result .. "<br>"
        end
        result = result ..
            (style_list[i] or "") .. (time_list[i] or "") .. (source_list[i] or "") .. (note_list[i] or "") .. p._search_icon("style", style_list[i] or "")
    end

    mw.smw.set(smwset_set_table)
    result = frame:preprocess(b.trim(tostring(result_init .. result)))
    return result
end

function p.backdrop(frame)
    local smwset_set_table = {}
    local result_init = "<div style='display: none;'>模板:背景</div>"
    local result = ""

    local backdrop_list = b.str_split(frame.args[1], "、")
    local time_list = b.str_split(frame.args["time"], "、")

    local source_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["来源"]))), "、")
    local note_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["注释"]))), "、")
    for key, value in ipairs(source_list) do
        source_list[key] = p.ref(value, "source")
    end
    for key, value in ipairs(note_list) do
        note_list[key] = p.ref(value, "note")
    end

    local maxnum = p.maxnum({ #backdrop_list, #time_list, #source_list, #note_list })

    for i = 1, maxnum do
        table.insert(smwset_set_table,
            '背景组=' ..
            (backdrop_list[i] or "") ..
            ";" ..
            (time_list[i] or "") ..
            ";" .. (source_list[i] or "") .. ";" .. (note_list[i] or ""))
        if #result ~= 0 then
            result = result .. "<br>"
        end
        result = result ..
            (backdrop_list[i] or "") .. (time_list[i] or "") .. (source_list[i] or "") .. (note_list[i] or "") .. p._search_icon("bg", backdrop_list[i] or "")
    end

    mw.smw.set(smwset_set_table)
    result = frame:preprocess(b.trim(tostring(result_init .. result)))
    return result
end

function p.homepage(frame)
    local term_list = b.str_split(frame.args["代数"], "、")

    local source_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["来源"]))), "、")
    local note_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["注释"]))), "、")
    for key, value in ipairs(source_list) do
        source_list[key] = p.ref(value, "source")
    end
    for key, value in ipairs(note_list) do
        note_list[key] = p.ref(value, "note")
    end

    local result_init = "<div style='display: none;'>模板:主页</div>"
    local result = ""

    local smwset_set_table = {}
    local link_list = {}
    local site_list = {}
    local SITE = false
    local NOW_ICON = false
    local IS_CAN_BREAK_LOOP = false
    local be_id2link_dict = p.id2link_homepage()
    local args1 = b.str_split(frame.args[1], "、")
    
    for _, value in pairs(be_id2link_dict) do
        for _, s_value in pairs(args1) do
            if value[1] == s_value then
                SITE = true
                break
            end
        end
    end

    if SITE then
        site_list = args1
        link_list = b.str_split(frame.args[2], "、")
    else
        link_list = args1
    end

    for i, value in ipairs(link_list) do
        if b.empty(value) then
            link_list[i] = p.https_re(value)
        end
    end

    if SITE == false then
        for _, value in ipairs(link_list) do
            IS_CAN_BREAK_LOOP = false
            for _, s_value in pairs(be_id2link_dict) do
                if IS_CAN_BREAK_LOOP then
                    break
                else
                    NOW_ICON = false
                end
                for _, link_match_string in ipairs(s_value[2]) do
                    link_match_string = b.escape_pattern_string(link_match_string)
                    if mw.ustring.find(value, link_match_string) then
                        table.insert(site_list, s_value[1])
                        NOW_ICON = true
                        IS_CAN_BREAK_LOOP = true
                        break
                    end
                end
            end
            if not NOW_ICON then table.insert(site_list, "") end
        end
    end

    for i = 1, #link_list do
        table.insert(smwset_set_table,
            '主页组=' ..
            link_list[i] ..
            ";" ..
            (site_list[i] or frame.args[1]) ..
            ";" ..
            (term_list[i] or "") ..
            ";" .. (source_list[i] or "") .. ";" .. (note_list[i] or ""))
        local icon_name = b.empty(site_list[i]) and site_list[i] or frame.args[1]
        result = result .. "[[file:" .. icon_name .. ".png|25px|link=" .. link_list[i] .. "]]"
        result = result .. (source_list[i] or "") .. (note_list[i] or "") .. p._search_icon("platform", site_list[i] or frame.args[1])
    end

    mw.smw.set(smwset_set_table)
    result = frame:preprocess(b.trim(tostring(result_init .. result)))
    return result
end

function p.licence(frame)
    local licence_list = b.str_split(frame.args[1], "、")

    local source_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["来源"]))), "、")
    local note_list = b.str_split(b.empty(mw.text.decode(mw.text.unstripNoWiki(frame.args["注释"]))), "、")
    for key, value in ipairs(source_list) do
        source_list[key] = p.ref(value, "source")
    end
    for key, value in ipairs(note_list) do
        note_list[key] = p.ref(value, "note")
    end

    local result_init = "<div style='display: none;'>模板:许可</div>"
    local result = ""
    local CC = false
    local OTHER = false
    local INLIST = false
    local smw_licence_result = ""

    local be_licence_list1 = { "CC" }
    local be_licence_list2 = cfg.licence.cc_components
    local be_licence_list3 = cfg.licence.cc_versions
    local be_licence_list_cc = cfg.licence.valid_combinations

    local be_licence_list_other = cfg.licence.other_licences

    local smwset_set_table = {}

    for i = 1, #licence_list do
        for _, value in ipairs(be_licence_list_other) do
            if mw.ustring.find(licence_list[i], value) then
                OTHER = true
                result = "{{" .. value .. "}}"
                smw_licence_result = smw_licence_result .. value
            end
        end

        for _, value in ipairs(be_licence_list1) do
            if mw.ustring.find(licence_list[i]:upper(), value) then
                CC = true
            end
        end

        for _, value in ipairs(be_licence_list2) do
            if mw.ustring.find(licence_list[i]:upper(), value) then
                result = result .. "[[文件:CC " .. value .. ".png|50px]]"
                CC = true
                if #smw_licence_result == 0 then
                    smw_licence_result = smw_licence_result .. value
                else
                    smw_licence_result = smw_licence_result .. "-" .. value
                end
            end
        end

        if CC == true and #smw_licence_result == 0 then
            for _, alias in ipairs(cfg.licence.cc0_aliases) do
                if mw.ustring.find(licence_list[i]:upper(), mw.ustring.upper(alias)) then
                    smw_licence_result = "CC 0"
                    break
                end
            end
        end

        for _, value in ipairs(be_licence_list3) do
            if mw.ustring.find(licence_list[i], value) then
                result = result .. value
                smw_licence_result = smw_licence_result .. " " .. value
            end
        end

        if CC == true then
            result = "[[文件:CC.png|50px]]" .. result
            if #smw_licence_result == 0 then
                smw_licence_result = "CC" .. smw_licence_result
            else
                smw_licence_result = "CC " .. smw_licence_result
            end
        end

        for _, value in ipairs(be_licence_list_cc) do
            if value == smw_licence_result then
                INLIST = true
                break
            end
        end

        if (CC == true and INLIST == false) then
            result = result .. "输入的许可中,CC协议的搭配不正确。"
        else
            table.insert(smwset_set_table,
                '许可组=' ..
                smw_licence_result .. ";" .. (source_list[i] or "") .. ";" .. (note_list[i] or ""))
        end

        if CC == false and OTHER == false then
            result = result .. licence_list[i]
            smw_licence_result = smw_licence_result .. licence_list[i]
            table.insert(smwset_set_table,
                '许可组=' ..
                smw_licence_result .. ";" .. (source_list[i] or "") .. ";" .. (note_list[i] or ""))
        end
    end

    mw.smw.set(smwset_set_table)
    result = frame:preprocess(b.trim(tostring(result_init .. result)))
    return result
end

return p