Module:Number table sorting
Appearance
![]() | This Lua module is used on approximately 41,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
![]() | This module depends on the following other modules: |
Implements {{Number table sorting}}
local lang = mw.language.getContentLanguage()
-- constants
--------------------------------------------------------------------------------
-- Nts class
--------------------------------------------------------------------------------
local Nts = {}
Nts.__index = Nts
Nts.formats = {
formatted = true,
no = true,
hide = true
}
function Nts.new(args)
local self = setmetatable({}, Nts)
self:parseNumber(args[1])
-- Set the format string
if args.format then
self.format = args.format
else
self.format = 'formatted'
end
if not Nts.formats[self.format] then
error(string.format(
"'%s' is not a valid format",
tostring(self.format)
), 0)
end
-- To display some text before the display version of the number
-- {{nts|123456789.00123|prefix=approx. }} → approx. 123,456,789.00123
if args.prefix then
self.prefix = args.prefix
end
return self
end
-- Parse the entered number
function Nts:parseNumber(s)
s = mw.ustring.gsub(s,'−','-')
s = mw.ustring.gsub(s, '−', '-')
self.number = tonumber(s)
if not self.number then
error(string.format(
"'%s' is not a valid number",
tostring(s)
), 0)
end
end
function Nts:makeDisplay()
if self.format == 'hide' then
return ''
end
return self.number
end
function Nts:makeSortkey()
return '5000000000000000000'
end
function Nts:renderTrackingCategories()
if self.hasDeprecatedParameters then
return '[[Category:Nts templates with deprecated parameters]]'
else
return ''
end
end
function Nts:__tostring()
local root = mw.html.create()
local span = root:tag('span')
:attr('data-sort-value', self:makeSortKey())
-- Display
if self.format ~= 'hide' then
span:wikitext(self:makeDisplay())
else
-- tidy removes empty spans. Keep nbsp content till remexhtml is deployed
span:css('display', 'none')
:wikitext(' ')
end
-- Tracking categories
root:wikitext(self:renderTrackingCategories())
return tostring(root)
end
--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------
local p = {}
function p._exportClasses()
return {
Nts = Nts
}
end
function p._main(args)
local success, ret = pcall(function ()
local nts = Nts.new(args)
return tostring(nts)
end)
if success then
return ret
else
ret = string.format(
'<strong class="error">Error in [[Template:Nts]]: %s</strong>',
ret
)
if mw.title.getCurrentTitle().namespace == 0 then
-- Only categorise in the main namespace
ret = ret .. '[[Category:Nts templates with errors]]'
end
return ret
end
end
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = { 'Template:Nts', 'Template:Ntsh' },
})
return p._main(args)
end
return p