PGN-Parser verbessert: robustes Tokenizing mit Kommentar- und %clk-Erkennung; app.js: manuelle Partieauswahl bleibt nach Klick erhalten

This commit is contained in:
2026-05-24 14:49:07 +02:00
parent 15b6f31f62
commit 0d94cac60c
2 changed files with 40 additions and 16 deletions

View File

@@ -66,21 +66,43 @@ function parseGameBlock(block) {
function parseMoves(movesText) {
const moves = [];
// Remove comments in curly braces
movesText = movesText.replace(/\{[^}]*\}/g, '');
let clockWhite = null;
let clockBlack = null;
let color = 'w';
// Remove move numbers
movesText = movesText.replace(/\d+\.\s*/g, '');
const tokenRegex = /\d+\.\s*|\{[^}]*\}|\S+/g;
const tokens = movesText.match(tokenRegex) || [];
// Split into individual moves
const tokens = movesText.split(/\s+/).filter(t => t.trim());
for (const token of tokens) {
for (let i = 0; i < tokens.length; i++) {
let token = tokens[i].trim();
if (['1-0', '0-1', '1/2-1/2', '*'].includes(token)) {
moves.push({ san: token, isResult: true });
} else if (token.length > 0) {
moves.push({ san: token, isResult: false });
moves.push({ san: token, isResult: true, whiteClock: clockWhite, blackClock: clockBlack });
continue;
}
if (/^\d+\.$/.test(token)) continue;
if (token.startsWith('{')) {
const clkMatch = token.match(/\[%clk\s+([\d:]+)\]/);
if (clkMatch && moves.length > 0) {
const lastMove = moves[moves.length - 1];
if (!lastMove.isResult && lastMove.color) {
if (lastMove.color === 'w') {
clockWhite = clkMatch[1];
} else {
clockBlack = clkMatch[1];
}
lastMove.whiteClock = clockWhite;
lastMove.blackClock = clockBlack;
}
}
continue;
}
const move = { san: token, isResult: false, whiteClock: clockWhite, blackClock: clockBlack, color };
moves.push(move);
color = color === 'w' ? 'b' : 'w';
}
return moves;