Module:Chess-from-fen

From Wikipedia

Documentation for this module may be created at Module:Chess-from-fen/doc

local p = {}

p.f = function(frame) 
    local fen,size,reverse
    
	function coord(ind) 
		return (reverse and (7 - ind) * size) or ind * size
	end
	
	function piecepict(file,row,piece) 
		return string.format('[[File:Chess %st45.svg|%dx%dpx]]', piece, size, size) 
	end
	
	function oneRow(s, row) 
		local file = 0
		local result = ''
		for piece in string.gmatch(s, "%\w") do
			if string.match(piece, "%\d") then 
				file = file + piece
			else 
				local ls = string.lower(piece)
				ls = ls .. ( (piece == ls and 'd') or 'l')
				result = result .. string.format('<div style="position:absolute;top:%dpx;left:%dpx;">%s</div>\n', coord(row), coord(file), piecepict(file,row,ls))
				file = file + 1
			end
		end
		return result
	end
	
	local row = 0;
	fen = frame.args['fen']
	size = frame.args['size'] or 30
	reverse = string.lower(frame.args['reverse'] or '') == "true"
	local result = string.format('<div class="chess-fen" style="position:relative;min-width:%dpx;min-height:%dpx;">', size*8,size*8)
	result = result .. string.format('<div style="position:absolute;z-index:0; top:0px; left:0px;">[[File:Chessboard480.png|%dx%dpx]]</div>', size * 8, size * 8)
	for srow in string.gmatch("/" .. fen, "/%w+") do
		result = result .. oneRow(srow, row)
		row = row + 1
	end
	result = result .. '</div>'
	return result
end

return p