Difference between revisions of "Parameters"
From Unofficial Stationeers Wiki
(Code output) |
(Testing compare fn) |
||
| Line 11: | Line 11: | ||
end | end | ||
| − | local function extractParams( | + | local function extractParams(source, transform) |
local removeDuplicates = require('Module:TableTools').removeDuplicates | local removeDuplicates = require('Module:TableTools').removeDuplicates | ||
| − | transform = transform or | + | transform = transform or function(s) return s end |
local parameters = {} | local parameters = {} | ||
| − | for parameter in string.gmatch( | + | for parameter in string.gmatch(source, '{{{(.-)%f[}|<>]') do |
| − | table.insert(parameters, | + | table.insert(parameters, transform(parameter)) |
end | end | ||
return removeDuplicates(parameters) | return removeDuplicates(parameters) | ||
| Line 23: | Line 23: | ||
function p._code(args) | function p._code(args) | ||
| + | local title = args._base or mw.title.getCurrentTitle().baseText | ||
return string.format([[{{%s | return string.format([[{{%s | ||
| − | %s}}]], | + | %s}}]], title, table.concat(extractParams(args.base, |
| − | (args. | + | function(s) return string.format('| %s = \n', s) end))) |
| − | + | end | |
| + | |||
| + | function p._compare(args) | ||
| + | local Set = require('Module:Set') | ||
| + | |||
| + | local baseParams = extractParams(args.base, | ||
| + | function(s) return string.format('* %s\n', s) end) | ||
| + | local baseNormParams = extractParams(args.base, | ||
| + | function(s) return string.format('* %s\n', | ||
| + | string.gsub(string.lower(s), '%A', '')) end) | ||
| + | local otherParams = extractParams(args.other, | ||
| + | function(s) return string.format('* %s\n', s) end) | ||
| + | local otherNormParams = extractParams(args.other, | ||
| + | function(s) return string.format('* %s\n', | ||
| + | string.gsub(string.lower(s), '%A', '')) end) | ||
| + | |||
| + | return string.format([[Identical: | ||
| + | %s | ||
| + | Similar: | ||
| + | %s | ||
| + | Not matched: | ||
| + | %s]], | ||
| + | table.concat(Set.valueIntersection(baseParams, otherParams)), | ||
| + | table.concat(Set.valueIntersection( | ||
| + | Set.valueComplement(baseParams, baseNormParams), otherNormParams)), | ||
| + | table.concat(Set.valueComplement(otherNormParams, baseNormParams))) | ||
end | end | ||
function p._demo(args) | function p._demo(args) | ||
| − | return string.format('{{Parameter names example | + | local title = args._base and ('|_template=' .. args._base) or '' |
| − | + | return string.format('{{Parameter names example%s|%s}}', title, | |
| + | table.concat(extractParams(args.base), '|')) | ||
end | end | ||
function p._list(args) | function p._list(args) | ||
| − | return table.concat(extractParams(args, '* %s\n')) | + | return table.concat(extractParams(args.base, |
| + | function(s) return string.format('* %s\n', s) end)) | ||
end | end | ||
| − | p.code = makeInvokeFunction('_code') | + | p.code = makeInvokeFunction('_code') |
| − | p.demo = makeInvokeFunction('_demo') | + | p.compare = makeInvokeFunction('_compare') |
| − | p.list = makeInvokeFunction('_list') | + | p.demo = makeInvokeFunction('_demo') |
| + | p.list = makeInvokeFunction('_list') | ||
return p | return p | ||
Revision as of 18:03, 6 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 baseParams = extractParams(args.base,
function(s) return string.format('* %s\n', s) end)
local baseNormParams = extractParams(args.base,
function(s) return string.format('* %s\n',
string.gsub(string.lower(s), '%A', '')) end)
local otherParams = extractParams(args.other,
function(s) return string.format('* %s\n', s) end)
local otherNormParams = extractParams(args.other,
function(s) return string.format('* %s\n',
string.gsub(string.lower(s), '%A', '')) end)
return string.format([[Identical:
%s
Similar:
%s
Not matched:
%s]],
table.concat(Set.valueIntersection(baseParams, otherParams)),
table.concat(Set.valueIntersection(
Set.valueComplement(baseParams, baseNormParams), otherNormParams)),
table.concat(Set.valueComplement(otherNormParams, baseNormParams)))
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
