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 21:58, 27 April 2018 (fix joinability (or whatever the term for this is)). 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

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"\n! Final→<br>Medial↓')
	for _, final in ipairs(finals) do
		output.add(('! %s'):format(final))
	end
	
	for _, medial in ipairs(medials) do
		output.row()
		output.add(('! %s'):format(medial))
		for _, final in ipairs(finals) do
			output.add(('| %s%s%s'):format(initial, medial, make_joinable(final)))
		end
	end
	
	output.add('|}')
	
	return table.concat(output, '\n')
end

return p