本模塊為Module:Zh-sortkey的所有資料模塊生成描述,這些資料模塊包含Unicode中定義的字元的排序鍵,確定字元所屬的Unicode區段並將其列出。Module:Zh-sortkey/data/014包含兩個區段的字元。

這些資料模塊都是本頁面的子頁面。唯一不處理單個Unicode字元的資料模塊是Module:Zh-sortkey/data/unsupported,其中包含表意文字描述序列的資料。


local export = {}

local function getBlock(char)
	if type(char) ~= "number" then
		char = mw.ustring.codepoint(char)
	end
	
	-- Don't return a block not currently serviced by [[Module:zh-sortkey]] (or an unassigned one).
	if 0x3400 <= char and char <= 0x9FEA or 0x20000 <= char and char <= 0x2EBE0 then
		return require("Module:Unicode data").lookup_block(char)
	else
		return nil
	end
end

-- A sortkey module applies to the code points of at most two blocks.
local function getBlocks(moduleNumber, start, moduleStart)
	local firstChar = ( moduleNumber - moduleStart ) * 500 + start
	local lastChar = firstChar + 500 - 1
	
	local blocks = {}
	table.insert( blocks, getBlock(firstChar) )
	table.insert( blocks, getBlock(lastChar) )
	
--	mw.log(firstChar, lastChar, blocks[1], blocks[2])
	
	return blocks
end

local function getCodePoints(title)
	local moduleTitle = title.fullText:match(".+%d+")
	local moduleContent = moduleTitle
		and mw.title.new(moduleTitle):getContent()
	if moduleContent then
		return tonumber(moduleContent:match("%[(%d+)%]")),
			tonumber(moduleContent:match(".+%[(%d+)%]"))
	end
end

function export.getDescription(frame)
	local title = mw.title.getCurrentTitle()
	local namespace = title.nsText
	local pagename = title.text
	local subpage = title.subpageText
	
	local moduleNumber = pagename:match("^Zh%-sortkey/data/(%d+)")
	if moduleNumber then
		moduleNumber = tonumber(moduleNumber)
	else
		require("Module:debug").track("zh-sortkey/failed")
		return nil
	end
	
	local blocks
	
	if moduleNumber <= 56 then
		blocks = getBlocks(moduleNumber, 0x3400, 1)
	elseif moduleNumber <= 177 then
		blocks = getBlocks(moduleNumber, 0x20000, 57)
	else
		require("Module:debug").track("zh-sortkey/failed")
		return nil
	end
	
	local function link(block_name)
		return "[[w:" .. block_name .. "|" .. block_name .. "]]區段"
			.. "([[:Category:" .. block_name .. "區段|分類]])"
			.. (subpage ~= "doc" and "[[Category:" .. block_name .. "區段| ]]"
				or "")
	end
	
	local out = {}
	-- Link any unique blocks that were found.
	for i = 1, 2 do
		if blocks[i] then
			-- Don't insert the same block twice.
			if not blocks[blocks[i]] then
				table.insert( out, link(blocks[i]) )
			end
			blocks[blocks[i]] = true
		end
	end
	if #out > 0 then
		out = table.concat(out, "和")
	else
		require("Module:debug").track("zh-sortkey/failed")
		return nil
	end
	
	local firstCodePoint, lastCodePoint = getCodePoints(title)
	local codePointText
	if firstCodePoint and lastCodePoint then
		codePointText = ("(碼位U+%04X&ndash;U+%04X)"):format(firstCodePoint, lastCodePoint)
	else
		codePointText = ""
	end
	
	return "此爲" .. out .. "中的漢字" .. codePointText .. "的排序鍵,用Unicode碼位(十進制)進行索引。"
end

return export