Jump to content
Wikipedia The Free Encyclopedia

Module:IPA

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
Warning This Lua module is used on approximately 176,000 pages .
To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them.

See Template:IPA.

The above documentation is transcluded from Module:IPA/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Subpages of this module.

 require('strict')
 localp={}

 localfunctionmultiFind(s,patterns,init)
 locali,j=mw.ustring.find(s,patterns[1],init)
 forn=2,#patternsdo
 locali2,j2=mw.ustring.find(s,patterns[n],init)
 ifi2and(notiori2<i)then
 i,j=i2,j2
 end
 end
 returni,j
 end

 localfunctionwrapAtSpaces(s)
 returnmw.ustring.gsub(s,'(%s+)','<span class="wrap">%1</span>')
 end

 localfunctionwrapAtSpacesSafely(s)
 localpatterns={
 '%[%[[^%]|]-%s[^%]|]-|',-- Piped links
 '</?[A-Za-z][^>]-%s[^>]->'-- HTML tags
 }
 s=mw.ustring.gsub(s,'%[%[([^%]|]-%s[^%]|]-)%]%]','[[%1|%1]]')-- Pipe all links
 localt={}
 localinit
 whiletruedo
 locali,j=multiFind(s,patterns,init)
 ifnotithen
 break
 end
 localpre=wrapAtSpaces(mw.ustring.sub(s,init,i-1))-- What precedes the match
 table.insert(t,pre)
 table.insert(t,mw.ustring.sub(s,i,j))-- The match
 init=j+1
 end
 localpost=wrapAtSpaces(mw.ustring.sub(s,init))-- What follows the last match
 table.insert(t,post)
 returntable.concat(t)
 end

 localfunctioncheckNamespace(isDebug)
 returnisDebugorrequire('Module:Category handler').main({true})
 end

 localfunctionrenderCats(cats,isDebug)
 ifnotcats[1]ornotcheckNamespace(isDebug)then
 return''
 end
 localt={}
 for_,vinipairs(cats)do
 table.insert(t,string.format(
 '[[%sCategory:%s]]',
 isDebugand':'or'',
 v
 ))
 end
 returntable.concat(t)
 end

 localfunctionresolveSynonym(s)
 returnmw.loadData('Module:Lang/ISO 639 synonyms')[s]ors
 end

 localfunctionsplitTag(s)
 locallangCode=s:gsub('%-.*',''):lower()
 langCode=resolveSynonym(langCode)
 localregionCode=s:match('%-(.+)')
 localisPrivate=regionCodeandregionCode:sub(1,2)=='x-'
 returnlangCode,regionCode,isPrivate
 end

 localfunctiongetLangName(code,link,raw)
 returnrequire('Module:Lang')._name_from_tag({
 code,
 link=link,
 raw=raw,
 -- Without linking, "{{IPA}}" gets expanded in some contexts
 template='[[Template:IPA|IPA]]'
 })
 end

 localfunctionlinkLang(name,target,link)
 returnlink=='yes'andstring.format(
 '[[%s|%s]]',
 targetorname..' language',
 name
 )orname
 end

 functionp._main(args)
 localret,cats={},{}
 localisDebug=args.debug=='yes'
 locals,langCode,regionCode,isPrivate

 -- Guide-linking mode
 ifargs[2]andargs[2]~=''then
 localdata=mw.loadData('Module:IPA/data')
 localisGeneric=args.generic=='yes'
 s=args[2]
 langCode,regionCode,isPrivate=splitTag(args[1])
 locallangData=data.langs[langCode]or{}
 ifregionCodethen
 ifnotisPrivatethen
 regionCode=regionCode:upper()
 end
 iflangData.dialectsandlangData.dialects[regionCode]then
 -- Overwrite language data with the dialect's
 localnewLangData={}
 fork,vinpairs(langData)do
 ifk~='dialects'then
 newLangData[k]=v
 end
 end
 localdialectData=langData.dialects[regionCode]
 ifdialectData.aliasOfthen
 -- Use the canonical region code
 regionCode=dialectData.aliasOf
 isPrivate=regionCode:sub(1,2)=='x-'
 dialectData=langData.dialects[regionCode]
 end
 -- Lowercase IANA variant
 ifdialectData.isVariantthen
 regionCode=regionCode:lower()
 end
 fork,vinpairs(dialectData)do
 newLangData[k]=v
 end
 langData=newLangData
 else
 isGeneric=true
 end
 end

 localfullLangCode=regionCodeandlangCode..'-'..regionCode
 orlangCode
 locallangName=langData.name
 andlinkLang(langData.name,langData.link,args.link)
 orgetLangName(fullLangCode,args.link)
 iflangName:sub(1,5)=='<span'then
 -- Module:Lang has returned an error
 returnlangName..renderCats({'IPA template errors'},isDebug)
 end
 ifargs.cat~='no'then
 localcatLangName=langData.name
 orgetLangName(fullLangCode,nil,'yes')
 ifcatLangName:sub(1,5)=='<span'then
 -- Module:Lang has returned an error, but it's not fatal
 table.insert(cats,'IPA template errors')
 mw.addWarning(catLangName)
 else
 table.insert(cats,string.format('Pages with %s IPA',catLangName))
 end
 end

 -- Label
 locallabel=args.label
 ifnotlabelthen
 locallabelCode=args[3]andargs[3]:lower()
 orlangData.defaultLabelCode
 iflabelCode==''then
 label=''
 else
 locallangText
 iflangData.textthen
 langText=linkLang(
 langData.text,
 mw.ustring.match(langName,'^%[%[([^|%]]+)'),
 args.link
 )
 else
 langText=mw.ustring.gsub(
 langName,
 '^%[%[(([^|]+) languages)%]%]$',
 '[[%1|%2]]'
 )
 langText=mw.ustring.gsub(
 langText,
 ' languages(%]?%]?)$',
 '%1'
 )
 end
 iflabelCodeanddata.labels[labelCode]then
 label=data.labels[labelCode]:format(langText)
 else
 label=data.defaultLabel:format(langText)
 end
 end
 end
 iflabelandlabel~=''then
 localspan=mw.html.create('span')
 :addClass('IPA-label')
 :wikitext(label)
 ifargs.small~='no'then
 span:addClass('IPA-label-small')
 table.insert(ret,mw.getCurrentFrame():extensionTag({
 name='templatestyles',
 args={src='Module:IPA/styles.css'}
 }))
 end
 table.insert(ret,tostring(span)..' ')
 end

 -- Brackets
 s=(notisGenericandlangData.formator'&#91;%s&#93;'):format(s)

 -- Link to key
 localkey=notisGenericandlangData.keyordata.defaultKey
 s=string.format('[[%s|%s]]',key,s)
 else
 -- Basic mode
 s=args[1]
 ifargs.langandargs.lang~=''then
 langCode,regionCode,isPrivate=splitTag(args.lang)
 end
 ifargs.cat~='no'then
 table.insert(cats,'Pages with plain IPA')
 end
 end

 -- Transcription
 do
 locallang=(langCodeor'und')..'-Latn'
 ifnotisPrivateandregionCodethen
 lang=lang..'-'..regionCode
 end
 lang=lang..'-fonipa'
 localspan=mw.html.create('span')
 :addClass('IPA')
 :addClass(args.class)
 :attr('lang',lang)
 -- wrap=all: Do nothing
 -- wrap=none: Never break
 -- Otherwise: Break at spaces only
 ifargs.wrap~='all'then
 span:addClass('nowrap')
 ifargs.wrap~='none'then
 s=wrapAtSpacesSafely(s)
 end
 end
 if(notargs[2]orargs[2]=='')andargs.tooltip~=''then
 localtooltip=args.tooltipor
 'Representation in the International Phonetic Alphabet (IPA)'
 span:attr('title',tooltip)
 end
 s=tostring(span:wikitext(s))
 table.insert(ret,s)
 end

 -- Audio
 localaudio=args.audio~=''andargs.audioorargs[4]~=''andargs[4]
 ifaudiothen
 localbutton=mw.getCurrentFrame():expandTemplate({
 title='Audio',
 args={audio,''}
 })
 table.insert(ret,' '..button)
 table.insert(cats,'Pages including recorded pronunciations')
 end

 -- Categories
 table.insert(ret,renderCats(cats,isDebug))

 returntable.concat(ret)
 end

 functionp.main(frame)
 localargs=frame:getParent().args
 ifnotargs[1]then
 return''
 end
 fori,vinipairs(args)do
 args[i]=mw.text.trim(v)
 end
 returnp._main(args)
 end

 returnp

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