mirror of
https://github.com/TheRealOwenRees/pgn_to_tex.git
synced 2026-11-18 12:52:06 +00:00
Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
79eda1beab
|
||
|
|
c500b1625d
|
||
|
|
fe9eb8ba45 | ||
|
|
5831ddfb5e
|
||
|
|
5f0521430d
|
||
|
|
a58a6504b4 | ||
|
|
b4816553c7
|
@@ -1,3 +1,18 @@
|
||||
## 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
|
||||
- remove old tests and commented out code
|
||||
|
||||
## v0.0.1-rc.2
|
||||
|
||||
- add correct versioning metadata
|
||||
|
||||
## v0.0.1-rc.1
|
||||
|
||||
- Update readme and add license file
|
||||
|
||||
@@ -11,7 +11,13 @@
|
||||
```ocaml
|
||||
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 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.
|
||||
|
||||
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:
|
||||
|
||||
```
|
||||
|
||||
+2
-17
@@ -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
|
||||
|
||||
+3
-2
@@ -15,9 +15,10 @@
|
||||
|
||||
(license AGPL-3.0-or-later)
|
||||
|
||||
(documentation https://github.com/TheRealOwenRees/pgn_to_tex/README.md)
|
||||
(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)
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
+46
-17
@@ -1,4 +1,5 @@
|
||||
module MoveMap = Ast.MoveMap
|
||||
module PathMap = Ast.PathMap
|
||||
|
||||
(* escape strings for LaTeX *)
|
||||
let escape_tex s =
|
||||
@@ -34,7 +35,7 @@ let build_title_string event title subtitle =
|
||||
match (event, title, subtitle) with
|
||||
| event, "", "" -> "\\title{" ^ escape_tex event ^ "}"
|
||||
| _event, title, subtitle ->
|
||||
"\\title{" ^ title ^ "}\\\\[2ex]\\large{" ^ subtitle ^ "}"
|
||||
"\\title{" ^ title ^ "\\\\[2ex]\\large{" ^ subtitle ^ "}}"
|
||||
|
||||
(* Date and Site string builder *)
|
||||
let build_date_site_string date site =
|
||||
@@ -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
|
||||
(* 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
|
||||
"\n\\par\\nobreak\\medskip\\chessboard[setfen=" ^ fen ^ ", vmargin=false"
|
||||
^ size ^ "]\\par\\medskip\n"
|
||||
else
|
||||
"\\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 ->
|
||||
if not clock then
|
||||
Some
|
||||
("\n\\par\\nobreak\\medskip\\chessboard[setfen=" ^ fen
|
||||
^ ", vmargin=false]\\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")
|
||||
| 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
|
||||
|
||||
@@ -28,15 +28,3 @@ let parse_pgn s =
|
||||
in
|
||||
try MenhirLib.Convert.Simplified.traditional2revised Parser.main provider
|
||||
with _ -> failwith "Parse error"
|
||||
|
||||
(* let parse_diagram_json json_str =
|
||||
let json = Yojson.Basic.from_string json_str in
|
||||
let json_assoc = Yojson.Basic.Util.to_assoc json in
|
||||
|
||||
List.fold_left
|
||||
(fun acc (key_str, json_value) ->
|
||||
let value_string = Yojson.Basic.Util.to_string json_value in
|
||||
match int_of_string_opt key_str with
|
||||
| Some k -> Pgn2tex.MoveMap.add k value_string acc
|
||||
| None -> acc)
|
||||
Pgn2tex.MoveMap.empty json_assoc *)
|
||||
|
||||
+14
-4
@@ -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
|
||||
|
||||
+7
-2
@@ -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"
|
||||
@@ -9,7 +9,7 @@ authors: ["Owen Rees <owenrees.dev@gmail.com>"]
|
||||
license: "AGPL-3.0-or-later"
|
||||
tags: ["pgn" "latex" "chess" "parser"]
|
||||
homepage: "https://github.com/therealowenrees/pgn_to_tex"
|
||||
doc: "https://github.com/TheRealOwenRees/pgn_to_tex/README.md"
|
||||
doc: "https://github.com/therealowenrees/pgn_to_tex/blob/main/README.md"
|
||||
bug-reports: "https://github.com/therealowenrees/pgn_to_tex/issues"
|
||||
depends: [
|
||||
"dune" {>= "3.21"}
|
||||
@@ -37,3 +37,8 @@ build: [
|
||||
]
|
||||
dev-repo: "git+https://github.com/therealowenrees/pgn_to_tex.git"
|
||||
x-maintenance-intent: ["(latest)"]
|
||||
x-ci-accept-failures: [
|
||||
"ocaml-base-compiler < 5.1.0"
|
||||
"ocaml-system < 5.1.0"
|
||||
"ocaml-variants < 5.1.0"
|
||||
]
|
||||
@@ -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
@@ -1,3 +1,4 @@
|
||||
(library
|
||||
(name helpers)
|
||||
(modules latex_helper))
|
||||
(modules latex_helper)
|
||||
(libraries pgn_to_tex))
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
module MoveMap = Map.Make (Int)
|
||||
open Pgn_logic
|
||||
open Ast
|
||||
|
||||
let diagram_data =
|
||||
MoveMap.empty
|
||||
|> MoveMap.add 5
|
||||
"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3"
|
||||
|> MoveMap.add 6
|
||||
"rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 3"
|
||||
|> 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"
|
||||
{
|
||||
by_ply =
|
||||
MoveMap.empty
|
||||
|> MoveMap.add 5
|
||||
"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3"
|
||||
|> MoveMap.add 6
|
||||
"rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 3"
|
||||
|> 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";
|
||||
by_path = PathMap.empty;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
[Result "*"]
|
||||
[Title "Some Title"]
|
||||
[Subtitle "Some Subtitle"]
|
||||
[Date "2025.12.23"]
|
||||
[Author "Some Author"]
|
||||
|
||||
1. d4 d5 2. c4 dxc4 3. e4 Nf6 4. e5 Nd5 5. Bxc4 Nb6 6. Bb3 e6 (6... Nc6) 7. Nf3 c5 8. Be3 N8d7 9. O-O c4 10. Bc2 Nd5 11. Nc3 N7b6 12. Be4 Bb4 13. Bxd5 Nxd5? (13... Bxc3) 14. Nxd5 exd5 15. Qa4+ Bd7 16. Qxb4 Qb6 17. Qxb6 axb6 18. Rfc1 O-O 19. Nd2 Bf5 20. b3 b5 21. a4 cxb3 22. Nxb3 bxa4 23. Nc5 b5 24. Bd2 Rfe8 25. Bb4 Rac8 26. Nb7 Rxc1+ 27. Rxc1 Rc8 28. Rxc8+ Bxc8 29. Nd6 Bd7 30. f4 g6 31. Kf2 Kg7 32. h3 *
|
||||
+7
-5
@@ -111,7 +111,7 @@ let test_title =
|
||||
ae "\\title{some event}"
|
||||
(Latex_helpers.build_title_string "some event" "" "") );
|
||||
( "Title and subtitle" >:: fun _ ->
|
||||
ae "\\title{Some title}\\\\[2ex]\\large{Some subtitle}"
|
||||
ae "\\title{Some title\\\\[2ex]\\large{Some subtitle}}"
|
||||
(Latex_helpers.build_title_string "irrelevant" "Some title"
|
||||
"Some subtitle") );
|
||||
]
|
||||
@@ -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 -> ()
|
||||
|
||||
+66
-59
@@ -2,26 +2,43 @@ 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 =
|
||||
MoveMap.empty
|
||||
|> MoveMap.add 5
|
||||
"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3"
|
||||
|> MoveMap.add 6
|
||||
"rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 3"
|
||||
|> 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"
|
||||
{
|
||||
by_ply =
|
||||
MoveMap.empty
|
||||
|> MoveMap.add 5
|
||||
"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3"
|
||||
|> MoveMap.add 6
|
||||
"rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 3"
|
||||
|> 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";
|
||||
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
|
||||
@@ -83,61 +137,14 @@ let test_pgn_with_clock_to_tex _ctxt =
|
||||
ae expected actual;
|
||||
close_in ic
|
||||
|
||||
let test_pgn_with_unsupported_text _ctxt =
|
||||
let filename = "pgn_examples/4.pgn" in
|
||||
let ic = In_channel.open_text filename in
|
||||
let input_string = In_channel.input_all ic in
|
||||
let actual = Pgn2tex.to_tex input_string ~diagram_data ~clock:true in
|
||||
let expected =
|
||||
{|\title{Casual Rapid game}
|
||||
\author{montillo2015 (1919) vs. reassessyourchess (1869)}
|
||||
\date{2023.12.03, https://lichess.org/9Z0taHcG}\maketitle
|
||||
\newchessgame
|
||||
\textbf{1.} \textbf{d4} \textbf{1...} \textbf{Nf6} \textbf{2.} \textbf{Nf3} \textbf{2...} \textbf{d5} \textbf{3.} \textbf{e3}\par\nobreak 0:10:01\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:00\par \textbf{3...} \textbf{e6}\par\nobreak 0:10:01\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:01\par \newline D05 Queen's Pawn Game: Colle System\par \textbf{4.} \textbf{a3} \textbf{4...} \textbf{c5} \textbf{5.} \textbf{c4} \textbf{5...} \textbf{Nc6}\par\nobreak 0:10:02\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:02\par \textbf{6.} \textbf{dxc5} \textbf{6...} \textbf{Bxc5} \textbf{7.} \textbf{b4} \textbf{7...} \textbf{Be7} \textbf{8.} \textbf{Bb2}\par\nobreak 0:10:00\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:03\par \textbf{8...} \textbf{dxc4} \textbf{9.} \textbf{Qxd8+} \textbf{9...} \textbf{Nxd8} \textbf{10.} \textbf{Bxc4} \textbf{10...} \textbf{O-O} \textbf{11.} \textbf{O-O} \textbf{11...} \textbf{a6} \textbf{12.} \textbf{Nc3} \textbf{12...} \textbf{b5} \textbf{13.} \textbf{Be2} \textbf{13...} \textbf{Bb7} \textbf{14.} \textbf{Nd4} \textbf{14...} \textbf{Ne4} \textbf{15.} \textbf{Nxe4} \textbf{15...} \textbf{Bxe4} \textbf{16.} \textbf{Bf3} \textbf{16...} \textbf{Bxf3} \textbf{17.} \textbf{Nxf3} \textbf{17...} \textbf{Nc6} \textbf{18.} \textbf{Rac1} \textbf{18...} \textbf{Rac8} \textbf{19.} \textbf{Rfd1} \textbf{19...} \textbf{Rfd8} \textbf{20.} \textbf{Rxd8+} \textbf{20...} \textbf{Rxd8} \textbf{21.} \textbf{g3} \textbf{21...} \textbf{Nb8} \textbf{22.} \textbf{Rc7} \textbf{22...} \textbf{Bd6} \textbf{23.} \textbf{Rc2} \textbf{23...} \textbf{f6} \textbf{24.} \textbf{Kg2} \textbf{24...} \textbf{e5} \textbf{25.} \textbf{h4} \textbf{25...} \textbf{e4} \textbf{26.} \textbf{Nd4} \textbf{26...} \textbf{Kf7} \textbf{27.} \textbf{Nf5} \textbf{27...} \textbf{Nd7} \textbf{28.} \textbf{Nxd6+} \newline Black resigns.\par \textbf{1-0}|}
|
||||
in
|
||||
ae expected actual;
|
||||
close_in ic
|
||||
|
||||
let test_pgn_with_custom_headers _ctxt =
|
||||
let filename = "pgn_examples/7.pgn" in
|
||||
let ic = In_channel.open_text filename in
|
||||
let input_string = In_channel.input_all ic in
|
||||
let actual = Pgn2tex.to_tex input_string ~diagram_data ~clock:false in
|
||||
let expected =
|
||||
{|\title{Casual Rapid game}
|
||||
\author{montillo2015 (1919) vs. reassessyourchess (1869)}
|
||||
\date{2023.12.03, https://lichess.org/9Z0taHcG}\maketitle
|
||||
\newchessgame
|
||||
\textbf{1.} \textbf{d4} \textbf{1...} \textbf{Nf6} \textbf{2.} \textbf{Nf3} \textbf{2...} \textbf{d5} \textbf{3.} \textbf{e3}\par\nobreak 0:10:01\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:00\par \textbf{3...} \textbf{e6}\par\nobreak 0:10:01\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:01\par \newline D05 Queen's Pawn Game: Colle System\par \textbf{4.} \textbf{a3} \textbf{4...} \textbf{c5} \textbf{5.} \textbf{c4} \textbf{5...} \textbf{Nc6}\par\nobreak 0:10:02\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:02\par \textbf{6.} \textbf{dxc5} \textbf{6...} \textbf{Bxc5} \textbf{7.} \textbf{b4} \textbf{7...} \textbf{Be7} \textbf{8.} \textbf{Bb2}\par\nobreak 0:10:00\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:03\par \textbf{8...} \textbf{dxc4} \textbf{9.} \textbf{Qxd8+} \textbf{9...} \textbf{Nxd8} \textbf{10.} \textbf{Bxc4} \textbf{10...} \textbf{O-O} \textbf{11.} \textbf{O-O} \textbf{11...} \textbf{a6} \textbf{12.} \textbf{Nc3} \textbf{12...} \textbf{b5} \textbf{13.} \textbf{Be2} \textbf{13...} \textbf{Bb7} \textbf{14.} \textbf{Nd4} \textbf{14...} \textbf{Ne4} \textbf{15.} \textbf{Nxe4} \textbf{15...} \textbf{Bxe4} \textbf{16.} \textbf{Bf3} \textbf{16...} \textbf{Bxf3} \textbf{17.} \textbf{Nxf3} \textbf{17...} \textbf{Nc6} \textbf{18.} \textbf{Rac1} \textbf{18...} \textbf{Rac8} \textbf{19.} \textbf{Rfd1} \textbf{19...} \textbf{Rfd8} \textbf{20.} \textbf{Rxd8+} \textbf{20...} \textbf{Rxd8} \textbf{21.} \textbf{g3} \textbf{21...} \textbf{Nb8} \textbf{22.} \textbf{Rc7} \textbf{22...} \textbf{Bd6} \textbf{23.} \textbf{Rc2} \textbf{23...} \textbf{f6} \textbf{24.} \textbf{Kg2} \textbf{24...} \textbf{e5} \textbf{25.} \textbf{h4} \textbf{25...} \textbf{e4} \textbf{26.} \textbf{Nd4} \textbf{26...} \textbf{Kf7} \textbf{27.} \textbf{Nf5} \textbf{27...} \textbf{Nd7} \textbf{28.} \textbf{Nxd6+} \newline Black resigns.\par \textbf{1-0}|}
|
||||
in
|
||||
ae expected actual;
|
||||
close_in ic
|
||||
|
||||
let test_pgn_with_different_unsupported_text _ctxt =
|
||||
let filename = "pgn_examples/10.pgn" in
|
||||
let ic = In_channel.open_text filename in
|
||||
let input_string = In_channel.input_all ic in
|
||||
let actual = Pgn2tex.to_tex input_string ~diagram_data ~clock:false in
|
||||
let expected =
|
||||
{|\title{Casual Rapid game}
|
||||
\author{montillo2015 (1919) vs. reassessyourchess (1869)}
|
||||
\date{2023.12.03, https://lichess.org/9Z0taHcG}\maketitle
|
||||
\newchessgame
|
||||
\textbf{1.} \textbf{d4} \textbf{1...} \textbf{Nf6} \textbf{2.} \textbf{Nf3} \textbf{2...} \textbf{d5} \textbf{3.} \textbf{e3}\par\nobreak 0:10:01\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:00\par \textbf{3...} \textbf{e6}\par\nobreak 0:10:01\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:01\par \newline D05 Queen's Pawn Game: Colle System\par \textbf{4.} \textbf{a3} \textbf{4...} \textbf{c5} \textbf{5.} \textbf{c4} \textbf{5...} \textbf{Nc6}\par\nobreak 0:10:02\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:02\par \textbf{6.} \textbf{dxc5} \textbf{6...} \textbf{Bxc5} \textbf{7.} \textbf{b4} \textbf{7...} \textbf{Be7} \textbf{8.} \textbf{Bb2}\par\nobreak 0:10:00\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:03\par \textbf{8...} \textbf{dxc4} \textbf{9.} \textbf{Qxd8+} \textbf{9...} \textbf{Nxd8} \textbf{10.} \textbf{Bxc4} \textbf{10...} \textbf{O-O} \textbf{11.} \textbf{O-O} \textbf{11...} \textbf{a6} \textbf{12.} \textbf{Nc3} \textbf{12...} \textbf{b5} \textbf{13.} \textbf{Be2} \textbf{13...} \textbf{Bb7} \textbf{14.} \textbf{Nd4} \textbf{14...} \textbf{Ne4} \textbf{15.} \textbf{Nxe4} \textbf{15...} \textbf{Bxe4} \textbf{16.} \textbf{Bf3} \textbf{16...} \textbf{Bxf3} \textbf{17.} \textbf{Nxf3} \textbf{17...} \textbf{Nc6} \textbf{18.} \textbf{Rac1} \textbf{18...} \textbf{Rac8} \textbf{19.} \textbf{Rfd1} \textbf{19...} \textbf{Rfd8} \textbf{20.} \textbf{Rxd8+} \textbf{20...} \textbf{Rxd8} \textbf{21.} \textbf{g3} \textbf{21...} \textbf{Nb8} \textbf{22.} \textbf{Rc7} \textbf{22...} \textbf{Bd6} \textbf{23.} \textbf{Rc2} \textbf{23...} \textbf{f6} \textbf{24.} \textbf{Kg2} \textbf{24...} \textbf{e5} \textbf{25.} \textbf{h4} \textbf{25...} \textbf{e4} \textbf{26.} \textbf{Nd4} \textbf{26...} \textbf{Kf7} \textbf{27.} \textbf{Nf5} \textbf{27...} \textbf{Nd7} \textbf{28.} \textbf{Nxd6+} \newline Black resigns.\par \textbf{1-0}|}
|
||||
in
|
||||
ae expected actual;
|
||||
close_in ic
|
||||
|
||||
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;
|
||||
(* "pgn_with_unsupported_text" >:: test_pgn_with_unsupported_text; *)
|
||||
(* "pgn_with_custom_headers" >:: test_pgn_with_custom_headers; *)
|
||||
(* "pgn_with_different_unsupported_text"
|
||||
>:: test_pgn_with_different_unsupported_text; *)
|
||||
]
|
||||
|
||||
let () = run_test_tt_main suite
|
||||
|
||||
Reference in New Issue
Block a user