Modul für Funktionalitäten rund um die Vorlage:Schachbrett

Das Modul ist noch in der Aufbauphase. Vorhandene Funktionen:


local function trim(text)
	text = mw.ustring.gsub(text,"\n","");
	while mw.ustring.sub(text,1,1) == " " do
		text=  mw.ustring.sub(text,2,-1);
	end
	while mw.ustring.sub(text,-1,-1) == " " do
		text=  mw.ustring.sub(text,1,-2);
	end
	return text;
end

local function checkcode(st)
	local legal = {'bd','bl','da','db','dl','dr','dw','kd','kl','la','lr','nd','nl','oo','ox','pd','pl','qd','ql','ra','rd','rl','ua','ud','ul','ur','xo','xx'}
	for i,v in ipairs(legal) do
		if st == v then
	   		return st
   		end
   end
   return '--'
end

local function GetWikiRow(aFen)
	local aLine = '';
	local aChar='';
	-- Aktuelle Austauschliste:
	local replace  = { ['-']='--',
				['b']='bd', ['k']='kd', ['n']='nd', ['p']='pd', ['q']='qd', ['r']='rd',
				['B']='bl', ['K']='kl', ['N']='nl', ['P']='pl', ['Q']='ql', ['R']='rl'}
	for i = 1,8 do
		aChar = string.sub(aFen,i,i);
		aLine = aLine .. replace[aChar] .. '/';
	end
	return aLine;
end

local function SplitFenLine(str)
	local Rows = {}
	local aRow = '';
	local pos = 0
	-- Ergaenze  "/"
	str = str .. "////////" ;

	for i = 8,1,-1 do
		pos = string.find(str,'/',1,true);
		if not pos then
			aRow =''
		elseif pos == 1 then
			aRow = '';
		else
			aRow = string.sub(str,1,pos-1);
		end
		-- Ziffern expandieren
		aRow = string.gsub(aRow,'1','-');
		aRow = string.gsub(aRow,'2','--');
		aRow = string.gsub(aRow,'3','---');
		aRow = string.gsub(aRow,'4','----');
		aRow = string.gsub(aRow,'5','-----');
		aRow = string.gsub(aRow,'6','------');
		aRow = string.gsub(aRow,'7','-------');
		aRow = string.gsub(aRow,'8','--------');
		-- Sicherstellen, dass der String 8 Zeichen hat:
		aRow = aRow .. '--------';
		aRow = string.sub(aRow ,1,8)
		-- Austausch der Zeichen durch die Kuerzel in der WP:
		aRow = GetWikiRow(aRow);
		Rows[i] = aRow;
		-- Abgearbeitete Zeile abtrennen:
		str = string.sub(str,pos+1);
	end
	return Rows;
end

local function CreateNowikiString(str)
	local text = str;
	text = mw.ustring.gsub(text,"&","&");
	text = mw.ustring.gsub(text,"'","'");
	text = mw.ustring.gsub(text,"%[","[");
	text = mw.ustring.gsub(text,"%]","]");
	text = mw.ustring.gsub(text,"<","&lt;");
	text = mw.ustring.gsub(text,">","&gt;");
	text = mw.ustring.gsub(text,"{","&#123;");
	text = mw.ustring.gsub(text,"|","&#124;");
	text = mw.ustring.gsub(text,"}","&#125;");
	return text;
end

local function getText(args)
	local square = {};
	local A = trim(args[1] or ' ');
	local T = trim(args[2] or ' ');
	local C = trim(args['center'] or '');
	local B = '';
	if args['Beschreibung'] then
		B = trim(args['Beschreibung']);
	else
		B = trim(args[67] or '');
	end
	if mw.ustring.find(B,'<center>') then
		C = 1
		B = mw.ustring.gsub(B,'<center>','');
		B = mw.ustring.gsub(B,'</center>','');
	end
	A = CreateNowikiString(A)
	if A == 'tleft' then A = 'links' end
	if A == 'tright' then A = 'rechts' end
	B = CreateNowikiString(B)
	T = CreateNowikiString(T)
	for i = 1,64 do
		square[i] = tostring(trim(args[i+2]) or '  ');
		square[i] = string.sub(square[i] .. '  ',1,2)
		square[i] = checkcode(square[i]);
	end
	local Text ='<pre>\n{{Schachbrett'..'\n';
	Text = Text .. '| Ausrichtung=' .. A .. '\n'
	Text = Text .. '| Titel=' .. T .. '\n'
	Text = Text .. '<!-- a &nbsp;b &nbsp;c &nbsp;d &nbsp;e &nbsp;f &nbsp;g &nbsp;h -->' .. '\n'
	Text = Text .. '| Z8='..square[1] ..'/'..square[2] ..'/'..square[3] ..'/'..square[4] ..'/'..square[5] ..'/'..square[6] ..'/'..square[7] ..'/'..square[8] ..'/\n'
	Text = Text .. '| Z7='..square[9] ..'/'..square[10]..'/'..square[11]..'/'..square[12]..'/'..square[13]..'/'..square[14]..'/'..square[15]..'/'..square[16]..'/\n'
	Text = Text .. '| Z6='..square[17]..'/'..square[18]..'/'..square[19]..'/'..square[20]..'/'..square[21]..'/'..square[22]..'/'..square[23]..'/'..square[24]..'/\n'
	Text = Text .. '| Z5='..square[25]..'/'..square[26]..'/'..square[27]..'/'..square[28]..'/'..square[29]..'/'..square[30]..'/'..square[31]..'/'..square[32]..'/\n'
	Text = Text .. '| Z4='..square[33]..'/'..square[34]..'/'..square[35]..'/'..square[36]..'/'..square[37]..'/'..square[38]..'/'..square[39]..'/'..square[40]..'/\n'
	Text = Text .. '| Z3='..square[41]..'/'..square[42]..'/'..square[43]..'/'..square[44]..'/'..square[45]..'/'..square[46]..'/'..square[47]..'/'..square[48]..'/\n'
	Text = Text .. '| Z2='..square[49]..'/'..square[50]..'/'..square[51]..'/'..square[52]..'/'..square[53]..'/'..square[54]..'/'..square[55]..'/'..square[56]..'/\n'
	Text = Text .. '| Z1='..square[57]..'/'..square[58]..'/'..square[59]..'/'..square[60]..'/'..square[61]..'/'..square[62]..'/'..square[63]..'/'..square[64]..'/\n'
	Text = Text .. '<!-- a &nbsp;b &nbsp;c &nbsp;d &nbsp;e &nbsp;f &nbsp;g &nbsp;h -->' .. '\n'
	Text = Text .. '| center='.. C  .. '\n'
	Text = Text .. '| Beschreibung='.. B .. '\n}}\n</pre>\n'
	return Text
end
local function getCheckData(args)
	local Data = {}
	local item= "";
	local text= ''
	for i = 1,67 do
		item= args[i] or "??";
		item =mw.ustring.gsub(item,' ','-');
		Data[i] = 'args[' .. i ..'] = "' .. item  .. '"<br />';
		if i % 8 == 2 or i== 1 or i == 67 then
			Data[i] = Data[i] .. "<br />";
		end
	end
	text=table.concat(Data);
	return text
end

local p = {}
	function p.Fen2Wiki(frame)
		local data = {};
		local Text ='';
		local Brett = frame.args['Fen'] or 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR'
		data = SplitFenLine(Brett);
		for i = 8,1,-1 do
			Text = Text .. '| Z' ..tostring(i) ..'=' .. data[i] .. '\n';
		end
		return Text;
	end
	function p.GetFenRow(frame)
		local data = {};
		local Text ='';
		local Brett = frame.args['Fen'] or 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR'
		local idx =  tonumber(frame.args['Z']) or 1 ;
		data = SplitFenLine(Brett);
		Text = data[idx];
		return Text;
	end
	function p.Convert(frame)
		local fr = frame:getParent()
		local text = getText(fr.args)
		return text
	end
	function p.ConvertCheck(frame)
		local fr = frame:getParent()
		local text = getCheckData(fr.args)
		return text
	end

	function p.Plainstring(frame)
		local str= frame.args[1] or '';
		return CreateNowikiString(str)
	end
	
return p;