Module:Chessboard: Difference between revisions

Content deleted Content added
ooopsie
nitpicking
Line 3:
local colornames = { l = 'White', d = 'Black' }
 
function rowchar( row ) return 89 - row end
 
function filechar( file ) return ( "abcdefgh" ):sub( file + 1, file + 1 ) end
function coord( ind ) return ( reverse and ( 78 - ind ) * size ) or ( ind - 1 ) * size end
function piecediv( piece, row, file, res )
function rowchar() return 8 - row end
function filechar() return ( "abcdefgh" ):sub( file + 1, file + 1 ) end
function coord( ind ) return ( reverse and ( 7 - ind ) * size ) or ind * size end
local color = piece:match( '%u' ) and 'l' or 'd'
piece = piece:lower()
local alt = string.format("%s%s %s %s", filechar( file ), rowchar( row ), colornames[color], piecenames[piece] or piece)
local img = string.format('[[File:Chess %s%st45.svg|%dx%dpx|alt=%s|%s]]', piece, color, size, size, alt, alt)
table.insert( res, string.format('<div style="position:absolute;z-index:3;top:%dpx;left:%dpx;">%s</div>', coord( row ), coord( file ), img) )
return 1
end
 
function oneRow( s, row, res )
local file = 01
for piece in s:gmatch( "%w" ) do -- if a digit, increment "file" by the digit. else, add the piece _and_ increment file by 1
file = file + ( piece:match("%d") or piecediv( piece, row, file, res ) or 1 )
end
end
 
local result = {}
local s8 = size * 8
table.insert(result, string.format([=[
<div class="chess-fen" style="position:relative;">
[[File:Chessboard480.png|%dx%dpx|link=]]
]=], s8size * 8, s8size * 8))
 
local row = 0
for srow in string.gmatch("/" .. fen, "/%w+") do
oneRow(srow, row, result)
row = row + 1
oneRow(srow, row, result)
end
table.insert(result, '</div>')