|
120 | 120 | SKIP_MOVETEXT_REGEX = re.compile(r""";|\{|\}""")
|
121 | 121 |
|
122 | 122 |
|
123 |
| -CLOCK_REGEX = re.compile(r"""\[%clk\s(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d+(?:\.\d*)?)\]""") |
124 |
| -EMT_REGEX = re.compile(r"""\[%emt\s(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d+(?:\.\d*)?)\]""") |
| 123 | +CLOCK_REGEX = re.compile(r"""(?P<prefix>\s?)\[%clk\s(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d+(?:\.\d*)?)\](?P<suffix>\s?)""") |
| 124 | +EMT_REGEX = re.compile(r"""(?P<prefix>\s?)\[%emt\s(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d+(?:\.\d*)?)\](?P<suffix>\s?)""") |
125 | 125 |
|
126 | 126 | EVAL_REGEX = re.compile(r"""
|
| 127 | + (?P<prefix>\s?) |
127 | 128 | \[%eval\s(?:
|
128 | 129 | \#(?P<mate>[+-]?\d+)
|
129 | 130 | |(?P<cp>[+-]?(?:\d{0,10}\.\d{1,2}|\d{1,10}\.?))
|
130 | 131 | )(?:
|
131 | 132 | ,(?P<depth>\d+)
|
132 | 133 | )?\]
|
| 134 | + (?P<suffix>\s?) |
133 | 135 | """, re.VERBOSE)
|
134 | 136 |
|
135 | 137 | ARROWS_REGEX = re.compile(r"""
|
| 138 | + (?P<prefix>\s?) |
136 | 139 | \[%(?:csl|cal)\s(?P<arrows>
|
137 | 140 | [RGYB][a-h][1-8](?:[a-h][1-8])?
|
138 | 141 | (?:,[RGYB][a-h][1-8](?:[a-h][1-8])?)*
|
139 | 142 | )\]
|
| 143 | + (?P<suffix>\s?) |
140 | 144 | """, re.VERBOSE)
|
141 | 145 |
|
| 146 | +def _condense_affix(infix: str): |
| 147 | + def repl(match): |
| 148 | + if infix: |
| 149 | + return match.group("prefix") + infix + match.group("suffix") |
| 150 | + else: |
| 151 | + return match.group("prefix") and match.group("suffix") |
| 152 | + return repl |
| 153 | + |
142 | 154 |
|
143 | 155 | TAG_ROSTER = ["Event", "Site", "Date", "Round", "White", "Black", "Result"]
|
144 | 156 |
|
@@ -434,7 +446,7 @@ def set_eval(self, score: Optional[chess.engine.PovScore], depth: Optional[int]
|
434 | 446 | elif score.white().mate():
|
435 | 447 | eval = f"[%eval #{score.white().mate()}{depth_suffix}]"
|
436 | 448 |
|
437 |
| - self.comment, found = EVAL_REGEX.subn(eval, self.comment, count=1) |
| 449 | + self.comment, found = EVAL_REGEX.subn(_condense_affix(eval), self.comment, count=1) |
438 | 450 |
|
439 | 451 | if not found and eval:
|
440 | 452 | if self.comment and not self.comment.endswith(" "):
|
@@ -471,16 +483,18 @@ def set_arrows(self, arrows: Iterable[Union[chess.svg.Arrow, Tuple[Square, Squar
|
471 | 483 | pass
|
472 | 484 | (csl if arrow.tail == arrow.head else cal).append(arrow.pgn()) # type: ignore
|
473 | 485 |
|
474 |
| - self.comment = ARROWS_REGEX.sub("", self.comment).strip() |
| 486 | + self.comment = ARROWS_REGEX.sub(_condense_affix(""), self.comment) |
475 | 487 |
|
476 | 488 | prefix = ""
|
477 | 489 | if csl:
|
478 | 490 | prefix += f"[%csl {','.join(csl)}]"
|
479 | 491 | if cal:
|
480 | 492 | prefix += f"[%cal {','.join(cal)}]"
|
481 | 493 |
|
482 |
| - if prefix: |
483 |
| - self.comment = prefix + " " + self.comment if self.comment else prefix |
| 494 | + if prefix and self.comment and not self.comment.startswith(" ") and not self.comment.startswith("\n"): |
| 495 | + self.comment = prefix + " " + self.comment |
| 496 | + else: |
| 497 | + self.comment = prefix + self.comment |
484 | 498 |
|
485 | 499 | def clock(self) -> Optional[float]:
|
486 | 500 | """
|
@@ -509,7 +523,7 @@ def set_clock(self, seconds: Optional[float]) -> None:
|
509 | 523 | seconds_part = f"{seconds:06.3f}".rstrip("0").rstrip(".")
|
510 | 524 | clk = f"[%clk {hours:d}:{minutes:02d}:{seconds_part}]"
|
511 | 525 |
|
512 |
| - self.comment, found = CLOCK_REGEX.subn(clk, self.comment, count=1) |
| 526 | + self.comment, found = CLOCK_REGEX.subn(_condense_affix(clk), self.comment, count=1) |
513 | 527 |
|
514 | 528 | if not found and clk:
|
515 | 529 | if self.comment and not self.comment.endswith(" ") and not self.comment.endswith("\n"):
|
@@ -543,7 +557,7 @@ def set_emt(self, seconds: Optional[float]) -> None:
|
543 | 557 | seconds_part = f"{seconds:06.3f}".rstrip("0").rstrip(".")
|
544 | 558 | emt = f"[%emt {hours:d}:{minutes:02d}:{seconds_part}]"
|
545 | 559 |
|
546 |
| - self.comment, found = EMT_REGEX.subn(emt, self.comment, count=1) |
| 560 | + self.comment, found = EMT_REGEX.subn(_condense_affix(emt), self.comment, count=1) |
547 | 561 |
|
548 | 562 | if not found and emt:
|
549 | 563 | if self.comment and not self.comment.endswith(" ") and not self.comment.endswith("\n"):
|
|
0 commit comments