Jump to content

Module:IPA symbol/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
mNo edit summary
sync
Line 4: Line 4:
local gsub = mw.ustring.gsub
local gsub = mw.ustring.gsub
local len = mw.ustring.len
local len = mw.ustring.len
local lower = mw.ustring.lower
local sub = mw.ustring.sub
local sub = mw.ustring.sub


Line 12: Line 11:
-- Look for 2-char matches first
-- Look for 2-char matches first
ret = t[sub(s, i, i + 1)] or t[sub(s, i, i)]
ret = t[sub(s, i, i + 1)] or t[sub(s, i, i)]
if ret then break end
if ret then return ret end
end
end
return ret or t[sub(s, -1)] -- Last character
ret = t[sub(s, -1)] -- Last character
if ret then return ret end
end
end


Line 64: Line 64:
if args.errortext == 'blank' then args.errortext = '' end
if args.errortext == 'blank' then args.errortext = '' end
return p._main(args.symbol, args.errortext, args.output)
return p._main(args.symbol, args.errortext, args.output)
end

local function renderAudio(s, file)
if s then
s = require('Module:Yesno')(s, s)
if s == true then s = file end
if s ~= '' then
s = mw.getCurrentFrame():expandTemplate{
title = 'Template:Audio',
args = { s, 'listen', help = 'no' }
}
s = ' <span class="nowrap" style="font-size:85%">(' .. s ..
')</span>'
end
else
s = ''
end
return s
end
end


Line 97: Line 79:
s = tostring(span:wikitext(s))
s = tostring(span:wikitext(s))
end
end
return s .. renderAudio(audio, t.audio)
if audio then
audio = require('Module:Yesno')(audio, audio)
if audio == true then audio = t.audio end
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
else
return errorText or returnError(s)
return errorText or returnError(s)
Line 112: Line 108:
return p._link(args.symbol, args.text, args.prefix, args.suffix, args.audio,
return p._link(args.symbol, args.text, args.prefix, args.suffix, args.audio,
args.span, args.errortext)
args.span, args.errortext)
end

function p._textlink(s, lowercase, errorText)
local t = returnData(s)
if t then
s = t.name
local article = ''
if s ~= t.article then
article = t.article .. '|'
end
if lowercase == 'yes' then
s = lower(sub(s, 1, 1)) .. sub(s, 2)
end
return string.format('[[:%s%s]]', article, s)
else
return errorText or returnError(s)
end
end

function p.textlink(frame)
local args = {}
for k, v in pairs(frame.args) do
args[k] = v ~= '' and v
end
if not args.symbol then return '' end -- Exit early
if args.errortext == 'blank' then args.errortext = '' end
return p._textlink(args.symbol, args.lowercase, args.errortext)
end
end



Revision as of 17:06, 27 January 2020

local data = mw.loadData('Module:IPA symbol/sandbox/data').data
local p = {}

local gsub = mw.ustring.gsub
local len = mw.ustring.len
local sub = mw.ustring.sub

local function reverseLook(t, s)
	local ret
	for i = 1, len(s) - 1 do
		-- Look for 2-char matches first
		ret = t[sub(s, i, i + 1)] or t[sub(s, i, i)]
		if ret then return ret end
	end
	ret = t[sub(s, -1)] -- Last character
	if ret then return ret end
end

local function returnData(s, dataType)
	for _, v in ipairs(data.univPatterns) do
		s = gsub(s, v.pat, v.rep)
	end
	local key = s
	for _, v in ipairs(data.keyPatterns) do
		key = gsub(key, v.pat, v.rep)
	end
	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()
	local ns = mw.title.getCurrentTitle().namespace
	if ns % 2 == 0 and ns ~= 2 then -- Non-talk and non-user
		return '[[Category:International Phonetic Alphabet pages needing attention]]'
	else
		return ''
	end
end

local function returnError(s)
	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)
	return returnData(s, output or 'article') or errorText or returnError(s)
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 '' end -- Exit early
	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)
			if audio == true then audio = t.audio end
			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)
	local args = {}
	for k, v in pairs(frame.args) do
		args[k] = v ~= '' and v
	end
	if not args.symbol then return '' end -- Exit early
	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

return p