require('strict');
local p = {}
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 p.format_error('Must provide Hangul.') end
if args['hanjaref'] and not hanja then
return p.format_error('No Hanja provided for given Hanja ref.')
end
if args['iparef'] and not args['ipa'] then
return p.format_error('No IPA provided for given IPA ref.')
end
if args['litref'] and not args['lit'] then
return p.format_error('No literal translation provided for given literal translation ref.')
end
-- apply name mode
if args['namemode'] == 'yes' then
hangul = '%' .. hangul
end
-- apply capitalization
if args['capitalize'] == 'yes' then
hangul = '^' .. hangul
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 ipa (label for ipa disabled by default; assumed obvious)
local ipa
if args['ipa'] then
ipa = ipa_module._main({'ko', args['ipa'], ['label'] = ''})
end
-- prep display order string
local order
-- if order was provided
if args['order'] then
order = args['order']
-- check if order is malformed
if not string.match(order, '^[hzrmil]+$') then return p.format_error('Order contains invalid characters.') end
-- check if any characters duplicated
for _, c in ipairs({'h', 'z', 'r', 'm', 'i', 'l'}) do
local _, count = order:gsub(c, '')
if count > 1 then return p.format_error('Order contains more than one "' .. c .. '".') end
end
if not string.find(order, 'h') then return p.format_error('Order is missing "h".') end
if not (hanja and string.find(order, 'z')) then return p.format_error('Order is missing "z" or Hanja not provided.') end
if not (rr_out and string.find(order, 'r')) then return p.format_error('Order is missing "r" or RR not provided.') end
if not (mr_out and string.find(order, 'm')) then return p.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, 'i') and not ipa) then return p.format_error('Order has "i" but IPA not provided.') end
if (string.find(order, 'l') and not args['lit']) then return p.format_error('Order has "l" but literal translation not provided.') end
else
-- default order (hangul, hangulref, hanja, hanjaref, rr, mr, ipa, iparef, lit)
order = 'h'
if hanja then order = order .. 'z' end
if rr_out then order = order .. 'r' end
if mr_out then order = order .. 'm' end
if ipa then order = order .. 'i' end
if args['lit'] then order = order .. 'l' end
end
-- add optional order parameters (i, l) to end if not already there
if (ipa and not string.find(order, 'i')) then order = order .. 'i' 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 = order:sub(i, i)
-- hangul processing
if c == 'h' then
-- language label insertion (more complicated conditionals in function)
if args['links'] == 'no' then
label = 'Korean: '
else
label = '[[Korean language|Korean]]: '
end
result = p._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
if args['context'] == 'north' then label = 'Hancha: ' else label = 'Hanja: ' end
else
if args['context'] == 'north' then label = '[[Hanja|Hancha]]: ' else label = '[[Hanja]]: ' end
end
result = p._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 = p._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 = p._prep_label(result, label, args, i)
table.insert(result, mr_out)
end
-- ipa processing
if c == 'i' then
table.insert(result, ipa)
table.insert(result, args['iparef'])
end
-- literal translation processing
if c == 'l' then
label = '<abbr style="font-size:85%;" title="literal translation">lit.</abbr> '
result = p._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
-- common label prepping to make loop more readable
function p._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
function p.format_error(message)
local result = {
'<span class="error"><code style="color:inherit; border:inherit; padding:inherit;">{{Korean}} error. ',
message,
' ([[Template:Korean|Help]])</code></span>',
'[[Category:Korean template errors]]'
}
return table.concat(result)
end
return p