Module:Brackets
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Brackets/doc
-- This module implements [[Template:Brackets]] and [[Template:IPA]].
local data = mw.loadData('Module:Brackets/data')
local p = {}
local function return_bracket(bracket_type, position)
local brackets = data[string.lower(bracket_type)]
if brackets then
if position == 1 then
return brackets[1]
elseif position == 2 then
-- Stipulation for non-pair bracket types
if brackets[2] then
return brackets[2]
else
return brackets[1]
end
else
return nil
end
else
return nil;
end
end
local function return_error(s)
if s then
return string.format(
'<span class="error">Error using {{[[Template:Brackets|Brackets]]}}: "%s" not found in bracket list</span>',
s)
else
return string.format(
'<span class="error">Error using {{[[Template:Brackets|Brackets]]}}: valid text or bracket type not found</span>')
end
end
local function affix_error(affix_code)
return string.format(
'<span class="error"> Error using {{[[Template:Brackets|Brackets]]}}: invalid affix code "%d"</span>',
affix_code)
end
function p._main(bracket_type, text, affix_code)
local bracket_open = return_bracket(bracket_type, 1)
local bracket_close = return_bracket(bracket_type, 2)
if bracket_open and bracket_close then
if affix_code == 1 then
return string.format('%s',
bracket_open)
elseif affix_code == 2 then
return string.format('%s',
bracket_close)
elseif affix_code == 0 then
text = string.format('%s%s%s',
bracket_open or '', text, bracket_close or '')
return text
end
else
return return_error(bracket_type)
end
end
function p.main(frame)
local args = {}
local affix = 0
for k, v in pairs(frame.args) do
args[k] = v ~= '' and v
end
if not args.type then
return return_error(nil)
end
if args.affix then
if tonumber(args.affix) > 2 or tonumber(args.affix) < 0 then
return affix_error(args.affix)
end
affix = tonumber(args.affix)
elseif not args.text then
return return_error(nil)
end
return p._main(args.type, args.text, affix)
end
return p