en.wikipedia.org

Module:Sandbox/SD0001/Chessboard - Wikipedia

From Wikipedia, the free encyclopedia

local Arguments = require('Module:Arguments')
local Pgn = require('Module:Pgn')

local p = {}

p.main = function(frame)
	local args = Arguments.getArgs(frame)
	metadata, moves = Pgn.main(args.pgn)
	
	local initalPos = args.initial or '1'
	local totalMoves = #moves -- Note this is actually the number of halfmoves + 1 (for initial position)
	
	local wikitext = '<div class="calculator-container" data-calculator-refresh-on-load="true">' ..
		'{{calculator|type=hidden|id=ply|default='..initalPos..'}}' ..
		'{{calculator button|contents=←|for=ply|type=default|formula=max(1, ply-1)}}' ..
		'{{calculator button|contents=→|for=ply|type=default|formula=min('..totalMoves..', ply+1)}}'

	for ply = 1, #moves, 1 do
		local fen = moves[ply]
		wikitext = wikitext ..
			'{{calculator-hideifzero' ..
			'|element=div' ..
			'|formula=not(ply-'..ply..')' ..
			'|text={{chess diagram|fen='..fen..'}}' ..
			'}}'
	end
	
	wikitext = wikitext .. '</div>'
	return frame:preprocess(wikitext)
end

return p