မာတိကာသို့ ခုန်သွားရန်

မော်ဂျူး:ko-symbol-nav

ဝစ်ရှင်နရီ မှ

Documentation for this module may be created at မော်ဂျူး:ko-symbol-nav/doc

local export = {}
local to_cp = mw.ustring.codepoint
local to_ch = mw.ustring.char
local len = mw.ustring.len

local function link(char, alt)
	return '[[' .. char .. '#Korean|' .. (alt or char) .. ']]'
end

local function mark(char)
	return '<mark><b>' .. char .. '</b></mark>'
end

function export.show(frame)
	local hangul_original = mw.title.getCurrentTitle().text

	if len(hangul_original) ~= 1 then
		return error("Template:ko-symbol-nav is for use on single-Hangul entries.")
	end

	local block_size = 28
	local offset = 44032 -- to_cp('가')
	local hangul_cv_ncr = to_cp(hangul_original) - ((to_cp(hangul_original) - offset) % block_size)
	local hangul_cv = to_ch(hangul_cv_ncr)

	-- Generate the Hangul syllables for the table.
	local i = 0
	local full_hangul_set = { hangul_cv }
	while i < block_size do
		full_hangul_set[i + 1] = to_ch(hangul_cv_ncr + i)
		i = i + 1
	end

	-- Build the table.
	local table_final = {}
	table.insert(table_final, '<table lang="ko" class="wikitable floatright Kore">')

	-- First table row
	table.insert(table_final, '<tr>')
	table.insert(table_final, '<td colspan="2" style="white-space: nowrap;">')
	-- Link to syllables.
	-- Note that ipairs() starts from table item 1, hence full_hangul_set[i + 1] above.
	for i, hangul in ipairs(full_hangul_set) do
		if hangul == hangul_original then
			table.insert(table_final, mark(hangul))
		else
			table.insert(table_final, link(hangul))
		end
		-- For every seventh syllable...
		if (i % 7) == 0 then
			table.insert(table_final, '<br />')
		end
	end
	table.insert(table_final, '</td>')
	table.insert(table_final, '</tr>')

	-- Second table row (footer)
	table.insert(table_final, '<tr>')
	table.insert(table_final, '<th style="width:50%; text-align:left;">')
	if hangul_cv ~= '가' then
		local prev = to_ch(hangul_cv_ncr - block_size)
		table.insert(table_final, link(prev, prev .. ' ←'))
	end
	table.insert(table_final, '</th>')
	table.insert(table_final, '<th style="width:50%; text-align:right;">')
	if hangul_cv ~= '히' then
		local next = to_ch(hangul_cv_ncr + block_size)
		table.insert(table_final, link(next, '→ ' .. next))
	end
	table.insert(table_final, '</th>')
	table.insert(table_final, '</tr>')

	table.insert(table_final, '</table>')

	return table.concat(table_final)
end

return export