feat(export): HTML shows full-width FEN and the PGN with the current move marked
public
feat(export): HTML shows full-width FEN and the PGN with the current move marked Each PGN move is a span; clicking it jumps there. Copy buttons sit next to the FEN and PGN headings. Internal Schach* headers stay out of the page, and names too when player heads are off. Scrolling keeps to the move list and PGN box. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | eddd4f2e0afc7183709be1264771f68d97a2250f |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | 1c86dc9e9a1e |
| Children | 069644be22f9 |
| Pushlog | by timo via ssh by timo via ssh |
modified schach/export.py +86 −22
| @@ -595,15 +595,23 @@ h1 { font-size: 1.4em; margin: 0 0 2px; | ||
| 595 | 595 | #bar span { display: block; background: #f2f2ee; transition: height .2s; } |
| 596 | 596 | .labels.flip, .flipped .labels.normal { display: none; } |
| 597 | 597 | .flipped .labels.flip { display: inline; } |
| 598 | -.caption { margin: 8px 0 4px; } | |
| 599 | -.fenrow { display: flex; gap: 6px; align-items: center; } | |
| 600 | -.fen { flex: 1; min-width: 0; overflow-wrap: anywhere; user-select: all; -webkit-user-select: all; | |
| 601 | - font: 13px/1.3 ui-monospace, monospace; background: var(--card); border: 1px solid var(--line); | |
| 602 | - border-radius: 6px; padding: 6px 8px; } | |
| 598 | +.caption { margin: 8px 0 0; } | |
| 599 | +.full { grid-column: 1 / -1; min-width: 0; } | |
| 600 | +.full h2 { font-size: .8em; font-weight: 600; letter-spacing: .04em; color: var(--muted); margin: 0; } | |
| 601 | +.fen, .pgn { display: block; margin: 0; font: 13px/1.45 ui-monospace, monospace; background: var(--card); | |
| 602 | + border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; overflow-wrap: anywhere; } | |
| 603 | +.fen { user-select: all; -webkit-user-select: all; } | |
| 604 | +.pgn { white-space: pre-wrap; max-height: 28em; overflow-y: auto; } | |
| 605 | +.pgn .mv { border-radius: 3px; padding: 0 1px; cursor: pointer; } | |
| 606 | +.pgn .mv:hover { background: color-mix(in srgb, var(--fg) 10%, transparent); } | |
| 607 | +.pgn .mv.sel { background: var(--accent); color: var(--accent-fg); } | |
| 608 | +.pgn .c, .pgn .h { color: var(--muted); } | |
| 603 | 609 | button { font: inherit; color: inherit; background: var(--card); border: 1px solid var(--line); |
| 604 | 610 | border-radius: 6px; padding: 4px 10px; cursor: pointer; } |
| 605 | 611 | button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; } |
| 606 | 612 | .nav { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; } |
| 613 | +.full .head { display: flex; align-items: end; justify-content: space-between; margin-bottom: 4px; } | |
| 614 | +.full .head button { font-size: .85em; padding: 2px 10px; } | |
| 607 | 615 | .moves { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 6px; |
| 608 | 616 | max-height: 70vh; overflow-y: auto; display: grid; grid-template-columns: auto 1fr 1fr; gap: 3px 4px; |
| 609 | 617 | align-content: start; } |
| @@ -624,6 +632,7 @@ _HTML_JS = """ | ||
| 624 | 632 | const svg = document.getElementById('board'); |
| 625 | 633 | const groups = [...svg.querySelectorAll('g.ply')]; |
| 626 | 634 | const buttons = [...document.querySelectorAll('.moves button[data-ply]')]; |
| 635 | + const pgnMoves = [...document.querySelectorAll('.pgn .mv')]; | |
| 627 | 636 | const fen = document.getElementById('fen'); |
| 628 | 637 | const cap = document.getElementById('caption'); |
| 629 | 638 | const bar = document.getElementById('bar'); |
| @@ -637,33 +646,74 @@ _HTML_JS = """ | ||
| 637 | 646 | } |
| 638 | 647 | if (bar) bar.classList.toggle('flip', flipped); |
| 639 | 648 | } |
| 649 | + function keep(el) { | |
| 650 | + // nur im eigenen Kasten scrollen – scrollIntoView würde die ganze Seite mitziehen | |
| 651 | + const box = el && el.closest('.moves, .pgn'); | |
| 652 | + if (!box) return; | |
| 653 | + const top = el.offsetTop - box.offsetTop, bottom = top + el.offsetHeight; | |
| 654 | + if (top < box.scrollTop) box.scrollTop = top - 4; | |
| 655 | + else if (bottom > box.scrollTop + box.clientHeight) box.scrollTop = bottom - box.clientHeight + 4; | |
| 656 | + } | |
| 657 | + function byPly(p) { return data.plies.findIndex(i => i.p === p); } | |
| 640 | 658 | function show(p) { |
| 641 | 659 | cur = Math.max(0, Math.min(data.plies.length - 1, p)); |
| 642 | 660 | const info = data.plies[cur]; |
| 643 | 661 | for (const g of groups) g.style.display = +g.dataset.ply === info.p ? '' : 'none'; |
| 644 | 662 | for (const b of buttons) b.classList.toggle('sel', +b.dataset.ply === info.p); |
| 645 | - const sel = buttons.find(b => +b.dataset.ply === info.p); | |
| 646 | - if (sel) sel.scrollIntoView({block: 'nearest'}); | |
| 663 | + for (const m of pgnMoves) m.classList.toggle('sel', +m.dataset.ply === info.p); | |
| 664 | + keep(buttons.find(b => +b.dataset.ply === info.p)); | |
| 665 | + keep(pgnMoves.find(m => +m.dataset.ply === info.p)); | |
| 647 | 666 | fen.textContent = info.fen; |
| 648 | 667 | cap.textContent = info.caption; |
| 649 | 668 | if (bar) bar.firstElementChild.style.height = (info.wp == null ? 50 : info.wp) + '%'; |
| 650 | 669 | } |
| 651 | - for (const b of buttons) b.addEventListener('click', () => show(data.plies.findIndex(i => i.p === +b.dataset.ply))); | |
| 670 | + for (const b of buttons) b.addEventListener('click', () => show(byPly(+b.dataset.ply))); | |
| 671 | + for (const m of pgnMoves) m.addEventListener('click', () => show(byPly(+m.dataset.ply))); | |
| 672 | + function copy(text, button) { | |
| 673 | + if (!navigator.clipboard) return; | |
| 674 | + navigator.clipboard.writeText(text).then(() => { | |
| 675 | + const label = button.textContent; | |
| 676 | + button.textContent = 'Kopiert ✓'; | |
| 677 | + setTimeout(() => { button.textContent = label; }, 1200); | |
| 678 | + }); | |
| 679 | + } | |
| 652 | 680 | const act = {first: () => show(0), prev: () => show(cur - 1), next: () => show(cur + 1), |
| 653 | 681 | last: () => show(data.plies.length - 1), flip: () => { flipped = !flipped; place(); }, |
| 654 | - copy: () => navigator.clipboard && navigator.clipboard.writeText(fen.textContent)}; | |
| 655 | - for (const b of document.querySelectorAll('[data-act]')) b.addEventListener('click', () => act[b.dataset.act]()); | |
| 682 | + 'copy-fen': b => copy(fen.textContent, b), 'copy-pgn': b => copy(data.pgn, b)}; | |
| 683 | + for (const b of document.querySelectorAll('[data-act]')) b.addEventListener('click', () => act[b.dataset.act](b)); | |
| 656 | 684 | document.addEventListener('keydown', e => { |
| 657 | 685 | if (e.altKey || e.ctrlKey || e.metaKey) return; |
| 658 | 686 | const k = {ArrowLeft: 'prev', ArrowRight: 'next', Home: 'first', End: 'last', f: 'flip'}[e.key]; |
| 659 | 687 | if (k) { act[k](); e.preventDefault(); } |
| 660 | 688 | }); |
| 661 | - document.querySelector('.nav').hidden = false; | |
| 689 | + for (const el of document.querySelectorAll('.nav, .js')) el.hidden = false; | |
| 662 | 690 | place(); show(cur); |
| 663 | 691 | })(); |
| 664 | 692 | """ |
| 665 | 693 | |
| 666 | 694 | |
| 695 | +def _html_pgn(pgn_text, selected): | |
| 696 | + """PGN als HTML: jeder Zug ein <span data-ply>, damit das Skript den aktiven markieren kann.""" | |
| 697 | + esc = html_mod.escape | |
| 698 | + game = chess.pgn.read_game(io.StringIO(pgn_text)) | |
| 699 | + lines = [f'<span class="h">{esc(line)}</span>' for line in pgn_text.split("\n\n", 1)[0].splitlines()] | |
| 700 | + words = [] | |
| 701 | + board = game.board() | |
| 702 | + for ply, node in enumerate(game.mainline(), start=1): | |
| 703 | + if board.turn == chess.WHITE: | |
| 704 | + words.append(f"{board.fullmove_number}.") | |
| 705 | + elif ply == 1: | |
| 706 | + words.append(f"{board.fullmove_number}...") | |
| 707 | + san = board.san(node.move) + "".join(f" ${nag}" for nag in sorted(node.nags)) | |
| 708 | + 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>') | |
| 712 | + board.push(node.move) | |
| 713 | + words.append(esc(game.headers.get("Result", "*"))) | |
| 714 | + return "\n".join(lines) + "\n\n" + " ".join(words) | |
| 715 | + | |
| 716 | + | |
| 667 | 717 | def to_html(state, opts): |
| 668 | 718 | plies = _plies(state, opts) |
| 669 | 719 | game = opts.scope == "game" and bool(state.board.move_stack) |
| @@ -677,7 +727,16 @@ def to_html(state, opts): | ||
| 677 | 727 | b = board_at(state, p) |
| 678 | 728 | entries.append({"p": p, "fen": b.fen(), "caption": caption(b, opts.notation), |
| 679 | 729 | "wp": _eval(state, p, opts)}) |
| 680 | - data = {"plies": entries, "start": plies_list.index(plies), "flipped": opts.flipped} | |
| 730 | + # PGN für die Seite: ohne die programminternen Schach…-Kopfzeilen, | |
| 731 | + # ohne Spielerköpfe auch ohne Namen | |
| 732 | + pgn_game = chess.pgn.read_game(io.StringIO(to_pgn(state, opts))) | |
| 733 | + for key in [k for k in pgn_game.headers if k.startswith("Schach")]: | |
| 734 | + del pgn_game.headers[key] | |
| 735 | + if not opts.players: | |
| 736 | + for key in ("Event", "White", "Black"): | |
| 737 | + pgn_game.headers[key] = "?" | |
| 738 | + pgn_text = str(pgn_game) + "\n" | |
| 739 | + data = {"plies": entries, "start": plies_list.index(plies), "flipped": opts.flipped, "pgn": pgn_text} | |
| 681 | 740 | data_json = json.dumps(data, ensure_ascii=False).replace("</", "<\\/") |
| 682 | 741 | |
| 683 | 742 | title = i.title if opts.players else "Schachstellung" if not game else "Schachpartie" |
| @@ -693,22 +752,27 @@ def to_html(state, opts): | ||
| 693 | 752 | bar = f'<div id="bar" class="{"flip" if opts.flipped else ""}" title="Gewinnchance Weiß"><span style="height:{height}%"></span></div>' |
| 694 | 753 | |
| 695 | 754 | svg = _html_board(state, opts, plies_list) |
| 696 | - nav = "" | |
| 755 | + nav = '<div class="nav" hidden>' | |
| 697 | 756 | if game: |
| 698 | - nav = ( | |
| 699 | - '<div class="nav" hidden><button data-act="first" title="Anfang (Pos1)">⏮</button>' | |
| 757 | + nav += ( | |
| 758 | + '<button data-act="first" title="Anfang (Pos1)">⏮</button>' | |
| 700 | 759 | '<button data-act="prev" title="Zurück (←)">◀</button>' |
| 701 | 760 | '<button data-act="next" title="Vor (→)">▶</button>' |
| 702 | 761 | '<button data-act="last" title="Ende (Ende)">⏭</button>' |
| 703 | - '<button data-act="flip" title="Brett drehen (F)">⇅</button></div>' | |
| 704 | 762 | ) |
| 705 | - else: | |
| 706 | - nav = '<div class="nav" hidden><button data-act="flip" title="Brett drehen (F)">⇅</button></div>' | |
| 763 | + nav += ( | |
| 764 | + '<button data-act="flip" title="Brett drehen (F)">⇅</button></div>' | |
| 765 | + ) | |
| 707 | 766 | left = ( |
| 708 | 767 | f'<section><div class="boardwrap">{bar}{svg}</div>{nav}' |
| 709 | - f'<p class="caption" id="caption">{esc(caption(board, opts.notation))}</p>' | |
| 710 | - f'<div class="fenrow"><code class="fen" id="fen" title="FEN – Klick markiert alles">{esc(board.fen())}</code>' | |
| 711 | - f'<button data-act="copy" title="FEN kopieren">Kopieren</button></div></section>' | |
| 768 | + f'<p class="caption" id="caption">{esc(caption(board, opts.notation))}</p></section>' | |
| 769 | + ) | |
| 770 | + below = ( | |
| 771 | + f'<section class="full"><div class="head"><h2>FEN</h2>' | |
| 772 | + f'<button data-act="copy-fen" class="js" hidden>Kopieren</button></div>' | |
| 773 | + f'<code class="fen" id="fen" title="Klick markiert alles">{esc(board.fen())}</code></section>' | |
| 774 | + f'<section class="full"><div class="head"><h2>PGN</h2>' | |
| 775 | + f'<button data-act="copy-pgn" class="js" hidden>Kopieren</button></div><pre class="pgn">{_html_pgn(pgn_text, plies if game else None)}</pre></section>' | |
| 712 | 776 | ) |
| 713 | 777 | |
| 714 | 778 | right = "" |
| @@ -736,7 +800,7 @@ def to_html(state, opts): | ||
| 736 | 800 | "<!doctype html>\n<html lang=\"de\">\n<head>\n<meta charset=\"utf-8\">\n" |
| 737 | 801 | "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n" |
| 738 | 802 | f"<title>{esc(title)}</title>\n<style>{_HTML_CSS}</style>\n</head>\n<body>\n" |
| 739 | - f'<main class="{"game" if game else "position"}">{head}{left}{right}</main>\n' | |
| 803 | + f'<main class="{"game" if game else "position"}">{head}{left}{right}{below}</main>\n' | |
| 740 | 804 | f'<script type="application/json" id="data">{data_json}</script>\n' |
| 741 | 805 | f"<script>{_HTML_JS}</script>\n</body>\n</html>\n" |
| 742 | 806 | ) |
modified tests/test_export.py +6 −0
| @@ -139,6 +139,12 @@ class ImageAndPageTest(unittest.TestCase | ||
| 139 | 139 | self.assertIn('"start": 8', page) |
| 140 | 140 | self.assertIn("user-select: all", page) |
| 141 | 141 | self.assertIn("Sxe5", page) |
| 142 | + # PGN mit Zügen als Spans, der gezeigte (8. Halbzug) ist markiert | |
| 143 | + self.assertIn('<span class="mv sel" data-ply="8">Qg5</span>', page) | |
| 144 | + self.assertEqual(len(re.findall(r'<span class="mv', page)), len(MOVES)) | |
| 145 | + self.assertIn('data-ply="7">Nxe5 $4</span>', page) | |
| 146 | + self.assertIn('"pgn": "[Event', page) | |
| 147 | + self.assertNotIn("SchachHuman", page) | |
| 142 | 148 | |
| 143 | 149 | def test_html_position_only(self): |
| 144 | 150 | page = export.export("html", sample(), opts(plies=3, players=False)) |