fix(export): keep PGN move number, move, short comment and result together in HTML
public
fix(export): keep PGN move number, move, short comment and result together in HTML Each move forms a non-breaking unit (white-space: nowrap); the result joins the last one, so neither '1.' nor '*' ends up alone on a line. Comments longer than 30 characters stay separate and may wrap. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | 069644be22f97e32a4af0fbb48c962618d258a87 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | eddd4f2e0afc |
| Children | 4768c7bfd61c |
| Pushlog | by timo via ssh by timo via ssh |
modified schach/export.py +28 −9
| @@ -606,6 +606,7 @@ h1 { font-size: 1.4em; margin: 0 0 2px; | ||
| 606 | 606 | .pgn .mv:hover { background: color-mix(in srgb, var(--fg) 10%, transparent); } |
| 607 | 607 | .pgn .mv.sel { background: var(--accent); color: var(--accent-fg); } |
| 608 | 608 | .pgn .c, .pgn .h { color: var(--muted); } |
| 609 | +.pgn .u { white-space: nowrap; } | |
| 609 | 610 | button { font: inherit; color: inherit; background: var(--card); border: 1px solid var(--line); |
| 610 | 611 | border-radius: 6px; padding: 4px 10px; cursor: pointer; } |
| 611 | 612 | button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; } |
| @@ -692,26 +693,44 @@ _HTML_JS = """ | ||
| 692 | 693 | """ |
| 693 | 694 | |
| 694 | 695 | |
| 696 | +PGN_COMMENT_INLINE = 30 # längere Kommentare dürfen umbrechen, kürzere bleiben beim Zug | |
| 697 | + | |
| 698 | + | |
| 695 | 699 | def _html_pgn(pgn_text, selected): |
| 696 | - """PGN als HTML: jeder Zug ein <span data-ply>, damit das Skript den aktiven markieren kann.""" | |
| 700 | + """PGN als HTML: jeder Zug ein <span data-ply>, damit das Skript den aktiven markieren kann. | |
| 701 | + | |
| 702 | + Zugnummer, Zug, Zeichen und kurzer Kommentar bilden eine Einheit (``.u``), die nicht | |
| 703 | + umbricht; das Ergebnis hängt an der letzten. So steht nie „1.“ oder „*“ allein in einer Zeile. | |
| 704 | + """ | |
| 697 | 705 | esc = html_mod.escape |
| 698 | 706 | game = chess.pgn.read_game(io.StringIO(pgn_text)) |
| 699 | 707 | lines = [f'<span class="h">{esc(line)}</span>' for line in pgn_text.split("\n\n", 1)[0].splitlines()] |
| 700 | - words = [] | |
| 708 | + units = [] # [(html, darf angehängt werden)] | |
| 701 | 709 | board = game.board() |
| 702 | 710 | for ply, node in enumerate(game.mainline(), start=1): |
| 711 | + parts = [] | |
| 703 | 712 | if board.turn == chess.WHITE: |
| 704 | - words.append(f"{board.fullmove_number}.") | |
| 713 | + parts.append(f"{board.fullmove_number}.") | |
| 705 | 714 | elif ply == 1: |
| 706 | - words.append(f"{board.fullmove_number}...") | |
| 715 | + parts.append(f"{board.fullmove_number}...") | |
| 707 | 716 | san = board.san(node.move) + "".join(f" ${nag}" for nag in sorted(node.nags)) |
| 708 | 717 | sel = " sel" if ply == selected else "" |
| 709 | - words.append(f'<span class="mv{sel}" data-ply="{ply}">{esc(san)}</span>') | |
| 710 | - if node.comment: | |
| 711 | - words.append(f'<span class="c">{{ {esc(node.comment)} }}</span>') | |
| 718 | + parts.append(f'<span class="mv{sel}" data-ply="{ply}">{esc(san)}</span>') | |
| 719 | + comment = f'<span class="c">{{ {esc(node.comment)} }}</span>' if node.comment else "" | |
| 720 | + if comment and len(node.comment) <= PGN_COMMENT_INLINE: | |
| 721 | + parts.append(comment) | |
| 722 | + comment = "" | |
| 723 | + units.append([" ".join(parts), True]) | |
| 724 | + if comment: | |
| 725 | + units.append([comment, False]) # lang: eigener, umbrechbarer Teil | |
| 712 | 726 | board.push(node.move) |
| 713 | - words.append(esc(game.headers.get("Result", "*"))) | |
| 714 | - return "\n".join(lines) + "\n\n" + " ".join(words) | |
| 727 | + result = esc(game.headers.get("Result", "*")) | |
| 728 | + if units and units[-1][1]: | |
| 729 | + units[-1][0] += " " + result | |
| 730 | + else: | |
| 731 | + units.append([result, True]) | |
| 732 | + body = " ".join(f'<span class="u">{html}</span>' if keep else html for html, keep in units) | |
| 733 | + return "\n".join(lines) + "\n\n" + body | |
| 715 | 734 | |
| 716 | 735 | |
| 717 | 736 | def to_html(state, opts): |
modified tests/test_export.py +11 −0
| @@ -145,6 +145,17 @@ class ImageAndPageTest(unittest.TestCase | ||
| 145 | 145 | self.assertIn('data-ply="7">Nxe5 $4</span>', page) |
| 146 | 146 | self.assertIn('"pgn": "[Event', page) |
| 147 | 147 | self.assertNotIn("SchachHuman", page) |
| 148 | + # Nummer, Zug, Kommentar und Ergebnis bleiben zusammen | |
| 149 | + self.assertIn('<span class="u">1. <span class="mv" data-ply="1">e4</span> <span class="c">{ [%wp 50.0] }</span></span>', page) | |
| 150 | + self.assertRegex(page, r'data-ply="14">Nf3# \$3</span> <span class="c">\{ \[%wp 63\.0\] \}</span> 0-1</span>') | |
| 151 | + | |
| 152 | + def test_html_pgn_long_comment_may_wrap(self): | |
| 153 | + state = sample() | |
| 154 | + text = export.export("pgn", state, opts(scope="game")) | |
| 155 | + text = text.replace("{ [%wp 63.0] }", "{ " + "sehr langer Kommentar " * 5 + "}") | |
| 156 | + html = export._html_pgn(text, None) | |
| 157 | + self.assertRegex(html, r'</span> <span class="c">\{ sehr langer') # nicht in der Einheit | |
| 158 | + self.assertTrue(html.endswith('<span class="u">0-1</span>')) | |
| 148 | 159 | |
| 149 | 160 | def test_html_position_only(self): |
| 150 | 161 | page = export.export("html", sample(), opts(plies=3, players=False)) |