跳转到内容

模組:Ko-symbol-nav

維基詞典,自由的多語言詞典


local export = {}
local to_cp = mw.ustring.codepoint
local to_ch = mw.ustring.char
local compose = mw.ustring.toNFC
local decompose = mw.ustring.toNFD
local split = mw.text.split
local replace = mw.ustring.gsub
local len = mw.ustring.len

local function link(char,alt)
	return '[[' .. char .. '#朝鮮語|' .. (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 hangul_letters = decompose(hangul_original)       -- Unicode decomposition
	hangul_letters = replace(hangul_letters, '[ᆨ-ᇿ]', '') -- Remove final consonants.
	local hangul_cv = compose(hangul_letters)               -- Unicode (re)composition
	local ncr = to_cp(hangul_cv)                            -- Convert to a [[w:numeric character reference]].

	-- Generate the other Hangul syllables that will populate the table.
	local i = 0
	local full_hangul_set = {}
	while i < 28 do
		full_hangul_set[i] = to_ch(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 the first Hangul syllable.
	if hangul_cv == hangul_original then
		table.insert(table_final, mark(hangul_cv))
	else
		table.insert(table_final, link(hangul_cv))
	end
	-- Link to the rest of the Hangul syllables.
	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 + 1) % 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(ncr - 28)
		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(ncr + 28)
		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