Skip to content

Commit cd5fd32

Browse files
committed
expand docs for .load_pgn, and include example of parsing sloppy pgn using options
1 parent fec5dc0 commit cd5fd32

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

README.md

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,22 @@ chess.load('4r3/8/X12XPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45');
311311
### .load_pgn(pgn, [ options ])
312312
Load the moves of a game stored in
313313
[Portable Game Notation](http://en.wikipedia.org/wiki/Portable_Game_Notation).
314-
Options is an optional parameter that may contain a `newline_char` which is a
315-
string representation of a RegExp (and should not be pre-escaped) and defaults
316-
to `\r?\n`). Options may also contain a `sloppy` flag which allows chess.js
317-
to parse moves in various non-standard notations (see `.move` documentation
318-
for more information). Returns true if the PGN was parsed successfully,
319-
otherwise false.
314+
`pgn` should be a string. Options is an optional `object` which may contain
315+
a string `newline_char` and a boolean `sloppy`.
316+
317+
The `newline_char` is a string representation of a valid RegExp fragment and is
318+
used to process the PGN. It defaults to `\r?\n`. Special characters
319+
should not be pre-escaped, but any literal special characters should be escaped
320+
as is normal for a RegExp. Keep in mind that backslashes in JavaScript strings
321+
must themselves be escaped (see `sloppy_pgn` example below). Avoid using
322+
a `newline_char` that may occur elsewhere in a PGN, such as `.` or `x`, as this
323+
will result in unexpected behavior.
324+
325+
The `sloppy` flag is a boolean that permits chess.js to parse moves in
326+
non-standard notations. See `.move` documentation for more information about
327+
non-SAN notations.
328+
329+
The method will return `true` if the PGN was parsed successfully, otherwise `false`.
320330

321331
```js
322332
var chess = new Chess();
@@ -342,10 +352,10 @@ pgn = ['[Event "Casual Game"]',
342352
chess.load_pgn(pgn.join('\n'));
343353
// -> true
344354

345-
chess.fen()
355+
chess.fen();
346356
// -> 1r3kr1/pbpBBp1p/1b3P2/8/8/2P2q2/P4PPP/3R2K1 b - - 0 24
347357

348-
chess.ascii()
358+
chess.ascii();
349359
// -> ' +------------------------+
350360
// 8 | . r . . . k r . |
351361
// 7 | p b p B B p . p |
@@ -357,6 +367,39 @@ chess.ascii()
357367
// 1 | . . . R . . K . |
358368
// +------------------------+
359369
// a b c d e f g h'
370+
371+
372+
// Parse non-standard move formats and unusual line separators
373+
var sloppy_pgn = ['[Event "Wijk aan Zee (Netherlands)"]',
374+
'[Date "1971.01.26"]',
375+
'[Result "1-0"]',
376+
'[White "Tigran Vartanovich Petrosian"]',
377+
'[Black "Hans Ree"]',
378+
'[ECO "A29"]',
379+
'',
380+
'1. Pc2c4 Pe7e5', // non-standard
381+
'2. Nc3 Nf6',
382+
'3. Nf3 Nc6',
383+
'4. g2g3 Bb4', // non-standard
384+
'5. Nd5 Nxd5',
385+
'6. c4xd5 e5-e4', // non-standard
386+
'7. dxc6 exf3',
387+
'8. Qb3 1-0'
388+
].join('|');
389+
390+
var options = {
391+
newline_char: '\\|', // Literal '|' character escaped
392+
sloppy: true
393+
};
394+
395+
chess.load_pgn(sloppy_pgn);
396+
// -> false
397+
398+
chess.load_pgn(sloppy_pgn, options);
399+
// -> true
400+
401+
chess.fen();
402+
// -> 'r1bqk2r/pppp1ppp/2P5/8/1b6/1Q3pP1/PP1PPP1P/R1B1KB1R b KQkq - 1 8'
360403
```
361404

362405
### .move(move, [ options ])

0 commit comments

Comments
 (0)