پودمان:Change

از ویکی‌پدیا، دانشنامهٔ آزاد
توضیحات پودمان[ایجاد] [پاکسازی]
-- This implements {{change}}
local p = {}

local function ntsh(outvalue)
	local sortkey = '0000000000000000000'
	if outvalue == nil then
		return '<span style="display:none" data-sort-value="' .. sortkey .. '♠"></span>'
	end
	
	-- This code was copied from [[Template:Convert]]	
	if outvalue == 0 then
		sortkey = '5000000000000000000'
	else
		local mag = math.floor(math.log10(math.abs(outvalue)) + 1e-14)
		local prefix
		if outvalue > 0 then
			prefix = 7000 + mag
		else
			prefix = 2999 - mag
			outvalue = outvalue + 10^(mag+1)
		end
		sortkey = mw.ustring.format('%d', prefix) .. mw.ustring.format('%015.0f', math.floor(outvalue * 10^(14-mag)))
	end
	return '<span style="display:none" data-sort-value="' .. sortkey .. '♠"></span>'
end

local function trim(s)
	return mw.ustring.match(s, '^%s*(.-)%s*$')
end

local function isnotempty(s)
	return s and trim(s) ~= ''
end

function change(args)
	
	local yesNo = require('Module:Yesno')
	local numConv = require('Module:Numeral converter').convert
	local leftRight = require('Module:Leftright').leftRight
	
	-- Localized Persian parameters
	if args['اعشار'] then args['dec'] = numConv('en', args['اعشار']) end
	if args['پیشوند'] then args['pre'] = args['پیشوند'] end
	if args['پیشوند۱'] then args['pre1'] = args['پیشوند۱'] end
	if args['پیشوند۲'] then args['pre2'] = args['پیشوند۲'] end
	if args['پسوند'] then args['suf'] = args['پسوند'] end
	if args['پسوند۱'] then args['suf1'] = args['پسوند۱'] end
	if args['پسوند۲'] then args['suf2'] = args['پسوند۲'] end
	if args['ترتیب'] then args['sort'] = args['ترتیب'] end
	if args['ایتالیک'] then args['italics'] = yesNo(args['ایتالیک']) end
	if args['کج'] then args['italics'] = yesNo(args['کج']) end
	if args['پررنگ'] then args['bold'] = yesNo(args['پررنگ']) end
	if args['معکوس'] then args['invert'] = args['معکوس'] end
	if args['تراز'] then args['align'] = leftRight(args['تراز'], 'left') end
	if args['طول ردیف'] then args['rowspan'] = numConv('en', args['طول ردیف']) end
	if args['پس‌زمینه'] then args['bgcolour'] = args['پس‌زمینه'] end
	if args['نمایش'] then args['disp'] = args['نمایش'] end
	
	local rnd = require('Module:Math')._round
	local prec_format = require('Module:Math')._precision_format
	local lang = mw.getContentLanguage()
	local function formatnum(num)
		return lang:parseFormattedNumber(num) and lang:formatNum(lang:parseFormattedNumber(num)) or num
	end
	
	local errorflag = nil
	
	local dsp = args['disp'] or 'row'
	local inv = args['invert'] or 'off'
	local srt = yesNo(args['sort']) or ((isnotempty(args['pre']) or isnotempty(args['sort'])) and true or false)
	local n1 = yesNo(inv) and tonumber(lang:parseFormattedNumber(args['2'])) or tonumber(lang:parseFormattedNumber(args['1']))
	local n2 = yesNo(inv) and tonumber(lang:parseFormattedNumber(args['1'])) or tonumber(lang:parseFormattedNumber(args['2']))
	local dec = tonumber(numConv('en', args['dec'])) or '2'
	local s = (yesNo((args['italics'] or 'off')) and "''" or "")
			.. (yesNo((args['bold'] or 'off')) and "'''" or "")
	
	local pc, pcr, pcrf = 'ن/م', 'ن/م'
	
	if n1 and n2 and n1 ~= 0 then
		pc = 100*(n2/n1 - 1)
		pcr = rnd(pc, dec)
		if pcr > 0 then
			pcrf = '<span style="color:green">' ..
				s .. '+' .. prec_format(pc, dec) .. '٪' .. s .. '</span>'
		elseif pcr < 0 then
			pcrf = '<span style="color:red">' ..
				s .. prec_format(pc, dec) .. '٪' .. s .. '</span>'
		else
			pcrf = s .. prec_format(0, dec) .. '٪' .. s
		end
		pcrf = ntsh(pcr) .. pcrf
	else
		pcrf = ntsh(nil) .. s .. 'ن/م' .. s
		if n1 == nil or n2 == nil then
			errorflag = 1
		end
	end
	
	if dsp == 'out' or dsp == 'خروجی' then
		return pcrf, errorflag
	else
		local pre1 = args['pre1'] or args['pre'] or ''
		local pre2 = args['pre2'] or args['pre'] or ''
		local suf1 = args['suf1'] or args['suf'] or ''
		local suf2 = args['suf2'] or args['suf'] or ''

		local rspn = 'rowspan=' .. (args['rowspan'] or '1') .. ' '
		local algn = 'text-align:' .. (args['align'] or 'left') .. ';'
		local bg = 'background-color:' .. (args['bgcolour'] or args['bgcolor'] or 'inherit') .. ';'

		if rspn == 'rowspan=1 ' then rspn = '' end
		if bg == 'background-color:inherit;' then bg = '' end
		local style = rspn .. 'style="' .. algn .. bg .. '"'
		
		local sk1, sk2 = '', ''
		
		if srt then
			sk1 = ntsh(n1)
			sk2 = ntsh(n2)
		end
		
		if n1 ~= nil then
			if n1 < 0 then
				n1 = '−' .. formatnum(-1*n1)
			else
				n1 = formatnum(n1)
			end
		else
			n1 = yesNo(inv) and (args['2'] or '') or (args['1'] or '')
		end
		
		if n2 ~= nil then
			if n2 < 0 then
				n2 = '−' .. formatnum(-1*n2)
			else
				n2 = formatnum(n2)
			end
		else
			n2 = yesNo(inv) and (args['1'] or '') or (args['2'] or '')
		end
		
		if dsp == 'row2' or dsp == 'ردیف۲' or dsp == 'ردیف ۲' then
			return style .. '|' .. s .. pre2 .. n2 .. suf2 .. s
				.. '\n|' .. style .. '|' .. pcrf, errorflag
		else
			if not (yesNo(inv)) then
				return style .. '|' .. s .. pre1 .. n1 .. suf1 .. s
					.. '\n|' .. style .. '|' .. s .. pre2 .. n2 .. suf2 .. s
					.. '\n|' .. style .. '|' .. pcrf, errorflag
			else
				return style .. '|' .. s .. pre1 .. n2 .. suf1 .. s
					.. '\n|' .. style .. '|' .. s .. pre2 .. n1 .. suf2 .. s
					.. '\n|' .. style .. '|' .. pcrf, errorflag
			end
		end
	end
end

function p.main(frame)
	local res, eflag = change((frame.args[1] or frame.args[2]) and frame.args or frame:getParent().args)
	if eflag then
		res = res .. frame:expandTemplate{title = 'تغییر/خطا'}
	end
	return res
end

return p