Module:User:Mr. Stradivarius/RGConvert

local data = [====[ |1000|1000mm|metre|meter|m|1|1m= |1435|1435mm|sg|standard= |56.5|56.5in|56.5"|4ft8.5in|4'8.5"|ussg|nasg|usstandard|nastandard= |1520|1520mm|1.52m|russian= |1668|1668mm|1.668m|iberian= |3mm= |4mm= |4.5mm= |0.177|0.177in|0.177"= |4.8mm=  |0.250|0.250in|0.250"|0.25|0.25in|0.25"=  |6.5mm=  |0.256|0.256in|0.256"= |0.276|0.276in|0.276"= |8mm=  |8.97mm=  |0.353|0.353in|0.353"= |9mm= |9.42mm= |10.5mm= |0.413|0.413in|0.413"= |0.470|0.470in|0.470"|0.47in|0.47"=  |12mm=  |12.7mm=  |0.5in|0.5"= |13mm= |13.5mm= |14mm= |14.125mm= |14.2mm= |14.28mm= |14.3mm= |0.563|0.563in|0.563"= |15.75mm=  |16.2mm=  |16.5mm=  |0.650|0.650in|0.650"|0.65in|0.65"=  |18mm=  |18.2mm=  |18.83mm=  |19mm=  |19.05mm=  |0.750in|0.750"|0.75in|0.75"=  |19.4mm=  |20.5mm=  |21mm=  |0.865|0.865in|0.865"= |22mm= |22.42mm= |0.883|0.883in|0.883"= |22.5mm=  |23mm=  |24mm=  |28.08mm=  |1.125|1.125in|1.125"= |1.177|1.177in|1.177"= |30mm=  |1.25|1.25in|1.25"= |32mm= |32.96mm= |33mm= |35mm= |36.75mm= |1.75in|1.75"= |1.766|1.766in|1.766"= |45mm= |49.20mm|49.2mm= |1.772|1.772in|1.772"= |50.8mm=  |2|2in|2"= |2.125in|2.125"= |63mm=  |63.5mm=  |2.5|2.5in|2.5"= |64|64mm= |2.781|2.781in|2.781"= |89|89mm=  |3.5|3.5in|3.5"= |108|108mm= |4.75|4.75in|4.75"= |127|127mm=  |5in|5"= |184|184mm= |7.25|7.25in|7.25"= |190.5mm=  |7.5|7.5in|7.5"= |8.25|8.25in|8.25"= |9|9in|9"= |9.5|9.5in|9.5"= |254|254mm=  |10|10in|10"= |260|260mm|0.26m= |10.25|10.25in|10.25"= |10.5in|10.5"= |305|305mm= |12|12in|12"= |311|311mm=  |12.25|12.25in|12.25"= |356|356mm= |381|381mm= |15|15in|15"|1ft3in|1'3"= |400|400mm|0.4m= |16|16in|16"|1ft4in|1'4"= |16.5in|16.5"|1ft4.5in|1'4.5"= |17|17in|17"|1ft5in|1'5"|1.5ft= |450|450mm= |457|457mm= |18|18in|18"|1ft6in|1'6"|1.6ft= |483|483mm= |19|19in|19"|1ft7in|1'7"= |19.5|19.5in|19.5"|1ft7.5in|1'7.5"= |500|500mm|0.5m= |508|508mm= |20|20in|20"|1ft8in|1'8"= |21in|21"|1ft9in|1'9"= |557mm= |559|559mm= |22|22in|22"|1ft10in|1'10"= |578|578mm= |22.75|22.75in|22.75"|1ft10.75in|1'10.75"= |597|597mm= |23.5|23.5in|23.5"|1ft11.5in|1'11.5"= |600|600mm|0.6m= |603|603mm= |23.75|23.75in|23.75"|1ft11.75in|1'11.75"= |610|610mm= |24|24in|24"|2ft|2'= |24.125|24.125in|24.125"|2ft0.125in|2'0.125"=  |622|622mm=  |24.5|24.5in|24.5"|2ft0.5in|2'0.5"=  |26|26in|26"|2ft2in|2'2"=  |686|686mm=  |27|27in|27"|2ft3in|2'3"=  |27.5|27.5in|27.5"|2ft3.5in|2'3.5"=  |700|700mm|0.7m=  |27.75|27.75in|27.75"|2ft3.75in|2'3.75"=  |711|711mm=  |28|28in|28"|2ft4in|2'4"=  |28.5|28.5in|28.5"|2ft4.5in|2'4.5"=  |29|29in|29"|2ft5in|2'5"=  |750|750mm|0.75m=  |29.5|29.5in|29.5"|2ft5.5in|2'5.5"=  |760|760mm|bosnian=  |762|762mm=  |30|30in|30"|2ft6in|2'6"|imperial=  |765|765mm=  |30.5|30.5in|30.5"|2ft6.5in|2'6.5"=  |783|783mm=  |785|785mm=  |800|800mm|0.8m=  |31.5|31.5in|31.5"|2ft7.5in|2'7.5"=  |802|802mm=  |32|32in|32"|2ft8in|2'8"=  |820|820mm=  |32.5|32.5in|32.5"|2ft8.5in|2'8.5"=  |838|838mm=  |33|33in|33"|2ft9in|2'9"=  |850|850mm=  |33.75|33.75in|33.75"|2ft9.75in|2'9.75"=  |860|860mm=  |34|34in|34"|2ft10in|2'10"=  |864|864mm=  |34.5|34.5in|34.5"|2ft10.5in|2'10.5"= |35|35in|35"|2ft11in|2'11"= |891|891mm= |900|900mm|0.9m= |914|914mm= |36|36in|36"|3|3ft|3'= |925|925mm=  |37|37in|37"|3ft1in|3'1"=  |37.125|37.125in|37.125"= |950|950mm= |955|955mm= |38|38in|38"|3ft2in|3'2"= |965|965mm= |38.25|38.25in|38.25"|3ft2.25in|3'2.25"= |39.375|39.375in|39.375"|3ft3.375in|3'3.75"= |1009|1009mm= |1016|1016mm= |40|40in|40"|3ft4in|3'4"= |40.5|40.5in|40.5"|3ft4.5in|3'4.5"= |40.75|40.75in|40.75"|3ft4.75in|3'4.75"= |1050|1050mm= |1055|1055mm= |41.5|41.5in|41.5"|3ft5.5in|3'5.5"= |1062|1062mm= |1067|1067mm= |42|42in|3ft6in|3.6ft|3'6"|cape= |1093|1093mm=  |43|43in|3ft7in|3.7ft|3'7"= |1100|1100mm|1.1m= |1106|1106mm= |43.5|43.5in|43.5"|3ft7.5in|3'7.5"= |44.25|44.25in|44.25"|3ft8.25in|3'8.25"= |44.5|44.5in|44.5"|3ft8.5in|3'8.5"= |45|45in|45"|3ft9in|3'9"= |45.5|45.5in|45.5"|3ft9.5in|3'9.5"= |1188|1188mm= |47|47in|47"|3ft11in|3'11"= |1200|1200mm= |1217|1217mm= |1219|1219mm= |48|48in|48"|4|4ft|4'= |1245|1245mm=  |49|49in|49"|4ft1in|4'1"=  |49.5|49.5in|49.5"|4ft1.5in|4'1.5"=  |1270|1270mm=  |50|50in|50"|4ft2in|4'2"=  |1295|1295mm=  |51|51in|51"|4ft3in|4'3"=  |52|52in|52"|4ft4in|4'4"=  |52.5|52.5in|52.5"|4ft4.5in|4'4.5"=  |1372|1372mm=  |54|54in|54"|4ft6in|4'6"|scotch=  |54.5|54.5in|54.5"|4ft6.5in|4'6.5"=  |55.5|55.5in|55.5"|4ft7.5in|4'7.5"=  |55.75|55.75in|55.75"|4ft7.75in|4'7.75"=  |1422|1422mm=  |56|56in|56"|4ft8in|5'8"=  |56.25|56.25in|56.25"|4ft8.25in|4'8.25"=  |1432|1432mm=  |1440|1440mm=  |1445|1445mm=  |57|57in|57"|4ft9in|4'9"=  |1450|1450mm=  |1458|1458mm=  |57.5|57.5"|57.5in|4ft9.5in|4'9.5"=  |1473|1473mm=  |58|58"|58in|4ft10in|4'10"|ohio=  |58.75|58.75in|58.75"|4ft10.75in|4'10.75"=  |1495|1495mm|toronto=  |58.875|58.875in|58.875"|4ft10.875in|4'10.875"=  |1500|1500mm|1.5m=  |1524|1524mm=  |60|60in|60"|5|5ft|5'= |60.5|60.5in|60.5"|5ft0.5in|5'0.5"= |62|62in|62"|5ft2in|5'2"= |62.1875|62.1875in|62.1875"|5ft2.1875in|5'2.1875"= |1581|1581mm= |62.25|62.25in|62.25"|5ft2.25in|5'2.25"|pennsylvanian|pennsylvania= |1588|1588mm= |62.5|62.5in|62.5"|5ft2.5in|5'2.5"= |1600|1600mm|1.6m|victorian|irish= |63|63in|63"|5ft3in|5'3"= |1664|1664mm= |1672|1672mm= |1676|1676mm= |66|66in|66"|5ft6in|5'6"|5.5ft|provincial|indian= |1680|1680mm= |1750|1750mm|1.75m= |1829|1829mm= |72|72in|72"|6|6ft|6'= |1880|1880mm=  |74|74in|74"|6ft2in|6'2"=  |1945|1945mm=  |2000|2000mm|2m=  |2140|2140mm|2.140m|2.14m=  |84|84in|84"|7|7ft|7'|84.25|84.25in|84.25"|7ft0.25in|7'0.25"|brunel= |3000|3000mm|3m= |120|120in|120"|10ft|10'= ]====] local named = [===[ |30|30in|30"|2ft6in|2'6"|imperial=&#32; |1067|1067mm|42|42in|3ft6in|3.5ft|3'6"|cape=&#32; |1435|1435mm|sg|standard|56.5|56.5in|56.5"|4ft8.5in|4'8.5"|ussg|nasg|usstandard|nastandard=&#32; |1473|1473mm|58|58"|58in|4ft10in|4'10"|ohio=&#32; |1495|1495mm|toronto=&#32; |1520|1520mm|1.52m|russian=&#32; |54|54in|54"|4ft6in|4'6"|scotch=&#32; |62.25|62.25in|62.25"|5ft2.25in|5'2.25"|1588|1588mm|pennsylvanian|pennsylvania =&#32; |1600|1600mm|1.6|1.6m|63|63in|63"|5ft3in|5'3"|victorian|irish=&#32; |1668|1668mm|1.668|1.668m|iberian=&#32; |1676|1676mm|66|66in|66"|5ft6in|5'6"|5.5ft|provincial|indian=&#32; |2140|2140mm|2.140m|2.14m|84|84in|84"|7|7ft|7'|84.25|84.25in|84.25"|7ft0.25in|7'0.25"| brunel=&#32; ]===]
 * 1065mm=
 * 760|760mm|bosnian=&#32;
 * 1000|1000mm|metre|meter|m|1m|39.375|39.375in|39.375"|3ft3.375in|3'3.75"=&#32;

local p = {} local dts = require('Module:User:Anomie/deepToString').deepToString -- for debugging -- Replaces bar characters with a pseudo-random string for converting back later. local function debar( s ) local s = mw.ustring.gsub( s, '|', 'DEBARRED-29vuy7dsgtz87465gfbq29gr1bzj-DERRABED' ) return s end -- Undoes the transformation done by debar. local function rebar( s ) local s = mw.ustring.gsub( s, 'DEBARRED%-29vuy7dsgtz87465gfbq29gr1bzj%-DERRABED', '|' ) return s end

local function findTemplate( s, pattern ) for braces in mw.ustring.gmatch( s, '{%b{}}' ) do       braces = mw.ustring.sub( braces, 3, -3 ) if mw.ustring.match( braces, pattern ) then return braces end local recursion = findTemplate( braces, pattern ) if recursion then return recursion end end end

-- Searches a string for the first instance of the specified template, and returns a table with the template arguments. local function getTemplateArgs( s, pattern ) -- Find the template text. local template = findTemplate( s, pattern ) if not template then return nil end -- Temporarily replace characters inside or  –  with a quasi-random code. The bars that are left delineate the template's parameters. template = mw.ustring.gsub( template, '{%b{}}', debar ) template = mw.ustring.gsub( template, '%[%b[]%]', debar ) -- Build the table of parameters. local params = {} for field in mw.ustring.gmatch( template, '|([^|]*)' ) do       local param, value = nil, nil if mw.ustring.find( field, '=' ) then param, value = mw.ustring.match( field, '%s*([^=]-)%s*=(.*)' ) value = value or '' value = mw.text.trim( value ) else value = field end if param then paramNum = tonumber( param ) if paramNum and paramNum >= 1 and math.floor( paramNum ) == paramNum and paramNum ~= math.huge then param = paramNum end if not value then params[ mw.ustring.lower( param ) ] = '' else params[ mw.ustring.lower( param ) ] = value end else table.insert( params, value ) end end -- Replace the quasi-random code with the bars again. for k, v in pairs( params ) do       params[ k ] = rebar( v ) end return params end

function p.main local template = data -- Temporarily replace characters inside or  –  with a quasi-random code. The bars that are left delineate the template's parameters. template = mw.ustring.gsub( template, '{%b{}}', debar ) template = mw.ustring.gsub( template, '%[%b[]%]', debar ) -- Build the table of parameters. local ret = {} local aliases = {} for field in mw.ustring.gmatch( template, '|([^|]*)' ) do       local param, value if mw.ustring.find( field, '=' ) then param, value = mw.ustring.match( field, '%s*([^=]-)%s*=(.*)' ) value = mw.text.trim( value ) else value = field end if param then table.insert( aliases, param ) value = rebar( value ) local params = {} params.compose = getTemplateArgs( value, '^%s*[rR]ailGauge/compose%s*[|}]' ) params.met = getTemplateArgs( value, '^%s*[rR]ailGauge/format met%s*[|}]' ) params.imp = getTemplateArgs( value, '^%s*[rR]ailGauge/format imp%s*[|}]' ) local name, link named = mw.ustring.gsub( named, '{%b{}}', debar ) named = mw.ustring.gsub( named, '%[%b[]%]', debar ) local naliases = {} for nfield in mw.ustring.gmatch( named, '|([^|]*)' ) do               local nparam, nvalue if mw.ustring.find( nfield, '=' ) then nparam, nvalue = mw.ustring.match( nfield, '%s*([^=]-)%s*=(.*)' ) nvalue = mw.text.trim( nvalue ) else nvalue = nfield end if nparam then table.insert( naliases, nvalue ) for i, nalias in ipairs( naliases ) do                       for j, alias in ipairs( aliases ) do                            if nalias == alias then local namedArgs = getTemplateArgs( rebar( nvalue ), '^#ifeq:|' ) name = namedArgs[ 3 ] link = namedArgs[ 2 ] end end end naliases = {} else table.insert( naliases, nvalue ) end end

local id = params.compose.id           local rowVals = {} table.insert( rowVals, mw.ustring.format( '["id"] = "%s"', id ) ) local aliasText = mw.ustring.format( '["aliases"] = { %s }', table.concat( aliases, ']], [[' ) ) table.insert( rowVals, aliasText ) if name then table.insert( rowVals, mw.ustring.format( '["name"] = "%s"', name ) ) end if link then table.insert( rowVals, mw.ustring.format( '["link"] = "%s"', link ) ) end local function insertRowVals( t ) local paramTable = t[1] local paramName = t[2] if params[ paramTable ][ paramName ] and mw.ustring.find( params[ paramTable ][ paramName ], '%S' ) then table.insert( rowVals, mw.ustring.format( '["%s"] = "%s"', paramName, params[ paramTable ][ paramName ] ) ) end end if params.compose.dflt1 == 'imp' then for i, v in ipairs( { {'compose', 'dflt1'}, {'imp', 'ft'}, {'imp', 'in'}, {'imp', 'num'}, {'imp', 'den'}, {'met', 'm'}, {'met', 'mm'} } ) do                   insertRowVals( v ) end else for i, v in ipairs( { {'compose', 'dflt1'}, {'met', 'm'}, {'met', 'mm'}, {'imp', 'ft'}, {'imp', 'in'}, {'imp', 'num'}, {'imp', 'den'} } ) do                   insertRowVals( v ) end end table.insert( ret, mw.ustring.format( '{\n       %s\n    }', table.concat( rowVals, ',\n        ') ) ) aliases = {} else table.insert( aliases, value ) end end return 'return {\n   ' .. table.concat( ret, ',\n   ' ) .. '\n}' end

return p