Documentation for this module may be created at ਮੌਡਿਊਲ:Fractions/doc

local p = {} --p stands for package

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')

function unicode_fraction(args)
	local numerator = args[1] or args.numerator or ''
	local denominator = args[2] or args.denominator or ''
	local frac = numerator .. '/' .. denominator
	
	local fractions = {
		['1/'] = '⅟',
		['1/2'] = '½',
		['0/3'] = '↉',
		['1/3'] = '⅓',
		['2/3'] = '⅔',
		['1/4'] = '¼',
		['3/4'] = '¾',
		['1/5'] = '⅕',
		['2/5'] = '⅖',
		['3/5'] = '⅗',
		['4/5'] = '⅘',
		['1/6'] = '⅙',
		['5/6'] = '⅚',
		['1/7'] = '⅐',
		['1/8'] = '⅛',
		['3/8'] = '⅜',
		['5/8'] = '⅝',
		['7/8'] = '⅞',
		['1/9'] = '⅑',
		['1/10'] = '⅒'
	}
	return fractions[frac]
end

function scripts_fraction(args)
	local numerator = args[1] or args.numerator or ''
	local denominator = args[2] or args.denominator or ''
	local wholenum = args[3] or args.wholenum
	
	local num_text = '<sup>' .. numerator .. '</sup>'
	local frasl = '&frasl;'
	local denom_text = '<sub>' .. denominator .. '</sub>'
	local frac = num_text .. frasl .. denom_text
	
	local frac_text
	if wholenum then
		local wholenum_text = wholenum .. '<s style="display:none">+</s>'
		frac_text = '<span style="white-space:nowrap">' .. wholenum_text .. '<span class="template-frac">' .. frac .. '</span></span>'
	else
		frac_text = '<span style="white-space:nowrap" class="template-frac">' .. frac .. '</span>'
	end
	
	return frac_text
end

function plain_fraction(args)
	local numerator = args[1] or args.numerator or ''
	local denominator = args[2] or args.denominator or ''
	return numerator .. '/' .. denominator
end

--[=[
Implements [[Template:Unifrac]]
]=]
function p.unifrac(frame)
	local args = getArgs(frame)
	
	local numerator = args[1] or args.numerator
	local denominator = args[2] or args.denominator
	local frac_args = {numerator, denominator}
	
	local plaintext = yesno(args.plaintext or 'no')
	local alt_frac
	if plaintext then
		alt_frac = plain_fraction(frac_args)
	else
		alt_frac = scripts_fraction(frac_args)
	end
	
	return unicode_fraction(frac_args) or alt_frac
end

--[=[
Implements [[Template:Frac]]
]=]
function p.frac(frame)
	local args = getArgs(frame)
	local script_args
	
	if args[1] and args[2] and args[3] then
		script_args = {
			numerator = args[2],
			denominator = args[3],
			wholenum = args[1]
		}
	elseif args[1] and args[2] then
		script_args = {
			numerator = args[1],
			denominator = args[2]
		}
	elseif args[1] then
		script_args = {
			numerator = '1',
			denominator = args[1]
		}
	else
		script_args = {
			numerator = '1',
			denominator = ''
		}
	end
	
	return scripts_fraction(script_args)
end

return p