mirror of
https://github.com/TheRealOwenRees/pgn_to_tex.git
synced 2026-11-18 04:42:07 +00:00
fix: address code review findings for public API
This commit is contained in:
@@ -5,23 +5,31 @@ open Yojson.Safe.Util
|
||||
let path_key path_moves = String.concat " " path_moves
|
||||
|
||||
let parse_diagrams_json diagrams_json_str =
|
||||
let fail () = raise Exit in
|
||||
try
|
||||
let json = Yojson.Safe.from_string diagrams_json_str in
|
||||
let diagrams_list = to_list json in
|
||||
List.fold_left
|
||||
(fun acc item ->
|
||||
let fen = item |> member "fen" |> to_string in
|
||||
let acc =
|
||||
match item |> member "ply" with
|
||||
| `Int ply ->
|
||||
{ acc with Ast.by_ply = MoveMap.add ply fen acc.Ast.by_ply }
|
||||
| _ -> acc
|
||||
let fields = match item with `Assoc fields -> fields | _ -> fail () in
|
||||
let fen =
|
||||
match List.assoc_opt "fen" fields with
|
||||
| Some (`String fen) -> fen
|
||||
| _ -> fail ()
|
||||
in
|
||||
match item |> member "path" with
|
||||
| `List path_items ->
|
||||
let acc =
|
||||
match List.assoc_opt "ply" fields with
|
||||
| None -> acc
|
||||
| Some (`Int ply) ->
|
||||
{ acc with Ast.by_ply = MoveMap.add ply fen acc.Ast.by_ply }
|
||||
| Some _ -> fail ()
|
||||
in
|
||||
match List.assoc_opt "path" fields with
|
||||
| None -> acc
|
||||
| Some (`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)
|
||||
| Some _ -> fail ())
|
||||
Ast.empty_diagrams diagrams_list
|
||||
with _ -> Ast.empty_diagrams
|
||||
|
||||
|
||||
@@ -33,11 +33,11 @@ let build_author_string author white black white_elo black_elo =
|
||||
(* Title string builder *)
|
||||
let build_title_string event title subtitle =
|
||||
match (event, title, subtitle) with
|
||||
| event, "", "" -> "\\title{" ^ escape_tex event ^ "}"
|
||||
| _event, title, "" -> "\\title{" ^ escape_tex title ^ "}"
|
||||
| event, "", "" -> "\\title{" ^ event ^ "}"
|
||||
| _event, "", subtitle -> "\\title{" ^ subtitle ^ "}"
|
||||
| _event, title, "" -> "\\title{" ^ title ^ "}"
|
||||
| _event, title, subtitle ->
|
||||
"\\title{" ^ escape_tex title ^ "\\\\[2ex]\\large{" ^ escape_tex subtitle
|
||||
^ "}}"
|
||||
"\\title{" ^ title ^ "\\\\[2ex]\\large{" ^ subtitle ^ "}}"
|
||||
|
||||
(* Date and Site string builder *)
|
||||
let build_date_site_string date site =
|
||||
|
||||
Reference in New Issue
Block a user