Difference between revisions of "Module:Convert/helper"

From blackwiki
Jump to navigation Jump to search
blackwiki>MusikAnimal
m (Protected "Module:Convert/helper": High-risk Lua module; 1,000+ transclusions ([Edit=Require autoconfirmed or confirmed access] (indefinite)))
m (8 revisions imported)
 
(4 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
-- This module is not used by Template:Convert or Module:Convert.
 
-- This module is not used by Template:Convert or Module:Convert.
  
local function strip_to_nil(text)
+
local function stripToNil(text)
 
-- If text is a non-empty string, return its trimmed content,
 
-- If text is a non-empty string, return its trimmed content,
 
-- otherwise return nothing (empty string or not a string).
 
-- otherwise return nothing (empty string or not a string).
Line 9: Line 9:
 
return text:match('(%S.-)%s*$')
 
return text:match('(%S.-)%s*$')
 
end
 
end
 +
end
 +
 +
-- Remove commas and references (any strip markers) from a number.
 +
-- First usage in Template:Infobox_UK_place/dist (June 2018)
 +
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
 
end
  
Line 26: Line 37:
 
]]
 
]]
 
local args = frame.args
 
local args = frame.args
local text = strip_to_nil(args[1]) or ''
+
local text = stripToNil(args[1]) or ''
 
if text == '' or tonumber(text) then
 
if text == '' or tonumber(text) then
 
return text  -- examples: '', '12', '12.3', '12.3e4', or negative
 
return text  -- examples: '', '12', '12.3', '12.3e4', or negative
 
end
 
end
text = text:gsub(' ', ' '):gsub('  +', ' '):gsub('⁄', '/'):gsub('⁄', '/')
+
text = text:gsub(' ', ' '):gsub('  +', ' '):gsub(' *%+ *', '+'):gsub('⁄', '/'):gsub('⁄', '/')
 
local integer, numerator, denominator, rest
 
local integer, numerator, denominator, rest
-- Look for a fraction of form '12 3/8' or '3/8'.
+
-- Look for a fraction of form '12 3/8' or '12+3/8' or '3/8'.
integer, numerator, denominator = text:match('^(%d+) (%d+)/(%d+)$')
+
integer, numerator, denominator = text:match('^(%d+)[ +](%d+)/(%d+)$')
 
if integer then
 
if integer then
 
return integer .. '+' .. numerator .. '/' .. denominator
 
return integer .. '+' .. numerator .. '/' .. denominator
Line 44: Line 55:
 
numerator, denominator = text:match('<sup>(%d+)</sup>/<sub>(%d+)</sub></span>')
 
numerator, denominator = text:match('<sup>(%d+)</sup>/<sub>(%d+)</sub></span>')
 
if numerator then
 
if numerator then
integer = text:match('(%d+)<span class="visualhide">') or text:match('^(%d+)%s*<span')
+
integer = text:match('(%d+)<span class="visualhide">') or
 +
text:match('^(%d+)%s*&#x200B;<span') or  -- Template:Frac outputs zwsp since December 2017
 +
text:match('^(%d+)%s*<span')
 
return (integer and (integer .. '+') or '') .. numerator .. '/' .. denominator
 
return (integer and (integer .. '+') or '') .. numerator .. '/' .. denominator
 
end
 
end
 
-- Look for a fraction of form '12¾' or '¾'.
 
-- Look for a fraction of form '12¾' or '¾'.
 
local fractions = {
 
local fractions = {
{ '½', '1/2' },
+
['½'] = '1/2',
{ '⅓', '1/3' },
+
['⅓'] = '1/3',
{ '⅔', '2/3' },
+
['⅔'] = '2/3',
{ '¼', '1/4' },
+
['¼'] = '1/4',
{ '¾', '3/4' },
+
['¾'] = '3/4',
{ '⅛', '1/8' },
+
['⅛'] = '1/8',
{ '⅜', '3/8' },
+
['⅜'] = '3/8',
{ '⅝', '5/8' },
+
['⅝'] = '5/8',
{ '⅞', '7/8' },
+
['⅞'] = '7/8',
 
}
 
}
 
integer, rest = text:match('^(%d*)%s*(.*)')
 
integer, rest = text:match('^(%d*)%s*(.*)')
for _, frac in ipairs(fractions) do
+
local expand = fractions[rest]
if rest == frac[1] then
+
if expand then
return (integer == '' and integer or (integer .. '+')) .. frac[2]
+
return (integer == '' and integer or (integer .. '+')) .. expand
end
 
 
end
 
end
 
return text
 
return text
Line 70: Line 82:
 
return {
 
return {
 
number = number,
 
number = number,
 +
cleanNumber = cleanNumber,
 
}
 
}

Latest revision as of 15:52, 26 September 2020

This module is not used by module:Convert. It can be used in templates to preprocess input from regular written text into formatted input. For example:

{{convert|{{#invoke:Convert/helper |number |1=15 4/3 }}|...}} → {{convert|15+4/3|...}}

Usage

Applied in {{NFL predraft}}.



-- This module provides some functions to prepare template parameters
-- for use with Template:Convert.
-- This module is not used by Template:Convert or Module:Convert.

local function stripToNil(text)
	-- If text is a non-empty string, return its trimmed content,
	-- otherwise return nothing (empty string or not a string).
	if type(text) == 'string' then
		return text:match('(%S.-)%s*$')
	end
end

-- Remove commas and references (any strip markers) from a number.
-- First usage in Template:Infobox_UK_place/dist (June 2018)
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('&nbsp;', ' '):gsub('  +', ' '):gsub(' *%+ *', '+'):gsub('&frasl;', '/'):gsub('⁄', '/')
	local integer, numerator, denominator, rest
	-- Look for a fraction of form '12 3/8' or '12+3/8' or '3/8'.
	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
	-- Look for an expanded fraction such as the result of {{frac|12|3|8}} or 12{{frac|3|8}} or {{frac|3|8}}.
	numerator, denominator = text:match('<sup>(%d+)</sup>/<sub>(%d+)</sub></span>')
	if numerator then
		integer = text:match('(%d+)<span class="visualhide">') or
			text:match('^(%d+)%s*&#x200B;<span') or  -- Template:Frac outputs zwsp since December 2017
			text:match('^(%d+)%s*<span')
		return (integer and (integer .. '+') or '') .. numerator .. '/' .. denominator
	end
	-- Look for a fraction of form '12¾' or '¾'.
	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,
}