Module:IPAc-en
Appearance
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This Lua module is used on approximately 53,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
![]() | This module depends on the following other modules: |
![]() | This module uses TemplateStyles: |
This module implements {{IPAc-en}}. Please see the template page for documentation.
To edit the diaphoneme data, go to Module:IPAc-en/phonemes, and to edit the pronunciation data go to Module:IPAc-en/pronunciation.
This module also uses a data-formatting module at Module:IPAc-en/data.
-- 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