The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Chess::PGN::Filter - Perl extension for converting PGN files to other formats.

SYNOPSIS

#!/usr/bin/perl
# 
use strict;
use warnings;
use Chess::PGN::Filter;

if ($ARGV[0]) {
    filter(source => $ARGV[0],filtertype => 'XML');
}

OR

#!/usr/bin/perl
# 
use strict;
use warnings;
use Chess::PGN::Filter;

if ($ARGV[0]) {
    my %substitutions = (
        hsmyers => 'Myers, Hugh S (ID)'
    );

    my @exclude = qw(
        WhiteElo
        BlackElo
        EventDate
    );

    filter(
        source => $ARGV[0],
        filtertype => 'TEXT',
        substitutions => \%substitutions,
        nags => 'yes',
        exclude => \@exclude,
    );
 }

OR

#!/usr/bin/perl
# 
use strict;
use warnings;
use Chess::PGN::Filter;

if ($ARGV[0]) {
    filter(
        source => $ARGV[0],
        filtertype => 'DOM',
    );
}

DESCRIPTION

This is a typical text in one side, different text out the otherside filter module. There are as of this writing, the following supported choices:

1XML -- Converts from .pgn to .xml using the included pgn.dtd as the validation document. This is for the most part a one to one transliteration of the PGN standard into XMLese. It does have the additional virtue of allowing positions to be encoded within the XML output. These are generated by an embedded NAG of {0} and automatically (user controlled) at the end of each game. As a kind of adjunct to the position diagrams, pgn.dtd optionally allows each move to include it's FEN string. This allows scripted animation for web pages generated this information.
1TEXT -- Although the PGN standard is widely available, many program that generate .pgn do so in an ill-formed way. This mode is an attempt to 'normalize' away the various flaws found in the 'wild'! This includes things like game text all on a single line without a preceding blank line. Or castling indicated with zeros rather than the letter 'O'. There is at least one application that carefully indents the first move! The list of oddities is probably as long as the list of applications.
1DOM -- A Document Object Model (DOM) makes for a very convenient interim form, common to all other filter types. Useful in both the design and debugging phases of filter construction. By way of self-documentation, here is an example of a single game that shows all of the obvious features of the DOM:
$VAR1 = {
         'Tags' => {
                     'Site' => 'Boise (ID)',
                     'Event' => 'Cabin Fever Open',
                     'Round' => '1',
                     'ECO' => '?',
                     'Date' => '1997.??.??',
                     'White' => 'Barrett Curtis',
                     'Black' => 'Myers Hugh S',
                     'Result' => '1-0'
                   },
         'Gametext' => [
                         {
                           'Movenumber' => '1',
                           'Epd' => 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3',
                           'Movetext' => 'e4'
                         },
                         {
                           'Movenumber' => '2',
                           'Epd' => 'rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq d6',
                           'Movetext' => 'd5'
                         },
                         {
                           'Movenumber' => '3',
                           'Epd' => 'rnbqkbnr/ppp1pppp/8/3pP3/8/8/PPPP1PPP/RNBQKBNR b KQkq -',
                           'Movetext' => 'e5'
                         },
                         {
                           'Movenumber' => '4',
                           'Comment' => 'Playing ...Bf5 before closing the c8-h3 diagonal has  some positive features.',
                           'Epd' => 'rnbqkbnr/ppp2ppp/4p3/3pP3/8/8/PPPP1PPP/RNBQKBNR w KQkq -',
                           'Movetext' => 'e6'
                         },
                         {
                           'Movenumber' => '5',
                           'Epd' => 'rnbqkbnr/ppp2ppp/4p3/3pP3/3P4/8/PPP2PPP/RNBQKBNR b KQkq d3',
                           'Movetext' => 'd4'
                         },
                         {
                           'Movenumber' => '6',
                           'Comment' => 'Time to think like a Frenchie - c7-c5!',
                           'Epd' => 'r1bqkbnr/ppp2ppp/2n1p3/3pP3/3P4/8/PPP2PPP/RNBQKBNR w KQkq -',
                           'Movetext' => 'Nc6',
                           'Rav' => [
                                      {
                                        'Movenumber' => '6',
                                        'Epd' => 'rnbqkbnr/pp3ppp/4p3/2ppP3/3P4/8/PPP2PPP/RNBQKBNR w KQkq c6',
                                        'Movetext' => 'c5'
                                      }
                                    ]
                         },
.
.
.
                         {
                           'Movenumber' => '29',
                           'Comment' => ' (Bxe5) Black could  still kick for a while if he had played ...Bxe5.',
                           'Epd' => 'r1bq1rk1/2p1npb1/2n1p2P/pp1pP1p1/3P2P1/2P4Q/PP2BP2/RNB1K2R b KQ -',
                           'Movetext' => 'h6'
                         }
                       ]
       };

Briefly, the DOM is a multiply nested data structure of hashes and arrays. In a sort of outline form, it more or less follows this schematic:

I PGN Document Root
A. Extra-Game Comments
1. Before 1st Game
2. After Each Game
B. Games
1. Tagset
2. Extra-Gametext Comments
3. Gametext
a. Moves
1.) Movetext
2.) Comment
3.) NAG
4.) RAV (essentially an instance of Gametext)

The 'extra' comments have not yet been implemented. See the TODO list.

Owing to a dearth of imagination, there is but one exported routine in the module:

filter(parameter_hash)

There are however, a small host of known keys for parameter_hash and they are as follows:

  • keys common to all filtertypes

    • filtertype -- essentially which filter to use. Values implemented are:

      1XML -- converts from .pgn text in, to .xml file out. Validated by supplied pgn.dtd.
      1TEXT -- converts from .pgn text in, to .pgn out with reformatting of ill-formed text and other modifications possible. Global correction of tag values, error checking for game text termination etc. Blank lines and paragraph wrapping emplemented to match PGN standard.
      1DOM -- converts from .pgn text to a Document Object Model as expressed using Data::Dumper.
    • source -- name of file to convert, with output sent to STDOUT.

  • keys for filtertype TEXT

    • substitute -- simple text substitution mechanism applied globally (file scope) to all tag text.

      This is actually a hash reference where the hash reffered to has the form of (text_to_change => text_to_change_to). For instance:

      my %substitutions = (
          hsmyers => 'Myers, Hugh S (ID)'
      );

      as used in the SYNOPSIS example would expand my user name into a full version for any tag the former might occur in.

    • comments -- switch to include/exclude comments (defaults to 'no'.)

    • ravs -- switch to include/exclude recursive annotated variations (defaults to 'no'.)

    • nags -- switch to include/exclude numberic annotation glyphs (defaults to 'no'.)

    • ECO -- switch to include/exclude ECO tag (defaults to 'yes'.)

    • NIC -- switch to include/exclude NIC tag (defaults to 'no'.)

    • Opening -- switch to include/exclude Opening tag (defaults to 'yes'.)

    • exclude -- an array reference of tags to be excluded (defaults to undef.)

      This is an array reference where the referent has the form of (tag_to_exclude_1..tag_to_exclude_n), i.e.:

      my @exclude = qw(
          WhiteElo
          BlackElo
          EventDate
      );

      again, as used in the SYNOPSIS example, this would eliminate the 'WhiteElo', 'BlackElo' and 'EventDate' tags from the .pgn file being processed.

    • sticky -- switch to turn on/off 'sticky' nature of the data in the 'Event', 'Site' and 'Date' tags (defaults to 'yes'.) Essentially this allows a tag to remember and use the previous games tag if the tag contents for current game is either '?' or empty.

    • autoround -- switch to turn on/off autoincrement for the 'Round' tag (default is 'yes'.) Similar to 'sticky', if a 'Round' tag is either empty or set to '?' then the current tag is set to the value of the previous tag plus one.

  • keys for filtertype XML. These control the appearence of embedded positions reached during the game as well as the final position of the game.

    • fen -- switch to include/exclude fen information for each move (defaults to 'no'.)

    • position -- switch to control position diagrams in a game (defaults to 'yes'.)

      Possible values are:

      • 'nag' -- insert diagram for each {0} in game text.

      • 'end' -- insert diagram at end of game.

      • 'no' -- no diagrams from either source.

      • 'yes' -- create diagrams based on both embedded nags as well as at end of game.

    • font -- name of font to specify for embedded diagrams (default is 'Chess Kingdom'.)

      Following list shows font name, font designer. They are available from http://www.enpassant.dk/chess/fonteng.htm

      1Chess Cases -- Matthieu Leschemelle
      1Chess Adventurer -- Armando H. Marroquin
      1Chess Alfonso-X -- Armando H. Marroquin
      1Chess Alpha -- Eric Bentzen
      1Chess Berlin -- Eric Bentzen
      1Chess Condal -- Armando H. Marroquin
      1Chess Harlequin -- Armando H. Marroquin
      1Chess Kingdom -- Armando H. Marroquin
      1Chess Leipzig -- Armando H. Marroquin
      1Chess Line -- Armando H. Marroquin
      1Chess Lucena -- Armando H. Marroquin
      1Chess Magnetic -- Armando H. Marroquin
      1Chess Mark -- Armando H. Marroquin
      1Chess Marroquin -- Armando H. Marroquin
      1Chess Maya -- Armando H. Marroquin
      1Chess Mediaeval -- Armando H. Marroquin
      1Chess Mérida -- Armando H. Marroquin
      1Chess Millennia -- Armando H. Marroquin
      1Chess Miscel -- Armando H. Marroquin
      1Chess Montreal -- Gary Katch
      1Chess Motif -- Armando H. Marroquin
      1Chess Plain -- Alan Hickey
      1Chess Regular -- Alistair Scott
      1Chess Usual -- Armando H. Marroquin
      1Chess Utrecht -- Hans Bodlaender
      1Tilburg -- Eric Schiller and Bill Cone
      1Traveller Standard v3 -- Alan Cowderoy
    • border, values can be either 'single' or 'double' (default is 'single'.)

    • corner, values can be either 'square' or 'rounded' (default is 'square'.)

    • legend, values can be either 'yes' or 'no' (default is 'no'.)

    • size, value ranging from 1 to 6 that controls the size of the embedded diagram (default is 5.)

    Note -- not all fonts support all combinations of 'border', 'corner' and 'legend'. No warnings or errors will be generated by unsupported options, you get the best a font can do, no more!

EXPORT

filter - given a source file and specification, convert to supported output. See details in Description.

DEPENDENCIES

use Chess::PGN::Parse;
use Chess::PGN::EPD;
use Text::DelimMatch;
use Carp;
use Data::Dumper;

TODO

  • Add other output types, PDF, DHTML, LaTeX.

  • Add regular expressions to substitution mechanism.

  • Allow for 'extra' and 'inter' semicolon comments.

KNOWN BUGS

None known; Unknown? Of course, though I try to be neat...

AUTHOR

Hugh S. Myers

Always: hsmyers@sdragons.com

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 920:

Non-ASCII character seen before =encoding in 'Mérida'. Assuming CP1252

Around line 957:

You forgot a '=back' before '=head2'