require('strict');
local p = {}
-- common label prepping to make loop more readable
local function prep_label(result, label, args, i)
if args['labels'] ~= 'no' then
-- labels for every case except for optional first term outside parentheses
if (args['out'] ~= 'yes') or (args['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="color:inherit; border:inherit; padding:inherit;">{{Korean}} error. ',
message,
' ([[Template:Korean|Help]])</span>',
'[[Category:Korean template errors]]'
}
return table.concat(result)
end
function p.ko(frame)
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame)
return p._ko(args)
end
function p._ko(args)
local lang_module = require('Module:Lang') -- used for wrapping non-English text
local translit = require('Module:Ko-translit') -- used for automatic romanization
local ipa_module = require('Module:IPA') -- used 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
-- validate input
if not hangul then return format_error('Must provide Hangul.') end
if args['hanjaref'] and not hanja then
return format_error('No Hanja provided for given Hanja ref.')
end
if args['en_iparef'] and not args['en_ipa'] then
return format_error('No English IPA provided for given English IPA ref.')
end
if args['ko_iparef'] and not args['ko_ipa'] then
return format_error('No Korean IPA provided for given Korean IPA ref.')
end
if args['litref'] and not args['lit'] then
return format_error('No literal translation provided for given literal translation ref.')
end
-- validate en_out
if args['en_out'] == 'yes' then
if args['out'] == 'no' then return format_error('if en_out is "yes", out cannot be "no"') end
if args['en_out'] == 'yes' then args['out'] = 'yes' end -- set out if it wasn't already specified
if not args['lit'] then return format_error('en_out specified but no literal translation was provided.') end
if (args['order'] and string.sub(args['order'], 1, 1) ~= 'l') then
return format_error('If giving both en_out and order, "l" should come first in order.')
end
end
-- romanize
local rr_out
if args['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 args['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.cleanHangul(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 (label for ipa disabled by default; assumed obvious)
local ko_ipa
if args['ko_ipa'] then
ko_ipa = ipa_module._main({'ko', args['ko_ipa'], ['label'] = ''})
end
local en_ipa
if args['en_ipa'] then
en_ipa = ipa_module._main({'en', args['en_ipa'], ['label'] = ''})
end
-- prep display order string
local order
-- if order was provided
if args['order'] then
order = args['order']
-- for en_out mode, add english ipa after literal if wasn't specified
if (args['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('Order 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('Order contains more than one "' .. c .. '".') end
end
if not string.find(order, 'h') then return format_error('Order is missing "h".') end
if not (hanja and string.find(order, 'z')) then return format_error('Order is missing "z" or Hanja not provided.') end
if not (rr_out and string.find(order, 'r')) then return format_error('Order is missing "r" or RR not provided.') end
if not (mr_out and string.find(order, 'm')) then return format_error('Order is missing "m" or RR not provided.') 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('Order has "e" but English IPA not provided.') end
if (string.find(order, 'k') and not ko_ipa) then return format_error('Order has "k" but Korean IPA not provided.') end
if (string.find(order, 'l') and not args['lit']) then return format_error('Order has "l" but literal translation not provided.') end
elseif args['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 args['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 (args['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 args['links'] == 'no' then
label = 'Hangul: '
else
label = '[[Hangul]]: '
end
result = prep_label(result, label, args, i)
-- hangul insertion
table.insert(result, hangul)
-- hangul ref insertion
if args['hangulref'] then table.insert(result, args['hangulref']) end
end
-- hanja processing
if c == 'z' then
if args['links'] == 'no' then
label = 'Hanja: '
else
label = '[[Hanja]]: '
end
result = prep_label(result, label, args, i)
table.insert(result, hanja)
if args['hanjaref'] then table.insert(result, args['hanjaref']) end
end
-- rr processing
if c == 'r' then
if args['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, args, i)
table.insert(result, rr_out)
end
-- mr processing
if c == 'm' then
if args['links'] == 'no' then
label = '<abbr title="McCune–Reischauer">MR</abbr>: '
else
label = '[[McCune–Reischauer|MR]]: '
end
result = prep_label(result, label, args, i)
table.insert(result, mr_out)
end
-- en_ipa processing
if c == 'e' then
table.insert(result, en_ipa)
table.insert(result, args['en_iparef'])
end
-- ko_ipa processing
if c == 'k' then
table.insert(result, ko_ipa)
table.insert(result, args['ko_iparef'])
end
-- literal translation processing
if c == 'l' then
label = '<abbr style="font-size:85%;" title="literal translation">lit.</abbr> '
result = prep_label(result, label, args, i)
table.insert(result, args['lit'])
table.insert(result, args['litref'])
end
-- add semicolon
if i < #order then
if not (args['out'] == 'yes' and i == 1) then
table.insert(result, '; ')
end
end
-- add parentheses if needed
if (i == 1 and args['out'] == 'yes') then table.insert(result, ' (') end
if (i == #order and args['out'] == 'yes') then table.insert(result, ')') end
end
return unpack(result)
end
return p