Module:Chessboard/sandbox: Difference between revisions

Content deleted Content added
Nevermind, more complicated, need piece labels as well, so better with a data submodule for different games
rename class "chess-board" to "chess-pieces", since the div contains only the pieces, and to avoid confusion with the new top-level "chessboard" class
 
(28 intermediate revisions by 5 users not shown)
Line 1:
local p = {}
 
local cfg, nrows, ncols
local cfg = mw.loadData('Module:Chessboard/configuration')
 
local function image_square( pc, row, col, size )
local piece = mw.ustring.gsub( pc, '^.*(%w)(%w).*$', '%1' ) or ''
local color = mw.ustring.gsub( pc, '^.*(%w)(%w).*$', '%2' ) or ''
local square = cfg.files[col] .. row
local message = mw.message.newRawMessage
local color_name = cfg.color_names[color]
local piece_name = cfg.piece_names[piece]
local symbol_name = cfg.symbol_names[piece .. color]
local alt = ''
if color_name and piece_name then
alt = message(
cfg.alt_with_color_and_piece,
square,
color_name,
piece_name
):plain()
elseif symbol_name then
alt = message(
cfg.alt_with_symbol,
square,
symbol_name
):plain()
else
alt = message(
cfg.alt_otherwise,
square,
piece,
color
):plain()
end
 
return string.format(
'[[File:Chess %s%st45.svg|%dx%dpx|alt=%s|%s|link=|class=notpageimage]]',
piece, color, size, size, alt, alt
)
 
end
local function innerboard(args, size, rev)
pattern = cfg.pattern or '%w%w'
local board = mw.html.create('div')
local board_sizeroot = 8 * sizemw.html.create('div')
root:addClass('chess-pieces notheme')
local size_px = size .. 'px'
:css('position', 'relative')
board:addClass('chessboard-board')
:wikitext(stringcfg.formatimage_board(size))
'[[File:Chessboard480.svg|%dx%dpx|link=|alt=Empty chess board|class=notpageimage]]',
board_size, board_size
))
for trow = 1,8nrows do
local row = rev and trow or ( 91 + nrows - trow )
for tcol = 1,8ncols do
local col = rev and ( 91 + ncols - tcol ) or tcol
local piece = args[8ncols * ( 8nrows - row ) + col + 2] or ''
local square =if piece:match('%w%w' pattern ) then
local img = cfg.image_square(piece:match(pattern), row, col, size )
if square then
root:tag('div')
local img = image_square(square, row, col, size )
board:tag('div')
:css('top', tostring(( trow - 1 ) * size) .. 'px')
:css('left', tostring(( tcol - 1 ) * size) .. 'px')
:css('width', size_px)
:css('height', size_px)
:wikitext(img)
end
Line 69 ⟶ 25:
end
 
return tostring(boardroot)
end
 
function chessboard(args, size, rev, letters, numbers, header, footer, align, clear)
function letters_row( rev, num_lt, num_rt )
local letters = cfg.letters()
local files_rowroot = mw.html.create('')
if num_lt then
files_rowroot:tag('td')
end
for k = 1,8ncols do
files_rowroot:tag('td')
:css('height', '18px')
:css('width', size .. 'px')
:wikitext(rev and cfg.filesletters[91+ncols-k] or cfg.filesletters[k])
end
if num_rt then
files_rowroot:tag('td')
end
return tostring(files_rowroot)
end
local letters_tp = letters:match( cfg.letters_both'both' ) or letters:match( cfg.letters_top'top' )
local letters_bt = letters:match( cfg.letters_both'both' ) or letters:match( cfg.letters_bottom'bottom' )
local numbers_lt = numbers:match( cfg.numbers_both'both' ) or numbers:match( cfg.numbers_left'left' )
local numbers_rt = numbers:match( cfg.numbers_both'both' ) or numbers:match( cfg.numbers_right'right' )
local width = 8ncols * size + 2
if ( numbers_lt ) then width = width + 18 end
if ( numbers_rt ) then width = width + 18 end
Line 101 ⟶ 58:
:addClass('chessboard')
:addClass('thumb')
:addClass('noviewer')
:addClass(align)
if( header and header ~= '' ) then
root:tag('div')
:addClass('chessboard-headercenter')
:css('maxline-widthheight', (width + 8) .. 'px130%')
:css('margin', '0 auto')
:wikitext(header)
:css('max-width', (width + ncols) .. 'px')
:wikitext(header)
end
local board_wrapperdiv = root:tag('div')
:addClass('thumbinner chessboard-board-wrapper')
:css('width', width .. 'px')
local board_with_notationb = board_wrapperdiv:tag('table')
:attr('cellpadding', '0')
:addClass('chessboard-board-and-notation')
:attr('cellspacing', '0')
 
if ( letters_tp ) then
board_with_notationb:tag('tr')
:wikitext(letters_row( rev, numbers_lt, numbers_rt ))
end
local tablerow = board_with_notationb:tag('tr')
local size_px = size .. 'px'
if ( numbers_lt ) then
tablerow:tag('td')
:css('width', '18px')
:css('height', size_pxsize .. 'px')
:wikitext(rev and 1 or 8nrows)
end
local td = tablerow:tag('td')
:attr('colspan', 8ncols)
:attr('rowspan', 8nrows)
:wikitext(innerboard(args, size, rev))
Line 134 ⟶ 94:
tablerow:tag('td')
:css('width', '18px')
:css('height', size_pxsize .. 'px')
:wikitext(rev and 1 or 8nrows)
end
if ( numbers_lt or numbers_rt ) then
for trow = 2, 8nrows do
local idx = rev and trow or ( 91 + nrows - trow )
tablerow = board_with_notationb:tag('tr')
if ( numbers_lt ) then
tablerow:tag('td')
:css('height', size_pxsize .. 'px')
:wikitext(idx)
end
if ( numbers_rt ) then
tablerow:tag('td')
:css('height', size_pxsize .. 'px')
:wikitext(idx)
end
Line 154 ⟶ 114:
end
if ( letters_bt ) then
board_with_notationb:tag('tr')
:addClass('chessboard-letters')
:wikitext(letters_row( rev, numbers_lt, numbers_rt ))
end
 
if (footer and mw.text.trim(footer )~= '') then
board_wrapperdiv:tag('div')
:addClass('thumbcaption')
:wikitext(footer)
end
 
return tostring(root) ..
mw.getCurrentFrame():extensionTag( 'templatestyles', '', { src = 'Module:Chessboard/styles.css' } )
end
 
Line 211 ⟶ 171:
local args = frame.args
local pargs = frame:getParent().args
local style = args.style or pargs.style or 'Chess'
cfg = require('Module:Chessboard/' .. style)
nrows, ncols = cfg.dims()
local size = args.size or pargs.size or '26'
local reverse = ( args.reverse or pargs.reverse or '' ):lower() == "true"
Line 216 ⟶ 180:
local numbers = ( args.numbers or pargs.numbers or 'both' ):lower()
local header = args[2] or pargs[2] or ''
local footer = args[67nrows*ncols + 3] or pargs[67nrows*ncols + 3] or ''
local align = ( args[1] or pargs[1] or 'tright' ):lower()
local clear = args.clear or pargs.clear or ( align:match('tright') and 'right' ) or 'none'
local fen = args.fen or pargs.fen
local pgn = args.pgn or pargs.pgn
 
size = mw.ustring.match( size, '[%d]+' ) or '26' -- remove px from size
if (pgn) then
local templatestyles = frame:extensionTag {
local pgnModule = require('Module:Pgn')
name = 'templatestyles', args = { src = 'Module:Chessboard/styles.css' }
metadata, moves = pgnModule.main(pgn)
}
fen = moves[#moves]
end
if (fen) then
align = args.align or pargs.align or 'tright'
Line 230 ⟶ 197:
header = args.header or pargs.header or ''
footer = args.footer or pargs.footer or ''
return templatestyles .. chessboard( convertFenToArgs( fen ), size, reverse, letters, numbers, header, footer, align, clear )
end
if args[3] then
return templatestyles .. chessboard(args, size, reverse, letters, numbers, header, footer, align, clear)
else
return templatestyles .. chessboard(pargs, size, reverse, letters, numbers, header, footer, align, clear)
end
end
 
function p.fen2ascii(frame)
-- {{#invoke:Chessboard|fen2ascii|fen=...}}
local b = convertFenToArgs( frame.args.fen )
local res = '|=\n'
local offset = 2
for row = 1,8 do
local n = (9 - row)
res = res .. n .. ' |' ..
table.concat(b, '|', 8*(row-1) + 1 + offset, 8*(row-1) + 8 + offset) .. '|=\n'
end
res = mw.ustring.gsub( res,'\| \|', '| |' )
res = mw.ustring.gsub( res,'\| \|', '| |' )
return res .. ' a b c d e f g h'
end
 
function p.ascii2fen( frame )
-- {{#invoke:Chessboard|ascii2fen|kl| | |....}}
return convertArgsToFen( frame.args, frame.args.offset or 1 )
end