Module:Track gauge: Difference between revisions
Content added Content deleted
Vivaporius (talk | contribs) 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>⁄<sub>%s</sub></span>', |
'<span class="frac">%s%s<sup>%s</sup>⁄<sub>%s</sub></span>', |
||
whole or '', whole and '<span class=" |
whole or '', whole and (templatestyles .. '<span class="sr-only"> </span>') or '', |
||
num, den) |
num, den) |
||
end |
end |
||
Line 212: | Line 215: | ||
-- formatAltName |
-- formatAltName |
||
----------------------------------------------------------------------------------- |
----------------------------------------------------------------------------------- |
||
function formatAltName(tgEntry, |
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 |
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 |
||
table.insert(retAlt, tgEntry.link or tgEntry.name) |
|||
⚫ | |||
⚫ | |||
else -- so must be unlinked .name to add |
|||
⚫ | |||
if engvar == 'en-us' then |
|||
⚫ | |||
-- Current implementations (2016): metER for metRE (1000-met, 1009-met) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
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 |
-- Categorise & preview warning when no track gauge definition was found. |
||
if tgEntry == nil then |
if tgEntry == nil then |
||
local |
local input = args[1] or '' |
||
local errorTail = '' |
|||
⚫ | |||
local sort1 |
|||
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 = ' ' |
|||
end |
|||
sort1 = args[1] .. ', ' |
|||
⚫ | |||
end |
|||
⚫ | |||
category = mw.ustring.format( |
|||
⚫ | |||
sort1, title.text) |
|||
end |
end |
||
return |
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 = ' |
joinText = ' / ' --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 |
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 |