Feature/variations (#5)

* support diagrams in variations.

* Diagram data now carries an optional `path` (the SAN move sequence from the root) alongside the legacy `ply`; diagrams whose `path` resolves inside a variation are rendered inline within the variation. `parse_diagrams_json` returns a `diagram_data` record with `by_ply` and `by_path` maps.

* make variation boards slightly smaller than the mainline boards

* update changelog v0.0.2
This commit is contained in:
2026-10-18 20:06:43 +02:00
committed by GitHub
parent 5831ddfb5e
commit fe9eb8ba45
12 changed files with 207 additions and 79 deletions
+6
View File
@@ -1,3 +1,9 @@
## 0.0.2
- support diagrams in variations. Diagram data now carries an optional `path` (the SAN move sequence from the root) alongside the legacy `ply`; diagrams whose `path` resolves inside a variation are rendered inline within the variation. `parse_diagrams_json` returns a `diagram_data` record with `by_ply` and `by_path` maps.
- make variation board diagrams slightly smaller than mainline board diagrams
## v0.0.1
- fix bug whereby a `subtitle` custom PGN header was rendering on a separate page. It now appears under the title
+2 -17
View File
@@ -2,21 +2,6 @@ open Js_of_ocaml
open Pgn_logic
open Ast
let parse_diagram_json json_str =
let json = Yojson.Basic.from_string json_str in
let json_list = Yojson.Basic.Util.to_list json in
List.fold_left
(fun acc item ->
let move_int =
item |> Yojson.Basic.Util.member "ply" |> Yojson.Basic.Util.to_int
in
let fen_str =
item |> Yojson.Basic.Util.member "fen" |> Yojson.Basic.Util.to_string
in
Pgn2tex.MoveMap.add move_int fen_str acc)
Pgn2tex.MoveMap.empty json_list
let convert_js pgn_js diagram_json_js display_clock =
let pgn = Js.to_string pgn_js in
@@ -24,8 +9,8 @@ let convert_js pgn_js diagram_json_js display_clock =
let json_str = Js.to_string diagram_json_js in
let diagram_data =
if json_str = "" || json_str = "{}" then Pgn2tex.MoveMap.empty
else parse_diagram_json json_str
if json_str = "" || json_str = "{}" then Ast.empty_diagrams
else Pgn2tex.parse_diagrams_json json_str
in
let result = Pgn2tex.to_tex pgn ~diagram_data ~clock:display_clock in
+1 -1
View File
@@ -18,7 +18,7 @@
(documentation
https://github.com/therealowenrees/pgn_to_tex/blob/main/README.md)
(version 0.0.1)
(version 0.0.2)
(package
(name pgn_to_tex)
+9
View File
@@ -1,7 +1,16 @@
module MoveMap = Map.Make (Int)
module PathMap = Map.Make (String)
type tag = { key : string; value : string }
(* Diagrams selected by the client. [by_ply] keeps the legacy mainline lookup
keyed on the ply number; [by_path] keys on the SAN move sequence from the
root (space separated), which uniquely identifies mainline and variation
positions alike. *)
type diagram_data = { by_ply : string MoveMap.t; by_path : string PathMap.t }
let empty_diagrams = { by_ply = MoveMap.empty; by_path = PathMap.empty }
type item =
| Move of string
| Number of string
+40 -15
View File
@@ -27,7 +27,10 @@ let tags_to_tex tags =
^ build_date_site_string date site
^ "\\maketitle\n\\newchessgame"
let rec item_to_tex is_mainline ply ?(diagram_data = MoveMap.empty) = function
let path_key path_moves = String.concat " " path_moves
let rec item_to_tex is_mainline ply ?(diagram_data = Ast.empty_diagrams)
?(branch_path = []) = function
| Number n -> if is_mainline then "\\textbf{" ^ n ^ "}" else n
| Move m ->
if is_mainline then "\\textbf{" ^ escape_tex m ^ "}" else escape_tex m
@@ -35,40 +38,53 @@ let rec item_to_tex is_mainline ply ?(diagram_data = MoveMap.empty) = function
if is_mainline then "\\newline " ^ escape_tex c ^ "\\par"
else escape_tex c
| Result r -> "\\textbf{" ^ r ^ "}"
| Variation v -> "( " ^ render_game false ~diagram_data v ^ " )"
| Variation v ->
"( " ^ render_game false ~diagram_data ~path:branch_path v ^ " )"
| Nag g -> " " ^ Nag.nag_to_tex g ^ "{}"
| Clock c -> c
and render_game is_mainline ?(diagram_data = MoveMap.empty) ?(clock = false)
items =
let rec aux ply interrupted ?(white_time = "0:00") ?(black_time = "0:00") =
and render_game is_mainline ?(diagram_data = Ast.empty_diagrams)
?(clock = false) ?(path = []) items =
let rec aux ply interrupted ~white_time ~black_time ~path ~branch_path =
function
| [] -> ""
| Comment c :: Move m :: tail when is_mainline && ply mod 2 != 0 ->
let next_ply = ply + 1 in
let next_path = path @ [ m ] in
let rendered_comment = "\\newline " ^ escape_tex c ^ "\\par" in
let rendered_move = "\\textbf{\\ldots{}" ^ escape_tex m ^ "}" in
let diag_str =
match get_diagram next_ply diagram_data with
match
get_diagram ~path:(path_key next_path) ~ply:next_ply diagram_data
with
| Some s -> s
| None -> ""
in
let has_diag = diag_str <> "" in
rendered_comment ^ " " ^ rendered_move ^ diag_str ^ " "
^ aux next_ply has_diag tail
^ aux next_ply has_diag ~white_time ~black_time ~path:next_path
~branch_path:path tail
| head :: tail -> (
match head with
| Clock c ->
if ply mod 2 = 0 then
aux ply interrupted ~white_time ~black_time:c tail
else aux ply interrupted ~white_time:c ~black_time tail
aux ply interrupted ~white_time ~black_time:c ~path ~branch_path
tail
else
aux ply interrupted ~white_time:c ~black_time ~path ~branch_path
tail
| _ ->
let is_move = match head with Move _ -> true | _ -> false in
let move_san = match head with Move m -> Some m | _ -> None in
let is_move = Option.is_some move_san in
let next_ply =
match head with Move _ when is_mainline -> ply + 1 | _ -> ply
in
let next_path =
match move_san with Some m -> path @ [ m ] | None -> path
in
let prefix =
if is_move && is_mainline && next_ply mod 2 == 0 && interrupted
then "\\ldots{}"
@@ -77,7 +93,7 @@ and render_game is_mainline ?(diagram_data = MoveMap.empty) ?(clock = false)
let rendered_head =
let raw_head =
item_to_tex is_mainline next_ply ~diagram_data head
item_to_tex is_mainline next_ply ~diagram_data ~branch_path head
in
if prefix <> "" && raw_head <> "" then
"\\textbf{" ^ prefix ^ "}" ^ raw_head
@@ -85,16 +101,23 @@ and render_game is_mainline ?(diagram_data = MoveMap.empty) ?(clock = false)
in
let diag_str =
if is_move && is_mainline then
get_diagram next_ply diagram_data ~clock ~white_time ~black_time
if is_move then
if is_mainline then
get_diagram ~path:(path_key next_path) ~ply:next_ply ~clock
~white_time ~black_time diagram_data
else
get_diagram_by_path ~path:(path_key next_path) ~small:true
diagram_data
else None
in
let diag_output = match diag_str with Some s -> s | None -> "" in
let is_comment = match head with Comment _ -> true | _ -> false in
let next_interrupted = diag_output <> "" || is_comment in
let next_branch_path = if is_move then path else branch_path in
let rest =
aux next_ply next_interrupted ~white_time ~black_time tail
aux next_ply next_interrupted ~white_time ~black_time
~path:next_path ~branch_path:next_branch_path tail
in
if rendered_head = "" then rest
@@ -102,7 +125,9 @@ and render_game is_mainline ?(diagram_data = MoveMap.empty) ?(clock = false)
rendered_head ^ diag_output ^ if rest = "" then "" else " " ^ rest
)
in
String.trim (aux 0 false items)
String.trim
(aux 0 false ~white_time:"0:00" ~black_time:"0:00" ~path ~branch_path:path
items)
let game_to_tex game ~diagram_data ~clock =
let header_tex = tags_to_tex game.tags in
+44 -15
View File
@@ -1,4 +1,5 @@
module MoveMap = Ast.MoveMap
module PathMap = Ast.PathMap
(* escape strings for LaTeX *)
let escape_tex s =
@@ -44,20 +45,48 @@ let build_date_site_string date site =
| d, "" -> "\\date{" ^ d ^ "}"
| d, s -> "\\date{" ^ d ^ ", " ^ s ^ "}"
(* generate a diagram with or without clock times from the passed in Map (JSON in JS) *)
let get_diagram ?(clock = false) ?(white_time = "0:00") ?(black_time = "0:00")
ply diagram_data =
match MoveMap.find_opt ply diagram_data with
| None -> None
| Some fen ->
(* board size options: mainline diagrams use the default size, variation
diagrams use the smaller "smallboard" so they don't dominate the subline *)
let board_options small = if small then ", smallboard" else ""
(* emit a diagram with or without clock times *)
let diagram_tex ?(clock = false) ?(white_time = "0:00") ?(black_time = "0:00")
?(small = false) fen =
let size = board_options small in
if not clock then
Some
("\n\\par\\nobreak\\medskip\\chessboard[setfen=" ^ fen
^ ", vmargin=false]\\par\\medskip\n")
"\n\\par\\nobreak\\medskip\\chessboard[setfen=" ^ fen ^ ", vmargin=false"
^ size ^ "]\\par\\medskip\n"
else
Some
("\\par\\medskip\\noindent" ^ "\\begin{minipage}{\\linewidth}"
^ black_time ^ "\\par\\nopagebreak\\smallskip\n"
^ "\\chessboard[setfen=" ^ fen
^ ", vmargin=false]\\par\\nopagebreak\\vspace{1em}\n" ^ white_time
^ "\\end{minipage}\\par\\medskip")
"\\par\\medskip\\noindent" ^ "\\begin{minipage}{\\linewidth}" ^ black_time
^ "\\par\\nopagebreak\\smallskip\n" ^ "\\chessboard[setfen=" ^ fen
^ ", vmargin=false" ^ size ^ "]\\par\\nopagebreak\\vspace{1em}\n"
^ white_time ^ "\\end{minipage}\\par\\medskip"
(* look up a diagram by its SAN path (root -> position, space separated) *)
let get_diagram_by_path ?(clock = false) ?(white_time = "0:00")
?(black_time = "0:00") ?(small = false) ~path diagram_data =
match PathMap.find_opt path diagram_data.Ast.by_path with
| None -> None
| Some fen -> Some (diagram_tex ~clock ~white_time ~black_time ~small fen)
(* look up a diagram by ply (legacy mainline keying) *)
let get_diagram_by_ply ?(clock = false) ?(white_time = "0:00")
?(black_time = "0:00") ply diagram_data =
match MoveMap.find_opt ply diagram_data.Ast.by_ply with
| None -> None
| Some fen -> Some (diagram_tex ~clock ~white_time ~black_time fen)
(* Prefer the path lookup, falling back to ply for older payloads that only
carry a mainline ply. *)
let get_diagram ?(clock = false) ?(white_time = "0:00") ?(black_time = "0:00")
?(small = false) ?path ~ply diagram_data =
let by_path =
match path with
| Some path ->
get_diagram_by_path ~clock ~white_time ~black_time ~small ~path
diagram_data
| None -> None
in
match by_path with
| Some _ as diagram -> diagram
| None -> get_diagram_by_ply ~clock ~white_time ~black_time ply diagram_data
+14 -4
View File
@@ -1,17 +1,27 @@
module MoveMap = Ast.MoveMap
module PathMap = Ast.PathMap
open Yojson.Safe.Util
let path_key path_moves = String.concat " " path_moves
let parse_diagrams_json diagrams_json_str =
try
let json = Yojson.Safe.from_string diagrams_json_str in
let diagrams_list = to_list json in
List.fold_left
(fun acc_map item ->
(fun acc item ->
let ply = item |> member "ply" |> to_int in
let fen = item |> member "fen" |> to_string in
MoveMap.add ply fen acc_map)
MoveMap.empty diagrams_list
with _ -> MoveMap.empty
let acc =
{ acc with Ast.by_ply = MoveMap.add ply fen acc.Ast.by_ply }
in
match item |> member "path" with
| `List path_items ->
let key = path_items |> List.map to_string |> path_key in
{ acc with Ast.by_path = PathMap.add key fen acc.Ast.by_path }
| _ -> acc)
Ast.empty_diagrams diagrams_list
with _ -> Ast.empty_diagrams
let to_tex pgn ~diagram_data ~clock =
let game = Parsing.parse_pgn pgn in
+1 -1
View File
@@ -1,6 +1,6 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.0.1"
version: "0.0.2"
synopsis: "A chess PGN to TeX conversion tool"
description:
"Convert PGN files to LaTeX markdown, including rendered images of selected positions"
+2 -1
View File
@@ -1,3 +1,4 @@
(library
(name helpers)
(modules latex_helper))
(modules latex_helper)
(libraries pgn_to_tex))
+7 -2
View File
@@ -1,6 +1,9 @@
module MoveMap = Map.Make (Int)
open Pgn_logic
open Ast
let diagram_data =
{
by_ply =
MoveMap.empty
|> MoveMap.add 5
"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3"
@@ -9,4 +12,6 @@ let diagram_data =
|> MoveMap.add 10
"r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5"
|> MoveMap.add 15
"r1bqkb1r/pp1ppppp/2n5/2p5/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 5 7"
"r1bqkb1r/pp1ppppp/2n5/2p5/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 5 7";
by_path = PathMap.empty;
}
+6 -4
View File
@@ -126,7 +126,7 @@ let test_author =
]
let test_diagram _ctxt =
match Latex_helpers.get_diagram 5 Helpers.Latex_helper.diagram_data with
match Latex_helpers.get_diagram ~ply:5 Helpers.Latex_helper.diagram_data with
| Some actual ->
let expected =
"\n\\par\\nobreak\\medskip\\chessboard[setfen="
@@ -138,8 +138,8 @@ let test_diagram _ctxt =
let test_diagram_clock _ctxt =
match
Latex_helpers.get_diagram 5 Helpers.Latex_helper.diagram_data ~clock:true
~white_time:"1:00" ~black_time:"0:21"
Latex_helpers.get_diagram ~ply:5 Helpers.Latex_helper.diagram_data
~clock:true ~white_time:"1:00" ~black_time:"0:21"
with
| Some actual ->
let expected =
@@ -151,7 +151,9 @@ let test_diagram_clock _ctxt =
| None -> assert_failure "Diagram for ply 5 was not found"
let test_no_diagram _ctxt =
let result = Latex_helpers.get_diagram 99 Helpers.Latex_helper.diagram_data in
let result =
Latex_helpers.get_diagram ~ply:99 Helpers.Latex_helper.diagram_data
in
match result with
| None -> ()
+58 -2
View File
@@ -2,13 +2,25 @@ open OUnit2
open Pgn_logic
open Ast
module MoveMap = Pgn2tex.MoveMap
module PathMap = Pgn2tex.PathMap
let ae expected actual = assert_equal ~printer:(fun x -> x) expected actual
let contains ~needle haystack =
let n = String.length needle and h = String.length haystack in
let rec loop i =
if i + n > h then false
else if String.sub haystack i n = needle then true
else loop (i + 1)
in
n = 0 || loop 0
let diagram_json_str =
{| [{"ply":1,"fen":"rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1"},{"ply":2,"fen":"rnbqkbnr/ppp1pppp/8/3p4/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 2"}] |}
let diagram_data =
{
by_ply =
MoveMap.empty
|> MoveMap.add 5
"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3"
@@ -17,11 +29,16 @@ let diagram_data =
|> MoveMap.add 10
"r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5"
|> MoveMap.add 15
"r1bqkb1r/pp1ppppp/2n5/2p5/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 5 7"
"r1bqkb1r/pp1ppppp/2n5/2p5/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 5 7";
by_path = PathMap.empty;
}
let variation_diagram_json_str =
{| [{"ply":4,"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1","path":["e4","e5","Nf3","d6"]}] |}
let test_json_str_to_map _ctxt =
let result = Pgn2tex.parse_diagrams_json diagram_json_str in
let actual_bindings = MoveMap.bindings result in
let actual_bindings = MoveMap.bindings result.by_ply in
let expected_bindings =
[
@@ -37,6 +54,43 @@ let test_json_str_to_map _ctxt =
assert_equal expected_bindings actual_bindings ~printer:string_of_bindings
let test_json_path_to_map _ctxt =
let result = Pgn2tex.parse_diagrams_json variation_diagram_json_str in
let actual = PathMap.bindings result.by_path in
let expected =
[
( "e4 e5 Nf3 d6",
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" );
]
in
assert_equal expected actual
let test_variation_diagram_to_tex _ctxt =
let pgn = "1. e4 e5 2. Nf3 Nc6 (2... d6) 3. Bb5" in
let diagrams = Pgn2tex.parse_diagrams_json variation_diagram_json_str in
let rendered = Pgn2tex.to_tex pgn ~diagram_data:diagrams ~clock:false in
let diagram =
match
Latex_helpers.get_diagram_by_path ~path:"e4 e5 Nf3 d6" ~small:true
diagrams
with
| Some d -> d
| None -> assert_failure "variation diagram not found by path"
in
(* the renderer trims the trailing newline of the diagram inside the
variation, but keeps the leading one *)
let embedded =
let len = String.length diagram in
if len > 0 && diagram.[len - 1] = '\n' then String.sub diagram 0 (len - 1)
else diagram
in
let expected_fragment = "( 2... d6" ^ embedded ^ " )" in
assert_bool
(Printf.sprintf "variation diagram misplaced in:\n%s" rendered)
(contains ~needle:expected_fragment rendered);
assert_bool "variation diagram should use smallboard"
(contains ~needle:"smallboard" rendered)
let test_basic_pgn_to_tex _ctxt =
let filename = "pgn_examples/1.pgn" in
let ic = In_channel.open_text filename in
@@ -87,6 +141,8 @@ let suite =
"pgn2tex tests"
>::: [
"json_str_to_map" >:: test_json_str_to_map;
"json_path_to_map" >:: test_json_path_to_map;
"variation_diagram_to_tex" >:: test_variation_diagram_to_tex;
"basic_pgn_to_tex" >:: test_basic_pgn_to_tex;
"pgn_with_clock_to_tex" >:: test_pgn_with_clock_to_tex;
]