Jump to content

Module:Sandbox/SD0001/Chessboard

From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by SD0001 (talk | contribs) at 17:50, 19 January 2025 (experimental). The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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