Actions

Module

Editing Parameters

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
 
-- This module implements [[Template:Parameters]].
 
-- This module implements [[Template:Parameters]].
 
-- [SublimeLinter luacheck-globals:mw]
 
-- [SublimeLinter luacheck-globals:mw]
 
local DEFINITIONS = {
 
alt = {
 
code = '<!-- text alternative for image; see WP:ALT -->',
 
dlist = 'text alternative for image; see [[WP:ALT]]'},
 
coordinates = {
 
code = '<!-- use {{Coord}} -->',
 
dlist = 'using {{tl|Coord}}'},
 
coords = {
 
code = '<!-- use {{Coord}} -->',
 
dlist = 'using {{tl|Coord}}'},
 
native_name = {
 
code = '<!-- name in local language; if more than one, separate ' ..
 
'using {{Plainlist}} use {{lang}}, and omit native_name_lang -->',
 
dlist = 'name in local language; if more than one, separate ' ..
 
'using {{tl|Plainlist}}, use {{tl|lang}}, and omit {{para|native_name_lang}}'},
 
native_name_lang = {
 
code = '<!-- language two- or three-letter ISO code -->',
 
dlist = 'language two- or three-letter ISO code'},
 
start_date = {
 
code = '<!-- {{Start date|YYYY|MM|DD|df=y}} -->',
 
dlist = 'use {{tlx|Start date|YYYY|MM|DD|df=y}}'},
 
end_date = {
 
code = '<!-- {{End date|YYYY|MM|DD|df=y}} -->',
 
dlist = 'use {{tlx|Start date|YYYY|MM|DD|df=y}}'},
 
url = {
 
code = '<!-- use {{URL|example.com}} -->',
 
dlist = 'using {{tl|URL}}'},
 
website = {
 
code = '<!-- use {{URL|example.com}} -->',
 
dlist = 'using {{tls|URL|example.com}}'},}
 
  
 
local p = {}
 
local p = {}
 
local removeDuplicates = require('Module:TableTools').removeDuplicates
 
local removeDuplicates = require('Module:TableTools').removeDuplicates
local yesno = require('Module:Yesno')
 
  
 
local function makeInvokeFunction(funcName)
 
local function makeInvokeFunction(funcName)
 
return function(frame)
 
return function(frame)
 
local getArgs = require('Module:Arguments').getArgs
 
local getArgs = require('Module:Arguments').getArgs
return p[funcName](getArgs(frame, {removeBlanks = false}))
+
return p[funcName](getArgs(frame))
 
end
 
end
 
end
 
end
  
local function extractParams(page)
+
local function extractParams(source, transform)
local source = mw.title.new(page, 'Template'):getContent()
+
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, parameter)
+
table.insert(parameters, transform(parameter))
 
end
 
end
 
return removeDuplicates(parameters)
 
return removeDuplicates(parameters)
end
 
 
local function map(tbl, transform)
 
local returnTable = {}
 
for k, v in pairs(tbl) do
 
returnTable[k] = transform(v)
 
end
 
return returnTable
 
end
 
 
local function strMap(tbl, transform)
 
local returnTable = map(tbl, transform)
 
return table.concat(returnTable)
 
end
 
 
function p._check(args)
 
local title = args._base or mw.title.getCurrentTitle().fullText
 
return string.format(
 
'{{#invoke:Check for unknown parameters|check|unknown=' ..
 
'[[Category:Pages using %s with unknown parameters]]|%s}}', title,
 
table.concat(extractParams(args.base), '|'))
 
 
end
 
end
  
 
function p._code(args)
 
function p._code(args)
local definitions = yesno(args.definitions)
 
local pad = yesno(args.pad)
 
 
local parameters = extractParams(args.base)
 
-- Space-pad the parameters to align the equal signs vertically
 
if pad then
 
local lengthPerPara = map(parameters, function (parameter)
 
return string.len(parameter) end)
 
-- Lua doesn't support printf's <*> to specify the width, apparently
 
local fs = string.format('%%-%ss', math.max(unpack(lengthPerPara)))
 
for i, parameter in pairs(parameters) do
 
parameters[i] = string.format(fs, parameter)
 
end
 
end
 
 
 
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, strMap(parameters,
+
%s}}]], title, table.concat(extractParams(args.base,
function(s)
+
function(s) return string.format('| %s = \n', s) end)))
if definitions then
 
return string.format('| %s = %s\n', s,
 
DEFINITIONS[s] and DEFINITIONS[s].code or '')
 
else
 
return string.format('| %s = \n', s)
 
end
 
end))
 
 
end
 
end
  
Line 108: Line 33:
  
 
local function normaliseParams(parameters)
 
local function normaliseParams(parameters)
local paramsNorm = {}
+
local paramsNorm, paramIndex = {}, {}
-- Prepare a key lookup metatable, which will hold the original
 
-- parameter names for each normalised parameter, e.g.
 
-- [test] = {TEST, Test}. paramIndex functions like a Python
 
-- defaultdict, where the default is a table.
 
local paramIndex = setmetatable({}, {__index = function(t, k)
 
if not rawget(t, k) then
 
rawset(t, k, {})
 
end
 
return rawget(t, k)
 
end})
 
 
for _, parameter in pairs(parameters) do
 
for _, parameter in pairs(parameters) do
 
table.insert(paramsNorm,
 
table.insert(paramsNorm,
 
string.lower(string.gsub(parameter, '%A', '')))
 
string.lower(string.gsub(parameter, '%A', '')))
table.insert(paramIndex[
+
paramIndex[
string.lower(string.gsub(parameter, '%A', ''))], parameter)
+
string.lower(string.gsub(parameter, '%A', ''))] = parameter
 
end
 
end
  
 
paramsNorm = removeDuplicates(paramsNorm)
 
paramsNorm = removeDuplicates(paramsNorm)
-- Overload key lookup in paramsNorm. While [[Module:Set]] will
 
-- operate on the table (which is to say, the normalised parameters
 
-- array), key access will be by way of the paramIndex metatable.
 
 
setmetatable(paramsNorm, {__index = paramIndex})
 
setmetatable(paramsNorm, {__index = paramIndex})
 
return paramsNorm
 
return paramsNorm
Line 140: Line 52:
 
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:
+
 
%s]],
+
Not matched:
strMap(Set.valueIntersection(baseParams, otherParams),
+
* %s]],
function(v) return string.format('* %s\n', v) end),
+
table.concat(Set.valueIntersection(baseParams, otherParams), '\n* '),
strMap(Set.valueIntersection(baseNormParams, otherNormParams),
+
table.concat(similar, '\n* '),
function(v) return string.format('* %s < %s [%s]\n',
+
table.concat(Set.valueComplement(otherParams, baseParams), '\n* '))
table.concat(baseNormParams[v], '; '),
 
table.concat(otherNormParams[v], '; '),
 
v)
 
end),
 
strMap(Set.valueComplement(otherNormParams, baseNormParams),
 
function(v) return strMap(baseNormParams[v],
 
function(s) return string.format('* %s\n', s) end)
 
end))
 
 
end
 
end
  
Line 164: Line 74:
 
return string.format('{{Parameter names example%s|%s}}', title,
 
return string.format('{{Parameter names example%s|%s}}', title,
 
table.concat(extractParams(args.base), '|'))
 
table.concat(extractParams(args.base), '|'))
end
 
 
function p._dlist(args)
 
local definitions = yesno(args.definitions, true)
 
return strMap(extractParams(args.base),
 
function(s)
 
if definitions then
 
return string.format('; %s: %s\n', s,
 
DEFINITIONS[s] and DEFINITIONS[s].dlist or '')
 
else
 
return string.format('; %s: \n', s)
 
end
 
end)
 
 
end
 
end
  
 
function p._list(args)
 
function p._list(args)
return strMap(extractParams(args.base),
+
return table.concat(extractParams(args.base,
function(s) return string.format('* %s\n', s) end)
+
function(s) return string.format('* %s\n', s) end))
 
end
 
end
  
p.check = makeInvokeFunction('_check')
 
 
p.code = makeInvokeFunction('_code')
 
p.code = makeInvokeFunction('_code')
 
p.compare = makeInvokeFunction('_compare')
 
p.compare = makeInvokeFunction('_compare')
 
p.demo = makeInvokeFunction('_demo')
 
p.demo = makeInvokeFunction('_demo')
p.dlist = makeInvokeFunction('_dlist')
 
 
p.list = makeInvokeFunction('_list')
 
p.list = makeInvokeFunction('_list')
  
 
return p
 
return p

Please note that all contributions to Unofficial Stationeers Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Unofficial Stationeers Wiki:Copyrights for details). Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel | Editing help (opens in new window)