Module:Sandbox/trappist the monk/template compare

--

require('strict');

--[[--< T E M P L A T E _ P A R A M S _ G E T >

gets parameters used by template from unparsed template wikitext and adds each to param_list.

returns true on success; false when template does not exist

]]

local function template_params_get (template, param_list) local content = mw.title.new (template):getContent;						-- get unparsed wikitext from the template if not content then return false;															-- announce failure end

for param in content:gmatch ('{{{([^}|]+)') do								-- fetch each parameter in the template param_list[param] = true;												-- and add it to its list table end

return true;																-- announce success end

--[[--< D I F F _ L I S T _ M A K E >--

loop through source template's parameter list looking for parameters not also found in target's parameter list. when parameter is unique to source, add it to diff list with source template's name

returns nothing

]]

local function 	diff_list_make (src, tgt, src_name, diff) for param, _ in pairs (src) do												-- look for source template parameters in target template's parameter list if not tgt[param] then diff[param] = src_name;												-- not found add to diff table with name of source template end end end

--[[--< C O M P A R E >

module entry point

create a list of parameter names not shared between two templates

returns the list of unique parameter names

]]

local function compare (frame) local A = {};																-- table to hold the parameters used in the first of two templates local B = {};																-- table to hold the parameters used in the second of two templates local diff = {};															-- table to hold the parameters that are not shared between the two templates

local A_src = frame.args[1];												-- get template names local B_src = frame.args[2]; if not A_src:match ('^Template:') then										-- if namespace missing A_src = 'Template:' .. A_src;											-- add it	end

if not B_src:match ('^Template:') then										-- if namespace missing B_src = 'Template:' .. B_src;											-- add it	end

local ret_val = template_params_get (A_src, A);								-- get parameters from first template if not ret_val then return ' error: no ' .. A_src .. ' ';	end ret_val = template_params_get (B_src, B);									-- get parameters from second template if not ret_val then return ' error: no ' .. B_src .. ' ';	end

diff_list_make (A, B, A_src:gsub ('^Template:', ''), diff);					-- add parameters used in A that are not used in B; strip namespace for readability diff_list_make (B, A, B_src:gsub ('^Template:', ''), diff);					-- add parameters used in B that are not used in A

return mw.dumpObject (diff);												-- for now; prettify later end

--[[--< L I S T >--

module entry point

create a list of parameter names

returns the list of parameter names

]]

local function list (frame) local A = {};																-- table to hold the parameters used in the first of two templates

local A_src = frame.args[1];												-- get template names

if not A_src:match ('^Template:') then										-- if namespace missing A_src = 'Template:' .. A_src;											-- add it	end

local ret_val = template_params_get (A_src, A);								-- get parameters from first template if not ret_val then return ' error: no ' .. A_src .. ' ';	end return mw.dumpObject (A);													-- for now; prettify later end

----< E X P O R T E D  F U N C T I O N S >--

return {	compare = compare, list = list, }