Skip to content

Commit 6fea49f

Browse files
committed
Add support for pre-move comments in GameNode and StringExporter
1 parent 4d9b3bf commit 6fea49f

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

chess/pgn.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ def __init__(self, *, comment: Union[str, list[str]] = "") -> None:
213213
self.variations = []
214214
self.comments = _standardize_comments(comment)
215215

216+
self.pre_comment = ""
217+
216218
# Deprecated: These should be properties of ChildNode, but need to
217219
# remain here for backwards compatibility.
218220
self.starting_comments = []
@@ -759,6 +761,9 @@ def _accept_node(self, parent_board: chess.Board, visitor: BaseVisitor[ResultT])
759761
if self.starting_comments:
760762
visitor.visit_comment(self.starting_comments)
761763

764+
if self.pre_comment:
765+
visitor.visit_pre_comment(self.pre_comment)
766+
762767
visitor.visit_move(parent_board, self.move)
763768

764769
parent_board.push(self.move)
@@ -1142,6 +1147,12 @@ def visit_move(self, board: chess.Board, move: chess.Move) -> None:
11421147
"""
11431148
pass
11441149

1150+
def visit_pre_comment(self, comment: str) -> None:
1151+
"""
1152+
Called for a comment that appears immediately before the move token.
1153+
"""
1154+
pass
1155+
11451156
def visit_board(self, board: chess.Board) -> None:
11461157
"""
11471158
Called for the starting position of the game and after each move.
@@ -1447,6 +1458,11 @@ def pgn_format(comments: list[str]) -> str:
14471458
self.write_token(pgn_format(comments) + " ")
14481459
self.force_movenumber = True
14491460

1461+
def visit_pre_comment(self, comment: str) -> None:
1462+
if self.comments and (self.variations or not self.variation_depth):
1463+
self.write_token("{ " + comment.replace("}", "").strip() + " } ")
1464+
self.force_movenumber = True
1465+
14501466
def visit_nag(self, nag: int) -> None:
14511467
if self.comments and (self.variations or not self.variation_depth):
14521468
self.write_token("$" + str(nag) + " ")

0 commit comments

Comments
 (0)