Jump to content
From Wikipedia, the free encyclopedia
-- This module implements [[Template:IPAc-en]].local data = mw.loadData('Module:IPAc-en/data/sandbox')local p = {}-- Global container for tracking categorieslocal categoryHandler = require('Module:Category handler').mainlocal categories = {}-- Trims whitespace from a stringlocal function trim(s)return s:match('^%s*(.-)%s*$')end-- This implements [[Template:Nowrap]].local function makeNowrapSpan(s)local span = mw.html.create('span'):addClass('rt-commentedText') -- Works with [[MediaWiki:Gadget-ReferenceTooltips.js]]:addClass('nowrap'):wikitext(s)return tostring(span)endlocal function makePronunciationText(id)id = id and string.lower(trim(id))if id and id ~= '' and data.pronunciation[id] thenreturn data.pronunciation[id].textendend-- This adds a tooltip icon to a label. It implements [[Template:H:title]].local function makeTooltip(label, tooltip)-- mw.html doesn't properly escape '|'return string.format('<span title="%s">%s</span>',mw.text.encode(tooltip, '|'),label)endlocal function formatPhonemeGroup(phonemes)if #phonemes > 0 thenlocal span = mw.html.create('span'):css('border-bottom', '1px dotted'):wikitext(table.concat(phonemes))return tostring(span)elsereturn ''endendlocal function renderCategories()local ret = ''if categoryHandler{ true } thenret = {}for cat in pairs(categories) dotable.insert(ret, string.format('[[Category:%s]]', cat))endtable.sort(ret)ret = table.concat(ret)elseret = ''endreturn retendfunction p._main(args)local ret = {}local i = 0 -- Keeps track of numbered args-- Pronunciationdolocal pron = {}while true doi = i + 1local pronItem = makePronunciationText(args[i])if pronItem thenpron[#pron + 1] = pronItempron[#pron + 1] = ' 'elsebreakendendif #pron > 0 thenret[#ret + 1] = mw.getCurrentFrame():extensionTag({name = 'templatestyles',args = { src = 'Module:IPA/styles.css' }})ret[#ret + 1] = string.format('<span class="IPA-label IPA-label-small">%s</span>',table.concat(pron))endend-- Phonemesdo-- Loop through the numbered args, separating them into phoneme groups-- and separator strings (both called "words" for convenience). We only-- underline the phoneme groups, not the separators.local words = {}words[#words + 1] = '/' -- Opening slashi = i - 1 -- Set up i again as it was changed in the pronunciation looplocal idrepeatlocal phonemes = {}local isWordEnd = falsewhile not isWordEnd doi = i + 1id = args[i]id = id and trim(id)if not id thenisWordEnd = truewords[#words + 1] = formatPhonemeGroup(phonemes)elseif id ~= '' thenlocal t = data.phonemes[id]if not t then-- We were passed an invalid id.isWordEnd = truecategories["Ill-formatted IPAc-en transclusions"] = truewords[#words + 1] = formatPhonemeGroup(phonemes)words[#words + 1] = makeTooltip(string.format("<strong class=\"error\">[invalid input: '%s']</strong>",id),'Unrecognized symbol')elseif not t.label then-- The data module contains bad data, so throw an error.error(string.format("no label was found for id '%s'",tostring(id)))elseif t.tooltip then-- We are dealing with a regular phoneme.phonemes[#phonemes + 1] = makeTooltip(t.label,t.tooltip)else-- We are dealing with a separator.isWordEnd = truewords[#words + 1] = formatPhonemeGroup(phonemes)words[#words + 1] = t.labelendendenduntil not idwords[#words + 1] = '/' -- Closing slash-- Wrap the words in a link to IPA help.local span = mw.html.create('span')-- Suppress Navigation popups and Page Previews (aka Hovercards):addClass('IPA nopopups noexcerpt'):attr('lang', 'en-fonipa'):wikitext(string.format('[[Help:IPA/English|%s]]',table.concat(words)))ret[#ret + 1] = tostring(span)end-- Audio linkdolocal file = args.audio and trim(args.audio)if file and file ~= '' thencategories["Pages including recorded pronunciations"] = trueret[#ret + 1] = mw.getCurrentFrame():expandTemplate{title = 'Template:IPA audio link', args = { file } }endend-- Nowrap and categoriesret = makeNowrapSpan(table.concat(ret)) .. renderCategories()-- Reset the categories table in case we are run again.categories = {}return retendfunction p.main(frame)return p._main(frame:getParent().args)endreturn p