Jump to content

Module:Kangxi radical

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Remsense (talk | contribs) at 04:57, 12 October 2023. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('strict')

local p = {}

function p.find(t, srch) -- search by value
	for k, _ in ipairs(t) do
  		if t[k] == srch then return k
  		end
	end
end

local labels = { -- ten per line
	"one", "line", "dot", "slash", "second", "hook", "two", "lid", "man", "legs",
	"enter", "eight", "down box", "cover", "ice", "table", "open box", "knife", "power", "wrap",
	"spoon", "right open box", "hiding enclosure", "ten", "divination", "seal", "cliff", "private", "again", "mouth",
	"enclosure", "earth", "scholar", "go", "go slowly", "evening", "big", "woman", "child", "roof",
	"inch", "small", "lame", "corpse", "sprout", "mountain", "river", "work", "oneself", "turban",
	"dry", "short thread", "dotted cliff", "long stride", "two hands", "shoot", "bow", "snout", "bristle", "step",
	"heart", "halberd", "door", "hand", "branch", "rap", "script", "dipper", "axe", "square",
	"not", "sun", "say", "moon", "tree", "lack", "stop", "death", "weapon", "do not",
	"compare", "fur", "clan", "steam", "water", "fire", "claw", "father", "double x", "half tree trunk",
	"slice", "fang", "cow", "dog", "profound", "jade", "melon", "tile", "sweet", "life",
	"use", "field", "bolt of cloth", "sickness", "dotted tent", "white", "skin", "dish", "eye", "spear",
	"arrow", "stone", "spirit", "track", "grain", "cave", "stand", "bamboo", "rice", "silk",
	"jar", "net", "sheep", "feather", "old", "and", "plow", "ear", "brush", "meat",
	"minister", "self", "arrive", "mortar", "tongue", "oppose", "boat", "stopping", "color", "grass",
	"tiger", "insect", "blood", "walk enclosure", "clothes", "west", "see", "horn", "speech", "valley",
	"bean", "pig", "badger", "shell", "red", "run", "foot", "body", "cart", "bitter",
	"morning", "walk", "city", "wine", "distinguish", "village", "gold", "long", "gate", "mound",
	"slave", "short tailed bird", "rain", "blue", "wrong", "face", "leather", "tanned leather", "leek", "sound",
	"leaf", "wind", "fly", "eat", "head", "fragrant", "horse", "bone", "tall", "hair", 
	"fight", "sacrificial wine", "cauldron", "ghost", "fish", "bird", "salt", "deer", "wheat", "hemp",
	"yellow", "millet", "black", "embroidery", "frog", "tripod", "drum", "rat", "nose", "even",
	"tooth", "dragon", "turtle", "flute",
}

local variants = {
	["一"] = 1,
	["乀"] = 2,
	["乁"] = 2,
	["人"] = 9,
	["亻"] = 9,
	["𠂉"] = 9,
	["馬"] = 187,
	["马"] = 187,
	["🐎"] = 187,
	["⻢"] = 187,
}

function p.Kxr(frame)
	local getArgs = require('Module:Arguments').getArgs
	return p._Kxr(getArgs(frame))
end

function p.utfEscape(hex)
	return	"&#" .. hex .. ";"
end

function p.toRadicalNumber(ch)
	local codepoint = mw.ustring.codepoint(ch)
	local result = nil
	
	if codepoint >= 0x2F00 and codepoint <= 0x2FD5 then
		result = codepoint - 0x2F00 + 1
	else
		result = variants[ch]
	end
	
	if not result then
		result = p.find(labels, ch)
	end
	return result
end

function p._Kxr(args)
	local num = nil
	if tonumber(args[1]) then
		num = math.floor(tonumber(args[1]))
	else
		num = p.toRadicalNumber(args[1])
	end
	local link = false
	
	if args["l"] or args["link"] or args["links"] then
		link = true
	end
	
	if num < 1 or num > 214 then
		error("Argument must be between 1 and 214")
	end
	
	local unicode = 0x2F00 + num - 1
	local label_style = "\"font-variant:small-caps;font-size:smaller\""
	
	local label = string.upper(labels[num])
	if link then
		label = "[[Radical " .. num .. "|".. label .. "]]"
	end
	label = "<span style=" .. label_style ..  ">'" ..  label .. "'</span>"
	local result  = "<span lang=\"und-Hani\">" .. p.utfEscape(unicode) .. "</span>&emsp13;" .. label
	return result
end

return p