From 29f778308b4d20f427238fa6662a2db4902df1f7 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Tue, 13 Jun 2023 22:11:51 +0200 Subject: [PATCH] Fix issue #1. But not the dangerous problems with (9) and (13). --- PgnParser.php | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/PgnParser.php b/PgnParser.php index 256224a..9403b43 100755 --- a/PgnParser.php +++ b/PgnParser.php @@ -64,25 +64,40 @@ class PgnParser private function cleanPgn() { $c = $this->pgnContent; - $c = preg_replace('/"\]\s{0,10}\[/s', "\"]\n[", $c); - $c = preg_replace('/"\]\s{0,10}([\.0-9]|{)/s', "\"]\n\n$1", $c); + /* remove CR and TAB */ + $c = preg_replace("/[\t\r]/s", "", $c); - $c = preg_replace("/{\s{0,6}\[%emt[^\}]*?\}/", "", $c); + /* set one '\n' between tags. Will also add a newline in comments like: + * { [%emt ...] [%clk ...] } + */ + $c = preg_replace('/"\]\s*\[/s', "\"]\n[", $c); + /* set '\n\n' between tags and movetext section */ + $c = preg_replace('/"\]\s*([\.0-9]|{)/s', "\"]\n\n$1", $c); + + /* separate first comment of variation + * NOTE: I am unsure a sub-variation can start with a comment. + */ $c = str_replace("({", "( {", $c); - $c = preg_replace("/{([^\[]*?)\[([^}]?)}/s", '{$1-SB-$2}', $c); - $c = preg_replace("/\r/s", "", $c); - $c = preg_replace("/\t/s", "", $c); - $c = preg_replace("/\]\s+\[/s", "]\n[", $c); + + /* USELESS? remove space before [ */ $c = str_replace(" [", "[", $c); + + /* Try to set '\n\n' between non tag line and tag line (between two games) + * DESTRUCTIVE, as it will destroy files with : + * ... { or 1.e4 + * [%action ...] } + * This needs to be handled in parser, not by regex. + */ $c = preg_replace("/([^\]])(\n+)\[/si", "$1\n\n[", $c); + + /* max 2 consecutive '\n */ $c = preg_replace("/\n{3,}/s", "\n\n", $c); - $c = str_replace("-SB-", "[", $c); + + /* replace 0 in castle with O */ $c = str_replace("0-0-0", "O-O-O", $c); $c = str_replace("0-0", "O-O", $c); - $c = preg_replace('/^([^\[])*?\[/', '[', $c); - return $c; }