-- This module implements the template.-- Data is in Module:Track gauge/datalocal p = local gaugeDataAll = nillocal dataPageName = 'Module:Track gauge/data' -- set /data/sandbox here to test data/sandbox------------------------------------------------------------------------------------- prepareArgs -- Normalise Arguments coming from an #invoke or from a module-----------------------------------------------------------------------------------local function prepareArgs(frame) local origArgs if frame
nil then -- Named argment if k
'first' then v = mw.ustring.lower(v) if v
'metric' then v = 'met' elseif v
'imperial' then v = 'imp' else k = 'trashparam_first' end args[k] = v elseif k
'wrap' then -- wrap=y deprecated; reading: nowrap=off v = mw.ustring.lower(v) if v
'off' or v
'all' then elseif v
'wrap' and v
1 then -- Unnamed argument, the alias to be searched -- Cleanup searchAlias = p.normaliseAliasInput(v) end end end args['searchAlias'] = searchAlias if rawDisp then args['rawDisp'] = rawDisp end return argsend------------------------------------------------------------------------------------- normaliseAliasInput-----------------------------------------------------------------------------------function p.normaliseAliasInput(aliasIn) local a 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, '⁄', '/') return a end------------------------------------------------------------------------------------- debugReturnArgs -- Debug function.-----------------------------------------------------------------------------------function p.debugReturnArgs(frame) local args = prepareArgs(frame) local retArgs = for k, a in pairs(args) do table.insert(retArgs, k .. '=' .. a) end return 'Args: ' .. table.concat(retArgs, '; ')end------------------------------------------------------------------------------------- getTrackGaugeEntry -- Find entry data for a single gauge (alias)-----------------------------------------------------------------------------------function p.getTrackGaugeEntry(searchAlias) gaugeDataAll = mw.loadData(dataPageName) if searchAlias
searchAlias then return tgEntry end end endend------------------------------------------------------------------------------------- noWrap -- Add span tags to prevent a string from wrapping.-----------------------------------------------------------------------------------local function noWrap(s) return mw.ustring.format('
%s', s)end------------------------------------------------------------------------------------- frac -- A slimmed-down version of the template (a nowrap is to be added with the unit)-----------------------------------------------------------------------------------local function frac(whole, num, den) -- normally would do the TemplateStyles expansion here, but instead we do -- it at the callsite because of return mw.ustring.format('%s%s⁄%s', whole and (whole .. '+') or , num, den )end------------------------------------------------------------------------------------- catMentions -- Wikicode for "article mentions gauge" categories-----------------------------------------------------------------------------------function p.catMentions(tgEntry, sortlabel, doReturn) local ns = 'Category:' local catif tgEntry
'fullpagename' then return ns .. cat elseif doReturn
'show' then -- colontrick return '' else -- unknown arg value return ns .. cat end else -- Returns straight categorisation (wikitext) return '' .. ns .. cat .. sortlabel .. '' endend------------------------------------------------------------------------------------- formatImp -- Formats imperial units size into a single text element-----------------------------------------------------------------------------------function p.formatImp(tgEntry, measurementToLink, setNowrap, addUnitlink) local ret = local ft = tgEntry.ft if ft then local ftlink = addUnitlink and measurementToLink ~= 'imp' and 'ft' or 'ft'
table.insert(ret, mw.ustring.format('%s %s', ft, ftlink)) end local inches = tgEntry['in'] local num = tgEntry.num local den = tgEntry.den local has_fraction = num and den if inches and not num and not den then
table.insert(ret, inches) elseif has_fraction then table.insert(ret, frac(inches, num, den)) end if inches or num and den then local incheslink = addUnitlink and measurementToLink ~= 'imp' and 'in' or 'in' table.insert(ret, incheslink) end local gaugeSize if setNowrap then gaugeSize = noWrap(table.concat(ret, ' ')) else gaugeSize = table.concat(ret, ' ') end -- we have do this here to work around local templatestyles if has_fraction then templatestyles = mw.getCurrentFrame:extensionTag else templatestyles = end if measurementToLink
'met' and tgEntry.pagename ~= nil then return mw.ustring.format('%s', tgEntry.pagename, gaugeSize) else return gaugeSize endend------------------------------------------------------------------------------------- formatAltName-----------------------------------------------------------------------------------function formatAltName(tgEntry, addGaugeName, addGaugeNameLink, disp, setNowrap, engvar) -- Assumed: at least one to add is true. if tgEntry.name
'br' then table.insert(retAlt, '
') else table.insert(retAlt, ' ') end if setNowrap then table.insert(retAlt, '
'en-us' then -- Current implementations (2016): metER for metRE (1000-met, 1009-met) table.insert(retAlt, tgEntry.en_US_name or tgEntry.name) else table.insert(retAlt, tgEntry.name) end end if setNowrap then --close tag table.insert(retAlt, '
') end return table.concat(retAlt, )end------------------------------------------------------------------------------------- main -- The basic module-----------------------------------------------------------------------------------function p.main(frame) -- In general: the tgEntry object (from TG/data) is passed to the functions, while arguments are processed here. local title = mw.title.getCurrentTitle local args = prepareArgs(frame) local tgEntry = p.getTrackGaugeEntry(args.searchAlias)-- Categorise & preview warning when no track gauge definition was found. if tgEntry
if title:inNamespaces(0, 14) then -- mainspace and category space errorTail = errorTail .. "" end return input .. errorTail end
-- Check and set args & tgEntry props: disp, first, nowrap, first local disp = args.disp or local first = args.first or tgEntry.def1 local unitlink = args.unitlink or local comma = args.comma or local nowrap = args.nowrap or local setNowrapElement = (nowrap
'off') -- To prevent nested nowrap tags local measurementToLink if args.lk
'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) local ret = -- nowrap opening tag if nowrap
'on' then table.insert(ret, '
') end -- First measure if first'on', comma
'on', comma
'1' then else local joinText = local closeDisp = if disp
'/' then joinText = ' / ' --spaces elseif disp
'[' or disp == '[]' then joinText = ' [' closeDisp = ']' elseif disp ~= then -- Is anytext joinText = ' ' .. args['rawDisp'] .. ' ' else joinText = ' (' closeDisp = ')' end table.insert(ret, joinText) if first ~= 'met' then table.insert(ret, p.formatMet(tgEntry, measurementToLink, setNowrapElement, unitlink
'off')) else table.insert(ret, p.formatImp(tgEntry, measurementToLink, setNowrapElement, unitlink
'on' then -- Closing tag table.insert(ret, '
') end -- Alternative name if args.al'on' then local setNowrapAltname = (nowrap
'on') -- Logic applied to prevent nested nowrap tags table.insert(ret, formatAltName(tgEntry, args.al
'on', disp, setNowrapAltname, args.engvar)) end -- Closing nowrap tag if nowrap
'no' then -- No categorization elseif title:inNamespaces(0) then -- switched off per -- 2016-12-19 -- table.insert(ret, p.catMentions(tgEntry)) end
-- Now sting the table together return table.concat(ret, )end
return p--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)