local p =
function chessboard(args, size, rows, cols, rev, trans, lightdark, altprefix, letters, numbers, header, footer, align, clear) function colchar(col) return (col <= 26) and ("abcdefghijklmnopqrstuvwxyz"):sub(col, col) or ("abcdefghijklmnopqrstuvwxyz"):sub(math.floor((col-1)/26), math.floor((col-1)/26)) .. ("abcdefghijklmnopqrstuvwxyz"):sub(col-math.floor((col-1)/26)*26, col-math.floor((col-1)/26)*26) end function image_square(pc, row, col, size, t, flip, altprefix) local colornames = local piecenames = local symnames = local color = mw.ustring.gsub(pc, '^.*(%w)(%w).*$', '%2') or local piece = mw.ustring.gsub(pc, '^.*(%w)(%w).*$', '%1') or local alt = altprefix .. colchar(col) .. row .. ' ' if (colornames[color] and piecenames[piece]) then alt = alt .. colornames[color] .. ' ' .. piecenames[piece] else alt = alt .. (symnames[piece .. color] or piece .. ' ' .. color) end local ld = t and 't' or ((((row + col + flip) % 2)
function letters_row(rev, num_lt, num_rt, cols) local res = '
local b = local caption =
if (letters_tp) then b = b .. letters_row(rev, numbers_lt, numbers_rt, cols) .. '\n' end for trow = 1,rows do local row = rev and trow or (rows - trow + 1) b = b .. '
0) and '#ffce9e' or '#d18b47' b = b .. '
if not footer:match('^%s*$') then caption = '
' .. footer .. '
\n' end b = '
if noframe then return b else return '
' .. '
' .. header .. '
' .. '\n
\n' .. b .. '\n' .. caption .. '
' end end
function convertFenToArgs(fen) -- converts FEN notation to an array of positions, offset by 2 local res = -- Loop over rows, which are delimited by / for srow in string.gmatch("/" .. fen, "/%w+") do srow = srow:gsub("/","") -- clean up row -- Loop over all letters and numbers in the row -- Since Lua regexes do not have the | operator, we have -- to spell things out local index = 1 local piece = "" -- Piece can also be empty squares local place = 0 local pstart = 0 local pend = 0 local length = srow:len while index <= length do -- Look for a number. Can have multiple digits pstart, pend = srow:find("%d+", index) if pstart
index then piece = srow:sub(pstart, pstart) index = pstart + 1 -- l: White (light); d: Black (dark) local color = piece:match('%u') and 'l' or 'd' piece = piece:lower table.insert(res, piece .. color) else index = length + 1 -- Break loop end end end end return resend
function p.board(frame) local args = frame.args local pargs = frame:getParent.args local size = (args.size or pargs.size) or '26' local reverse = (args.reverse or pargs.reverse or ):lower
"true" local lightdark = (args.lightdark or pargs.lightdark or ):lower
"true" local fen = args.fen or pargs.fen
size = mw.ustring.match(size, '[%d]+') or '26' -- remove px from size if (fen) then align = (args.align or pargs.align or 'tright'):lower clear = (args.clear or pargs.clear) or (align:match('tright') and 'right' or 'none') header = args.header or pargs.header or footer = args.footer or pargs.footer or return chessboard(convertFenToArgs(fen), size, rows, cols, reverse, trans, lightdark, altprefix, letters, numbers, header, footer, align, clear, noframe) end if args[3] then return chessboard(args, size, rows, cols, reverse, trans, lightdark, altprefix, letters, numbers, header, footer, align, clear, noframe) else return chessboard(pargs, size, rows, cols, reverse, trans, lightdark, altprefix, letters, numbers, header, footer, align, clear, noframe) end end
return p