Jump to content

Module:Hangul

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Erutuon (talk | contribs) at 22:38, 27 April 2018 (make collapsed, add initial in header). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

-- Put these two in data module?
local medials = {
	'ᅡ',
	'ᅢ',
	'ᅣ',
	'ᅤ',
	'ᅥ',
	'ᅦ',
	'ᅧ',
	'ᅨ',
	'ᅩ',
	'ᅪ',
	'ᅫ',
	'ᅬ',
	'ᅭ',
	'ᅮ',
	'ᅯ',
	'ᅰ',
	'ᅱ',
	'ᅲ',
	'ᅳ',
	'ᅴ',
	'ᅵ',
}

local finals = {
	'',
	'ᆨ',
	'ᆩ',
	'ᆪ',
	'ᆫ',
	'ᆬ',
	'ᆭ',
	'ᆮ',
	'ᆯ',
	'ᆰ',
	'ᆱ',
	'ᆲ',
	'ᆳ',
	'ᆴ',
	'ᆵ',
	'ᆶ',
	'ᆷ',
	'ᆸ',
	'ㅄ',
	'ㅅ',
	'ㅆ',
	'ㅇ',
	'ㅈ',
	'ᆾ',
	'ᆿ',
	'ᇀ',
	'ᇁ',
	'ᇂ',
}

local tocodepoint, tochar = mw.ustring.codepoint, mw.ustring.char
local function make_joinable(letter)
	local codepoint = tocodepoint(letter)
	
	if not codepoint then
		return ''
	
	-- Hangul Compatibility Jamo (U+3130-318F) rather than Hangul Jamo (U+1100-11FF)
	elseif 0x3130 <= codepoint and codepoint <= 0x318F then
		return tochar(codepoint - 0x1F8B)
	else
		return letter
	end
end


---- From [[wikt:Module:ko-hangul]
 
-- Given the "syllable index" of a precomposed Hangul syllable (see
-- above), returns "indices" representing the three constituent jamo
-- ("lead", i.e. initial consonant; "vowel"; and "tail", i.e. final
-- consonant, except that zero denotes the absence of a final consonant).
local function syllableIndex2JamoIndices(syllableIndex)
    local lIndex = math.floor(syllableIndex / 588)
    local vIndex = math.floor((syllableIndex % 588) / 28)
    local tIndex = syllableIndex % 28
 
    return lIndex, vIndex, tIndex
end

---- From [[wikt:Module:Unicode data]].

-- http://www.unicode.org/Public/UNIDATA/Jamo.txt
local hangul_leads = {
	[0] = "G", "GG", "N", "D", "DD", "R", "M", "B", "BB", "S", "SS",
	"", "J", "JJ", "C", "K", "T", "P", "H"
}

local hangul_vowels = {
	[0] = "A", "AE", "YA", "YAE", "EO", "E", "YEO", "YE", "O", "WA",
	"WAE", "OE", "YO", "U", "WEO", "WE", "WI", "YU", "EU", "YI",
	"I"
}

local hangul_trails = {
	[0] = "", "G", "GG", "GS", "N", "NJ", "NH", "D", "L", "LG", "LM", "LB",
	"LS", "LT", "LP", "LH", "M", "B", "BS", "S", "SS", "NG", "J", "C", "K",
	"T", "P", "H"
}

-- Cheaper to derive names from decomposed form of syllable?
local function get_name(syllable)
	local li, vi, ti = syllableIndex2JamoIndices(tocodepoint(syllable) - 0xAC00)

	return ("HANGUL SYLLABLE %s%s%s"):format(
		hangul_leads[li], hangul_vowels[vi], hangul_trails[ti])
end

----

function p.show(frame)
	local initial = frame.args[1] or 'ᄀ'
	local codepoint = mw.ustring.codepoint(initial)
	if not (0x1100 <= codepoint and codepoint <= 0x1112) then
		error('Incorrect initial ' .. initial .. '. Should be between U+1100 and U+1112.')
	end
	
	local output = {}
	local i = 0
	function output.add(text)
		i = i + 1
		output[i] = text
	end
	function output.row()
		output.add('|-\n')
	end
	
	output.add(
([[
{| class="wikitable collapsible collapsed" style="width: 96px; height: 96px;"
! colspan="29" | Initial&nbsp;%s
|-
! Final→<br>Medial↓]]):format(initial))

	for _, final in ipairs(finals) do
		output.add(('! %s'):format(final))
	end
	
	for _, medial in ipairs(medials) do
		output.row()
		output.add(('! scope="row" | %s'):format(medial))
		for _, final in ipairs(finals) do
			output.add(('| %s%s%s'):format(initial, medial, make_joinable(final)))
		end
	end
	
	output.add('|}')
	
	output = table.concat(output, '\n')
	output = mw.ustring.toNFC(output)
	output = mw.ustring.gsub( -- Add names of syllable codepoints.
		output,
		'[가-힣]',
		function (syllable)
			return ('title="%s" | %s'):format(get_name(syllable), syllable)
		end)
	
	return output
end

return p