Module:Track gauge: Difference between revisions

Content added Content deleted
m (1 revision imported)
en>Centrist16
(Created page with "-- This module implements the {{Track gauge}} template. -- Data is in Module:Track gauge/data local p = {} local gaugeDataAll = nil local dataPageName = 'Module:Track gauge/da...")
 
Line 3: Line 3:
local p = {}
local p = {}
local gaugeDataAll = nil
local gaugeDataAll = nil
local dataPageName = 'Module:Track gauge/data' -- sandbox here
local dataPageName = 'Module:Track gauge/data' -- set /data/sandbox here
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-- prepareArgs -- Normalise Arguments coming from an #invoke or from a module
-- prepareArgs -- Normalise Arguments coming from an #invoke or from a module
Line 110: Line 110:
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
local function frac(whole, num, den)
local function frac(whole, num, den)
local templatestyles = mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = 'Screen reader-only/styles.css' }
}
return mw.ustring.format(
return mw.ustring.format(
'<span class="frac">%s%s<sup>%s</sup>&frasl;<sub>%s</sub></span>',
'<span class="frac">%s%s<sup>%s</sup>&frasl;<sub>%s</sub></span>',
whole or '', whole and '<span class="visualhide">&nbsp;</span>' or '',
whole or '', whole and (templatestyles .. '<span class="sr-only">&nbsp;</span>') or '',
num, den)
num, den)
end
end
Line 212: Line 215:
-- formatAltName
-- formatAltName
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
function formatAltName(tgEntry, addArticleLink, disp, setNowrap, engvar)
function formatAltName(tgEntry, addGaugeName, addGaugeNameLink, disp, setNowrap, engvar)
-- Assumed: at least one to add is true.
-- Asserted: al=on
if tgEntry.name == nil then
if tgEntry.name == nil then
-- Not checked: link does exist alone
return ''
return ''
end
end
local retAlt = {}
local retAlt = {}
if disp == 'br' then
if disp == 'br' then
table.insert(retAlt, '<br>')
table.insert(retAlt, '<br />')
else
else
table.insert(retAlt, ' ')
table.insert(retAlt, ' ')
Line 226: Line 230:
table.insert(retAlt, '<span class="nowrap">')
table.insert(retAlt, '<span class="nowrap">')
end
end
if addArticleLink then
if addGaugeNameLink then
if engvar == 'en-us' then
if engvar == 'en-us' then
-- Current implementations: metER for metRE (1000-met, 1009-met)
-- Current implementations (2016): metER for metRE (1000-met, 1009-met)
table.insert(retAlt, tgEntry.en_US_link or tgEntry.link)
table.insert(retAlt, tgEntry.en_US_link or tgEntry.link or tgEntry.name)
else
else
if tgEntry.link then -- If not defined then fallback to unlinked name
table.insert(retAlt, tgEntry.link or tgEntry.name)
end
table.insert(retAlt, tgEntry.link)
else -- so must be unlinked .name to add
else
if engvar == 'en-us' then
table.insert(retAlt, tgEntry.name)
-- Current implementations (2016): metER for metRE (1000-met, 1009-met)
end
table.insert(retAlt, tgEntry.en_US_name or tgEntry.name)
else
table.insert(retAlt, tgEntry.name)
end
end
end
end
Line 247: Line 254:
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
function p.main(frame)
function p.main(frame)
-- In general: tgEntry (from TG/data) is passed to the functions, arguments are processed here.
-- In general: the tgEntry object (from TG/data) is passed to the functions, while arguments are processed here.
local title = mw.title.getCurrentTitle()
local title = mw.title.getCurrentTitle()
local args = prepareArgs(frame)
local args = prepareArgs(frame)
local tgEntry = p.getTrackGaugeEntry(args.searchAlias)
local tgEntry = p.getTrackGaugeEntry(args.searchAlias)


-- Categorise the page if no gauge information was found.
-- Categorise & preview warning when no track gauge definition was found.
if tgEntry == nil then
if tgEntry == nil then
local category = ''
local input = args[1] or ''
local errorTail = ''
if title:inNamespaces(0, 14) then
local sort1
if (args[1] or '') == '' then -- Blank input, sort top.
if frame:preprocess("{{REVISIONID}}") == "" then
errorTail = '<div class="hatnote" style="color:red"><strong>Warning:</strong> ' .. 'Track gauge ' .. (input .. ' ' or '') .. 'not in [[:Template:Track_gauge#List_of_defined_track_gauges|List of defined track gauges]] ([[Template talk:Track gauge|talk]]).' .. ' (This message is shown only in preview.)</div>'
sort1 = ' '
else
end

sort1 = args[1] .. ', '
if title:inNamespaces(0, 14) then -- mainspace and category space
end
errorTail = errorTail .. "[[Category:Articles using template 'Track gauge' with unrecognized input]]"
category = mw.ustring.format(
"[[Category:Articles using template 'Track gauge' with unrecognized input|%s%s]]",
sort1, title.text)
end
end
return (args[1] or '') .. category
return input .. errorTail
end
end


Line 303: Line 308:
local closeDisp = ''
local closeDisp = ''
if disp == 's' or disp == '/' then
if disp == 's' or disp == '/' then
joinText = '/&#x200b;'
joinText = '&nbsp;/ ' --spaces
elseif disp == 'br' then
elseif disp == 'br' then
joinText = '<br>('
joinText = '<br />('
closeDisp = ')'
closeDisp = ')'
elseif disp == '[' or disp == '[]' then
elseif disp == '[' or disp == '[]' then
Line 330: Line 335:
end
end
-- Alternative name
-- Alternative name
if args.al or args.allk ~= nil then
if args.al == 'on' or args.allk == 'on' then
local setNowrapAltname = (nowrap == '' or nowrap == 'on') -- Logic applied to prevent nested nowrap tags
local setNowrapAltname = (nowrap == '' or nowrap == 'on') -- Logic applied to prevent nested nowrap tags
table.insert(ret, formatAltName(tgEntry, args.allk == 'on', disp, setNowrapAltname, args.engvar))
table.insert(ret, formatAltName(tgEntry, args.al == 'on', args.allk == 'on', disp, setNowrapAltname, args.engvar))
end
end
-- Closing nowrap tag
-- Closing nowrap tag
Line 354: Line 359:
return p
return p
--20161219: maintenance categorisation switched off per CfD
--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