跳转到内容

模組:WikipediaHonors

维基百科,自由的百科全书

这是本页的一个历史版本,由SunAfterRain留言 | 贡献2020年2月15日 (六) 02:01编辑。这可能和当前版本存在着巨大的差异。

local data = require('Module:WikipediaHonor/data')
local yesno = require('Module:Yesno')
local function error (text)
	return require('Module:Error').error{[1] = text}
end

local z = {}

function typelist(type)
	local typelist = {
		["pagename"] = "頁面",
		["section"] = "章節",
		["honor"] = "榮譽"
	}
	if typelist[type] == nil then
		return "Unknow type <code>" .. type .. '</code>'
	end
	return typelist[type]
end

function extractAliases(item)
    allnames = {item['code']}
    table.insert(allnames, item['fullname'])
    for k, v in ipairs(item['aliases']) do
        table.insert(allnames, v)
    end
    table.insert(allnames, item['name'])
    return allnames
end

function z.data(frame)
	local wt = [=[
{|class="wikitable
|-
!資料類型!!編碼!!可使用的代碼!!名稱!!英文全名!!頒發原因及描述!!章節!!對應模板!!對應分類
	]=]
    for i, item in ipairs(data) do
        allnames = extractAliases(item)
        wt = wt .. string.format([=[
|-
|%s
|%s
|
* %s
|%s
|%s
|style="width:200px;"|
* %s
|%s
|%s
|%s
        ]=], 
        typelist(item['type']), 
        item['code'], 
        '-{' .. mw.text.listToText(allnames, '}-\n* -{', '}-\n* -{') .. '}-', 
        item['name'], 
        item['fullname'], 
        ((item['reason'] and item['reason'] .. '\n* ') or '') .. ((item['description'] and item['description']:gsub(',', '\n* '):gsub('及', ' + '):gsub('或', ' / ')) or ''), 
        ((item['section'] and '[[WP:WPH#' .. item['section'] .. ']]') or ''), 
        ((item['template'] and '[[:Template:' .. item['template'] .. ']]') or ''), 
        ((item['category'] and '[[:Category:' .. item['category'] .. ']]') or '')
        )
    end
    wt = wt .. '|}'
    return frame:preprocess(wt)
end

function main(arg,itemarg)
    map = {}
    for i, item in ipairs(data) do
        map[item['code']:lower()] = i
        for j, alias in ipairs(item['aliases']) do
            map[alias:lower()] = i
        end
    end
    if arg ~= nil then
        if arg and map[mw.text.trim(arg:lower())] then
            arg = data[map[mw.text.trim(arg:lower())]]
            return arg[itemarg]
        elseif arg and mw.text.trim(arg) ~= '' then
            return 'error'
        end
    else
       	return nil
    end
end

function z.getlevel(frame)
	if frame.args.parent then
        args = frame:getParent().args
    else
        args = frame.args
    end
    local item = main(args[1],'code')
    if item == nil then
    	return error('[[Module:WikipediaHonor]]錯誤:沒有參數<code>1</code>')
    elseif item == 'error' then
    	return error('未知榮譽類型:<code>' .. args[1] .. '</code>')
    elseif yesno(args['nolink']) then
    	return main(args[1],'name')
    else
    	return '[[WP:WPH#' .. (main(args[1],'section') or '') .. '|' .. main(args[1],'name') .. ']]'
    end
end

function z.getnewrequest(frame)
	local args = frame.args
    local item = main(args[1],'code')
    if item == nil then
    	return error('[[Module:WikipediaHonor]]錯誤:沒有參數<code>1</code>')
    elseif item == 'error' then
    	return error('未知榮譽類型:<code>' .. args[1] .. '</code>')
    else
    	local arglist = {
    		['title'] = 'Wikipedia:維基榮譽/申請與變更/申請區',
    		['paid'] = 'action=edit&section=new&preload=Template:HonorsApplication/preload2&preloadparams[]=' .. main(args[1],'section'),
    		['text'] = '點此申請'
    	}
    	return frame:expandTemplate{title = "Anchor",args = {[1] = main(args[1],'section')}} .. '<span class="mw-editsection"><span class="mw-editsection-bracket">[</span>' .. require('Module:Fullurl')._fullurl(arglist) .. '<span class="mw-editsection-bracket">]</span></span>'
    end
end

function z.getleveltemplate(frame)
	if frame.args.parent then
        args = frame:getParent().args
    else
        args = frame.args
    end
    local item = main(args[1],'code')
    if item == nil then
    	return error('[[Module:WikipediaHonor]]錯誤:沒有參數<code>1</code>')
    elseif item == 'error' then
    	return error('未知榮譽類型:<code>' .. args[1] .. '</code>')
    elseif yesno(args['nolink']) then
    	return main(args[1],'name')
    else
    	local arg = main(args[1],'args') or {''}
    	if arg == 'args[2]' then
    		arg = {[1] = args[2]}
    	end
    	return frame:expandTemplate{title = main(args[1],'template'),args = arg}
    end
end

function z.getcatlink(frame)
	args = frame.args
    local item = main(args[1],'type')
    if item == nil then
    	return error('[[Module:WikipediaHonor]]錯誤:沒有參數<code>1</code>')
    elseif item == 'error' then
    	return error('未知榮譽類型:<code>' .. args[1] .. '</code>')
     elseif item ~= 'honor' then
     	return error('[[Module:WikipediaHonor]]錯誤:參數<code>' .. args[1] .. '</code>類型為' .. typelist(main(args[1],'type')) .. ',不是榮譽')
     else
    	return '[[:Category:' .. main(args[1],'category') .. '|' .. main(args[1],'name') .. ']]'
    end
end

return z