Module:Korean/sandbox
Appearance
| This is the module sandbox page for Module:Korean (diff). |
| This module is rated as beta. It is considered ready for widespread use, but as it is still relatively new, it should be applied with some caution to ensure results are as expected. |
| This module depends on the following other modules: |
This module provides the logic for {{Korean/auto}}. For an overview of the parameters and logic, see that template's documentation.
Usage
This module can be called by other Lua modules (use p._ko()) and can be invoked
{{#invoke:Korean|ko|args_here}}.
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
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/sandbox') -- 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
-- 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']
-- validate input
if not hangul then return format_error('Must provide Hangul.') end
if hanjaref and not hanja then
return format_error('No Hanja provided for given Hanja ref.')
end
if en_iparef and not en_ipa then
return format_error('No English IPA provided for given English IPA ref.')
end
if ko_iparef and not ko_ipa then
return format_error('No Korean IPA provided for given Korean IPA ref.')
end
if litref and not lit then
return format_error('No literal translation provided for given literal translation ref.')
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 out=yes, you must provide at least one non-reference text input.')
end
-- validate en_out
if en_out == 'yes' then
if out == 'no' then return format_error('if en_out is "yes", out cannot be "no"') end
if en_out == 'yes' then out = 'yes' end -- set out if it wasn't already specified
if not lit then return format_error('en_out specified but no literal translation was provided.') end
if (order and string.sub(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 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
return rr, rr_out
-- -- 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 (label for ipa disabled by default; assumed obvious)
-- if ko_ipa then
-- ko_ipa = ipa_module._main({'ko', ko_ipa, ['label'] = ''})
-- end
-- if en_ipa then
-- en_ipa = ipa_module._main({'en', en_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('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 == 'yes' and string.find(order, 'r')) then return format_error('Order is missing "r" or RR not provided.') end
-- if not (mr == 'yes' 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 lit) then return format_error('Order has "l" but literal translation not provided.') 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> '
-- 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