Actions

Module

Difference between revisions of "Parameters"

From Unofficial Stationeers Wiki

(h)
(This and that)
Line 12: Line 12:
 
end
 
end
  
local function extractParams(source, transform)
+
local function extractParams(source)
transform = transform or function(s) return s end
 
 
 
 
local parameters = {}
 
local parameters = {}
 
for parameter in string.gmatch(source, '{{{(.-)%f[}|<>]') do
 
for parameter in string.gmatch(source, '{{{(.-)%f[}|<>]') do
table.insert(parameters, transform(parameter))
+
table.insert(parameters, parameter)
 
end
 
end
 
return removeDuplicates(parameters)
 
return removeDuplicates(parameters)
 +
end
 +
 +
local function strMap(iter, transform)
 +
local returnTable = {}
 +
for _, v in pairs(iter) do
 +
table.insert(returnTable, transform(v))
 +
end
 +
return table.concat(returnTable)
 
end
 
end
  
Line 25: Line 31:
 
local title = args._base or mw.title.getCurrentTitle().baseText
 
local title = args._base or mw.title.getCurrentTitle().baseText
 
return string.format([[{{%s
 
return string.format([[{{%s
%s}}]], title, table.concat(extractParams(args.base,
+
%s}}]], title, strMap(extractParams(args.base),
function(s) return string.format('| %s = \n', s) end)))
+
function(s) return string.format('| %s = \n', s) end))
 
end
 
end
  
Line 52: Line 58:
 
local otherNormParams = normaliseParams(otherParams)
 
local otherNormParams = normaliseParams(otherParams)
  
local similar = {}
 
for _, v in pairs(Set.valueIntersection(baseNormParams, otherNormParams)) do
 
table.insert(similar, string.format('%s < %s (%s)',
 
baseNormParams[v], otherNormParams[v], v))
 
end
 
 
return string.format([[Identical:
 
return string.format([[Identical:
* %s
+
%s
 
 
 
Similar:
 
Similar:
* %s
+
%s
 
+
Disparate:
Not matched:
+
%s]],
* %s]],
+
strMap(Set.valueIntersection(baseParams, otherParams),
table.concat(Set.valueIntersection(baseParams, otherParams), '\n* '),
+
function(v) return string.format('* %s\n', v) end),
table.concat(similar, '\n* '),
+
strMap(Set.valueIntersection(baseNormParams, otherNormParams),
table.concat(Set.valueComplement(otherParams, baseParams), '\n* '))
+
function(v) return string.format('* %s < %s (%s)\n',
 +
baseNormParams[v], otherNormParams[v], v) end),
 +
strMap(Set.valueComplement(otherNormParams, baseNormParams),
 +
function(v) return string.format('* %s\n', baseNormParams[v]) end))
 
end
 
end
  
Line 77: Line 80:
  
 
function p._list(args)
 
function p._list(args)
return table.concat(extractParams(args.base,
+
return strMap(extractParams(args.base),
function(s) return string.format('* %s\n', s) end))
+
function(s) return string.format('* %s\n', s) end)
 
end
 
end
  

Revision as of 11:25, 7 June 2015


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

local p = {}
local removeDuplicates = require('Module:TableTools').removeDuplicates

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

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

local function strMap(iter, transform)
	local returnTable = {}
	for _, v in pairs(iter) do
		table.insert(returnTable, transform(v))
	end
	return table.concat(returnTable)
end

function p._code(args)
	local title = args._base or mw.title.getCurrentTitle().baseText
	return string.format([[{{%s
%s}}]], title, strMap(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, 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

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

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

	return string.format([[Identical:
%s
Similar:
%s
Disparate:
%s]],
	strMap(Set.valueIntersection(baseParams, otherParams),
		function(v) return string.format('* %s\n', v) end),
	strMap(Set.valueIntersection(baseNormParams, otherNormParams),
		function(v) return string.format('* %s < %s (%s)\n',
			baseNormParams[v], otherNormParams[v], v) end),
	strMap(Set.valueComplement(otherNormParams, baseNormParams),
		function(v) return string.format('* %s\n', baseNormParams[v]) end))
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 strMap(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