Модуль:Math/tonumber
Дакументацыю да гэтага модуля можна стварыць у Модуль:Math/tonumber/Дакументацыя
--[[
This module convert strings to numbers.
]]
local p = {}
-- Get first integer number from string.
function p.integer( frame )
local s = frame.args[1]
s = string.gsub( s, '<[^<>]+>', '' ) -- strip HTML tags
s = string.gsub( s, '[^0-9]', ' ' )
s = mw.text.trim( s )
s = mw.text.split( s, ' ' )[1]
return tonumber( s )
end
-- Get number from Wikidata quantity.
function p.quantity( frame )
local s = frame.args[1]
s = string.gsub( s, ' ', '' )
s = string.gsub( s, '±.*$', '' )
return tonumber( s )
end
-- Get year value from string.
function p.year( frame )
local n = nil
local cat = frame.args['cat']
-- 'году' or 'года'
local yearForm = 'годзе'
if frame.args['form'] and frame.args['form'] ~= '' then
yearForm = frame.args['form']
end
local s = frame.args[1]
s = string.gsub( s, '<[^<>]+>', '' ) -- strip HTML tags
local isBce = string.match( s, 'да%sн\.%s?э' )
s = string.gsub( s, 'год%sда%sн\.э\.?', '' )
s = string.gsub( s, 'да%sн\.э\.?', '' )
-- Get first 3- or 4-digit integer number from string.
local sParts = string.gsub( s, '[^0-9]', ' ' )
sParts = mw.text.trim( sParts )
sParts = mw.text.split( sParts, ' +' )
for k, v in pairs( sParts ) do
if string.match( v, '^[12]?%d%d%d$' ) then
n = tonumber( v )
break
end
end
-- The entire string is a number.
if not n then
s = string.gsub( s, '[%[%]]', '' )
s = mw.text.trim( s )
if string.match( s, '^%d%d?%d?%d?$' ) then
n = tonumber( s )
end
end
-- Generate category
if n then
if isBce then
if cat then
return '[[Катэгорыя:' .. cat .. ' ' .. n .. ' ' .. yearForm .. ' да н.э.]]'
end
n = -n
else
if cat then
return '[[Катэгорыя:' .. cat .. ' ' .. n .. ' ' .. yearForm .. ']]'
end
end
end
return n or frame.args['default']
end
return p