Module:Korean/sandbox

require('strict');
local p = {}

-- common label prepping to make loop more readable
local function prep_label(result, label, show_labels, out, i)
	if show_labels ~= 'no' then
		-- labels for every case except for optional first term outside parentheses
		if (out ~= 'yes') or (out == 'yes' and i > 1) then
			table.insert(result, label)
		end
	end
	return result
end

-- function to generate error message
local function format_error(message)
	local result = {
		'<span class="error" style="border:inherit; padding:inherit;">{{[[Template:Korean/auto|Korean/auto]]}} error. ',
		message,
		' ([[Template:Korean/auto|Help]])</span>',
		'[[Category:Korean/auto template errors]]'
	}
	return table.concat(result)
end

function p.ko(frame)
	local getArgs = require('Module:Arguments').getArgs
	return p._ko(getArgs(frame))
end

function p._ko(args)
	local lang_module = require('Module:Lang')      -- for wrapping non-English text
	local utils_module = require('Module:Ko-utils/sandbox') -- for a function to check hangul
	local translit = require('Module:Ko-translit/sandbox')  -- for automatic romanization
	local ipa_module = require('Module:IPA')        -- for IPA (duh)
	local template = 'Korean'
	
	-- assign positional params
	local hangul, hanja
	if args['hangul'] then hangul = args['hangul'] else hangul = args[1] end
	if args['hanja'] then hanja = args['hanja'] else hanja = args[2] end
	
	-- assign named params
	local lit, rr, mr = args['lit'], args['rr'], args['mr']
	local hangulref, hanjaref, en_iparef, ko_iparef, litref = args['hangulref'], args['hanjaref'], args['en_iparef'], args['ko_iparef'], args['litref']
	local en_ipa, ko_ipa = args['en_ipa'], args['ko_ipa']
	local out, en_out, order = args['out'], args['en_out'], args['order']
	local show_labels, show_links = args['labels'], args['links']
	local capitalize, namemode = args['capitalize'], args['namemode']
	
	-- validate input
	if not hangul then return format_error('<code>hangul</code> must not be empty.') end
	if not utils_module.contains_hangul(hangul) then
		return format_error('<code>hangul</code> must contain at least one Hangul character.')
	end
	if utils_module.contains_reference(hangul) then
		return format_error('<code>hangul</code> should not contain a reference; use <code>hangulref</code> for that.')
	end
	if utils_module.hangul_contains_syntax(hangul) and not (rr or mr) then
		return format_error('<code>hangul</code> contains special syntax but romanization not enabled; remove or escape (<code>\\</code>) special syntax.')
	end
	if hanjaref and not hanja then
		return format_error('<code>hanja</code> cannot be empty when <code>hanjaref</code> is given.')
	end
	if en_iparef and not en_ipa then
		return format_error('<code>en_ipa</code> cannot be empty when <code>en_iparef</code> is given.')
	end
	if ko_iparef and not ko_ipa then
		return format_error('<code>ko_ipa</code> cannot be empty when <code>ko_iparef</code> is given.')
	end
	if litref and not lit then
		return format_error('<code>lit</code> cannot be empty when <code>litref</code> is given.')
	end
	if ((out == "yes" or en_out == "yes") and not (lit or rr or mr or en_ipa or ko_ipa)) then
		return format_error('If <code>out=yes</code>, a parameter other than <code>hangul</code> or <code>hangulref</code> must be provided.')
	end
	if en_out == 'yes' then
		if out == 'no' then return format_error('If <code>en_out=yes</code>, <code>out</code> cannot be <code>no</code>') end
		if en_out == 'yes' then out = 'yes' end -- set out if it wasn't already specified 
		if not lit then return format_error('<code>en_out</code> specified but no <code>lit</code> was provided.') end
		if (order and string.sub(order, 1, 1) ~= 'l') then
			return format_error('If giving both <code>en_out</code> and <code>order</code>, <code>l</code> should come first in <code>order</code>.')
		end
	end
	if (namemode == 'yes' and not (rr or mr)) then
		return format_error('If <code>namemode=yes</code>, <code>rr</code> or <code>mr</code> should be <code>yes</code>.')
	end
	if (namemode == 'yes' and string.sub(hangul, 1, 1) == "%") then
		return format_error('If <code>namemode=yes</code>, <code>%</code> is not needed at beginning of <code>hangul</code> input.')
	end
	if (capitalize == 'yes' and not (rr or mr)) then
		return format_error('If <code>capitalize=yes</code>, <code>rr</code> or <code>mr</code> should be <code>yes</code>.')
	end
	if (capitalize == 'yes' and string.sub(hangul, 1, 1) == "^") then
		return format_error('If <code>capitalize=yes</code>, <code>^</code> is not needed at beginning of <code>hangul</code> input.')
	end
	
	-- apply name mode
	if namemode == 'yes' then
		hangul = '%' .. hangul
	end
	
	-- apply capitalization
	if capitalize == 'yes' then
		hangul = '^' .. hangul
	end
	
	-- romanize
	local rr_out
	if rr == 'yes' then
		rr_out = translit.rr({hangul})                                          -- generate automatic RR
		rr_out = lang_module._xlit({'ko', 'rr', rr_out, ['template']=template}) -- wrap in {{translit|ko|rr}}
	end
	local mr_out
	if mr == 'yes' then
		mr_out = translit.mr({hangul})
		mr_out = lang_module._xlit({'ko', 'mr', mr_out, ['template']=template})
	end
	
	-- prep hangul for display
	hangul = translit.clean_hangul({hangul})                                    -- remove syntax characters
	hangul = lang_module._lang({'ko', hangul, ['template']=template})           -- wrap in lang template
	
	-- prep hanja for display
	if hanja then
		hanja = lang_module._lang({'ko', hanja, ['template']=template})
	end
	
	-- prep ipas
	local ipa_label
	if ko_ipa or en_ipa then
		if show_labels ~= 'no' then
			ipa_label = 'pronounced'
		else
			ipa_label = ''
		end
	end
	if ko_ipa then
		ko_ipa = ipa_module._main({'ko', ko_ipa, ['label'] = ipa_label})
	end
	if en_ipa then
		en_ipa = ipa_module._main({'en', en_ipa, ['label'] = ipa_label})
	end
	
	-- prep display order string
	if order then
		-- for en_out mode, add english ipa after literal if wasn't specified
		if (en_out == 'yes' and en_ipa and not string.find(order, 'e')) then
			order = string.sub(order, 1, 1) .. 'e' .. string.sub(order, 2, #order)
		end
		
		-- check if order is malformed
		if not string.match(order, '^[hzrmkle]+$') then return format_error('<code>order</code> contains invalid characters.') end
		-- check if any characters duplicated
		for _, c in ipairs({'h', 'z', 'r', 'm', 'e', 'k', 'l'}) do
			local _, count = string.gsub(order, c, '')
			if count > 1 then return format_error('<code>order</code> contains more than one <code>"' .. c .. '"</code>.') end
		end
		if not string.find(order, 'h') then return format_error('<code>order</code> is missing "<code>h</code>".') end
		if (hanja and not string.find(order, 'z')) or (not hanja and string.find(order, 'z')) then return format_error('<code>order</code> is missing "<code>z</code>" or <code>hanja</code> is empty.') end
		if (rr == 'yes' and not string.find(order, 'r')) then
			return format_error('<code>order</code> is missing "<code>r</code>".')
		end
		if (rr ~= 'yes' and string.find(order, 'r')) then
			return format_error('<code>order</code> contains "<code>r</code>" but <code>rr</code> is not <code>yes</code>.')
		end
		if (mr == 'yes' and not string.find(order, 'm')) then
			return format_error('<code>order</code> is missing "<code>m</code>".')
		end
		if (mr ~= 'yes' and string.find(order, 'm')) then
			return format_error('<code>order</code> contains "<code>m</code>" but <code>mr</code> is not <code>yes</code>.')
		end
		-- "i" and "l" are optional to specify, so only check if order is given but not value
		if (string.find(order, 'e') and not en_ipa) then return format_error('<code>order</code> has "<code>e</code>" but <code>en_ipa</code> is empty.') end
		if (string.find(order, 'k') and not ko_ipa) then return format_error('<code>order</code> has "<code>k</code>" but <code>ko_ipa</code> is empty.') end
		if (string.find(order, 'l') and not lit) then return format_error('<code>order</code> has "<code>l</code>" but <code>lit</code> is empty.') end
	elseif en_out == 'yes' then
		-- if no order was provided but en_out was given, build default order
		-- lit[litref] (en_ipa[en_iparef]; hangul[hangulref]; hanja[hanjaref]; rr; mr; ko_ipa[ko_iparef])
		order = 'l'
		if en_ipa then order = order .. 'eh' else order = order .. 'h' end -- hangul also added here
		if hanja then order = order .. 'z' end
		if rr_out then order = order .. 'r' end
		if mr_out then order = order .. 'm' end
		if ko_ipa then order = order .. 'k' end
	else
		-- default order
		-- en_ipa[en_ipa]; hangul[hangulref]; hanja[hanjaref]; rr; mr; ko_ipa[ko_iparef]; lit[litref]
		if en_ipa then order = 'eh' else order = 'h' end -- hangul also added here
		if hanja then order = order .. 'z' end
		if rr_out then order = order .. 'r' end
		if mr_out then order = order .. 'm' end
		if ko_ipa then order = order .. 'k' end
		if lit then order = order .. 'l' end
	end
	-- add optional order parameters (k, l) to end if not already there
	if (ko_ipa and not string.find(order, 'k')) then order = order .. 'k' end
	if (lit and not string.find(order, 'l')) then order = order .. 'l' end
	
	-- iterate through the order and add elements to output
	local result = {}
	local c, label
	for i = 1, #order do
	    c = string.sub(order, i, i)
	    
	    -- hangul processing
	    if c == 'h' then
	    	-- language label insertion (more complicated conditionals in function)
	    	if show_links == 'no' then
	    		label = 'Korean: '
    		else
    			label = '[[Korean language|Korean]]: '
			end
	    	result = prep_label(result, label, show_labels, out, i)
	    	
    		-- hangul insertion
	    	table.insert(result, hangul)
	    	
	    	-- hangul ref insertion
	    	if hangulref then table.insert(result, hangulref) end
    	end
    	
	    -- hanja processing
	    if c == 'z' then
	    	if show_links == 'no' then
	    		label = 'Hanja: '
    		else
    			label = '[[Hanja]]: '
			end
	    	result = prep_label(result, label, show_labels, out, i)
	    	table.insert(result, hanja)
	    	if hanjaref then table.insert(result, hanjaref) end
    	end
    	
    	-- rr processing
	    if c == 'r' then
	    	if show_links == 'no' then
	    		label = '<abbr title="Revised Romanization of Korean">RR</abbr>: '
    		else
    			label = '[[Revised Romanization of Korean|RR]]: '
			end
	    	result = prep_label(result, label, show_labels, out, i)
	    	table.insert(result, rr_out)
    	end

    	-- mr processing
	    if c == 'm' then
	    	if show_links == 'no' then
	    		label = '<abbr title="McCune–Reischauer">MR</abbr>: '
    		else
    			label = '[[McCune–Reischauer|MR]]: '
			end
	    	result = prep_label(result, label, show_labels, out, i)
	    	table.insert(result, mr_out)
    	end
    	
		-- en_ipa processing
    	if c == 'e' then
			table.insert(result, en_ipa)
			table.insert(result, en_iparef)
    	end
    	
		-- ko_ipa processing
    	if c == 'k' then
			table.insert(result, ko_ipa)
			table.insert(result, ko_iparef)
    	end
    	
    	-- literal translation processing
    	if c == 'l' then
			label = '<abbr style="font-size:85%;" title="literal translation">lit.</abbr> '
			lit = "'" .. lit .. "'" -- single quotes around gloss
			result = prep_label(result, label, show_labels, out, i)
			table.insert(result, lit)
			table.insert(result, litref)
    	end
	
	    -- add semicolon
	    if i < #order then
	    	if not (out == 'yes' and i == 1) then
	    		table.insert(result, '; ')
    		end
    	end
	    -- add parentheses if needed 
		if (i == 1 and out == 'yes') then table.insert(result, ' (') end
		if (i == #order and out == 'yes') then table.insert(result, ')') end
	end
	return unpack(result)
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.