diff --git a/chess/pgn.py b/chess/pgn.py index f40980d4..2c96b75f 100644 --- a/chess/pgn.py +++ b/chess/pgn.py @@ -213,6 +213,8 @@ def __init__(self, *, comment: Union[str, list[str]] = "") -> None: self.variations = [] self.comments = _standardize_comments(comment) + self.pre_comment = "" + # Deprecated: These should be properties of ChildNode, but need to # remain here for backwards compatibility. self.starting_comments = [] @@ -759,6 +761,9 @@ def _accept_node(self, parent_board: chess.Board, visitor: BaseVisitor[ResultT]) if self.starting_comments: visitor.visit_comment(self.starting_comments) + if self.pre_comment: + visitor.visit_pre_comment(self.pre_comment) + visitor.visit_move(parent_board, self.move) parent_board.push(self.move) @@ -1142,6 +1147,12 @@ def visit_move(self, board: chess.Board, move: chess.Move) -> None: """ pass + def visit_pre_comment(self, comment: str) -> None: + """ + Called for a comment that appears immediately before the move token. + """ + pass + def visit_board(self, board: chess.Board) -> None: """ Called for the starting position of the game and after each move. @@ -1447,6 +1458,11 @@ def pgn_format(comments: list[str]) -> str: self.write_token(pgn_format(comments) + " ") self.force_movenumber = True + def visit_pre_comment(self, comment: str) -> None: + if self.comments and (self.variations or not self.variation_depth): + self.write_token("{ " + comment.replace("}", "").strip() + " } ") + self.force_movenumber = True + def visit_nag(self, nag: int) -> None: if self.comments and (self.variations or not self.variation_depth): self.write_token("$" + str(nag) + " ")