3 Commits
Author SHA1 Message Date
owenrees 79eda1beab add directive to skip or accept failures in ocaml versions < 5.1.0 2026-10-18 21:05:11 +02:00
owenrees c500b1625d update readme to account for variations 2026-10-18 20:26:00 +02:00
owenrees fe9eb8ba45 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
2026-10-18 20:06:43 +02:00
14 changed files with 228 additions and 80 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 ## v0.0.1
- fix bug whereby a `subtitle` custom PGN header was rendering on a separate page. It now appears under the title - fix bug whereby a `subtitle` custom PGN header was rendering on a separate page. It now appears under the title
+11 -1
View File
@@ -11,7 +11,13 @@
```ocaml ```ocaml
open Pgn_logic open Pgn_logic
let diagrams_json_stringified = {| [{"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 diagrams_json_stringified =
{| [
{"ply":1,"fen":"rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1","path":["d4"]},
{"ply":2,"fen":"rnbqkbnr/ppp1pppp/8/3p4/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 2","path":["d4","d5"]},
{"ply":4,"fen":"rnbqkbnr/ppp1pppp/3p4/8/3PP3/8/PPP2PPP/RNBQKBNR b KQkq - 0 3","path":["d4","d5","e4","d6"]}
] |}
let diagram_data = Pgn2tex.parse_diagrams_json diagrams_json_stringified let diagram_data = Pgn2tex.parse_diagrams_json diagrams_json_stringified
let game_tex = Pgn2tex.to_tex pgn ~diagram_data ~clock:false let game_tex = Pgn2tex.to_tex pgn ~diagram_data ~clock:false
@@ -19,6 +25,10 @@ let game_tex = Pgn2tex.to_tex pgn ~diagram_data ~clock:false
This will return a LaTex markdown representation of the PGN file along with chess board diagrams at the specified ply. Clock times can be rendered for both sides if you pass `~clock:true` to the `to_tex` function. This will return a LaTex markdown representation of the PGN file along with chess board diagrams at the specified ply. Clock times can be rendered for both sides if you pass `~clock:true` to the `to_tex` function.
Each diagram is placed at the position identified by its path — the SAN move sequence from the start of the game (e.g. ["d4","d5","e4","d6"]). `path` uniquely identifies positions in both the mainline and in variations, and diagrams inside a variation are rendered inline within that variation. The legacy `ply` field is still accepted and used as a fallback for mainline diagrams when path is omitted.
Variation diagrams are rendered at a slightly smaller size (smallboard) so they don't dominate the subline. Clock times can be rendered for both sides by passing ~clock:true to to_tex.
Latex preample and document setup markdown is not included in the output. You will need to add something similar to below around the `game_tex` output to create a valid document: Latex preample and document setup markdown is not included in the output. You will need to add something similar to below around the `game_tex` output to create a valid document:
``` ```
+2 -17
View File
@@ -2,21 +2,6 @@ open Js_of_ocaml
open Pgn_logic open Pgn_logic
open Ast 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 convert_js pgn_js diagram_json_js display_clock =
let pgn = Js.to_string pgn_js in 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 json_str = Js.to_string diagram_json_js in
let diagram_data = let diagram_data =
if json_str = "" || json_str = "{}" then Pgn2tex.MoveMap.empty if json_str = "" || json_str = "{}" then Ast.empty_diagrams
else parse_diagram_json json_str else Pgn2tex.parse_diagrams_json json_str
in in
let result = Pgn2tex.to_tex pgn ~diagram_data ~clock:display_clock in let result = Pgn2tex.to_tex pgn ~diagram_data ~clock:display_clock in
+1 -1
View File
@@ -18,7 +18,7 @@
(documentation (documentation
https://github.com/therealowenrees/pgn_to_tex/blob/main/README.md) https://github.com/therealowenrees/pgn_to_tex/blob/main/README.md)
(version 0.0.1) (version 0.0.2)
(package (package
(name pgn_to_tex) (name pgn_to_tex)
+9
View File
@@ -1,7 +1,16 @@
module MoveMap = Map.Make (Int) module MoveMap = Map.Make (Int)
module PathMap = Map.Make (String)
type tag = { key : string; value : 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 = type item =
| Move of string | Move of string
| Number of string | Number of string
+40 -15
View File
@@ -27,7 +27,10 @@ let tags_to_tex tags =
^ build_date_site_string date site ^ build_date_site_string date site
^ "\\maketitle\n\\newchessgame" ^ "\\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 | Number n -> if is_mainline then "\\textbf{" ^ n ^ "}" else n
| Move m -> | Move m ->
if is_mainline then "\\textbf{" ^ escape_tex m ^ "}" else escape_tex 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" if is_mainline then "\\newline " ^ escape_tex c ^ "\\par"
else escape_tex c else escape_tex c
| Result r -> "\\textbf{" ^ r ^ "}" | 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 ^ "{}" | Nag g -> " " ^ Nag.nag_to_tex g ^ "{}"
| Clock c -> c | Clock c -> c
and render_game is_mainline ?(diagram_data = MoveMap.empty) ?(clock = false) and render_game is_mainline ?(diagram_data = Ast.empty_diagrams)
items = ?(clock = false) ?(path = []) items =
let rec aux ply interrupted ?(white_time = "0:00") ?(black_time = "0:00") = let rec aux ply interrupted ~white_time ~black_time ~path ~branch_path =
function function
| [] -> "" | [] -> ""
| Comment c :: Move m :: tail when is_mainline && ply mod 2 != 0 -> | Comment c :: Move m :: tail when is_mainline && ply mod 2 != 0 ->
let next_ply = ply + 1 in let next_ply = ply + 1 in
let next_path = path @ [ m ] in
let rendered_comment = "\\newline " ^ escape_tex c ^ "\\par" in let rendered_comment = "\\newline " ^ escape_tex c ^ "\\par" in
let rendered_move = "\\textbf{\\ldots{}" ^ escape_tex m ^ "}" in let rendered_move = "\\textbf{\\ldots{}" ^ escape_tex m ^ "}" in
let diag_str = 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 | Some s -> s
| None -> "" | None -> ""
in in
let has_diag = diag_str <> "" in let has_diag = diag_str <> "" in
rendered_comment ^ " " ^ rendered_move ^ diag_str ^ " " 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 -> ( | head :: tail -> (
match head with match head with
| Clock c -> | Clock c ->
if ply mod 2 = 0 then if ply mod 2 = 0 then
aux ply interrupted ~white_time ~black_time:c tail aux ply interrupted ~white_time ~black_time:c ~path ~branch_path
else aux ply interrupted ~white_time:c ~black_time tail 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 = let next_ply =
match head with Move _ when is_mainline -> ply + 1 | _ -> ply match head with Move _ when is_mainline -> ply + 1 | _ -> ply
in in
let next_path =
match move_san with Some m -> path @ [ m ] | None -> path
in
let prefix = let prefix =
if is_move && is_mainline && next_ply mod 2 == 0 && interrupted if is_move && is_mainline && next_ply mod 2 == 0 && interrupted
then "\\ldots{}" then "\\ldots{}"
@@ -77,7 +93,7 @@ and render_game is_mainline ?(diagram_data = MoveMap.empty) ?(clock = false)
let rendered_head = let rendered_head =
let raw_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 in
if prefix <> "" && raw_head <> "" then if prefix <> "" && raw_head <> "" then
"\\textbf{" ^ prefix ^ "}" ^ raw_head "\\textbf{" ^ prefix ^ "}" ^ raw_head
@@ -85,16 +101,23 @@ and render_game is_mainline ?(diagram_data = MoveMap.empty) ?(clock = false)
in in
let diag_str = let diag_str =
if is_move && is_mainline then if is_move then
get_diagram next_ply diagram_data ~clock ~white_time ~black_time 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 else None
in in
let diag_output = match diag_str with Some s -> s | 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 is_comment = match head with Comment _ -> true | _ -> false in
let next_interrupted = diag_output <> "" || is_comment in let next_interrupted = diag_output <> "" || is_comment in
let next_branch_path = if is_move then path else branch_path in
let rest = 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 in
if rendered_head = "" then rest 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 rendered_head ^ diag_output ^ if rest = "" then "" else " " ^ rest
) )
in 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 game_to_tex game ~diagram_data ~clock =
let header_tex = tags_to_tex game.tags in let header_tex = tags_to_tex game.tags in
+44 -15
View File
@@ -1,4 +1,5 @@
module MoveMap = Ast.MoveMap module MoveMap = Ast.MoveMap
module PathMap = Ast.PathMap
(* escape strings for LaTeX *) (* escape strings for LaTeX *)
let escape_tex s = let escape_tex s =
@@ -44,20 +45,48 @@ let build_date_site_string date site =
| d, "" -> "\\date{" ^ d ^ "}" | d, "" -> "\\date{" ^ d ^ "}"
| d, s -> "\\date{" ^ d ^ ", " ^ s ^ "}" | d, s -> "\\date{" ^ d ^ ", " ^ s ^ "}"
(* generate a diagram with or without clock times from the passed in Map (JSON in JS) *) (* board size options: mainline diagrams use the default size, variation
let get_diagram ?(clock = false) ?(white_time = "0:00") ?(black_time = "0:00") diagrams use the smaller "smallboard" so they don't dominate the subline *)
ply diagram_data = let board_options small = if small then ", smallboard" else ""
match MoveMap.find_opt ply diagram_data with
| None -> None (* emit a diagram with or without clock times *)
| Some fen -> 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 if not clock then
Some "\n\\par\\nobreak\\medskip\\chessboard[setfen=" ^ fen ^ ", vmargin=false"
("\n\\par\\nobreak\\medskip\\chessboard[setfen=" ^ fen ^ size ^ "]\\par\\medskip\n"
^ ", vmargin=false]\\par\\medskip\n")
else else
Some "\\par\\medskip\\noindent" ^ "\\begin{minipage}{\\linewidth}" ^ black_time
("\\par\\medskip\\noindent" ^ "\\begin{minipage}{\\linewidth}" ^ "\\par\\nopagebreak\\smallskip\n" ^ "\\chessboard[setfen=" ^ fen
^ black_time ^ "\\par\\nopagebreak\\smallskip\n" ^ ", vmargin=false" ^ size ^ "]\\par\\nopagebreak\\vspace{1em}\n"
^ "\\chessboard[setfen=" ^ fen ^ white_time ^ "\\end{minipage}\\par\\medskip"
^ ", vmargin=false]\\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 MoveMap = Ast.MoveMap
module PathMap = Ast.PathMap
open Yojson.Safe.Util open Yojson.Safe.Util
let path_key path_moves = String.concat " " path_moves
let parse_diagrams_json diagrams_json_str = let parse_diagrams_json diagrams_json_str =
try try
let json = Yojson.Safe.from_string diagrams_json_str in let json = Yojson.Safe.from_string diagrams_json_str in
let diagrams_list = to_list json in let diagrams_list = to_list json in
List.fold_left List.fold_left
(fun acc_map item -> (fun acc item ->
let ply = item |> member "ply" |> to_int in let ply = item |> member "ply" |> to_int in
let fen = item |> member "fen" |> to_string in let fen = item |> member "fen" |> to_string in
MoveMap.add ply fen acc_map) let acc =
MoveMap.empty diagrams_list { acc with Ast.by_ply = MoveMap.add ply fen acc.Ast.by_ply }
with _ -> MoveMap.empty 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 to_tex pgn ~diagram_data ~clock =
let game = Parsing.parse_pgn pgn in let game = Parsing.parse_pgn pgn in
+6 -1
View File
@@ -1,6 +1,6 @@
# This file is generated by dune, edit dune-project instead # This file is generated by dune, edit dune-project instead
opam-version: "2.0" opam-version: "2.0"
version: "0.0.1" version: "0.0.2"
synopsis: "A chess PGN to TeX conversion tool" synopsis: "A chess PGN to TeX conversion tool"
description: description:
"Convert PGN files to LaTeX markdown, including rendered images of selected positions" "Convert PGN files to LaTeX markdown, including rendered images of selected positions"
@@ -37,3 +37,8 @@ build: [
] ]
dev-repo: "git+https://github.com/therealowenrees/pgn_to_tex.git" dev-repo: "git+https://github.com/therealowenrees/pgn_to_tex.git"
x-maintenance-intent: ["(latest)"] x-maintenance-intent: ["(latest)"]
x-ci-accept-failures: [
"ocaml-base-compiler < 5.1.0"
"ocaml-system < 5.1.0"
"ocaml-variants < 5.1.0"
]
+5
View File
@@ -0,0 +1,5 @@
x-ci-accept-failures: [
"ocaml-base-compiler < 5.1.0"
"ocaml-system < 5.1.0"
"ocaml-variants < 5.1.0"
]
+2 -1
View File
@@ -1,3 +1,4 @@
(library (library
(name helpers) (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 = let diagram_data =
{
by_ply =
MoveMap.empty MoveMap.empty
|> MoveMap.add 5 |> MoveMap.add 5
"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3" "rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3"
@@ -9,4 +12,6 @@ let diagram_data =
|> MoveMap.add 10 |> MoveMap.add 10
"r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5" "r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5"
|> MoveMap.add 15 |> 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 = 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 -> | Some actual ->
let expected = let expected =
"\n\\par\\nobreak\\medskip\\chessboard[setfen=" "\n\\par\\nobreak\\medskip\\chessboard[setfen="
@@ -138,8 +138,8 @@ let test_diagram _ctxt =
let test_diagram_clock _ctxt = let test_diagram_clock _ctxt =
match match
Latex_helpers.get_diagram 5 Helpers.Latex_helper.diagram_data ~clock:true Latex_helpers.get_diagram ~ply:5 Helpers.Latex_helper.diagram_data
~white_time:"1:00" ~black_time:"0:21" ~clock:true ~white_time:"1:00" ~black_time:"0:21"
with with
| Some actual -> | Some actual ->
let expected = let expected =
@@ -151,7 +151,9 @@ let test_diagram_clock _ctxt =
| None -> assert_failure "Diagram for ply 5 was not found" | None -> assert_failure "Diagram for ply 5 was not found"
let test_no_diagram _ctxt = 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 match result with
| None -> () | None -> ()
+58 -2
View File
@@ -2,13 +2,25 @@ open OUnit2
open Pgn_logic open Pgn_logic
open Ast open Ast
module MoveMap = Pgn2tex.MoveMap module MoveMap = Pgn2tex.MoveMap
module PathMap = Pgn2tex.PathMap
let ae expected actual = assert_equal ~printer:(fun x -> x) expected actual 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 = 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"}] |} {| [{"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 = let diagram_data =
{
by_ply =
MoveMap.empty MoveMap.empty
|> MoveMap.add 5 |> MoveMap.add 5
"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3" "rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3"
@@ -17,11 +29,16 @@ let diagram_data =
|> MoveMap.add 10 |> MoveMap.add 10
"r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5" "r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5"
|> MoveMap.add 15 |> 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 test_json_str_to_map _ctxt =
let result = Pgn2tex.parse_diagrams_json diagram_json_str in 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 = let expected_bindings =
[ [
@@ -37,6 +54,43 @@ let test_json_str_to_map _ctxt =
assert_equal expected_bindings actual_bindings ~printer:string_of_bindings 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 test_basic_pgn_to_tex _ctxt =
let filename = "pgn_examples/1.pgn" in let filename = "pgn_examples/1.pgn" in
let ic = In_channel.open_text filename in let ic = In_channel.open_text filename in
@@ -87,6 +141,8 @@ let suite =
"pgn2tex tests" "pgn2tex tests"
>::: [ >::: [
"json_str_to_map" >:: test_json_str_to_map; "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; "basic_pgn_to_tex" >:: test_basic_pgn_to_tex;
"pgn_with_clock_to_tex" >:: test_pgn_with_clock_to_tex; "pgn_with_clock_to_tex" >:: test_pgn_with_clock_to_tex;
] ]