Module:zh-sortkey/data

Avy amin'i Wikibolana — Rakibolana malagasy malalaka

La documentation pour ce module peut être créée à Module:zh-sortkey/data/doc

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 .. "]] block "
			.. "([[:Category:" .. block_name .. " block|category]])"
			.. (subpage ~= "documentation" and "[[Category:" .. block_name .. " block| ]]"
				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, " and ")
	else
		require("Module:debug").track("zh-sortkey/failed")
		return nil
	end
	
	local firstCodePoint, lastCodePoint = getCodePoints(title)
	local codePointText
	if firstCodePoint and lastCodePoint then
		codePointText = ("(code points U+%04X&ndash;U+%04X) "):format(firstCodePoint, lastCodePoint)
	else
		codePointText = ""
	end
	
	return " These are sortkeys for Chinese characters " .. codePointText
		.. "from " .. out .. ", indexed by Unicode code point (in decimal base)."
end

return export