模块:指令/适用模板

来自GCW
跳到导航 跳到搜索

此模块的文档可以在模块:指令/适用模板/doc创建

--p
local p = {}
function p.applicabletemplates(frame)
    -- 获取外部参数
    local args = frame.args

    --方法-分割字符串
    local function split(input, delimiter)
        input = tostring(input)
        delimiter = tostring(delimiter)
        if (delimiter == '') then return false end
        local pos, arr = 0, {}
        -- for each divider found
        for st, sp in function() return string.find(input, delimiter, pos, true) end do
            table.insert(arr, string.sub(input, pos, st - 1))
            pos = sp + 1
        end
        table.insert(arr, string.sub(input, pos))
        return arr
    end

    --方法-排除数组指定元素
    local function arrRemove(arr1, arr2)
        local arrText = ""
        --根据排除长度循环
        for k = 1, #arr2, 1 do
            --根据值长度循环
            for i = 1, #arr1, 1 do
                if (arr1[i] == arr2[k]) then
                    table.remove(arr1, i)
                end
            end
        end
        arrText = arr1
        return arrText
    end

    --方法-超链接
    local function link(url, name)
        local res = ''
        res = '[[' .. url .. '|' .. name .. ']]'
        return res
    end

    --获取数组
    local templateIndex = split(args[1], ",")
    local templateIndex2 = split(args[2], ",")
    local templateRemove = split(args[3], ",")
    for i = 1, #templateRemove, 1 do
        templateRemove[i] = "游戏模板:" .. templateRemove[i]
    end
    --最终值
    local tempArr = arrRemove(templateIndex2, templateRemove)

	--smwSet初始化
	local templateSmwSet = {['适用模板'] = {args[1]}}
	
    --html初始化
    local html = ""
    --html生成
    --父结构
    for i = 1, #templateIndex, 1 do
        local tempUrlName = "游戏模板:"
        tempUrlName = tempUrlName .. templateIndex[i]
        html = html .. "<tr><td>" .. link(tempUrlName, string.gsub(tempUrlName, "游戏模板:", ""))
        html = html .. "</td><td>"

        --子结构
        if (#tempArr == 0) then
            html = html .. "无"
        else
        for i = 1, #tempArr, 1 do
        	local templateName = string.gsub(tempArr[i], "游戏模板:", "")
        		table.insert(templateSmwSet['适用模板'],templateName)
                html = html .. link(tempArr[i],templateName)
                if (i ~= #tempArr) then
                    html = html .. ", "
                end
            end
        end
        html = html .. "</td></tr>"
    end
	mw.smw.set( templateSmwSet )
    --返回html结构
    return html
end

--最终返回值p
return p