| @@ -13,6 +13,7 @@ import datetime |
| 13 | 13 | import html as html_mod |
| 14 | 14 | import io |
| 15 | 15 | import json |
| 16 | +import re |
| 16 | 17 | import os |
| 17 | 18 | import shutil |
| 18 | 19 | import subprocess |
| @@ -22,7 +23,7 @@ import textwrap |
| 22 | 23 | import chess |
| 23 | 24 | import chess.pgn |
| 24 | 25 | |
| 25 | | -from . import notation, savegames |
| 26 | +from . import notation, opening, savegames |
| 26 | 27 | from .diagram import ( |
| 27 | 28 | BADGE, BAR_BLACK, BAR_WHITE, BUILTIN_THEME, CHECK, DEFAULT_SCHEME, Diagram, hex_color, piece_svg, |
| 28 | 29 | scheme_colors, |
| @@ -101,6 +102,12 @@ class Info: |
| 101 | 102 | date: str # TT.MM.JJJJ |
| 102 | 103 | result: str # "1-0", "0-1", "1/2-1/2", "*" |
| 103 | 104 | end_text: str # z. B. "Schwarz hat aufgegeben." |
| 105 | + opening: str = "" # „C55 Zweispringerspiel“ |
| 106 | + |
| 107 | + def meta(self): |
| 108 | + """Zeile unter der Überschrift: Anlass · Datum · Ergebnis · Ende · Eröffnung.""" |
| 109 | + parts = (self.event, self.date, RESULT_TEXT.get(self.result, ""), self.end_text, self.opening) |
| 110 | + return " · ".join(p for p in parts if p and p != "?") |
| 104 | 111 | |
| 105 | 112 | @property |
| 106 | 113 | def title(self): |
| @@ -115,9 +122,42 @@ def info(state): |
| 115 | 122 | white=headers.get("White", "?"), black=headers.get("Black", "?"), |
| 116 | 123 | event=headers.get("Event", ""), date=date, result=headers.get("Result", "*"), |
| 117 | 124 | end_text=state.final_text or "", |
| 125 | + opening=" ".join(book_end(state)[1:]), |
| 118 | 126 | ) |
| 119 | 127 | |
| 120 | 128 | |
| 129 | +_NAMES = None |
| 130 | +BOOK_SEARCH_PLIES = 60 # weiter hinten stehen keine Eröffnungen mehr in eco.db |
| 131 | + |
| 132 | + |
| 133 | +def _names(): |
| 134 | + global _NAMES |
| 135 | + if _NAMES is None: |
| 136 | + _NAMES = opening.Names() |
| 137 | + return _NAMES |
| 138 | + |
| 139 | + |
| 140 | +def book_end(state, plies=None): |
| 141 | + """(Halbzug-Index, ECO, Name) des letzten Zugs, dessen Stellung eco.db kennt; sonst (None, "", ""). |
| 142 | + |
| 143 | + ``plies``: nur bis zu dieser Stellung suchen (für den Export einer Stellung). |
| 144 | + """ |
| 145 | + names = _names() |
| 146 | + found = (None, "", "") |
| 147 | + board = state.board.root() |
| 148 | + stop = len(state.board.move_stack) if plies is None else plies |
| 149 | + for ply, move in enumerate(state.board.move_stack[:min(stop, BOOK_SEARCH_PLIES)]): |
| 150 | + board.push(move) |
| 151 | + entry = names.exact_eco(board) |
| 152 | + if entry: |
| 153 | + found = (ply, *entry) |
| 154 | + return found |
| 155 | + |
| 156 | + |
| 157 | +def book_comment(eco, name): |
| 158 | + return f"Bis hier: {eco} {name}".replace(" ", " ") |
| 159 | + |
| 160 | + |
| 121 | 161 | RESULT_TEXT = {"1-0": "1–0", "0-1": "0–1", "1/2-1/2": "½–½", "*": ""} |
| 122 | 162 | |
| 123 | 163 | |
| @@ -194,23 +234,53 @@ def to_fen(state, opts): |
| 194 | 234 | |
| 195 | 235 | |
| 196 | 236 | def to_pgn(state, opts): |
| 237 | + """PGN mit Eröffnung (Kopfzeilen ECO/Opening, Kommentar „Bis hier: …“ am letzten Buchzug).""" |
| 197 | 238 | game = chess.pgn.read_game(io.StringIO(savegames.to_pgn(state))) |
| 198 | 239 | if opts.scope == "position": |
| 199 | | - board = board_at(state, _plies(state, opts)) |
| 240 | + plies = _plies(state, opts) |
| 241 | + board = board_at(state, plies) |
| 200 | 242 | position = chess.pgn.Game() |
| 201 | 243 | for key in ("Event", "Date", "White", "Black"): |
| 202 | 244 | position.headers[key] = game.headers.get(key, "?") |
| 203 | 245 | position.headers["Result"] = "*" |
| 246 | + _, eco, name = book_end(state, plies) |
| 247 | + if name: |
| 248 | + position.headers["ECO"], position.headers["Opening"] = eco or "?", name |
| 204 | 249 | position.setup(board) |
| 205 | 250 | return str(position) + "\n" |
| 206 | | - for node in game.mainline(): |
| 251 | + book_ply, eco, name = book_end(state) |
| 252 | + if name: |
| 253 | + game.headers["ECO"], game.headers["Opening"] = eco or "?", name |
| 254 | + for ply, node in enumerate(game.mainline()): |
| 207 | 255 | if not opts.symbols: |
| 208 | 256 | node.nags.clear() |
| 209 | 257 | if not opts.evals: |
| 210 | 258 | node.comment = "" |
| 259 | + if ply == book_ply: |
| 260 | + node.comment = " ".join(filter(None, [book_comment(eco, name), node.comment])) |
| 211 | 261 | return str(game) + "\n" |
| 212 | 262 | |
| 213 | 263 | |
| 264 | +_NAG_GLYPH = savegames.SYMBOL_FOR_NAG # 4 → "??" |
| 265 | + |
| 266 | + |
| 267 | +def page_pgn(state, opts): |
| 268 | + """PGN für die HTML-Seite: Zeichen statt NAG-Nummern, ohne [%wp] und ohne Schach…-Kopfzeilen; |
| 269 | + ohne Spielerköpfe auch ohne Namen.""" |
| 270 | + game = chess.pgn.read_game(io.StringIO(to_pgn(state, opts))) |
| 271 | + for key in [k for k in game.headers if k.startswith("Schach")]: |
| 272 | + del game.headers[key] |
| 273 | + if not opts.players: |
| 274 | + for key in ("Event", "White", "Black"): |
| 275 | + game.headers[key] = "?" |
| 276 | + for node in game.mainline(): |
| 277 | + node.comment = " ".join(savegames.WP_RE.sub("", node.comment or "").split()) |
| 278 | + text = str(game) + "\n" |
| 279 | + head, _, moves = text.partition("\n\n") |
| 280 | + moves = re.sub(r" \$(\d+)", lambda m: _NAG_GLYPH.get(int(m.group(1)), m.group(0)), moves) |
| 281 | + return head + "\n\n" + moves |
| 282 | + |
| 283 | + |
| 214 | 284 | _ASCII_DE = {"K": "K", "Q": "D", "R": "T", "B": "L", "N": "S", "P": "B"} |
| 215 | 285 | |
| 216 | 286 | |
| @@ -242,7 +312,7 @@ def to_ascii(state, opts): |
| 242 | 312 | i = info(state) |
| 243 | 313 | if opts.players: |
| 244 | 314 | result = RESULT_TEXT.get(i.result, "") |
| 245 | | - lines += [f"{i.title}{' ' + result if result else ''}", i.date, ""] |
| 315 | + lines += [f"{i.title}{' ' + result if result else ''}", " · ".join(filter(None, [i.date, i.opening])), ""] |
| 246 | 316 | lines += _ascii_board(board, opts) |
| 247 | 317 | lines += ["", caption(board, opts.notation), "FEN: " + board.fen()] |
| 248 | 318 | if opts.scope == "game" and state.board.move_stack: |
| @@ -295,7 +365,7 @@ def to_latex(state, opts): |
| 295 | 365 | ] |
| 296 | 366 | if opts.players: |
| 297 | 367 | out.append(f"\\section*{{{_tex(i.white)} -- {_tex(i.black)}}}") |
| 298 | | - meta = [m for m in (i.event, i.date) if m and m != "?"] |
| 368 | + meta = [m for m in (i.event, i.date, i.opening) if m and m != "?"] |
| 299 | 369 | out.append("\\noindent " + _tex(" · ".join(meta)).replace("·", "\\textperiodcentered{}") + "\n") |
| 300 | 370 | root = state.board.root() |
| 301 | 371 | setfen = "" if root.fen() == chess.STARTING_FEN else f"[setfen={root.fen()}]" |
| @@ -390,8 +460,7 @@ def to_pdf(state, opts): |
| 390 | 460 | y = PDF_MARGIN |
| 391 | 461 | if opts.players: |
| 392 | 462 | y += text(html_mod.escape(i.title), left, y, size=18, bold=True, w=text_width) |
| 393 | | - meta = [m for m in (i.event, i.date, RESULT_TEXT.get(i.result, ""), i.end_text) if m and m != "?"] |
| 394 | | - y += text(html_mod.escape(" · ".join(meta)), left, y + 2, size=10, w=text_width) + 14 |
| 463 | + y += text(html_mod.escape(i.meta()), left, y + 2, size=10, w=text_width) + 14 |
| 395 | 464 | |
| 396 | 465 | plies = _plies(state, opts) |
| 397 | 466 | board = board_at(state, plies) |
| @@ -607,6 +676,8 @@ h1 { font-size: 1.4em; margin: 0 0 2px; |
| 607 | 676 | .pgn .mv.sel { background: var(--accent); color: var(--accent-fg); } |
| 608 | 677 | .pgn .c, .pgn .h { color: var(--muted); } |
| 609 | 678 | .pgn .u { white-space: nowrap; } |
| 679 | +.pgn b.s { font-weight: 800; } |
| 680 | +.pgn .mv.sel b.s { color: inherit !important; } |
| 610 | 681 | button { font: inherit; color: inherit; background: var(--card); border: 1px solid var(--line); |
| 611 | 682 | border-radius: 6px; padding: 4px 10px; cursor: pointer; } |
| 612 | 683 | button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; } |
| @@ -713,9 +784,15 @@ def _html_pgn(pgn_text, selected): |
| 713 | 784 | parts.append(f"{board.fullmove_number}.") |
| 714 | 785 | elif ply == 1: |
| 715 | 786 | parts.append(f"{board.fullmove_number}...") |
| 716 | | - san = board.san(node.move) + "".join(f" ${nag}" for nag in sorted(node.nags)) |
| 787 | + san = esc(board.san(node.move)) |
| 788 | + for nag in sorted(node.nags): |
| 789 | + glyph = _NAG_GLYPH.get(nag) |
| 790 | + if glyph: |
| 791 | + san += f'<b class="s" style="color:{hex_color(BADGE[glyph])}">{esc(glyph)}</b>' |
| 792 | + else: |
| 793 | + san += f" ${nag}" |
| 717 | 794 | sel = " sel" if ply == selected else "" |
| 718 | | - parts.append(f'<span class="mv{sel}" data-ply="{ply}">{esc(san)}</span>') |
| 795 | + parts.append(f'<span class="mv{sel}" data-ply="{ply}">{san}</span>') |
| 719 | 796 | comment = f'<span class="c">{{ {esc(node.comment)} }}</span>' if node.comment else "" |
| 720 | 797 | if comment and len(node.comment) <= PGN_COMMENT_INLINE: |
| 721 | 798 | parts.append(comment) |
| @@ -746,23 +823,14 @@ def to_html(state, opts): |
| 746 | 823 | b = board_at(state, p) |
| 747 | 824 | entries.append({"p": p, "fen": b.fen(), "caption": caption(b, opts.notation), |
| 748 | 825 | "wp": _eval(state, p, opts)}) |
| 749 | | - # PGN für die Seite: ohne die programminternen Schach…-Kopfzeilen, |
| 750 | | - # ohne Spielerköpfe auch ohne Namen |
| 751 | | - pgn_game = chess.pgn.read_game(io.StringIO(to_pgn(state, opts))) |
| 752 | | - for key in [k for k in pgn_game.headers if k.startswith("Schach")]: |
| 753 | | - del pgn_game.headers[key] |
| 754 | | - if not opts.players: |
| 755 | | - for key in ("Event", "White", "Black"): |
| 756 | | - pgn_game.headers[key] = "?" |
| 757 | | - pgn_text = str(pgn_game) + "\n" |
| 826 | + pgn_text = page_pgn(state, opts) |
| 758 | 827 | data = {"plies": entries, "start": plies_list.index(plies), "flipped": opts.flipped, "pgn": pgn_text} |
| 759 | 828 | data_json = json.dumps(data, ensure_ascii=False).replace("</", "<\\/") |
| 760 | 829 | |
| 761 | 830 | title = i.title if opts.players else "Schachstellung" if not game else "Schachpartie" |
| 762 | 831 | head = "" |
| 763 | 832 | if opts.players: |
| 764 | | - meta = [m for m in (i.event, i.date, RESULT_TEXT.get(i.result, ""), i.end_text) if m and m != "?"] |
| 765 | | - head = f'<header><h1>{esc(i.title)}</h1><p class="meta">{esc(" · ".join(meta))}</p></header>' |
| 833 | + head = f'<header><h1>{esc(i.title)}</h1><p class="meta">{esc(i.meta())}</p></header>' |
| 766 | 834 | |
| 767 | 835 | bar = "" |
| 768 | 836 | wp = _eval(state, plies, opts) |