localexport={}localnamespace=mw.title.getCurrentTitle().nsTextlocalsubstring=mw.ustring.sublocalfunctionlog(...)ifnamespace=="Module"thenmw.log(...)endend--[[The number of characters or ideographic sequences that must follow eachideographic description character.]]localIDchars={["⿰"]=2,["⿱"]=2,["⿲"]=3,["⿳"]=3,["⿴"]=2,["⿵"]=2,["⿶"]=2,["⿷"]=2,["⿸"]=2,["⿹"]=2,["⿺"]=2,["⿻"]=2,--[[-- in future perhaps: https://www.unicode.org/L2/L2018/18012-irgn2273-four-new-idcs.pdf[mw.ustring.char(0x2FFC)] = 2,[mw.ustring.char(0x2FFD)] = 2,[mw.ustring.char(0x2FFE)] = 1,[mw.ustring.char(0x2FFF)] = 1,--]]}--[[Returns the index in the string where the ideographic description sequence(IDS) ends, or the index of the end of the string. Iterates wheneveranother ideographic description character (IDC) is found.]]localfunctionfindEndOfIDS(text,IDchar,i)ifnot(textandIDcharandi)thenreturnnilendlocalj=ilocalcomponent=1-- Number of components expected after current IDC.localcomponents=IDchars[IDchar]whilecomponent<=componentsdoj=j+1localchar=substring(text,j,j)ifchar==""thenbreakelseifIDchars[char]thenj=findEndOfIDS(text,char,j)endcomponent=component+1end--[[If the expected number of components has been found,return the current index in the text.]]ifcomponent-components==1thenreturnjelsereturnnilendendlocalmodule_cache={}functionexport.getData(char,returnModule)iftype(char)=="string"thenchar=mw.ustring.codepoint(char)elseiftype(char)~=numberthenerror("getData must operate on a single character or codepoint.")endlocaldata=mw.loadData("Module:Hani-sortkey/data/sandbox")localsectionStart={0x3400,0xFA0E,0x20000,0x30000,math.huge}fork,vinipairs(sectionStart)doifchar<vthenlocalstart=5*(char-sectionStart[k-1])+1returndata[k-1]:sub(start,start+4)endendendlocalunsupported_datafunctionexport.makeSortKey(text,lang,sc)localscripts={Hani=true,Hans=true,Hant=true,Jpan=true,Kore=true}ifscandnotscripts[sc]thenreturnmw.ustring.upper(text)endlocalsort={}locali=1whilei<=mw.ustring.len(text)dolocalcharacter=substring(text,i,i)--[=[If we encounter an ideographic description character (IDC),find out if it begins a valid ideographic description sequence (IDS).If the IDS is valid and a sortkey for it is listed in[[Module:Hani-sortkey/data/unsupported]], then returnthe sortkey, and move to the next character after theIDS.Otherwise, insert the IDC into the sortkey and move to the nextcharacter after the IDC.If the IDS is valid and no sortkey for it is found, track it.]=]ifIDchars[character]thenlocalj=findEndOfIDS(text,character,i)localIDS,dataifjthenIDS=substring(text,i,j)unsupported_data=unsupported_dataormw.loadData("Module:Hani-sortkey/data/unsupported")data=unsupported_data[IDS]endifnotdatathenifIDSthenrequire("Module:debug").track("Hani-sortkey/IDS-without-sortkey")mw.log("ideographic description sequence without sortkey: '"..IDS.."'")elserequire("Module:debug").track("Hani-sortkey/invalid-IDS")mw.log("invalid ideographic description sequence at the beginning of '"..substring(text,i).."'")endendifIDSanddatathentable.insert(sort,data)i=jelsetable.insert(sort,character)endelsetable.insert(sort,export.getData(character)orcharacter)endi=i+1endsort=table.concat(sort)returnsortendreturnexport