Jump to content
Wikipedia The Free Encyclopedia

Module:Convert/helper

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]

This module is not used by module:Convert. It can be used in templates to preprocess input from regular written text into formatted input. For example:

{{convert|{{#invoke:Convert/helper |number |1=15 4/3 }}|...}} → {{convert|15+4/3|...}}

Usage

Applied in {{NFL predraft }}.

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

 -- This module provides some functions to prepare template parameters
 -- for use with Template:Convert.
 -- This module is not used by Template:Convert or Module:Convert.

 localfunctionstripToNil(text)
 -- If text is a non-empty string, return its trimmed content,
 -- otherwise return nothing (empty string or not a string).
 iftype(text)=='string'then
 returntext:match('(%S.-)%s*$')
 end
 end

 -- Remove commas and references (any strip markers) from a number.
 -- First usage in Template:Infobox_UK_place/dist (June 2018)
 localfunctioncleanNumber(frame)
 localargs=frame.args
 localtext=stripToNil(args[1])or''
 iftext==''ortonumber(text)then
 returntext
 end
 returnmw.text.killMarkers(text):gsub(',','')
 end

 localfractions={
 ['1⁄2']='1/2',
 ['1⁄3']='1/3',
 ['2⁄3']='2/3',
 ['1⁄4']='1/4',
 ['3⁄4']='3/4',
 ['1⁄8']='1/8',
 ['3⁄8']='3/8',
 ['5⁄8']='5/8',
 ['7⁄8']='7/8',
 }

 localfractionNumbers={
 ['1⁄2']=1/2,
 ['1⁄3']=1/3,
 ['2⁄3']=2/3,
 ['1⁄4']=1/4,
 ['3⁄4']=3/4,
 ['1⁄8']=1/8,
 ['3⁄8']=3/8,
 ['5⁄8']=5/8,
 ['7⁄8']=7/8,
 }

 -- Format regular input with fraction (MOS-confirmant) into Convert-format "12+3/8" ("+" added).
 -- First usage in Template:NFL_predraft (August 2017)
 localfunctionnumber(frame)
 --[[ Preprocess a template parameter to translate a number to be used as
 	input for {{convert}}.
 	{{#invoke:convert/helper|number|12 3/8}} → 12+3/8
 		Input				Output
 		12					12
 		12 3/8				12+3/8
 		{{frac|12|3|8}}		12+3/8
 		12{{frac|3|8}}		12+3/8
 		123⁄8					12+3/8
 	Template:Fraction redirects to Template:Frac so either may be used in the input.
 	]]
 localargs=frame.args
 localtext=stripToNil(args[1])or''
 iftext==''ortonumber(text)then
 returntext-- examples: '', '12', '12.3', '12.3e4', or negative
 end
 text=text:gsub(' ',' '):gsub(' +',' '):gsub(' *%+ *','+'):gsub('⁄','/'):gsub('⁄','/')
 localinteger,numerator,denominator,rest
 -- Look for a fraction of form '12 3/8' or '12+3/8' or '3/8'.
 integer,numerator,denominator=text:match('^(%d+)[ +](%d+)/(%d+)$')
 ifintegerthen
 returninteger..'+'..numerator..'/'..denominator
 end
 numerator,denominator=text:match('^(%d+)/(%d+)$')
 ifnumeratorthen
 returnnumerator..'/'..denominator
 end
 -- Look for an expanded fraction such as the result of {{frac|12|3|8}} or 12{{frac|3|8}} or {{frac|3|8}}.
 numerator,denominator=text:match('<sup>(%d+)</sup>/<sub>(%d+)</sub></span>')
 ifnumeratorthen
 integer=text:match('(%d+)<span class="sr-only">')or
 text:match('^(%d+)%s*&#x200B;<span')or-- Template:Frac outputs zwsp since December 2017
 text:match('^(%d+)%s*<span')
 return(integerand(integer..'+')or'')..numerator..'/'..denominator
 end
 -- Look for a fraction of form '123⁄4' or '3⁄4'.
 integer,rest=text:match('^(%d*)%s*(.*)')
 localexpand=fractions[rest]
 ifexpandthen
 return(integer==''andintegeror(integer..'+'))..expand
 end
 returntext
 end

 localfunctiondistanceNumber(text)
 -- Return a number corresponding to text (0 if text is empty) or throw an error if invalid.
 text=textor0
 iftonumber(text)then
 returntonumber(text)
 end
 -- Look for a fraction of form '123⁄4' or '3⁄4'.
 localinteger,expand=text:match('^(%d*)%s*(.*)')
 ifinteger==''then
 integer=0
 else
 integer=tonumber(integer)
 end
 ifexpand==''then
 expand=0
 else
 expand=fractionNumbers[expand]
 end
 ifintegerandexpandthen
 returninteger+expand
 end
 error('Invalid number "'..text..'"',0)
 end

 -- First usage in Template:Horse_race_distance (January 2024)
 localfunctionhorseRaceDistance(frame)
 localargs=frame:getParent().args
 localmiles=stripToNil(args[1])
 localfurlongs=stripToNil(args[2])
 localyards=stripToNil(args[3])
 localshow={}
 ifmilesthen
 table.insert(show,miles..'m')
 end
 iffurlongsthen
 table.insert(show,furlongs..'f')
 end
 ifyardsthen
 table.insert(show,yards..'y')
 end
 miles=distanceNumber(miles)
 furlongs=distanceNumber(furlongs)
 yards=distanceNumber(yards)
 localmeters=miles*1609.344+furlongs*201.168+yards*0.9144
 return
 '<span data-sort-value="'..
 tostring(meters)..
 '&nbsp;!">'..
 table.concat(show,' ')..
 '</span>'
 end

 return{
 number=number,
 cleanNumber=cleanNumber,
 horseRaceDistance=horseRaceDistance,
 }

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