Actions

Module

Difference between revisions of "Parameters"

From Unofficial Stationeers Wiki

(Finalise compare)
(Split normalise fn)
Line 16: Line 16:
  
 
local parameters = {}
 
local parameters = {}
local paramIndex = {}
 
 
for parameter in string.gmatch(source, '{{{(.-)%f[}|<>]') do
 
for parameter in string.gmatch(source, '{{{(.-)%f[}|<>]') do
 
table.insert(parameters, transform(parameter))
 
table.insert(parameters, transform(parameter))
paramIndex[transform(parameter)] = parameter
 
 
end
 
end
parameters = removeDuplicates(parameters)
+
return removeDuplicates(parameters)
setmetatable(parameters, {__index = paramIndex})
 
 
 
return parameters
 
 
end
 
end
  
Line 36: Line 31:
 
function p._compare(args)
 
function p._compare(args)
 
local Set = require('Module:Set')
 
local Set = require('Module:Set')
 +
 +
local function normaliseParams(parameters)
 +
local paramsNorm = {}
 +
local paramIndex = {}
 +
for _, parameter in pairs(parameters) do
 +
table.insert(paramsNorm,
 +
string.lower(string.gsub(parameter, '%A', '')))
 +
paramIndex[
 +
string.lower(string.gsub(parameter, '%A', ''))] = parameter
 +
end
 +
 +
setmetatable(paramsNorm, {__index = paramIndex})
 +
return paramsNorm
 +
end
  
 
local baseParams = extractParams(args.base)
 
local baseParams = extractParams(args.base)
local baseNormParams = extractParams(args.base,
 
function(s) return string.lower(string.gsub(s, '%A', '')) end)
 
 
local otherParams = extractParams(args.other)
 
local otherParams = extractParams(args.other)
local otherNormParams = extractParams(args.other,
+
local baseNormParams = normaliseParams(Set.valueComplement(
function(s) return string.lower(string.gsub(s, '%A', '')) end)
+
baseParams, otherParams))
 +
local otherNormParams = normaliseParams(otherParams)
  
 
local similar = {}
 
local similar = {}
for _, v in pairs(Set.valueIntersection(Set.valueComplement(
+
for _, v in pairs(Set.valueIntersection(baseNormParams, otherNormParams)) do
baseParams, baseNormParams), otherNormParams)) do
 
 
table.insert(similar, string.format('%s (%s; %s)',
 
table.insert(similar, string.format('%s (%s; %s)',
 
v, baseNormParams[v], otherNormParams[v]))
 
v, baseNormParams[v], otherNormParams[v]))
 
end
 
end
 
 
return string.format([[Identical:
 
return string.format([[Identical:
 
* %s
 
* %s

Revision as of 08:01, 7 June 2015


-- This module implements [[Template:Parameters]].
-- [SublimeLinter luacheck-globals:mw]

local p = {}

local function makeInvokeFunction(funcName)
	return function(frame)
		local getArgs = require('Module:Arguments').getArgs
		return p[funcName](getArgs(frame))
	end
end

local function extractParams(source, transform)
	local removeDuplicates = require('Module:TableTools').removeDuplicates
	transform = transform or function(s) return s end

	local parameters = {}
	for parameter in string.gmatch(source, '{{{(.-)%f[}|<>]') do
		table.insert(parameters, transform(parameter))
	end
	return removeDuplicates(parameters)
end

function p._code(args)
	local title = args._base or mw.title.getCurrentTitle().baseText
	return string.format([[{{%s
%s}}]], title, table.concat(extractParams(args.base,
		function(s) return string.format('| %s = \n', s) end)))
end

function p._compare(args)
	local Set = require('Module:Set')

	local function normaliseParams(parameters)
		local paramsNorm = {}
		local paramIndex = {}
		for _, parameter in pairs(parameters) do
			table.insert(paramsNorm,
				string.lower(string.gsub(parameter, '%A', '')))
			paramIndex[
				string.lower(string.gsub(parameter, '%A', ''))] = parameter
		end

		setmetatable(paramsNorm, {__index = paramIndex})
		return paramsNorm
	end

	local baseParams = extractParams(args.base)
	local otherParams = extractParams(args.other)
	local baseNormParams = normaliseParams(Set.valueComplement(
			baseParams, otherParams))
	local otherNormParams = normaliseParams(otherParams)

	local similar = {}
	for _, v in pairs(Set.valueIntersection(baseNormParams, otherNormParams)) do
		table.insert(similar, string.format('%s (%s; %s)',
			v, baseNormParams[v], otherNormParams[v]))
	end
	return string.format([[Identical:
* %s

Similar:
* %s

Not matched:
* %s]],
		table.concat(Set.valueIntersection(baseParams, otherParams), '\n* '),
		table.concat(similar, '\n* '),
		table.concat(Set.valueComplement(otherNormParams,
			baseNormParams), '\n* '))
end

function p._demo(args)
	local title = args._base and ('|_template=' .. args._base) or ''
	return string.format('{{Parameter names example%s|%s}}', title,
		table.concat(extractParams(args.base), '|'))
end

function p._list(args)
	return table.concat(extractParams(args.base,
		function(s) return string.format('* %s\n', s) end))
end

p.code    = makeInvokeFunction('_code')
p.compare = makeInvokeFunction('_compare')
p.demo    = makeInvokeFunction('_demo')
p.list    = makeInvokeFunction('_list')

return p