模組:沙盒/Ythlev

维基百科,自由的百科全书
文档图示 模块文档[创建]
local p = {}
function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	for _, v in ipairs({'electorate', 'threshold', 'agree', 'disagree', 'invalid'}) do
		p[v] = tonumber(args[v]) or 0
	end
	p.valid = p.agree + p.disagree
	local lang = mw.getContentLanguage()
	local function fmt(n)
		return lang:formatNum(n)
	end
	local root = mw.html.create('table')
	root
		:addClass('wikitable')
		:css('text-align', 'right')
		:tag('caption')
			:wikitext(args.caption)
		:tag('tr')
			:tag('th')
			:tag('th')
				:wikitext('票數')
			:tag('th')
				:wikitext('%')
			:tag('th')
				:wikitext('佔選舉人數%')
		:tag('tr')
			:tag('td')
				:wikitext('選舉人數')
			:tag('td')
				:wikitext(fmt(p.electorate))
			:tag('td')
				:attr('colspan', 2)
				:wikitext('100.00%')
		:tag('tr')
			:tag('td')
				:wikitext('門檻票數')
			:tag('td')
				:wikitext(fmt(math.ceil(p.electorate * p.threshold)))
			:tag('td')
				:attr('colspan', 2)
				:wikitext(string.format('%.2f%%', p.threshold * 100))
		:tag('tr')
			:tag('td')
				:wikitext('同意票數')
			:tag('td')
				:wikitext(fmt(p.agree))
			:tag('td')
				:wikitext(string.format('%.2f%%', p.agree / p.valid * 100))
			:tag('td')
				:wikitext(string.format('%.2f%%', p.agree / p.electorate * 100))
		:tag('tr')
			:tag('td')
				:wikitext('不同意票數')
			:tag('td')
				:wikitext(fmt(p.disagree))
			:tag('td')
				:wikitext(string.format('%.2f%%', p.disagree / p.valid * 100))
			:tag('td')
				:wikitext(string.format('%.2f%%', p.disagree / p.electorate * 100))
		:tag('tr')
			:tag('td')
				:wikitext('有效票數')
			:tag('td')
				:wikitext(fmt(p.valid))
			:tag('td')
				:wikitext(string.format('%.2f%%', p.valid / (p.valid + p.invalid) * 100))
			:tag('td')
				:wikitext(string.format('%.2f%%', p.valid / p.electorate * 100))
		:tag('tr')
			:tag('td')
				:wikitext('無效票數')
			:tag('td')
				:wikitext(fmt(p.invalid))
			:tag('td')
				:wikitext(string.format('%.2f%%', p.invalid / (p.valid + p.invalid) * 100))
			:tag('td')
				:wikitext(string.format('%.2f%%', p.invalid / p.electorate * 100))
		:tag('tr')
			:css('font-weight', 'bold')
			:tag('td')
				:wikitext('總投票數')
			:tag('td')
				:wikitext(fmt(p.valid + p.invalid))
			:tag('td')
				:wikitext('100.00%')
			:tag('td')
				:wikitext(string.format('%.2f%%', (p.valid + p.invalid) / p.electorate * 100))
		:tag('tr')
			:tag('td')
				:wikitext('結果')
			:tag('td')
				:attr('colspan', 3)
				:wikitext()
	return tostring(root)
end
return p