Module:IPA symbol: Difference between revisions

1,331 bytes added ,  2 years ago
m
1 revision imported
en>Centrist16
(Created page with "local p, __output, args, data = {}, {}, {}, mw.loadData("Module:IPA symbol/data") local id, output __output.name = function() return data.correspondences[id]["name"] end __ou...")
 
m (1 revision imported)
 
(4 intermediate revisions by the same user not shown)
Line 1:
local p, __output, args, data = {}, {}, {}, mw.loadData("'Module:IPA symbol/data"').data
local id,p output= {}
 
local gsub = mw.ustring.gsub
__output.name = function() return data.correspondences[id]["name"] end
local len = mw.ustring.len
__output.wikipage = function() return data.correspondences[id]["wikipage"] end
local sub = mw.ustring.sub
__output.soundfile = function() return data.correspondences[id]["soundfile"] end
__output.type = function() return data.correspondences[id]["type"] end
 
local function html_errorreverseLook(messaget, s)
local ret
if args[2] then
for i = 1, len(s) - 1 do
return args[2]
-- Look for 2-char matches first
else
ret = t[sub(s, i, i + 1)] or t[sub(s, i, i)]
return mw.ustring.format(
if ret then
'<strong class="error">Error using {{[[Template:IPA symbol|IPA symbol]]}}: %s%s</strong>'
return ret
,message
end
,mw.title.getCurrentTitle().isContentPage and ("[[Category:International Phonetic Alphabet pages needing attention|" .. (args[1] or "") .. "]]") or ""
end
)
ret = t[sub(s, -1)] -- Last character
end
if ret then
return ret
end
end
 
local function p.mainreturnData(frames, dataType)
for _, v in ipairs(data.univPatterns) do
-- all input is trimmed; accepted parameters are:
s = gsub(s, v.pat, v.rep)
-- name description
end
-- ==== ===========
local key = s
-- (1) the input
for _, v in ipairs(data.keyPatterns) do
-- (2) overwrite the error message
key = gsub(key, v.pat, v.rep)
-- (3) overwrite the output when input is empty
end
-- output the output, one of: input; name; wikipage; soundfile; type
local ret = data.sounds[key] or data.diacritics[key]
or reverseLook(data.diacritics, s)
if ret and dataType then
if ret[dataType] then
ret = ret[dataType]
else
error(string.format('Invalid data type "%s"', dataType))
end
end
return ret
end
 
local function returnErrorCat()
for k, v in pairs(frame:getParent().args) do
return require('Module:Category handler').main{
args[k] = v and mw.text.trim(v)
'[[Category:International Phonetic Alphabet pages needing attention]]',
end
other = ''
args.output = args.output or 'wikipage'
}
end
 
local function returnError(s)
if not args[1] or args[1] == "" then return args[3] or "" end
return string.format(
'<span class="error">Error using {{[[Template:IPA symbol|IPA symbol]]}}: "%s" not found in list</span>%s',
s, returnErrorCat())
end
 
function p._main(s, errorText, output)
id = data.symbols[args[1]]
return returnData(s, output or 'article') or errorText or returnError(s)
if not id then return html_error('"' .. args[1] .. '" not found in list') end
end
 
function p.main(frame)
local args = {}
for k, v in pairs(frame.args) do
args[k] = v ~= '' and v
end
if not args.symbol then
return '' -- Exit early
end
if args.errortext == 'blank' then
args.errortext = ''
end
return p._main(args.symbol, args.errortext, args.output)
end
 
function p._link(s, displayText, prefix, suffix, audio, addSpan, errorText)
local t = returnData(s)
if t then
s = string.format('%s[[:%s|%s]]%s',
prefix or '', t.article, displayText or s, suffix or '')
if addSpan ~= 'no' then
local span = mw.html.create('span'):addClass('IPA')
if prefix or suffix then
span:addClass('nowrap'):attr('title',
'Representation in the International Phonetic Alphabet (IPA)')
end
s = tostring(span:wikitext(s))
end
if audio then
audio = require('Module:Yesno')(audio, audio)
audio = audio == true and t.audio or audio
if audio ~= '' then
audio = mw.getCurrentFrame():expandTemplate{
title = 'Template:Audio',
args = { audio, 'listen', help = 'no' }
}
audio = ' <span class="nowrap" style="font-size:85%">(' .. audio
.. ')</span>'
end
else
audio = ''
end
return s .. audio
else
return errorText or returnError(s)
end
end
 
function p.link(frame)
return __output[args.output] and __output[args.output]() or html_error("No such output option")
local args = {}
for k, v in pairs(frame.args) do
args[k] = v ~= '' and v
end
if not args.symbol then
return '' -- Exit early
end
if args.errortext == 'blank' then
args.errortext = ''
end
return p._link(args.symbol, args.text, args.prefix, args.suffix, args.audio,
args.span, args.errortext)
end