Modul:Convert/helper
Qiyofa
Bu modul uchun Modul:Convert/helper/doc nomli hujjat sahifasini yaratishingiz mumkin
local function stripToNil(text)
if type(text) == 'string' then
return text:match('(%S.-)%s*$')
end
end
local function cleanNumber(frame)
local args = frame.args
local text = stripToNil(args[1]) or ''
if text == '' or tonumber(text) then
return text
end
return mw.text.killMarkers(text):gsub(',', '')
end
-- Format regular input with fraction (MOS-confirmant) into Convert-format "12+3/8" ("+" added).
-- First usage in Template:NFL_predraft (August 2017)
local function number(frame)
--[[ Preprocess a template parameter to translate a number to be used as
input for {{convert}}.
{{#invoke:convert/helper|number|12 3/8}} → 12+3/8
Input Output
12 12
12 3/8 12+3/8
{{frac|12|3|8}} 12+3/8
12{{frac|3|8}} 12+3/8
12⅜ 12+3/8
Template:Fraction redirects to Template:Frac so either may be used in the input.
]]
local args = frame.args
local text = stripToNil(args[1]) or ''
if text == '' or tonumber(text) then
return text -- examples: '', '12', '12.3', '12.3e4', or negative
end
text = text:gsub(' ', ' '):gsub(' +', ' '):gsub(' *%+ *', '+'):gsub('⁄', '/'):gsub('⁄', '/')
local integer, numerator, denominator, rest
integer, numerator, denominator = text:match('^(%d+)[ +](%d+)/(%d+)$')
if integer then
return integer .. '+' .. numerator .. '/' .. denominator
end
numerator, denominator = text:match('^(%d+)/(%d+)$')
if numerator then
return numerator .. '/' .. denominator
end
numerator, denominator = text:match('<sup>(%d+)</sup>/<sub>(%d+)</sub></span>')
if numerator then
integer = text:match('(%d+)<span class="sr-only">') or
text:match('^(%d+)%s*​<span') or -- Template:Frac outputs zwsp since December 2017
text:match('^(%d+)%s*<span')
return (integer and (integer .. '+') or '') .. numerator .. '/' .. denominator
end
local fractions = {
['½'] = '1/2',
['⅓'] = '1/3',
['⅔'] = '2/3',
['¼'] = '1/4',
['¾'] = '3/4',
['⅛'] = '1/8',
['⅜'] = '3/8',
['⅝'] = '5/8',
['⅞'] = '7/8',
}
integer, rest = text:match('^(%d*)%s*(.*)')
local expand = fractions[rest]
if expand then
return (integer == '' and integer or (integer .. '+')) .. expand
end
return text
end
return {
number = number,
cleanNumber = cleanNumber,
}