跳转到内容

模組:WikipediaHonors/parse

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

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

local parse = {}

function parse.new(self)
	local ntable = {}
	setmetatable(ntable, parse)
	self.__index = self
	return ntable
end

parse.data = mw.loadData('Module:WikipediaHonors/data')
parse.frame = mw.getCurrentFrame()
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(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
		]=], 
		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 self.frame:preprocess(wt)
end

function parse.honor_TypeError (self, arg)
	return self.error('參數<code>' .. arg .. '</code>類型為' .. self.typelist(self.getItemVal(arg, 'type')) .. ',不是榮譽')
end

return parse