Jump to content

Module:Sandbox/Frietjes

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 15:30, 19 May 2023. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local math_module = require("Module:Math")

local function rnd(num, digits)
	-- This function implements {{rnd}}
	return math_module._precision_format(tostring(num), digits)
end

local function clean(s)

	s = mw.ustring.gsub(s, 'metre', 'm')
	s = mw.ustring.gsub(s, '([^a])meter', '%1m') -- prevents "parameter" from being changed to "param"
	s = mw.ustring.gsub(s, 'centi', 'c') -- changes "centim" to "cm"
	s = mw.ustring.gsub(s, 'ms', 'm')
 	s = mw.ustring.gsub(s, 'm[%.,]', 'm')

	s = mw.ustring.gsub(s, 'feet', 'ft')
	s = mw.ustring.gsub(s, 'foot', 'ft')
	s = mw.ustring.gsub(s, 'ft[%.,]', 'ft')

	s = mw.ustring.gsub(s, 'inches', 'in')
	s = mw.ustring.gsub(s, 'inch', 'in')
	s = mw.ustring.gsub(s, 'ins', 'in')
	s = mw.ustring.gsub(s, 'in[%.,]', 'in')

	s = mw.ustring.gsub(s, '%[%[[Mm]%]%]s', '[[Metre|m]]')
	s = mw.ustring.gsub(s, '%[%[[Cc]m%]%]s', '[[Centimetre|cm]]')
	s = mw.ustring.gsub(s, '%[%[[Cc]entim|cm%]%]', '[[Centimetre|cm]]')
	s = mw.ustring.gsub(s, '%[%[[Ii]n|in%]%]', '[[inch|in]]')

	return s
end

local function isnumber(s)
	if s then
		s = mw.ustring.gsub(s, '%+%s*%d+%s*/%s*%d+%s*$', '')
		return tonumber(s)
	end
	return nil
end

local function get_convert_args(s, prefer, enforce, ri)
	local prefer_m = (prefer or '') == 'm'
	local force_m = (enforce or '') == 'm'
	local prefer_cm = (prefer or '') == 'cm'
	local force_cm = (enforce or '') == 'cm'
	
	ri = ri or '' -- make this more restricted?
	
	unconverted = clean(s or '') -- basic unit cleaning
	
	s = mw.ustring.gsub(unconverted, '&[Nn][Bb][Ss][Pp];', ' ')
	
	local m = mw.ustring.find(s, 'm')
	local c = mw.ustring.find(s, 'cm')
	local f = mw.ustring.find(s, 'ft')
	local i = mw.ustring.find(s, 'in')
	
	if m == nil and f == nil and i == nil then
		return '', unconverted
	end
	
	local adj = nil
	
	if c ~= nil and f == nil and i == nil then
		local n = mw.ustring.sub(s, 1, c - 1)
		if isnumber(n) then
			if force_m then
				local prec = require("Module:Math")._precision(n)
				adj = (prec == 1) and 'ri3' or 'ri2'
			end
			if ri ~= '' then
				adj = force_m and 'ri2' or 'ri0'
			end
			return force_m
				and {n/100,'m','ftin',0,['abbr']='on', ['adj']=adj}
				or {n,'cm','ftin',0,['abbr']='on', ['adj']=adj}, mw.ustring.sub(s, c+2)
		end
		return '', unconverted
	end
	
	if m ~= nil and c == nil and f == nil and i == nil then
		local n = mw.ustring.sub(s, 1, m - 1)
		if ri ~= '' then
			adj = force_cm and 'ri0' or 'ri2'
		end
		if isnumber(n) then
			return force_cm 
				and {n*100,'cm','ftin',0,['abbr']='on',['adj']=adj}
				or {n,'m','ftin',0,['abbr']='on',['adj']=adj}, mw.ustring.sub(s, m+1)
		end
		return '', unconverted
	end
	
	if f ~= nil and i ~=nil and m == nil then
		local n1 = mw.ustring.sub(s, 1, f - 1)
		local n2 = mw.ustring.sub(s, f+2, i - 1)
		if isnumber(n1) and isnumber(n2) then
			if ri ~= '' then
				adj = 'ri0'
			end
			return (force_m or prefer_m)
				and {n1,'ft',n2,'in', 'm',2,['abbr']='on',['adj']=adj}
				or {n1,'ft',n2,'in', 'cm',0,['abbr']='on',['adj']=adj}, mw.ustring.sub(s, i+2)
		end
		return '', unconverted
	end
	
	if f ~= nil and i == nil and m == nil then
		local n = mw.ustring.sub(s, 1, f - 1)
		if isnumber(n) then
			if ri ~= '' then
				local n2 = 12*(n - math.floor(n))
				adj = 'ri0'
				return (force_m or prefer_m)
					and {math.floor(n),'ft',n2, 'in','m',2,['abbr']='on',['adj']=adj}
					or {math.floor(n),'ft',n2,'in','cm',0,['abbr']='on',['adj']=adj}, mw.ustring.sub(s, f+2)
			end
			return (force_m or prefer_m)
				and {n,'ft','m',2,['abbr']='on'}
				or {n,'ft','cm',0,['abbr']='on'}, mw.ustring.sub(s, f+2)
		end
		return '', unconverted
	end
	
	if i ~= nil and f == nil and m == nil then
		local n = mw.ustring.sub(s, 1, i - 1)
		if isnumber(n) then
			if ri ~= '' then
				adj = 'ri0'
			end
			return (force_m or prefer_m)
				and {n,'in','m',2,['abbr']='on'}
				or {n,'in','cm',0,['abbr']='on'}, mw.ustring.sub(s, i+2)
		end
		return '', unconverted
	end
	
	return '', unconverted
end

function convert(frame, args)
	local targs, str = get_convert_args(args[1], args['prefer'] or '', args['enforce'] or '', args['ri'] or '')

	if type(targs) == 'table' then
		return targs
	else
		return str
	end
end

function p.cvt(frame)
	return convert(frame, frame.args[1] and frame.args or frame:getParent().args)
end

function p.wpct(frame)
	local w = tonumber(frame.args[1]) or 0
	local l = tonumber(frame.args[2]) or 0
	local pct = '–'
	if (w + l) > 0 then
		pct = rnd(w / (w + l), 3):gsub('^0', '')
	end
	return pct
end

function p.extractcolor(frame)
	local str = frame.args[1] or ''
	local color = mw.ustring.match(';' .. str .. ';', '.*;%s*([Cc][Oo][Ll][Oo][Rr]%s*:%s*.-)%s*;')
	return color or 'NO MATCH'
end

local function getBestStatement(item_id, property_id)
	if not(item_id) or not(mw.wikibase.isValidEntityId(item_id)) or not(mw.wikibase.entityExists(item_id)) then
		return false
	end
	local statements = mw.wikibase.getBestStatements(item_id, property_id)
	if not statements or #statements == 0 then
		return false
	end
	local hasNoValue = ( statements[1].mainsnak and statements[1].mainsnak.snaktype == 'novalue' )
	if hasNoValue then
		return false
	end
	return statements[1]
end

function p.hasOSM(frame)
	return getBestStatement(mw.wikibase.getEntityIdForCurrentPage(), 'P402') and 'yes' or 'no'
end

function p.main(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame, {parentFirst = true,
	valueFunc = function (key, val)
		if key == 'text_IPS' then
			return nil
		end
		if type(val) == 'string' then
			val = val:match('^%s*(.-)%s*$')
			if val == '' then
				return nil
			else
				return val
			end
		else
			return val
		end
	end
	})
	local team_list = {}
	local ii = 1
	while args['team'..ii] ~= nil do
		team_list[args['team'..ii]] = ii
		ii = ii + 1
	end
	local max_team = ii - 1
	
	local first_team, last_team = 1, max_team
	if args['showteam'] and team_list[args['showteam']] then
		first_team = team_list[args['showteam']] - 2
		last_team = first_team + 4
		if first_team < 1 then
			first_team = 1
			last_team = first_team + 4
		end
		if last_team > max_team then
			last_team = max_team
			first_team = max_team - 4
		end
		if first_team < 1 then first_team = 1 end
	end
		
	local hasnotes = false
	
	local ii = first_team
	local res = '{| class="wikitable"\n'
	res = res .. '! Pos. !! Team !! Result\n'
	while args['team'..ii] ~= nil and (ii <= last_team) do
		res = res .. '|-\n'
		res = res .. '| ' .. ii .. '\n'
		res = res .. '| ' .. (args['name_'..args['team' .. ii]] or '') .. '\n'
		local text_result = args['result'..ii] and args['text_'..args['result'..ii]] or ''
		local style_text = ''
		if text_result:match('fbmulticompefn') then
			hasnotes = true
			style_text = style_text .. 'padding:0;'
		end
		style_text = style_text .. (args['result'..ii] and ('background:' .. args['col_'..args['result'..ii]]) or '')
		res = res .. '| style="' .. style_text .. '" | ' .. text_result .. '\n'
		ii = ii + 1
	end
	res = res .. '|}'
	
	if hasnotes == true then
		res = res .. '<b>Table notes:</b>' .. frame:expandTemplate{ title = 'notelist'}
	end
	
	-- Generate tracking
	if not args['notracking'] then
		-- Step 1: Generate a team and result list
		for k,v in pairs(args) do
			-- nothing
		end
	end

	return res
end

return p