Jump to content
Wikipedia The Free Encyclopedia

Module:Track gauge/sandbox

From Wikipedia, the free encyclopedia
This is the module sandbox page for Module:Track gauge (diff).
Module documentation[view] [edit] [history] [purge]
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Warning This Lua module is used on approximately 25,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 implements the {{Track gauge }} template. Please see the template page for documentation on how to use the main TrackGauge function.

Gauge data

[edit ]

The gauge information is stored at Module:Track gauge/data; to add new gauges, see the instructions there.

Data checks

[edit ]

This module includes a function that checks the data page for errors. It is used with the following code:

{{#invoke:Track gauge/autodocument|checkData|name of data page}}

The first positional parameter is the name of the data page that you want to be checked. If this is omitted, the module checks Module:Track gauge/data.

Tracking category

[edit ]

Module set

[edit ]
The above documentation is transcluded from Module:Track gauge/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
 -- This module implements the {{Track gauge}} template.
 -- Data is in Module:Track gauge/data
 localp={}
 localgaugeDataAll=nil
 localdataPageName='Module:Track gauge/data/sandbox'-- set /data/sandbox here to test data/sandbox
 -----------------------------------------------------------------------------------
 -- prepareArgs -- Normalise Arguments coming from an #invoke or from a module
 -----------------------------------------------------------------------------------
 localfunctionprepareArgs(frame)
 localorigArgs
 ifframe==mw.getCurrentFrame()then
 origArgs=frame:getParent().args
 fork,vinpairs(frame.args)do
 origArgs=frame.args
 break
 end
 else
 origArgs=frame
 end
 localargs={}
 -- searchAlias is the cleaned value of args[1]. args[1] is kept as rawInput for error message
 localsearchAlias=''
 localrawDisp
 fork,vinpairs(origArgs)do
 iftonumber(k)==nilthen-- Named argment
 ifk=='disp'then
 rawDisp=v-- Keep raw disp input to pass through plain (wiki)text
 args[k]=mw.ustring.lower(v)
 elseifk=='first'then
 v=mw.ustring.lower(v)
 ifv=='met'orv=='metric'then
 v='met'
 elseifv=='imp'orv=='imperial'then
 v='imp'
 elsek='trashparam_first'end
 args[k]=v
 elseifk=='nowrap'ork=='wrap'then-- wrap=y deprecated; reading: nowrap=off
 v=mw.ustring.lower(v)
 ifv==''orv=='off'orv=='on'orv=='all'then
 elseifv=='inline'or(k=='wrap'andv=='y')then
 v='off'
 elsev=''end
 args['nowrap']=v
 else
 args[k]=mw.ustring.lower(v)
 end
 else
 args[k]=v-- Keep rawInput in [1] for error message
 ifk==1then
 -- Unnamed argument, the alias to be searched
 -- Cleanup
 searchAlias=p.normaliseAliasInput(v)
 end
 end
 end
 args['searchAlias']=searchAlias
 ifrawDispthenargs['rawDisp']=rawDispend
 returnargs
 end
 -----------------------------------------------------------------------------------
 -- normaliseAliasInput
 -----------------------------------------------------------------------------------
 functionp.normaliseAliasInput(aliasIn)
 locala
 a=mw.ustring.lower(mw.ustring.gsub(aliasIn,'[%s%,]',''))
 a=mw.ustring.gsub(a,' ','')
 a=mw.ustring.gsub(a,'gauge$','')
 a=mw.ustring.gsub(a,"'","ft")
 a=mw.ustring.gsub(a,'"','in')
 a=mw.ustring.gsub(a,'⁄','/')
 a=mw.ustring.gsub(a,'⁄','/')
 returna
 end
 -----------------------------------------------------------------------------------
 -- debugReturnArgs -- Debug function.
 -----------------------------------------------------------------------------------
 functionp.debugReturnArgs(frame)
 localargs=prepareArgs(frame)
 localretArgs={}
 fork,ainpairs(args)do
 table.insert(retArgs,k..'='..a)
 end
 return'Args: '..table.concat(retArgs,'; ')
 end
 -----------------------------------------------------------------------------------
 -- getTrackGaugeEntry -- Find entry data for a single gauge (alias)
 -----------------------------------------------------------------------------------
 functionp.getTrackGaugeEntry(searchAlias)
 gaugeDataAll=mw.loadData(dataPageName)
 ifsearchAlias==''then
 returnnil
 end
 localtgEntry=nil
 fori,tgEntryinipairs(gaugeDataAll)do
 forj,aliasinipairs(tgEntry.aliases)do
 ifalias==searchAliasthen
 returntgEntry
 end
 end
 end
 end
 -----------------------------------------------------------------------------------
 -- noWrap -- Add span tags to prevent a string from wrapping.
 -----------------------------------------------------------------------------------
 localfunctionnoWrap(s)
 returnmw.ustring.format('<span class="nowrap">%s</span>',s)
 end
 -----------------------------------------------------------------------------------
 -- frac -- A slimmed-down version of the {{frac}} template (a nowrap is to be added with the unit)
 -----------------------------------------------------------------------------------
 localfunctionfrac(whole,num,den)
 -- normally would do the TemplateStyles expansion here, but instead we do
 -- it at the callsite because of [[:phab:T200704]]
 returnmw.ustring.format(
 '<span class="frac" role="math">%s<span class="num">%s</span>&frasl;<span class="den">%s</span></span>',
 wholeand(whole..'<span class="sr-only">+</span>')or'',
 num,
 den
 )
 end
 -----------------------------------------------------------------------------------
 -- catMentions -- Wikicode for "article mentions gauge" categories
 -----------------------------------------------------------------------------------
 functionp.catMentions(tgEntry,sortlabel,doReturn)
 localns='Category:'
 localcat

 iftgEntry==nilthen
 -- Parent, the container cat
 cat='Articles that mention a specific track gauge'
 else
 cat='Articles that mention track gauge '..tgEntry.id..' mm'
 end
 -- Argument 'label' can be used to add a catsort. Catsort is not used (as of 20 May 2014)
 ifsortlabel~=nilthen
 sortlabel='|'..sortlabel
 else
 sortlabel=''
 end
 ifdoReturn~=nilthen
 ifdoReturn=='fullpagename'then
 returnns..cat
 elseifdoReturn=='pagename'then-- plaintext, no namespace
 returncat
 elseifdoReturn=='show'then-- colontrick
 return'[[:'..ns..cat..sortlabel..']]'
 else-- unknown arg value
 returnns..cat
 end
 else-- Returns straight categorisation (wikitext)
 return'[['..ns..cat..sortlabel..']]'
 end
 end
 -----------------------------------------------------------------------------------
 -- formatImp -- Formats imperial units size into a single text element
 -----------------------------------------------------------------------------------
 functionp.formatImp(tgEntry,measurementToLink,setNowrap,addUnitlink)
 localret={}
 localft=tgEntry.ft
 ifftthen
 localftlink=addUnitlinkandmeasurementToLink~='imp'and'[[Foot (unit)|ft]]'or'ft'

 table.insert(ret,mw.ustring.format('%s&nbsp;%s',ft,ftlink))
 end
 localinches=tgEntry['in']
 localnum=tgEntry.num
 localden=tgEntry.den
 localhas_fraction=numandden
 ifinchesandnotnumandnotdenthen

 table.insert(ret,inches)
 elseifhas_fractionthen
 table.insert(ret,frac(inches,num,den))
 end
 ifinchesornumanddenthen
 localincheslink=addUnitlinkandmeasurementToLink~='imp'and'[[inch|in]]'or'in'
 table.insert(ret,incheslink)
 end
 localgaugeSize
 ifsetNowrapthen
 gaugeSize=noWrap(table.concat(ret,'&nbsp;'))
 else
 gaugeSize=table.concat(ret,'&nbsp;')
 end
 -- we have do this here to work around [[phab:T200704]]
 localtemplatestyles
 ifhas_fractionthen
 templatestyles=mw.getCurrentFrame():extensionTag{
 name='templatestyles',args={src='Fraction/styles.css'}
 }
 else
 templatestyles=''
 end

 ifmeasurementToLink=='imp'andtgEntry.pagename~=nilthen
 returnmw.ustring.format(
 '%s[[%s|%s]]',
 templatestyles,
 tgEntry.pagename,
 gaugeSize
 )
 else
 returntemplatestyles..gaugeSize
 end
 end
 -----------------------------------------------------------------------------------
 -- formatMet -- Formats metric measurements into a single formatted text element. Public for autodocument
 -----------------------------------------------------------------------------------
 functionp.formatMet(tgEntry,measurementToLink,setNowrap,addUnitlink,removeComma)
 localm=tgEntry.m
 localgaugeSize
 ifmthen
 localmUnit=addUnitlinkandmeasurementToLink~='met'and'[[metre|m]]'or'm'
 gaugeSize=mw.ustring.format('%s&nbsp;%s',m,mUnit)
 else
 localmm=tgEntry.mm
 mm=tonumber(mm)
 ifmmthen
 mm=mw.getContentLanguage():formatNum(mm)
 ifremoveCommathen
 mm=string.gsub(mm,",","")
 end
 end
 localmmUnit=addUnitlinkandmeasurementToLink~='met'and'[[millimetre|mm]]'or'mm'
 gaugeSize=mw.ustring.format('%s&nbsp;%s',mm,mmUnit)
 end
 ifsetNowrapthen
 gaugeSize=noWrap(gaugeSize)
 end
 ifmeasurementToLink=='met'andtgEntry.pagename~=nilthen
 returnmw.ustring.format('[[%s|%s]]',tgEntry.pagename,gaugeSize)
 else
 returngaugeSize
 end
 end
 -----------------------------------------------------------------------------------
 -- formatAltName
 -----------------------------------------------------------------------------------
 functionformatAltName(tgEntry,addGaugeName,addGaugeNameLink,disp,setNowrap,engvar)
 -- Assumed: at least one to add is true.
 iftgEntry.name==nilthen
 -- Not checked: link does exist alone
 return''
 end
 localretAlt={}
 ifdisp=='br'then
 table.insert(retAlt,'<br />')
 else
 table.insert(retAlt,' ')
 end
 ifsetNowrapthen
 table.insert(retAlt,'<span class="nowrap">')
 end
 ifaddGaugeNameLinkthen
 ifengvar=='en-us'then
 -- Current implementations (2016): metER for metRE (1000-met, 1009-met)
 table.insert(retAlt,tgEntry.en_US_linkortgEntry.linkortgEntry.name)
 else
 table.insert(retAlt,tgEntry.linkortgEntry.name)
 end
 else-- so must be unlinked .name to add
 ifengvar=='en-us'then
 -- Current implementations (2016): metER for metRE (1000-met, 1009-met)
 table.insert(retAlt,tgEntry.en_US_nameortgEntry.name)
 else
 table.insert(retAlt,tgEntry.name)
 end
 end
 ifsetNowrapthen--close tag
 table.insert(retAlt,'</span>')
 end
 returntable.concat(retAlt,'')
 end
 -----------------------------------------------------------------------------------
 -- main -- The basic module
 -----------------------------------------------------------------------------------
 functionp.main(frame)
 -- In general: the tgEntry object (from TG/data) is passed to the functions, while arguments are processed here.
 localtitle=mw.title.getCurrentTitle()
 localargs=prepareArgs(frame)
 localtgEntry=p.getTrackGaugeEntry(args.searchAlias)

 -- Categorise & preview warning when no track gauge definition was found.
 iftgEntry==nilthen
 localinput=args[1]or''
 localerrorTail=require('Module:If preview')._warning({
 'Track gauge '..
 input..
 ' not in [[:Template:Track_gauge#List_of_defined_track_gauges|List of defined track gauges]] ([[Template talk:Track gauge|talk]]).'
 })

 iftitle:inNamespaces(0,14)then-- mainspace and category space
 errorTail=errorTail.."[[Category:Articles using Template:Track gauge with unrecognized input]]"
 end
 returninput..errorTail
 end

 -- Check and set args & tgEntry props: disp, first, nowrap, first
 localdisp=args.dispor''
 localfirst=args.firstortgEntry.def1
 localunitlink=args.unitlinkor''
 localcomma=args.commaor''
 localnowrap=args.nowrapor''
 localsetNowrapElement=(nowrap==''ornowrap=='off')-- To prevent nested nowrap tags
 localmeasurementToLink
 ifargs.lk=='on'then
 ifdisp=='1'then
 measurementToLink=first-- Can make metric text links to the imp linked page
 else
 measurementToLink=tgEntry.def1-- When first=swapped, this could link 2nd measure.
 end
 end
 -- String the text elements together (compose the return table)
 localret={}
 -- nowrap opening tag
 ifnowrap=='all'ornowrap=='on'then
 table.insert(ret,'<span class="nowrap">')
 end
 -- First measure
 iffirst=='met'then
 table.insert(ret,
 p.formatMet(tgEntry,measurementToLink,setNowrapElement,unitlink=='on',comma=='off'))
 else
 table.insert(ret,
 p.formatImp(tgEntry,measurementToLink,setNowrapElement,unitlink=='on',comma=='off'))
 end
 -- The joint and the second measure
 ifdisp~='1'then
 localjoinText=''
 localcloseDisp=''
 ifdisp=='s'ordisp=='/'then
 joinText='&nbsp;/ '--spaces
 elseifdisp=='br'then
 joinText='<br />('
 closeDisp=')'
 elseifdisp=='['ordisp=='[]'then
 joinText=' ['
 closeDisp=']'
 elseifdisp~=''then-- Is anytext
 joinText=' '..args['rawDisp']..' '
 else
 joinText=' ('
 closeDisp=')'
 end
 table.insert(ret,joinText)
 iffirst~='met'then
 table.insert(ret,
 p.formatMet(tgEntry,measurementToLink,setNowrapElement,unitlink=='on',comma=='off'))
 else
 table.insert(ret,
 p.formatImp(tgEntry,measurementToLink,setNowrapElement,unitlink=='on'))
 end
 table.insert(ret,closeDisp)-- Could be ''
 end
 ifnowrap=='on'then-- Closing tag
 table.insert(ret,'</span>')
 end
 -- Alternative name
 ifargs.al=='on'orargs.allk=='on'then
 localsetNowrapAltname=(nowrap==''ornowrap=='on')-- Logic applied to prevent nested nowrap tags
 table.insert(ret,formatAltName(tgEntry,args.al=='on',args.allk=='on',disp,setNowrapAltname,args.engvar))
 end
 -- Closing nowrap tag
 ifnowrap=='all'then
 table.insert(ret,'</span>')
 end

 -- Category mentionings (maintenance)
 ifargs.addcator''=='no'then
 -- No categorization
 elseiftitle:inNamespaces(0)then
 -- switched off per [[Wikipedia:Categories_for_discussion/Log/2016_December_6#Category:Articles_that_mention_a_specific_track_gauge]]
 -- 2016年12月19日
 -- table.insert(ret, p.catMentions(tgEntry))
 end

 -- Now sting the table together
 returntable.concat(ret,'')
 end

 returnp
 --20161219: maintenance categorisation switched off per CfD
 --20170602: fix bug, show name when al=on
 --20180708: show preview warning when gauge is not in list
 --20190124: with disp=/ (slash) value separator: surround by spaces
 --20210304: add option comma=off (mm only)

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