Jump to content
Wikimedia Meta-Wiki

Module:IPAc-en

From Meta, a Wikimedia project coordination wiki
Module documentation
You might want to create a documentation page for this Scribunto module.
Editors can experiment in this module’s sandbox (create | mirror) and testcases (create) pages.
Please add categories to the /doc subpage. Subpages of this module.
 -- This module implements [[Template:IPAc-en]].

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

 -- Global container for tracking categories
 localcategories={}

 -- Trims whitespace from a string
 localfunctiontrim(s)
 returns:match('^%s*(.-)%s*$')
 end

 -- This implements [[Template:Nowrap]].
 localfunctionmakeNowrapSpan(s)
 localspan=mw.html.create('span')
 :addClass('nowrap')
 :wikitext(s)
 returntostring(span)
 end

 localfunctionmakePronunciationText(id)
 id=idandstring.lower(trim(id))
 ifidandid~=''anddata.pronunciation[id]then
 returndata.pronunciation[id].text
 end
 end

 localfunctiongetFilepath(file)
 returnmw.getCurrentFrame():callParserFunction('filepath',file)
 end

 localfunctionmakeAudioLink(file)
 categories["Articles including recorded pronunciations"]=true
 localspan=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))
 returntostring(span)
 end

 -- This adds a tooltip icon to a label. It implements [[Template:H:title]].
 localfunctionmakeTooltip(label,tooltip)
 localspan=mw.html.create('span')
 :attr('title',tooltip)
 :wikitext(label)
 returntostring(span)
 end

 localfunctionformatPhonemeGroup(phonemes)
 if#phonemes>0then
 localspan=mw.html.create('span')
 :css('border-bottom','1px dotted')
 :wikitext(table.concat(phonemes))
 returntostring(span)
 else
 return''
 end
 end

 localfunctionrenderCategories()
 localret={}
 forcatinpairs(categories)do
 table.insert(ret,string.format('[[Category:%s]]',cat))
 end
 table.sort(ret)
 returntable.concat(ret)
 end

 functionp._main(args)
 localret={}
 locali=0-- Keeps track of numbered args

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

 -- Audio link
 do
 localfile=args.audioandtrim(args.audio)
 iffileandfile~=''then
 ret[#ret+1]=makeAudioLink(file)
 end
 end

 -- Phonemes
 do
 -- 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.
 localwords={}
 words[#words+1]='/'-- Opening slash
 i=i-1-- Set up i again as it was changed in the pronunciation loop
 localid
 repeat
 localphonemes={}
 localisWordEnd=false
 whilenotisWordEnddo
 i=i+1
 id=args[i]
 id=idandtrim(id)
 ifnotidthen
 isWordEnd=true
 words[#words+1]=formatPhonemeGroup(phonemes)
 elseifid~=''then
 localt=data.phonemes[id]
 ifnottthen
 -- We were passed an invalid id.
 isWordEnd=true
 categories["Ill-formatted IPAc-en transclusions"]=true
 words[#words+1]=formatPhonemeGroup(phonemes)
 words[#words+1]=makeTooltip(
 "'''[unsupported input]'''",
 'Unrecognized symbol'
 )
 elseifnott.labelthen
 -- The data module contains bad data, so throw an error.
 error(string.format(
 "no label was found for id '%s'",
 tostring(id)
 ))
 elseift.tooltipthen
 -- We are dealing with a regular phoneme.
 phonemes[#phonemes+1]=makeTooltip(
 t.label,
 t.tooltip
 )
 else
 -- We are dealing with a separator.
 isWordEnd=true
 words[#words+1]=formatPhonemeGroup(phonemes)
 words[#words+1]=t.label
 end
 end
 end
 untilnotid
 words[#words+1]='/'-- Closing slash

 -- Wrap the words in a link to IPA help.
 localspan=mw.html.create('span')
 :addClass('IPA nopopups')
 :wikitext(string.format(
 '[[en:Help:IPA/English|%s]]',
 table.concat(words)
 ))

 ret[#ret+1]=tostring(span)
 end

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

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

 returnret
 end

 functionp.main(frame)
 returnp._main(frame:getParent().args)
 end

 returnp

AltStyle によって変換されたページ (->オリジナル) /