模組:WikipediaHonors

local yesno = require('Module:Yesno')
local parse = require('Module:WikipediaHonors/parse').new()
local p = {}

local getArgs = require('Module:Arguments').getArgs

local function makeInvokeFunc(funcName)
	return function (frame)
		local args = getArgs(frame, {
			parentFirst = true
		})
		return p[funcName](args)
	end
end

p.data = makeInvokeFunc('_data')

function p._data(args) 
	return parse:getDataTable()
end

p.getlevel = makeInvokeFunc('_getlevel')

function p._getlevel(args) 
	return parse:check(args[1], 1, (function()
		if yesno(args['nolink']) then
			return parse:getItemVal(args[1],'name')
		else
			return '[[WP:WPH#' .. (parse:getItemVal(args[1], 'section') or '') .. '|' .. parse:getItemVal(args[1],'name') .. ']]'
		end
	end), (function()
		if string.gsub(mw.text.trim(args[1]:lower()), '((%d{1,2}[級级]|)([维維]基|)[创創]作[奖獎]|produce)', '') == mw.text.trim(args[1]:lower()) then
			return false
		else
			return parse:error('檢測到維基創作獎')
		end
	end))
end

p.getnewrequest = makeInvokeFunc('_getnewrequest')

function p._getnewrequest(args)
	return parse:check(args[1], 1, (function()
		local arglist = {
			['title'] = 'Wikipedia:維基榮譽/申請與變更/申請區',
			['paid'] = 'action=edit&section=new&preload=Template:HonorsApplication/preload2&preloadparams[]=' .. parse:getItemVal(args[1], 'section'),
			['text'] = '點此申請'
		}
		return parse.frame:expandTemplate{ title = "Anchor", args = {[1] = parse:getItemVal(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

p.getleveltemplate = makeInvokeFunc('_getleveltemplate')

function p._getleveltemplate(args)
	return parse:check(args[1], 1, (function()
		local arg = parse:getItemVal(args[1], 'args') or {[1] = args[2]}
		if arg == 'args[2]' then
			arg = {[1] = args[2]}
		end
		if mw.isSubsting() then
			local ret = '{{' .. parse:getItemVal(args[1],'template')
			local i = 1	
			while true do
				if arg[i] then
					ret = ret .. '|' .. arg[i]
					i = i + 1
				else
					break
				end
			end
			ret = ret .. '}}'
			return ret
		else
			return parse.frame:expandTemplate{title = parse:getItemVal(args[1],'template'), args = arg}
		end
	end))
end


p.getcatlink = makeInvokeFunc('_getcatlink')

function p._getcatlink(args)
	return parse:check(args[1], 1, (function()
		return '[[:Category:' .. parse:getItemVal(args[1], 'category') .. '|' .. parse:getItemVal(args[1], 'name') .. ']]'
	end))
end

return p