跳转到内容

模組:PRC admin/utils

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

这是本页的一个历史版本,由SunAfterRain留言 | 贡献2025年1月24日 (五) 05:46 建立内容为“local p = {} local partLengths = { 2, 2, 2, 3, 3 } local separator = '/' function clone(t) local t2 = {} for k, v in pairs(t) do if type(v) == 'table' then t2[k] = clone(v) else t2[k] = v end end return t2 end local function splitCode(code) if type(code) ~= type('') then error('invalid code') end code = mw.text.trim(code) if code == '' then return {} end local parts = mw.text.split(code, separator, true) if #parts…”的新页面)编辑。这可能和当前版本存在着巨大的差异。

(差异) ←上一修订 | 最后版本 (差异) | 下一修订→ (差异)
local p = {}

local partLengths = { 2, 2, 2, 3, 3 }
local separator = '/'

function clone(t)
	local t2 = {}
	for k, v in pairs(t) do
		if type(v) == 'table' then
			t2[k] = clone(v)
		else
			t2[k] = v
		end
	end
	return t2
end

local function splitCode(code)
	if type(code) ~= type('') then
		error('invalid code')
	end
	code = mw.text.trim(code)
	if code == '' then
		return {}
	end
	local parts = mw.text.split(code, separator, true)
	if #parts > 5 then
		error('invalid code')
	end
	for i, part in ipairs(parts) do
		local partLength = partLengths[i]
		if not part:match('^%d+$') or #part ~= partLength then
			error('invalid code')
		end
	end
	return parts
end

local function stripCode(parts)
	for i = #parts, 1, -1 do
		if parts[i] == '00' or parts[i] == '000' then
			table.remove(parts)
		end
	end
	return parts
end

local function fillCode(parts)
	local result = {}
	for i, partLength in ipairs(partLengths) do
		if parts[i] then
			result[i] = parts[i]
		else
			result[i] = string.rep('0', partLength)
		end
	end
	return result
end

local PRCAdminCode = {}
PRCAdminCode.__index = PRCAdminCode

function PRCAdminCode.new(code)
	local parts = stripCode(splitCode(code))
	return PRCAdminCode.newFromParts(parts)
end

function PRCAdminCode.newFromParts(parts)
	local self = setmetatable({}, PRCAdminCode)
	self.parts = parts
	return self
end

-- 轉換成已填充代碼結尾的00或000項的字串
function PRCAdminCode:tostring()
	return table.concat(fillCode(self.parts), separator)
end

PRCAdminCode.__tostring = PRCAdminCode.tostring

-- 轉換成沒有代碼結尾的00或000項的字串
function PRCAdminCode:tostripstring()
	return table.concat(self.parts, separator)
end

-- 取得父代碼
-- @param {number} [level] 向上層級,預設為1
function PRCAdminCode:parent(code, level)
	level = level or 1
	local parts = clone(self.parts)
	if #parts > 0 then
		for i = 1, level, 1 do
			table.remove(parts)
		end
	end
	return PRCAdminCode.newFromParts(parts)
end

-- {{PRC_admin/code2page}}
function PRCAdminCode:toPage(type)
	type = type or 'data'
	return string.format('Template:PRC admin/%s/%s', type, self.tostring())
end

-- {{PRC_admin/stripslashes}}
-- 取得沒有分隔符的代碼
-- @return {string}
function PRCAdminCode:tostripslashes()
	return table.concat(self.parts, '')
end

p.PRCAdminCode = PRCAdminCode

-- {{PRC admin/name}}
function p._name(args)
	if args.wikidata then
		return mw.wikibase.label(mw.text.trim(args.wikidata))
	else
		return args.name
	end
end

-- {{PRC_admin/plainname}}
function p._plainname(code)
	return mw.getCurrentFrame():expandTemplate {
		title = PRCAdminCode.new(code):toPage(),
		args = { 'PRC admin/name' }
	}
end

return p