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 08:52, 17 June 2015 (add missing table key). 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 checkType = require('libraryUtil').checkType
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 string.format('<small>%s </small>', 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, isDotted, isLinked)
	checkType('makeTooltip', 1, label, 'string')
	checkType('makeTooltip', 2, tooltip, 'string')
	local span = mw.html.create('span')
	span:attr('title', tooltip)
	if isDotted ~= false then
		span:css('border-bottom', '1px dotted')
	end
	span:wikitext(label)
	span = tostring(span)
	if isLinked then
		return string.format('[[%s|%s]]', label, span)
	else
		return span
	end		
end

local function makePhoneme(id)
	local display
	id = id and trim(id)
	if id and data.tooltips[id] then
		local label = data.tooltips[id].label
		local tooltip = data.tooltips[id].tooltip
		if not label then
			error(string.format("no label was found for id '%s'", tostring(id)), 2)
		end
		if tooltip then
			display = makeTooltip(label, tooltip)
		else
			display = label
		end
	else
		categories["Ill-formatted IPAc-en transclusions"] = true
		display = makeTooltip(
			"'''[unsupported input]'''",
			'Unrecognized symbol',
			false
		)
	end
	local span = mw.html.create('span')
		:addClass('IPA nopopups')
		:wikitext(string.format('[[Help:IPA for English|%s]]', display))
	return tostring(span)
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
	while true do
		i = i + 1
		local pronunciation = makePronunciationText(args[i])
		if pronunciation then
			ret[#ret + 1] = pronunciation
		else
			break
		end
	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
	while true do
		i = i + 1
		local id = args[i]
		if id then
			ret[#ret + 1] = makePhoneme(id)
		else
			break
		end
	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