mirror of
https://github.com/TheRealOwenRees/pgn_to_tex.git
synced 2026-11-18 04:42:07 +00:00
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:
@@ -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
|
||||
|
||||
+45
-16
@@ -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
|
||||
(* 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
|
||||
|
||||
+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
|
||||
|
||||
Reference in New Issue
Block a user