PDA

View Full Version : Perl code: PGN file reformatter



Spiny Norman
17-10-2009, 04:04 PM
I use the following application to reformat the moves for each game in a PGN file. The Croydon Chess website uses a PGN viewer that can get a bit temperamental if the number of moves in a game is large and is all on a single line (no carriage returns and no line feeds). There's all the issue that if the move number is separated from the White move (e.g. 23. Ne6) then sometimes the display wraps inappropriately. So we like to remove the space after each line number (e.g. 23.Ne6). Since it is laborious to do this manually, I use this Perl script that I wrote today:


#!/usr/bin/perl
#
# Name : ReformatPGNforWebsite.pl
# Author : Stephen Frost
# Date Written: October 2009
# Description : Reads PGN and reformats it for website use
#

$| = 1;

#
# enable reading of a whole paragraph at a time
#

$/ = "";

&SetUserDefinedVariables();
&DoMainProcessing();
&EndProcessing();

exit;


sub SetUserDefinedVariables {

$PreferredLineLength = 80;

$PGNFile = 'C:\Temp\Chess Club\CroydonChess.pgn';
$OutputFile = 'C:\Temp\Chess Club\CroydonChess.pgn.txt';

}


sub DoMainProcessing {

open(PGNFILE, "$PGNFile") || &SystemError('Cannot open PGN file');
open(OUTPUTFILE, ">$OutputFile") || &SystemError('Cannot open OUTPUT file');

while (<PGNFILE>) {

# REMEMBER, due to the $/ above we are reading a paragraph at a time!

# so if we have a left-bracket in the text then we are processing a PGN header
# paragraph ... so we just write it straight out to the output file without
# making any changes

if (/\[/) {

print OUTPUTFILE;

} else {

# okay, we must have a paragraph of game moves ...

# we replace any newlines, returns, formfeeds and tabs with spaces

s/\n/ /g;
s/\r/ /g;
s/\f/ /g;
s/\t/ /g;

# and replace any double spaces with single spaces

s/ / /g;

# and finally convert all ". " to "."

s/\. /\./g;

# at this point we have the whole game in a single string
# now we need to step through it "one move at a time" and
# write it out once we reach the preferred line length

$movecount = 0;
$unformatted = $_;
$reformatted = "";

while (length($unformatted) > 0) {
($whitemove, $blackmove, $remainingmoves) = split(/ /, $unformatted, 3);
$movecount = $movecount + 1;
$nextmove = "$whitemove $blackmove";
if ((length($reformatted) + 1 + length($nextmove)) > $PreferredLineLength) {
print OUTPUTFILE "$reformatted\n";
$reformatted = "";
$movecount = 1;
}
if ($movecount == 1) {
$reformatted = $nextmove;
} else {
$reformatted = "$reformatted $nextmove";
}
$unformatted = $remainingmoves;
}

print OUTPUTFILE "$reformatted\n\n";

}

}

close(PGNFILE);
close(OUTPUTFILE);
}


sub EndProcessing {
}


sub SystemError {
local($errormessage) = $_[0];
print "Processing error: $errormessage ($!)\n";
exit;
}

Spiny Norman
17-10-2009, 04:06 PM
Here's an example of how it handles some games. It reformats from this:


[Event "Club Championship 09"]
[Site "Croydon Chess Club"]
[Date "2009.10.15"]
[Round "1.1"]
[White "Matthews, Rodney"]
[Black "West, Guy"]
[Result "0-1"]
[ECO "B01"]
[WhiteElo "1114"]
[BlackElo "2310"]
[PlyCount "34"]

1.e4 d5 2.exd5 Nf6 3.c4 e6 4.dxe6 Bxe6 5.Nf3 Nc6 6.Be2 Bc5 7.O-O O-O 8.Nc3 Re8 9.Na4 Bd4 10.Nxd4 Nxd4 11.b3 Bf5 12.Re1 Bc2 13.Nc3 Bxd1 14.Rxd1 Nxe2+ 15.Nxe2 Rxe2 16.d4 Ng4 17.f3 Qxd4+ 0-1

[Event "Club Championship 09"]
[Site "Croydon Chess Club"]
[Date "2009.10.15"]
[Round "1.3"]
[White "Gibson, Kyle"]
[Black "Lacey, David"]
[Result "0-1"]
[ECO "C24"]
[WhiteElo "1107"]
[BlackElo "1700"]
[PlyCount "90"]

1.e4 e5 2.Bc4 Nf6 3.d3 c6 4.Bb3 d5 5.exd5 cxd5 6.Nf3 Nc6 7.O-O Bd6 8.Bg5 Be6 9.Nc3 Bc7 10.Re1 O-O 11.Qc1 Kh8 12.h3 Rg8 13.Kf1 Qd7 14.Ng1 a6 15.Rb1 Nd4 16.Na4 Nxb3 17.cxb3 b6 18.Qc2 Bf5 19.Rbc1 Rac8 20.Qd1 d4 21.Qf3 Ne8 22.Qe2 f6 23.Bd2 Nd6 24.Bb4 Qb5 25.Red1 Qxb4 26.g4 Bd7 27.Qc2 Bxa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championship 09"]
[Site "Croydon Chess Club"]
[Date "2009.10.15"]
[Round "1.5"]
[White "Fleming, John M."]
[Black "Kenmure, Jamie"]
[Result "0-1"]
[ECO "C02"]
[PlyCount "94"]

1.e4 e6 2.d4 d5 3.e5 c5 4.c3 Qb6 5.Nf3 Bd7 6.Bd3 cxd4 7.cxd4 Bb5 8.Nc3 Bxd3 9.Qxd3 Nc6 10.a3 Nge7 11.O-O Nf5 12.Be3 Be7 13.Nxd5 Qd8 14.Nxe7 Nfxe7 15.Ng5 Nf5 16.Nf3 Nfe7 17.Ng5 Nxe5 18.Qb5+ N5c6 19.Qxb7 Rb8 20.Qa6 Rxb2 21.Qa4 Rb7 22.Rfc1 Qd5 23.Qc2 Na5 24.Rab1 Rxb1 25.Rxb1 Nec6 26.Qa4 O-O 27.Rb5 Qd8 28.Qc2 g6 29.Qe4 Qd7 30.Nf3 Rb8 31.Rxb8+ Nxb8 32.Bh6 Qd5 33.Qxd5 exd5 34.Ne5 Nac6 35.Nxc6 Nxc6 36.Be3 f5 37.g3 Kf7 38.Kf1 Na5 39.Ke2 Nc4 40.a4 Nxe3 41.fxe3 g5 42.Kf3 Kf6 43.h4 g4+ 44.Kf4 h5 45.a5 a6 46.e4 fxe4 47.Ke3 Ke6 48.Kf4 Kd6 49.Ke3 Kc6 0-1

[Event "Club Championship 09"]
[Site "Croydon Chess Club"]
[Date "2009.10.15"]
[Round "1.6"]
[White "Watson, Jean"]
[Black "Yung, Pearl"]
[Result "1-0"]
[ECO "D05"]
[WhiteElo "1286"]
[BlackElo "768"]
[PlyCount "61"]

1.d4 d5 2.Nf3 Nf6 3.e3 e6 4.Be2 c5 5.O-O Bd6 6.b3 O-O 7.Bb2 Nc6 8.dxc5 Bxc5 9.c4 Qe7 10.a3 a6 11.b4 Ba7 12.Nc3 Rd8 13.cxd5 exd5 14.Qc2 d4 15.exd4 Bxd4 16.Na4 b5 17.Bxd4 Nxd4 18.Nxd4 Bb7 19.Nf5 Qe5 20.Nc5 Rac8 21.Ng3 Be4 22.Ngxe4 Nxe4 23.Qxe4 Qxe4 24.Nxe4 Rc2 25.Bf3 f5 26.Nc5 g5 27.Ne6 Rd3 28.Nxg5 h6 29.Ne6 Rcc3 30.Nc5 Rxa3 31.Nxd3 1-0

[Event "Club Championship 09"]
[Site "Croydon Chess Club"]
[Date "2009.10.15"]
[Round "1.7"]
[White "Lim, Denise"]
[Black "Kloprogge, Ion"]
[Result "1-0"]
[ECO "B40"]
[WhiteElo "731"]
[BlackElo "1219"]
[PlyCount "71"]

1.e4 e6 2.c4 c5 3.Nf3 Nc6 4.Nc3 g6 5.Qc2 Bg7 6.d3 a6 7.b3 Nb4 8.Qb2 Ne7 9.a3 Nbc6 10.Be3 d6 11.Be2 O-O 12.O-O Ne5 13.Nxe5 Bxe5 14.Bh6 Re8 15.Rac1 Nc6 16.Be3 Bg7 17.Qd2 Qh4 18.Bg5 Bh6 19.Bxh6 Qe7 20.Bg5 f6 21.Be3 Na5 22.Rb1 Nc6 23.b4 cxb4 24.axb4 Ne5 25.d4 Nf7 26.Ra1 f5 27.h3 Qh4 28.Kh2 e5 29.f3 f4 30.Bf2 Qh5 31.Nd5 Kg7 32.Nc7 Bd7 33.Nxa8 Rxa8 34.dxe5 dxe5 35.Qxd7 Rd8 36.Qg4 1-0

[Event "Club Championship 09"]
[Site "Croydon Chess Club"]
[Date "2009.10.15"]
[Round "1.8"]
[White "Lyons, Linden"]
[Black "Yung, Cameron"]
[Result "1-0"]
[ECO "C01"]
[WhiteElo "1116"]
[BlackElo "325"]
[PlyCount "29"]

1.e4 e6 2.d4 d5 3.exd5 exd5 4.c4 Nf6 5.Nc3 Bb4 6.a3 Ba5 7.b4 c5 8.bxa5
Qxa5 9.Bd2 Qd8 10.dxc5 O-O 11.cxd5 Qe7+ 12.Be3 Qe5 13.Qd4 Qf5 14.Bd3 Qh5
15.Nf3 1-0


to this:


[Event "Club Championship 09"]
[Site "Croydon Chess Club"]
[Date "2009.10.15"]
[Round "1.1"]
[White "Matthews, Rodney"]
[Black "West, Guy"]
[Result "0-1"]
[ECO "B01"]
[WhiteElo "1114"]
[BlackElo "2310"]
[PlyCount "34"]

1.e4 d5 2.exd5 Nf6 3.c4 e6 4.dxe6 Bxe6 5.Nf3 Nc6 6.Be2 Bc5 7.O-O O-O 8.Nc3 Re8
9.Na4 Bd4 10.Nxd4 Nxd4 11.b3 Bf5 12.Re1 Bc2 13.Nc3 Bxd1 14.Rxd1 Nxe2+
15.Nxe2 Rxe2 16.d4 Ng4 17.f3 Qxd4+ 0-1

[Event "Club Championship 09"]
[Site "Croydon Chess Club"]
[Date "2009.10.15"]
[Round "1.3"]
[White "Gibson, Kyle"]
[Black "Lacey, David"]
[Result "0-1"]
[ECO "C24"]
[WhiteElo "1107"]
[BlackElo "1700"]
[PlyCount "90"]

1.e4 e5 2.Bc4 Nf6 3.d3 c6 4.Bb3 d5 5.exd5 cxd5 6.Nf3 Nc6 7.O-O Bd6 8.Bg5 Be6
9.Nc3 Bc7 10.Re1 O-O 11.Qc1 Kh8 12.h3 Rg8 13.Kf1 Qd7 14.Ng1 a6 15.Rb1 Nd4
16.Na4 Nxb3 17.cxb3 b6 18.Qc2 Bf5 19.Rbc1 Rac8 20.Qd1 d4 21.Qf3 Ne8 22.Qe2 f6
23.Bd2 Nd6 24.Bb4 Qb5 25.Red1 Qxb4 26.g4 Bd7 27.Qc2 Bxa4 28.bxa4 Bb8 29.Qb3 Qxb3
30.axb3 Rxc1 31.Rxc1 Rc8 32.Rxc8+ Nxc8 33.Ke2 Ne7 34.Nf3 Nd5 35.h4 Bd6 36.g5 Bb4
37.gxf6 gxf6 38.Ne1 Nf4+ 39.Kd1 Bxe1 40.Kxe1 Nxd3+ 41.Ke2 Nc5 42.b4 Nxa4
43.b3 Nc3+ 44.Kd3 b5 45.h5 Kg7 0-1

[Event "Club Championship 09"]
[Site "Croydon Chess Club"]
[Date "2009.10.15"]
[Round "1.4"]
[White "Goldsmith, Richard"]
[Black "Lim, Ethan"]
[Result "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xb7 Rb8 20.Qa6 Rxb2 21.Qa4 Rb7
22.Rfc1 Qd5 23.Qc2 Na5 24.Rab1 Rxb1 25.Rxb1 Nec6 26.Qa4 O-O 27.Rb5 Qd8 28.Qc2 g6
29.Qe4 Qd7 30.Nf3 Rb8 31.Rxb8+ Nxb8 32.Bh6 Qd5 33.Qxd5 exd5 34.Ne5 Nac6
35.Nxc6 Nxc6 36.Be3 f5 37.g3 Kf7 38.Kf1 Na5 39.Ke2 Nc4 40.a4 Nxe3 41.fxe3 g5
42.Kf3 Kf6 43.h4 g4+ 44.Kf4 h5 45.a5 a6 46.e4 fxe4 47.Ke3 Ke6 48.Kf4 Kd6
49.Ke3 Kc6 0-1

[Event "Club Championship 09"]
[Site "Croydon Chess Club"]
[Date "2009.10.15"]
[Round "1.6"]
[White "Watson, Jean"]
[Black "Yung, Pearl"]
[Result "1-0"]
[ECO "D05"]
[WhiteElo "1286"]
[BlackElo "768"]
[PlyCount "61"]

1.d4 d5 2.Nf3 Nf6 3.e3 e6 4.Be2 c5 5.O-O Bd6 6.b3 O-O 7.Bb2 Nc6 8.dxc5 Bxc5
9.c4 Qe7 10.a3 a6 11.b4 Ba7 12.Nc3 Rd8 13.cxd5 exd5 14.Qc2 d4 15.exd4 Bxd4
16.Na4 b5 17.Bxd4 Nxd4 18.Nxd4 Bb7 19.Nf5 Qe5 20.Nc5 Rac8 21.Ng3 Be4
22.Ngxe4 Nxe4 23.Qxe4 Qxe4 24.Nxe4 Rc2 25.Bf3 f5 26.Nc5 g5 27.Ne6 Rd3 28.Nxg5 h6
29.Ne6 Rcc3 30.Nc5 Rxa3 31.Nxd3 1-0

[Event "Club Championship 09"]
[Site "Croydon Chess Club"]
[Date "2009.10.15"]
[Round "1.7"]
[White "Lim, Denise"]
[Black "Kloprogge, Ion"]
[Result "1-0"]
[ECO "B40"]
[WhiteElo "731"]
[BlackElo "1219"]
[PlyCount "71"]

1.e4 e6 2.c4 c5 3.Nf3 Nc6 4.Nc3 g6 5.Qc2 Bg7 6.d3 a6 7.b3 Nb4 8.Qb2 Ne7
9.a3 Nbc6 10.Be3 d6 11.Be2 O-O 12.O-O Ne5 13.Nxe5 Bxe5 14.Bh6 Re8 15.Rac1 Nc6
16.Be3 Bg7 17.Qd2 Qh4 18.Bg5 Bh6 19.Bxh6 Qe7 20.Bg5 f6 21.Be3 Na5 22.Rb1 Nc6
23.b4 cxb4 24.axb4 Ne5 25.d4 Nf7 26.Ra1 f5 27.h3 Qh4 28.Kh2 e5 29.f3 f4
30.Bf2 Qh5 31.Nd5 Kg7 32.Nc7 Bd7 33.Nxa8 Rxa8 34.dxe5 dxe5 35.Qxd7 Rd8
36.Qg4 1-0

[Event "Club Championship 09"]
[Site "Croydon Chess Club"]
[Date "2009.10.15"]
[Round "1.8"]
[White "Lyons, Linden"]
[Black "Yung, Cameron"]
[Result "1-0"]
[ECO "C01"]
[WhiteElo "1116"]
[BlackElo "325"]
[PlyCount "29"]

1.e4 e6 2.d4 d5 3.exd5 exd5 4.c4 Nf6 5.Nc3 Bb4 6.a3 Ba5 7.b4 c5 8.bxa5 Qxa5
9.Bd2 Qd8 10.dxc5 O-O 11.cxd5 Qe7+ 12.Be3 Qe5 13.Qd4 Qf5 14.Bd3 Qh5 15.Nf3 1-0