模組:WikipediaHonors
外观
local data = require('Module:WikipediaHonors/data')
local yesno = require('Module:Yesno')
local function error (text)
return require('Module:Error').error{[1] = '[[Module:WikipediaHonors]]錯誤:' .. text}
end
local z = {}
function typelist(type)
local typelist = {
["type"] = "類型",
["page"] = "頁面",
["section"] = "章節",
["honor"] = "榮譽"
}
if typelist[type] == nil then
return error("Unknow type <code>" .. type .. '</code>')
end
return typelist[type]
end
function extractAliases(item)
allnames = {item['code']:lower()}
table.insert(allnames, item['fullname']:lower())
for k, v in ipairs(item['aliases']) do
table.insert(allnames, v:lower())
end
table.insert(allnames, item['name']:lower())
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'] or ''),
((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
local function item (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
local function typeerror (arg)
return error('參數<code>' .. arg .. '</code>類型為' .. typelist(item(arg,'type')) .. ',不是榮譽')
end
function z.getlevel(frame)
if frame.args.parent then
args = frame:getParent().args
else
args = frame.args
end
local code = item(args[1],'code')
if code == nil then
return error('沒有參數<code>1</code>')
elseif code == 'error' then
return error('未知榮譽類型:<code>' .. args[1] .. '</code>')
elseif yesno(args['nolink']) then
return item(args[1],'name')
else
return '[[WP:WPH#' .. (item(args[1],'section') or '') .. '|' .. item(args[1],'name') .. ']]'
end
end
function z.getnewrequest(frame)
local args = frame.args
local code = item(args[1],'code')
if code == nil then
return error('沒有參數<code>1</code>')
elseif code == 'error' then
return error('未知榮譽類型:<code>' .. args[1] .. '</code>')
--elseif code ~= 'honor' then
-- return typeerror (args[1])
else
local arglist = {
['title'] = 'Wikipedia:維基榮譽/申請與變更/申請區',
['paid'] = 'action=edit§ion=new&preload=Template:HonorsApplication/preload2&preloadparams[]=' .. item(args[1],'section'),
['text'] = '點此申請'
}
return frame:expandTemplate{title = "Anchor",args = {[1] = item(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 code = item(args[1],'code')
if code == nil then
return error('沒有參數<code>1</code>')
elseif code == 'error' then
return error('未知榮譽類型:<code>' .. args[1] .. '</code>')
elseif yesno(args['nolink']) then
return item(args[1],'name')
--elseif code ~= 'honor' then
-- return typeerror (args[1])
else
local arg = item(args[1],'args') or {''}
if arg == 'args[2]' then
arg = {[1] = args[2]}
end
return frame:expandTemplate{title = item(args[1],'template'),args = arg}
end
end
function z.getcatlink(frame)
args = frame.args
local code = item(args[1],'type')
if code == nil then
return error('沒有參數<code>1</code>')
elseif code == 'error' then
return error('未知榮譽類型:<code>' .. args[1] .. '</code>')
--elseif code ~= 'honor' then
-- return typeerror (args[1])
else
return '[[:Category:' .. item(args[1],'category') .. '|' .. item(args[1],'name') .. ']]'
end
end
return z