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
+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