มอดูล:zh-sortkey/data

จาก วิกิพจนานุกรม พจนานุกรมเสรี
ไปยังการนำทาง ไปยังการค้นหา

--[=[ This module generates descriptions for all the data modules of Module:zh-sortkey that contain sortkeys for characters defined in Unicode. It determines the Unicode block or blocks that the characters belong to and lists them. (See Module:zh-sortkey/data/014 for a data module containing characters from two blocks.)

These data modules are subpages of this page. The only data module that does not handle individual Unicode characters is Module:zh-sortkey/data/unsupported, which contains data for ideographic description sequences.

]=]


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 char >= 13312 and char <= 40938 or char >= 131072 and char <= 191456 then
		return require("Module:Unicode data").lookup_block(char)
	else
		return nil
	end
end

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

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, 13312, 1)
	elseif moduleNumber <= 177 then
		blocks = getBlocks(moduleNumber, 131072, 57)
	else
		require("Module:debug").track("zh-sortkey/failed")
		return nil
	end
	
	local function link(text)
		return "บล็อก [[w:" .. text .. "|" .. text .. "]] ([[:หมวดหมู่:บล็อก " .. text .. "|หมวดหมู่]])[[หมวดหมู่:บล็อก " .. text .. "| ]]"
	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, " and ")
	else
		require("Module:debug").track("zh-sortkey/failed")
		return nil
	end
	
	return " These are sortkeys for Chinese characters from " .. out .. ", indexed by Unicode codepoint (in decimal base)."
end

return export