跳转到内容

模組:ProduceEncouragement

被永久保护的模块
维基百科,自由的百科全书
local p = {}
local getArgs = require('Module:Arguments').getArgs

function p._get_level(level, count)
	return count
		and math.floor(count / 5)
		or level
end

local category_suffix = '级维基创作奖'

function p._get_category_name(level)
	if level < 10 then
		return tostring(level) .. category_suffix
	end
	-- level = f_level * (10 ^ n_level), 1 <= f_level < 10 (未對f_level執行無條件捨去前)
	local n_level = math.floor(math.log(level) / math.log(10))
	local f_level = math.floor(level / math.pow(10, n_level))
	return tostring(f_level) .. ('0'):rep(n_level) .. '—' .. tostring(f_level) .. ('9'):rep(n_level) .. category_suffix
end

function p._get_category_index(level)
	-- 2024 -> 024
	return tostring(level):sub(2)
end

-- //// ---

local function _arg_to_level(args)
	return p._get_level(tonumber(args['1']) or 1, tonumber(args['count']))
end

local function makeInvokeFunc(func)
	return function (frame)
		local args = getArgs(frame, {
			parentFirst = true
		})
		local level = _arg_to_level(args)
		return func(level)
	end
end

p.get_level = makeInvokeFunc(function (level) return tostring(level) end)
p.get_category_name = makeInvokeFunc(p._get_category_name)
p.get_category_index = makeInvokeFunc(p._get_category_index)

return p