diff --git a/README.md b/README.md index 062616a..9655518 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ ## Usage ```ocaml -open Pgn_logic +open Pgn_to_tex let diagrams_json_stringified = {| [ @@ -18,9 +18,9 @@ let diagrams_json_stringified = {"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 = parse_diagrams_json diagrams_json_stringified -let game_tex = Pgn2tex.to_tex pgn ~diagram_data ~clock:false +let game_tex = to_tex ~diagram_data ~clock:false pgn ``` 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. diff --git a/bin/main.ml b/bin/main.ml index 285363f..5914644 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -1,6 +1,5 @@ open Js_of_ocaml -open Pgn_logic -open Ast +open Pgn_to_tex let convert_js pgn_js diagram_json_js display_clock = let pgn = Js.to_string pgn_js in @@ -9,11 +8,11 @@ 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 Ast.empty_diagrams - else Pgn2tex.parse_diagrams_json json_str + if json_str = "" || json_str = "{}" then Pgn_to_tex.empty_diagrams + else Pgn_to_tex.parse_diagrams_json json_str in - let result = Pgn2tex.to_tex pgn ~diagram_data ~clock:display_clock in + let result = Pgn_to_tex.to_tex ~diagram_data ~clock:display_clock pgn in Js.string result (* Exporting the module to the global JavaScript scope *) diff --git a/lib/dune b/lib/dune index 46c8902..191010b 100644 --- a/lib/dune +++ b/lib/dune @@ -1,10 +1,5 @@ (library - (name pgn_logic) + (name pgn_to_tex) (public_name pgn_to_tex) - (libraries sedlex menhirLib yojson) - (modules ast parser lexer parsing latex latex_helpers pgn2tex nag) - (preprocess - (pps sedlex.ppx))) - -(menhir - (modules parser)) + (modules pgn_to_tex) + (libraries pgn_to_tex_internal)) diff --git a/lib/ast.ml b/lib/internal/ast.ml similarity index 100% rename from lib/ast.ml rename to lib/internal/ast.ml diff --git a/lib/pgn2tex.ml b/lib/internal/converter.ml similarity index 100% rename from lib/pgn2tex.ml rename to lib/internal/converter.ml diff --git a/lib/internal/dune b/lib/internal/dune new file mode 100644 index 0000000..6d79096 --- /dev/null +++ b/lib/internal/dune @@ -0,0 +1,10 @@ +(library + (name pgn_to_tex_internal) + (package pgn_to_tex) + (libraries sedlex menhirLib yojson) + (modules ast parser lexer parsing latex latex_helpers converter nag) + (preprocess + (pps sedlex.ppx))) + +(menhir + (modules parser)) diff --git a/lib/latex.ml b/lib/internal/latex.ml similarity index 100% rename from lib/latex.ml rename to lib/internal/latex.ml diff --git a/lib/latex_helpers.ml b/lib/internal/latex_helpers.ml similarity index 100% rename from lib/latex_helpers.ml rename to lib/internal/latex_helpers.ml diff --git a/lib/lexer.ml b/lib/internal/lexer.ml similarity index 100% rename from lib/lexer.ml rename to lib/internal/lexer.ml diff --git a/lib/nag.ml b/lib/internal/nag.ml similarity index 100% rename from lib/nag.ml rename to lib/internal/nag.ml diff --git a/lib/parser.mly b/lib/internal/parser.mly similarity index 100% rename from lib/parser.mly rename to lib/internal/parser.mly diff --git a/lib/parsing.ml b/lib/internal/parsing.ml similarity index 100% rename from lib/parsing.ml rename to lib/internal/parsing.ml diff --git a/lib/pgn_to_tex.ml b/lib/pgn_to_tex.ml new file mode 100644 index 0000000..1e2caf6 --- /dev/null +++ b/lib/pgn_to_tex.ml @@ -0,0 +1,8 @@ +type diagram_data = Pgn_to_tex_internal.Ast.diagram_data + +let empty_diagrams = Pgn_to_tex_internal.Ast.empty_diagrams + +let parse_diagrams_json = Pgn_to_tex_internal.Converter.parse_diagrams_json + +let to_tex ?(diagram_data = empty_diagrams) ?(clock = false) pgn = + Pgn_to_tex_internal.Converter.to_tex pgn ~diagram_data ~clock diff --git a/lib/pgn_to_tex.mli b/lib/pgn_to_tex.mli new file mode 100644 index 0000000..9311c4f --- /dev/null +++ b/lib/pgn_to_tex.mli @@ -0,0 +1,39 @@ +(** Convert chess PGN games to LaTeX. + + [Pgn_to_tex] is the public entry point for the library. Open it to convert + a PGN string without referring to any internal parsing or rendering + modules: + + {[ + let tex = Pgn_to_tex.to_tex pgn + ]} + + Diagrams are optional and selected by passing {!diagram_data} built with + {!empty_diagrams} or {!parse_diagrams_json}. *) + +(** Diagram selection data. + + The representation is intentionally abstract; construct values with + {!empty_diagrams} or {!parse_diagrams_json}. *) +type diagram_data + +val empty_diagrams : diagram_data +(** [empty_diagrams] selects no diagrams. It is the default used by + {!to_tex}. *) + +val parse_diagrams_json : string -> diagram_data +(** [parse_diagrams_json json] parses diagram selections from a JSON string. + + The input is expected to be a JSON array of objects. Each object contains + a [fen] string and may contain a [ply] number and a [path] array of SAN + moves. Malformed JSON yields {!empty_diagrams} rather than raising. *) + +val to_tex : ?diagram_data:diagram_data -> ?clock:bool -> string -> string +(** [to_tex pgn] converts the PGN game in [pgn] to a LaTeX fragment. + + @param diagram_data + diagram selections to render; defaults to {!empty_diagrams}. + @param clock + when [true], render clock times alongside diagrams; defaults to [false]. + + @raise Failure "Parse error" if [pgn] is not a valid PGN game. *) diff --git a/test/dune b/test/dune index 086c88b..68569e6 100644 --- a/test/dune +++ b/test/dune @@ -1,6 +1,6 @@ (test (name test_suite) (modules (:standard)) - (libraries ounit2 sedlex sedlex.ppx pgn_to_tex helpers) + (libraries ounit2 sedlex sedlex.ppx pgn_to_tex pgn_to_tex_internal helpers) (deps (source_tree pgn_examples))) diff --git a/test/helpers/dune b/test/helpers/dune index b76dd7b..df2bf89 100644 --- a/test/helpers/dune +++ b/test/helpers/dune @@ -1,4 +1,4 @@ (library (name helpers) (modules latex_helper) - (libraries pgn_to_tex)) + (libraries pgn_to_tex_internal)) diff --git a/test/helpers/latex_helper.ml b/test/helpers/latex_helper.ml index cdfab44..df60cb2 100644 --- a/test/helpers/latex_helper.ml +++ b/test/helpers/latex_helper.ml @@ -1,4 +1,4 @@ -open Pgn_logic +open Pgn_to_tex_internal open Ast let diagram_data = diff --git a/test/test_latex.ml b/test/test_latex.ml index ad78f71..1da0b8b 100644 --- a/test/test_latex.ml +++ b/test/test_latex.ml @@ -1,5 +1,5 @@ open OUnit2 -open Pgn_logic +open Pgn_to_tex_internal open Ast open Helpers diff --git a/test/test_lexer.ml b/test/test_lexer.ml index cf063bb..82ad695 100644 --- a/test/test_lexer.ml +++ b/test/test_lexer.ml @@ -1,5 +1,5 @@ open OUnit2 -open Pgn_logic +open Pgn_to_tex_internal open Parser let lex_all ?(is_header = false) s = diff --git a/test/test_nag.ml b/test/test_nag.ml index 327d8a0..777ed6a 100644 --- a/test/test_nag.ml +++ b/test/test_nag.ml @@ -1,5 +1,5 @@ open OUnit2 -open Pgn_logic +open Pgn_to_tex_internal open Ast open Nag diff --git a/test/test_pgn2tex.ml b/test/test_pgn_to_tex.ml similarity index 86% rename from test/test_pgn2tex.ml rename to test/test_pgn_to_tex.ml index 3fc8194..949b3a3 100644 --- a/test/test_pgn2tex.ml +++ b/test/test_pgn_to_tex.ml @@ -1,8 +1,8 @@ open OUnit2 -open Pgn_logic +open Pgn_to_tex_internal open Ast -module MoveMap = Pgn2tex.MoveMap -module PathMap = Pgn2tex.PathMap +module MoveMap = Ast.MoveMap +module PathMap = Ast.PathMap let ae expected actual = assert_equal ~printer:(fun x -> x) expected actual @@ -18,26 +18,21 @@ let contains ~needle haystack = 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 = - { - 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 game_diagram_json_str = + {| [ + {"ply":5,"fen":"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3"}, + {"ply":6,"fen":"rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 3"}, + {"ply":10,"fen":"r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5"}, + {"ply":15,"fen":"r1bqkb1r/pp1ppppp/2n5/2p5/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 5 7"} + ] |} + +let diagram_data = Pgn_to_tex.parse_diagrams_json game_diagram_json_str 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 result = Converter.parse_diagrams_json diagram_json_str in let actual_bindings = MoveMap.bindings result.by_ply in let expected_bindings = @@ -55,7 +50,7 @@ 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 result = Converter.parse_diagrams_json variation_diagram_json_str in let actual = PathMap.bindings result.by_path in let expected = [ @@ -67,12 +62,15 @@ let test_json_path_to_map _ctxt = 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 diagrams = Pgn_to_tex.parse_diagrams_json variation_diagram_json_str in + let rendered = Pgn_to_tex.to_tex ~diagram_data:diagrams ~clock:false pgn in + let internal_diagrams = + Converter.parse_diagrams_json variation_diagram_json_str + in let diagram = match Latex_helpers.get_diagram_by_path ~path:"e4 e5 Nf3 d6" ~small:true - diagrams + internal_diagrams with | Some d -> d | None -> assert_failure "variation diagram not found by path" @@ -95,7 +93,7 @@ let test_basic_pgn_to_tex _ctxt = let filename = "pgn_examples/1.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 actual = Pgn_to_tex.to_tex ~diagram_data ~clock:false input_string in let expected = {|\title{URS-chJ} \author{Ibragimov, Ildar (2455)\\Kramnik, Vladimir (2480)} @@ -118,7 +116,7 @@ let test_pgn_with_clock_to_tex _ctxt = let filename = "pgn_examples/2.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 actual = Pgn_to_tex.to_tex ~diagram_data ~clock:true input_string in let expected = {|\title{Casual Rapid game} \author{montillo2015 (1919)\\reassessyourchess (1869)} @@ -137,13 +135,27 @@ let test_pgn_with_clock_to_tex _ctxt = ae expected actual; close_in ic +let test_default_arguments _ctxt = + let actual = Pgn_to_tex.to_tex "1. e4 e5" in + let expected = + "\\title{}\n\\author{}\n\\date{}\\maketitle\n\\newchessgame\n\ + \\textbf{1.} \\textbf{e4} \\textbf{e5}" + in + ae expected actual + +let test_invalid_pgn_raises _ctxt = + assert_raises (Failure "Parse error") (fun () -> + ignore (Pgn_to_tex.to_tex "1. e4 e5 (2. Nf3")) + let suite = - "pgn2tex tests" + "pgn_to_tex 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; + "default_arguments" >:: test_default_arguments; + "invalid_pgn_raises" >:: test_invalid_pgn_raises; ] diff --git a/test/test_suite.ml b/test/test_suite.ml index 3309e3c..0017bd3 100644 --- a/test/test_suite.ml +++ b/test/test_suite.ml @@ -6,7 +6,7 @@ let () = >::: [ Test_lexer.suite; Test_latex.suite; - Test_pgn2tex.suite; + Test_pgn_to_tex.suite; Test_nag.suite; ] in