Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions chess/pgn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) + " ")
Expand Down
Loading