Jump to content

Module:IPAc-en

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 10:13, 17 June 2015 (make underlining look nice, at the expense of having to underline unsupported input warnings as well). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module implements [[Template:IPAc-en]].

local data = mw.loadData('Module:IPAc-en/data')
local p = {}

-- Constants
local IPA_MARKER = '[[Help:IPA for English|/]]'

-- Global container for tracking categories
local categories = {}

-- Trims whitespace from a string
local function trim(s)
	return s:match('^%s*(.-)%s*$')
end

-- This implements [[Template:Nowrap]].
local function makeNowrapSpan(s)
	local span = mw.html.create('span')
		:addClass('nowrap')
		:wikitext(s)
	return tostring(span)
end

-- This implements [[Template:IPA]].
local function makeIPASpan(s)
	local span = mw.html.create('span')
		:attr('title', 'Representation in the International Phonetic Alphabet (IPA)')
		:addClass('IPA')
		:wikitext(s)
	return tostring(span)
end

local function makePronunciationText(id)
	id = id and string.lower(trim(id))
	if id and id ~= '' and data.pronunciation[id] then
		return data.pronunciation[id].text
	end
end

local function getFilepath(file)
	return mw.getCurrentFrame():callParserFunction('filepath', file)
end

local function makeAudioLink(file)
	categories["Articles including recorded pronunciations"] = true
	local span = mw.html.create('span')
	span
		:addClass('noexcerpt')
		:wikitext(string.format(
			'[[File:Speakerlink-new.svg|11px|link=%s|Listen]]',
			getFilepath(file)
		))
		:tag('sup')
			:tag('span')
				:css('color', '#00e')
				:css('font', 'bold 80% sans-serif')
				:css('padding', '0 .1em')
				:addClass('IPA')
				:wikitext(string.format('[[:File:%s|i]]', file))
	return tostring(span)
end

-- This adds a tooltip icon to a label. It implements [[Template:H:title]].
local function makeTooltip(label, tooltip)
	local span = mw.html.create('span')
		:attr('title', tooltip)
		:wikitext(label)
	return tostring(span)
end

local function makePhoneme(id)
	local display
	id = id and trim(id)
	if not id or id == '' then
		return ''
	elseif data.phonemes[id] then
		local label = data.phonemes[id].label
		local tooltip = data.phonemes[id].tooltip
		if not label then
			error(string.format("no label was found for id '%s'", tostring(id)), 2)
		end
		if tooltip then
			return makeTooltip(label, tooltip)
		else
			return label
		end
	else
		categories["Ill-formatted IPAc-en transclusions"] = true
		return makeTooltip(
			"'''[unsupported input]'''",
			'Unrecognized symbol',
			false
		)
	end
end

local function renderCategories()
	local ret = {}
	for cat in pairs(categories) do
		table.insert(ret, string.format('[[Category:%s]]', cat))
	end
	table.sort(ret)
	return table.concat(ret)
end

function p._main(args)
	local ret = {}
	local i = 0 -- Keeps track of numbered args

	-- Pronunciation
	do
		local pron = {}
		while true do
			i = i + 1
			local pronItem = makePronunciationText(args[i])
			if pronItem then
				pron[#pron + 1] = pronItem
				pron[#pron + 1] = ' '
			else
				break
			end
		end
		ret[#ret + 1] = string.format('<small>%s</small>', table.concat(pron))
	end

	-- Audio link
	do
		local file = args.audio and trim(args.audio)
		if file and file ~= '' then
			ret[#ret + 1] = makeAudioLink(file)
		end
	end

	-- Opening /
	ret[#ret + 1] = makeIPASpan(IPA_MARKER)

	-- Phonemes
	i = i - 1
	do
		local phonemes = {}
		while true do
			i = i + 1
			local id = args[i]
			if id then
				phonemes[#phonemes + 1] = makePhoneme(id)
			else
				break
			end
		end
		local span = mw.html.create('span')
			:addClass('IPA nopopups')
			:css('border-bottom', '1px dotted')
			:wikitext(string.format(
				'[[Help:IPA for English|%s]]',
				table.concat(phonemes)
			))
		ret[#ret + 1] = tostring(span)
	end

	-- Closing /
	ret[#ret + 1] = makeIPASpan(IPA_MARKER)

	-- Nowrap and categories
	ret = makeNowrapSpan(table.concat(ret)) .. renderCategories()

	-- Reset the categories table in case we are run again.
	categories = {}

	return ret
end

function p.main(frame)
	return p._main(frame:getParent().args)
end

return p