Module:Chessboard/sandbox: Difference between revisions

Content deleted Content added
Test
Tag: Reverted
Nevermind, more complicated, need piece labels as well, so better with a data submodule for different games
Line 1:
local p = {}
 
local cfg = mw.loadData('Module:Chessboard/configuration')
local function image_square( pc, row, col, size, piece_image )
 
local colornames = { l = 'white', d = 'black', u = 'unknown color' }
local function image_square( pc, row, col, size )
local piecenames = {
local piece = mw.ustring.gsub( pc, '^.*(%w)(%w).*$', '%1' ) or ''
p = 'pawn',
r = 'rook',
n = 'knight',
b = 'bishop',
q = 'queen',
k = 'king',
a = 'archbishop',
c = 'chancellor',
z = 'champion',
w = 'wizard',
t = 'fool',
M = 'mann',
h = 'upside-down pawn',
m = 'upside-down rook',
B = 'upside-down bishop',
N = 'upside-down knight',
f = 'upside-down king',
g = 'upside-down queen',
e = 'elephant',
s = 'boat',
G = 'giraffe',
U = 'unicorn',
Z = 'zebra'
}
local symnames = {
xx = 'black cross',
ox = 'white cross',
xo = 'black circle',
oo = 'white circle',
ul = 'up-left arrow',
ua = 'up arrow',
ur = 'up-right arrow',
la = 'left arrow',
ra = 'right arrow',
dl = 'down-left arrow',
da = 'down arrow',
dr = 'down-right arrow',
lr = 'left-right arrow',
ud = 'up-down arrow',
db = 'up-right and down-left arrow',
dw = 'up-left and down-right arrow',
x0 = 'zero',
x1 = 'one',
x2 = 'two',
x3 = 'three',
x4 = 'four',
x5 = 'five',
x6 = 'six',
x7 = 'seven',
x8 = 'eight',
x9 = 'nine'
}
local colchar = {'a','b','c','d','e','f','g','h'}
local color = mw.ustring.gsub( pc, '^.*(%w)(%w).*$', '%2' ) or ''
local piecesquare = mwcfg.ustring.gsub(files[col] pc, '^.*(%w)(%w).*$', '%1' ) or ''row
local altmessage = colchar[col] mw.message. row .. ' 'newRawMessage
local color_name = cfg.color_names[color]
local piece_name = cfg.piece_names[piece]
if colornames[color] and piecenames[piece] then
local symbol_name = cfg.symbol_names[piece .. color]
alt = alt .. colornames[color] .. ' ' .. piecenames[piece]
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(
alt = alt .. ( symnames[piece .. color] or piece .. ' ' .. color )
cfg.alt_otherwise,
square,
piece,
color
):plain()
end
 
return string.format(
'[[File:'Chess %s%st45.. piece_image .. 'svg|%dx%dpx|alt=%s|%s|link=|class=notpageimage]]',
piece, color, size, size, alt, alt )
)
 
end
local function innerboard(args, size, rev, board_image, piece_image)
local rootboard = mw.html.create('div')
local board_size = 8 * size
root:addClass('chess-board')
local size_px = size .. 'px'
:css('position', 'relative')
board:addClass('chessboard-board')
:wikitext(string.format( '[[File:%s|%dx%dpx|link=|class=notpageimage]]', board_image, 8 * size, 8 * size ))
: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 82 ⟶ 56:
local col = rev and ( 9 - tcol ) or tcol
local piece = args[8 * ( 8 - row ) + col + 2] or ''
iflocal square = piece:match( '%w%w' ) then
if square then
local img = image_square(piece:match('%w%w'), row, col, size, piece_image )
local img = image_square(square, row, col, size )
root:tag('div')
board:csstag('position', 'absolutediv')
:css('z-index', '3')
:css('top', tostring(( trow - 1 ) * size) .. 'px')
:css('left', tostring(( tcol - 1 ) * size) .. 'px')
:css('width', size .. 'px'size_px)
:css('height', size .. 'px'size_px)
:wikitext(img)
end
Line 96 ⟶ 69:
end
 
return tostring(rootboard)
end
 
function chessboard(args, size, rev, letters, numbers, header, footer, align, clear, board_image, piece_image)
function letters_row( rev, num_lt, num_rt )
local letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
local rootfiles_row = mw.html.create('')
if num_lt then
rootfiles_row:tag('td')
:css('vertical-align', 'inherit')
:css('padding', '0')
end
for k = 1,8 do
rootfiles_row:tag('td')
:css('padding', '0')
:css('vertical-align', 'inherit')
:css('text-align', 'center')
:css('height', '18px')
:css('width', size .. 'px')
:wikitext(rev and letterscfg.files[9-k] or letterscfg.files[k])
end
if num_rt then
rootfiles_row:tag('td')
:css('vertical-align', 'inherit')
:css('padding', '0')
end
return tostring(rootfiles_row)
end
local letters_tp = letters:match( 'both'cfg.letters_both ) or letters:match( 'top'cfg.letters_top )
local letters_bt = letters:match( 'both'cfg.letters_both ) or letters:match( 'bottom'cfg.letters_bottom )
local numbers_lt = numbers:match( 'both'cfg.numbers_both ) or numbers:match( 'left'cfg.numbers_left )
local numbers_rt = numbers:match( 'both'cfg.numbers_both ) or numbers:match( 'right'cfg.numbers_right )
local width = 8 * size + 2
if ( numbers_lt ) then width = width + 18 end
Line 134 ⟶ 99:
 
local root = mw.html.create('div')
:addClass('chessboard')
:addClass('thumb')
:addClass('noviewer')
:addClass(align)
if( header and header ~= '' ) then
root:tag('div')
root:tag('div'):addClass('center'):css('line-height', '130%'):css('margin', '0 auto'):css('max-width', (width + 8) .. 'px'):wikitext(header)
:addClass('chessboard-header')
:css('max-width', (width + 8) .. 'px')
:wikitext(header)
end
local divboard_wrapper = root:tag('div')
:addClass('thumbinner chessboard-board-wrapper')
:css('width', width .. 'px')
local bboard_with_notation = divboard_wrapper:tag('table')
:addClass('chessboard-board-and-notation')
:attr('cellpadding', '0')
: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
bboard_with_notation:tag('tr')
:css('vertical-align', 'middle')
:wikitext(letters_row( rev, numbers_lt, numbers_rt ))
end
local tablerow = bboard_with_notation: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 .. 'px'size_px)
:wikitext(rev and 1 or 8)
end
Line 170 ⟶ 129:
:attr('colspan', 8)
:attr('rowspan', 8)
:wikitext(innerboard(args, size, rev))
:css('padding', '0')
:css('vertical-align', 'inherit')
:wikitext(innerboard(args, size, rev, board_image, piece_image))
if ( numbers_rt ) then
tablerow:tag('td')
:css('padding', '0')
:css('vertical-align', 'inherit')
:css('text-align', 'center')
:css('width', '18px')
:css('height', size .. 'px'size_px)
:wikitext(rev and 1 or 8)
end
Line 186 ⟶ 140:
for trow = 2, 8 do
local idx = rev and trow or ( 9 - trow )
tablerow = bboard_with_notation:tag('tr')
:css('vertical-align', 'middle')
if ( numbers_lt ) then
tablerow:tag('td')
:css('paddingheight', '0'size_px)
:css('vertical-align', 'inherit')
:css('text-align', 'center')
:css('height', size .. 'px')
:wikitext(idx)
end
if ( numbers_rt ) then
tablerow:tag('td')
:css('paddingheight', '0'size_px)
:css('vertical-align', 'inherit')
:css('text-align', 'center')
:css('height', size .. 'px')
:wikitext(idx)
end
Line 207 ⟶ 154:
end
if ( letters_bt ) then
bboard_with_notation:tag('tr')
:cssaddClass('verticalchessboard-align', 'middleletters')
:wikitext(letters_row( rev, numbers_lt, numbers_rt ))
end
 
if (footer and footer ~= '') then
divboard_wrapper:tag('div')
:addClass('thumbcaption')
:wikitext(footer)
Line 273 ⟶ 220:
local clear = args.clear or pargs.clear or ( align:match('tright') and 'right' ) or 'none'
local fen = args.fen or pargs.fen
local board_image = args['board_image'] or pargs['board_image'] or 'Chessboard480.svg'
local piece_image = args['piece_image'] or pargs['piece_image'] or 'Chess ##t45.svg'
 
piece_image = mw.ustring.gsub(piece_image, '#', '%%s')
 
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 285 ⟶ 230:
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, board_image, piece_image )
end
if args[3] then
return templatestyles .. chessboard(args, size, reverse, letters, numbers, header, footer, align, clear, board_image, piece_image)
else
return templatestyles .. chessboard(pargs, size, reverse, letters, numbers, header, footer, align, clear, board_image, piece_image)
end
end
Line 306 ⟶ 251:
res = mw.ustring.gsub( res,'\| \|', '| |' )
res = mw.ustring.gsub( res,'\| \|', '| |' )
res =return res .. ' a b c d e f g h'
return res
end