Module:Key value table

From Wikisource
Jump to navigation Jump to search

local p = {} --p stands for package
local getArgs = require('Module:Arguments').getArgs

function p._sanity (frame)
	local x="label:data"
	local s=mw.text.split(x,':',plain)
	local t=mw.ustring.find(x,'label')
	return s[1]..s[2]..tostring(t)
end


function p._table( frame )
	local w={}
	local labels={}
	local class={}
	local data={}
	local args = getArgs(frame)
	local number=''
	local x=''
	local debug=''
	maxidx=0
	for _label, _data in pairs(args) do 
		x=x.._label..':'.._data..';';
		end
		--[[
 Iterate over the string x looking for 
  /;[a-z][0-4]:[^;];/
  if match to /dated([0-9]*)/ dated[n]= pattern found after next ':'' ; if n>maxIdx  maxIdx=n
  if match to /label([0-9]*)/ label[n] = pattern found after next ':' ; if n>maxIdx  maxIdx=n
  if match to /class([0-9]*)/ class[n] = pattern found after next ':' ; if n>maxIdx  maxIdx=n
  In the use case that led to this Module... label0 is Made  label1 is Laid before Parliament  label2 is Coming into force 
  --]]
  
  for z in mw.text.gsplit( x, ';', plain ) do
  w=mw.text.split(z,':',plain)
  	--[[ validate to format labelN,dataN,classN only. ]]--
  	if not (w[1]=='') then   
  		debug=debug.."label:"
  		debug=debug..w[1]..'\n\n'
  	  	debug=debug.."value:"..w[2]..'\n\n'
  		name=string.match(w[1],'[a-z]*'); debug=debug..'name:'..name..'\n\n'
  		local i,j=string.find(w[1],'[a-z]*'); debug=debug..'bounds:'..tostring(i)..':'..tostring(j)..'\n\n'
  		number=string.match(w[1],'[0-9]*',j+1); debug=debug..'number:'..number..'\n\n'
  		local k,l=string.find(w[1],'[0-9]*',j+1);debug=debug..'number-bounds:'..tostring(k)..tostring(l)..'\n\n'
  		suffix=string.match(w[1],'[a-z_]*',l+1) debug=debug..'suffix:'..suffix..'\n\n'
        local m,n=string.find(w[1],'[a-z_]*',l+1);debug=debug..'suffix-bounds:'..tostring(m)..tostring(n)..'\n\n'
        valid=false;
        if ((number=='') or number==nil) or not(suffix=='')  then 
        	valid=false; 
        	else
        		valid=true;
        	end
		if  valid==true then 
			debug=debug..'Valid number\n\n'
			if name=='label' then labels[tonumber(number)]=w[2]; debug=debug..'Label['..number..']'..labels[tonumber(number)]..'\n\n'  end
			if name=='data' then data[tonumber(number)]=w[2]; debug=debug..'data['..number..']'..data[tonumber(number)]..'\n\n' end
			if name=='class' then class[tonumber(number)]=w[2]; debug=debug..'class['..number..']'..class[tonumber(number)]..'\n\n' end
			n=tonumber(number);
			if maxidx<n then
				maxidx=n
				end
		end
  	end;
  end--[[ for loop --]]
  		
  
  _mytable=mw.html.create();
  _mytable:wikitext("{|");
  	if (args.class) then
  		_mytable:wikitext('class="'..args.class..'"')
  	end
  	_mytable:wikitext('\n')
  	
   	for n = 1,  maxidx do
   		if not (labels[n]==nil or labels[n]=='') then
   			valid=false;
   			if (n<=tonumber(args.defaults_idx) and (data[n]==nil or data[n]=='')) then 
   			else
   				_mytable:wikitext('|-');		
   				if not (class[n]==nil or class[n]=='') then
   					_mytable:wikitext(' class="'..class[n]..'"');
   					end
   					_mytable:wikitext('\n');
   					_mytable:wikitext('|'..labels[n]..'\n')
   					if not (data[n]==nil or data[n]=='') then
   						_mytable:wikitext('|'..data[n]..'\n');
   						end
   			end
   		end
   	end
   	_mytable:wikitext("|}");
    return tostring(_mytable);
end

return p