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
testing refactor
Line 1:
local p = {}
 
local image_board, image_square
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)
local boardroot = mw.html.create('div')
root:addClass('chess-board')
local board_size = 8 * size
:css('position', 'relative')
local size_px = size .. 'px'
:wikitext(image_board(size))
board:addClass('chessboard-board')
:wikitext(string.format(
'[[File:Chessboard480.svg|%dx%dpx|link=|alt=Empty chess board|class=notpageimage]]',
board_size, board_size
))
for trow = 1,8 do
Line 56 ⟶ 14:
local col = rev and ( 9 - tcol ) or tcol
local piece = args[8 * ( 8 - row ) + col + 2] or ''
local square =if piece:match( '%w%w' ) then
local img = image_square(piece:match('%w%w'), row, col, size )
if square then
root:tag('div')
local img = image_square(square, row, col, size )
board :tagcss('divposition', 'absolute')
:css('z-index', '3')
:css('top', tostring(( trow - 1 ) * size) .. 'px')
:css('left', tostring(( tcol - 1 ) * size) .. 'px')
:css('width', size_pxsize .. 'px')
:css('height', size_pxsize .. 'px')
:wikitext(img)
end
Line 69 ⟶ 28:
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 = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
local files_rowroot = mw.html.create('')
if num_lt then
files_rowroot:tag('td')
:css('vertical-align', 'inherit')
:css('padding', '0')
end
for k = 1,8 do
files_rowroot:tag('td')
:css('padding', '0')
:css('vertical-align', 'inherit')
:css('text-align', 'center')
:css('height', '18px')
:css('width', size .. 'px')
:wikitext(rev and cfg.filesletters[9-k] or cfg.filesletters[k])
end
if num_rt then
files_rowroot:tag('td')
:css('vertical-align', 'inherit')
:css('padding', '0')
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 = 8 * size + 2
if ( numbers_lt ) then width = width + 18 end
Line 99 ⟶ 66:
 
local root = mw.html.create('div')
:addClass('chessboard')
:addClass('thumb')
:addClass('noviewer')
:addClass(align)
if( header and header ~= '' ) then
root:tag('div'):addClass('center'):css('line-height', '130%'):css('margin', '0 auto'):css('max-width', (width + 8) .. 'px'):wikitext(header)
root:tag('div')
:addClass('chessboard-header')
:css('max-width', (width + 8) .. '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')
:css('background', 'white')
:css('font-size', '88%')
:css('border' , '1px #c8ccd1 solid')
:css('padding', '0')
:css('margin', 'auto')
 
if ( letters_tp ) then
board_with_notationb:tag('tr')
:css('vertical-align', 'middle')
:wikitext(letters_row( rev, numbers_lt, numbers_rt ))
end
local tablerow = board_with_notationb:tag('tr'):css('vertical-align','middle')
local size_px = size .. 'px'
if ( numbers_lt ) then
tablerow:tag('td')
:css('padding', '0')
:css('vertical-align', 'inherit')
:css('text-align', 'center')
:css('width', '18px')
:css('height', size_pxsize .. 'px')
:wikitext(rev and 1 or 8)
end
Line 129 ⟶ 102:
:attr('colspan', 8)
:attr('rowspan', 8)
:css('padding', '0')
:css('vertical-align', 'inherit')
:wikitext(innerboard(args, size, rev))
if ( numbers_rt ) then
tablerow:tag('td')
:css('padding', '0')
:css('vertical-align', 'inherit')
:css('text-align', 'center')
:css('width', '18px')
:css('height', size_pxsize .. 'px')
:wikitext(rev and 1 or 8)
end
Line 140 ⟶ 118:
for trow = 2, 8 do
local idx = rev and trow or ( 9 - trow )
tablerow = board_with_notationb:tag('tr')
:css('vertical-align', 'middle')
if ( numbers_lt ) then
tablerow:tag('td')
:css('heightpadding', size_px'0')
:css('vertical-align', 'inherit')
:css('text-align', 'center')
:css('height', size .. 'px')
:wikitext(idx)
end
if ( numbers_rt ) then
tablerow:tag('td')
:css('heightpadding', size_px'0')
:css('vertical-align', 'inherit')
:css('text-align', 'center')
:css('height', size .. 'px')
:wikitext(idx)
end
Line 154 ⟶ 139:
end
if ( letters_bt ) then
board_with_notationb:tag('tr')
:addClasscss('chessboardvertical-lettersalign', 'middle')
:wikitext(letters_row( rev, numbers_lt, numbers_rt ))
end
 
if (footer and footer ~= '') then
board_wrapperdiv:tag('div')
:addClass('thumbcaption')
:wikitext(footer)
Line 220 ⟶ 205:
local clear = args.clear or pargs.clear or ( align:match('tright') and 'right' ) or 'none'
local fen = args.fen or pargs.fen
local style = args.style or pargs.style or 'Chess'
image_board = require('Module:Chessboard/' .. style).image_board
image_square = require('Module:Chessboard/' .. style).image_square
size = mw.ustring.match( size, '[%d]+' ) or '26' -- remove px from size
local templatestyles = frame:extensionTag {
name = 'templatestyles', args = { src = 'Module:Chessboard/styles.css' }
}
if (fen) then
align = args.align or pargs.align or 'tright'
Line 230 ⟶ 216:
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