模組:WikipediaHonors/parse
外观
local parse = {}
parse.__index = parse
function parse.new(self)
local obj = {}
obj.data = mw.loadData('Module:WikipediaHonors/data')
obj.frame = mw.getCurrentFrame()
setmetatable(obj, parse)
return obj
end
function parse.error(self, text)
return require('Module:Error').error{[1] = '[[Module:WikipediaHonors]]錯誤:' .. text}
end
function parse.typelist(self, 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 parse.extractAliases(self, item)
local allnames = {}
if item['code']:lower() == "~-" then
allnames = {
item['code']:lower()
}
end
table.insert(allnames, item['fullname']:lower())
for k, v in ipairs(item['aliases']) do
table.insert(allnames, v:lower())
end
return allnames
end
function parse.getItemVal (self, call, key)
local map = {}
for i, item in ipairs(self.data) do
map[item['code']:lower()] = i
for j, alias in ipairs(item['aliases']) do
map[alias:lower()] = i
end
end
if call ~= nil then
if call and map[mw.text.trim(call:lower())] then
call = self.data[map[mw.text.trim(call:lower())]]
return call[key]
elseif call and mw.text.trim(call) ~= '' then
return 'error'
end
end
return nil
end
function parse.getDataTable(self)
local wt = [=[
{|class="wikitable"
|-class="nowrap"
!資料類型!!編碼!!可使用的代碼!!名稱!!英文全名!!頒發原因及描述!!章節!!對應模板!!對應分類
]=]
for i, item in pairs(self.data) do
allnames = self.extractAliases(self, item)
wt = wt .. string.format([=[
|-
|class="nowrap"|%s
|class="nowrap"|%s
|class="nowrap"|
* %s
|class="nowrap"|%s
|class="nowrap"|%s
|style="width:200px;"|
* %s
|class="nowrap"|%s
|class="nowrap"|%s
|class="nowrap"|%s
]=],
self.typelist(self, 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 self.frame:preprocess(wt)
end
function parse.check (self, val, i, when_done_do, when_error_do)
local code = parse.getItemVal(self, val, 'code')
if code == nil then
return self.error(self, '沒有參數<code>' .. i .. '</code>')
elseif code == 'error' then
if when_error_do and when_error_do() ~= false then
return when_error_do()
else
return self.error(self, '未知榮譽類型:<code>' .. val .. '</code>')
end
else
return when_done_do()
end
end
return parse